//=========================== Get Todays Date =================================
function getdates()
{
var today_date= new Date()
var month=today_date.getMonth()
var dates=today_date.getDate()
var year=today_date.getYear()
var day= today_date.getDay()
var today;

if (year < 2000){ year += 1900;}

var months = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
var today = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");

var datestr = today[day]+" "+months[month]+" "+dates+", "+year;

return(datestr);
}

//===================================== Popup Window ====================================
function newwindow(locat){	
popup= window.open(locat,'newwindow_id','toolbar=yes,location=yes,directories=no,status=no,scrollbars=yes,resizable=yes,copyhistory=no,width=1000,height=600');
ndoc= popup.document;
}


//===================================== Bookmark this page ====================================
function bookmark(url,title){
  if ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4)) {
  window.external.AddFavorite(url,title);
  } else if (navigator.appName == "Netscape") {
    window.sidebar.addPanel(title,url,"");
  } else {
    alert("Press CTRL-D (Netscape) or CTRL-T (Opera) to bookmark");
  }
}

//-----------------------------------------------
// Find screen width function
function getwidth(margin){
 w = screen.width;
 h = screen.height;
 width=w-margin;
 
return width;
}

//===================================== Popup Window ====================================
function submitlogin(){
popup= window.open('','popwindow','toolbar=no,location=no,directories=no,status=yes,scrollbars=yes,resizable=yes,copyhistory=no,width=750,height=400');
sstr  = "<html><body>";
sstr += "<form name='login' method='post' action='account.php'>";
sstr += "<input type=hidden name=username value='"+document.loginform.username.value+"'>";
sstr += "<input type=hidden name=password value='"+document.loginform.password.value+"'>";
sstr += "</form>";
sstr += "<script language='javascript'> document.login.submit(); </";
sstr += "script></body></html>";
ndoc= popup.document;
ndoc.write(sstr);
 return;
}

//=========================== Textbox Length Code =================================
function check_textbox(varfield, length_warn, length_max)
{
//varfield     == Question field pointer -- this
//length_warn  == Question warning -- approaching max length for question
//length_max   == Question max length

//Calculate how many characters left
var char_left = length_max - length_warn;

if (varfield.value.length == length_warn)
{ alert("You're approaching your max input length for this field ("+char_left+" characters left)"); }


if (varfield.value.length > length_max)
{ alert("You've reached your max input length for this field ("+length_max+" characters)"); }

if (varfield.value.length > length_max){ return false;}
else { return true; }
}

//=========================== Navigation Menu Code =================================
/***********************************************
* Switch Menu script- by Martial B of http://getElementById.com/
* Modified by Dynamic Drive for format & NS4/IE4 compatibility
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

if (document.getElementById){ //DynamicDrive.com change
document.write('<style type="text/css">\n');
document.write('.submenu{display: none;}\n');
document.write('</style>\n');
}

function SwitchMenu(obj){
	if(document.getElementById){
	var el = document.getElementById(obj);
	var ar = document.getElementById("tool_left").getElementsByTagName("span"); //DynamicDrive.com change
		if(el.style.display != "block"){ //DynamicDrive.com change
			for (var i=0; i<ar.length; i++){
				if (ar[i].className=="submenu") //DynamicDrive.com change
				ar[i].style.display = "none";
			}
			el.style.display = "block";
		}else{
			el.style.display = "none";
		}
	}
}


//============================================ color chooser code
var SetBG = top.SetBG ;
var Tog = top.Tog ;

var colordelim = "000|003|006|009|00C|00F|030|033|036|039|03C|03F|060|063|066|069|06C|06F|090|093|096|099|09C|09F|0C0|0C3|0C6|0C9|0CC|0CF|0F0|0F3|0F6|0F9|0FC|0FF|300|303|306|309|30C|30F|330|333|336|339|33C|33F|360|363|366|369|36C|36F|390|393|396|399|39C|39F|3C0|3C3|3C6|3C9|3CC|3CF|3F0|3F3|3F6|3F9|3FC|3FF|600|603|606|609|60C|60F|630|633|636|639|63C|63F|660|663|666|669|66C|66F|690|693|696|699|69C|69F|6C0|6C3|6C6|6C9|6CC|6CF|6F0|6F3|6F6|6F9|6FC|6FF|900|903|906|909|90C|90F|930|933|936|939|93C|93F|960|963|966|969|96C|96F|990|993|996|999|99C|99F|9C0|9C3|9C6|9C9|9CC|9CF|9F0|9F3|9F6|9F9|9FC|9FF|C00|C03|C06|C09|C0C|C0F|C30|C33|C36|C39|C3C|C3F|C60|C63|C66|C69|C6C|C6F|C90|C93|C96|C99|C9C|C9F|CC0|CC3|CC6|CC9|CCC|CCF|CF0|CF3|CF6|CF9|CFC|CFF|F00|F03|F06|F09|F0C|F0F|F30|F33|F36|F39|F3C|F3F|F60|F63|F66|F69|F6C|F6F|F90|F93|F96|F99|F9C|F9F|FC0|FC3|FC6|FC9|FCC|FCF|FF0|FF3|FF6|FF9|FFC|FFF" ;

var colors = colordelim.split( "|" ) ;

function WriteColorTable( document, ncols ) {
    document.writeln( "<TABLE cellpadding=0 cellspacing=0 border=1>" ) ;

    for( var i = 0 ; i < colors.length ; i++ ) {
	if( (i % ncols) == 0 ) {
	    document.write( "\n<TR>" ) ;
	}
	var rgb = colors[i].split( "" ) ;
	var bgcol = "#"+rgb[0]+rgb[0]+rgb[1]+rgb[1]+rgb[2]+rgb[2] ;
	document.write( "<TD bgcolor=\"" + bgcol + "\">" +
		"<A href=\'javascript:Tog();\' " +
		"onMouseOver=\'SetBG(\"" + bgcol + "\");\' " +
		"onClick=\'updateColor(\""+ bgcol +"\");\'>" +
		"&nbsp;&nbsp;&nbsp;</A></TD>" ) ;
    }
    document.writeln( "</TABLE>" ) ;
}

// int.toString(radix) is not reliable:
// var x = 160 ; x.toString(16) ; produces ":0"??
var hexvals = new Array( "0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F" ) ;

function WriteGrayTable( document, ncols ) {
    document.writeln( "<TABLE cellpadding=0 cellspacing=0 border=1><TR>" ) ;

    // Include 0 and 255 in the range.
    var grayincr = 255.0 / (ncols-1) ;
    var grayval = 0.0 ;
    for( var i = 0 ; i < ncols ; i++ ) {
	var igray = Math.round( grayval ) ;
	var graystr = (igray < 16) ? "0"+hexvals[igray] :
		hexvals[Math.floor(igray/16)]+hexvals[igray%16] ;
	var bgcol = "#"+graystr+graystr+graystr ;
	document.write( "<TD bgcolor=\"" + bgcol + "\">" +
		"<A href=\'javascript:Tog();\' " +
		"onMouseOver=\'SetBG(\"" + bgcol + "\");\' " +
		"onClick=\'updateColor(\""+ bgcol +"\");\'>" +
		"&nbsp;&nbsp;&nbsp;</A></TD>" ) ;
	grayval += grayincr ;
    }
    document.writeln( "</TABLE>" ) ;
}


//-------------------------------------------------------------------------------------------
// This function creates a new window and displays the colorchooser code and returns the value selected to 
//  the form that called it.

function color_window(field){		
 colorpopup= open('html/colorchooser.php?field='+field,'color_window','toolbar=no,location=no,directories=no,status=yes,scrollbars=no,resizable=no,copyhistory=no,width=530,height=220');
 if (colorpopup.opener == null) colorpopup.opener = self;
 return;
}


//-------------------------------------------------------------------------------------------
//  This form validates a given form and then, if it passes, it submits the form.

function validate_form(form_name){

switch(form_name){
  case 'user_account': {
    if(document.mod.newusername.value == ''){ 
       alert("Must type a username for this account."); document.mod.newusername.focus(); return false; }  

// --------------------------------------------- check passwords - 	   
  if(document.mod.current_password.value == ''){  // -------- creating new account	   
    if(document.mod.newpass1.value == ''){ 
       alert("Must type a password for this account."); document.mod.newpass1.focus(); return false; }  
    if(document.mod.newpass2.value == ''){ 
       alert("Must retype the password for this account."); document.mod.newpass2.focus(); return false; }  
    if(!(verify_password(document.mod.newpass1.value, document.mod.newpass2.value))){ return false; }   // --- verify the password passes the security requirements	   
  }
  else{ //------- modifying account
  if(document.mod.newpass1.value == ''){  //----- if modify record and not setting new password
   document.mod.newpass1.value = document.mod.current_password.value;  
   document.mod.newpass2.value = document.mod.current_password.value;  
  }
  if(!(verify_password(document.mod.newpass1.value, document.mod.newpass2.value))){ return false; }   // --- verify the password passes the security requirements	   
  }
//----------------------------------------------  
    if(document.mod.email.value == ''){ 
       alert("Must type an email for this account."); document.mod.email.focus(); return false; }  
   document.mod.submit();
   return;
  break;
  }

}

return true;
}

//-------------------------------------------------------------------------------------------
// -------------- verify password is compliant with necessary security procedures
// Passwords require:
//		- A minimal length of 6 characters and maximum of 20 characters
//		- A password must consist of at least 1 number or special character

function verify_password(pass1, pass2){
 if(pass1 != pass2)   { alert("The passwords entered do not match each other."); document.mod.newpass1.focus(); return false; }  
 if(pass1.length < 6) { alert("The password you entered needs to be at least 6 characters long."); document.mod.newpass1.focus(); return false; }  
 if(pass1.length > 20){ alert("The password you entered needs to be at less than 20 characters long."); document.mod.newpass1.focus(); return false; }  
 if(pass1.search(/[^A-Za-z]/) < 0){ alert("The password you entered needs to have at least 1 number or special character (ie - 1,2,3,!,@,#)."); document.mod.newpass1.focus(); return false; }  
 if(pass1.search(/[A-Za-z]/) < 0){ alert("The password you entered needs to have at least 1 regular character (a to z)."); document.mod.newpass1.focus(); return false; }  

 return true;
}
//-------------------------------------------------------------------------------------------




//-------------------------------------------------------------------------------------------
// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}


//=========================== Greyout Button Code =================================

function disable_var(varfield, status){

 switch(status){
  case 'T': { varfield.disabled = true; break; }
  case 'F': { varfield.disabled = false; break; }
 }
 
 return; 
}

//=========================== Confirm Delete Code =================================

function confirm_delete(action){

  if(confirm("Are you sure you want to delete this record?")){
   eval(action); return true;
  }
return false;
}  


//=========================== Confirm Continue Code =================================

function confirm_continue(action){

  if(confirm("Are you sure you want to do this?")){
   eval(action); return true;
  }
return false;
}  


//-------------------------------------------------------------------------------------------
//  This form validates a given form and then, if it passes, it submits the form.

function check_category(category_flag){

   if(category_flag == 'F'){ 
	  document.mod.action.value='createnew';   
      alert("Must select a category before creating page."); 
	}  
	else{ document.mod.action.value='insert'; }
	
return;
}


//-------------------------------------------------------------------------------------------
//  This script sets the recs to display

function setrecs(){
alert("cool");
//document.searchform.recs_to_display.value = document.search_recs.recs_to_display.value;
 document.searchform.submit();
return true;
}


//-------------------------------------------------------------------------------------------------------------
//------ set the cookie
function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
var cookie_str= c_name + "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
//alert(cookie_str);
document.cookie = cookie_str;
}

//-------------------------------------------------------------------------------------------------------------
//------ get the survey complete cookie -- if it exists
function getCookie(c_name)
{
if (document.cookie.length > 0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1; 
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    } 
  }
return "";
}


//-------------------------------------------------------------------------------------------------------------
//------- check cookie info
function checkCookie(c_name)
{
surveyid=getCookie(c_name);
if (surveyid!=null && surveyid!="")
{ alert('Cookie is set'); }
else{ document.surveyform.submit(); }
} 


//-------------------------------------------------------------------------------------------------------------
//------- remove this cookie
function delete_cookie ( cookie_name )
{
  var cookie_date = new Date ( );  // current date & time
  cookie_date.setTime ( cookie_date.getTime() - 1 );
  document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}

//-------------------------------------------------------------------------------------------------------------
//------- parish searching functions

var xmlHttp
function showResult(r_id,str,tab)
{
if (str.length==0)
 { 
// document.getElementById("livesearch").innerHTML="";
// document.getElementById("livesearch").style.border="0px";
 document.getElementById(r_id).innerHTML="";
 document.getElementById(r_id).style.border="0px";
 return
 }
 xmlHttp=GetXmlHttpObject()
 
 if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 } 

var url="html/livesearch.php"
url=url+"?q="+str+"&tab="+tab+"&r_id="+r_id
url=url+"&sid="+Math.random()
switch(r_id){
 case "livesearch": xmlHttp.onreadystatechange=stateChanged_Live;
 case "search_results": xmlHttp.onreadystatechange=stateChanged_Results;
} 
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
} 

// ------------------------------------------------- displays live search results of parish/town lists
function stateChanged_Live() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
 document.getElementById("livesearch").
 innerHTML=xmlHttp.responseText;
 document.getElementById("livesearch").
 style.border="1px solid #A5ACB2";
 } 
}
function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 // Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}

// ----------------------- displays all parish info after a parish has been selected
function stateChanged_Results() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
 document.getElementById("search_results").
 innerHTML=xmlHttp.responseText;
 document.getElementById("search_results").
 style.border="1px solid #A5ACB2";
 } 
}
function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 // Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}


//---------------------------------------------------------------------------------------------------------
// handle enter key issues with parish searching
function handleEnter(e,type,q,tab)
{
	var characterCode;
	if(e && e.which)
	{
		e = e;
		characterCode = e.which;
	} 
	else 
	{
		e = event;
		characterCode = e.keyCode;
	}

	if(characterCode == 13)
	{
//		document.getElementById('nextfield').focus();
		return false;
	} 
	else
	{
  	    showResult(type,q,tab);
		return true;
	}
}
//--------------------------------------------------------------------------------------

