function clearDefaultText(input) {
	if(input.value == input.defaultValue) {
		input.value = '';
	}
	input.className = 'black';
}

function addDefaultText(input) {
	if(input.value == '') {
		input.value = input.defaultValue;
		input.className = 'gray';
	}
}


function clearDefaultText2(input) {
	if(input.value == input.defaultValue) {
		input.value = '';
	}
	input.id = 'normal';
}

function addDefaultText2(input) {
	if(input.value == '') {
		input.value = input.defaultValue;
		input.id = 'gray';
	}
}

function checkMe(box) {
	box = document.getElementById(box);
	box.checked = true;
}

function checkWarning() {
	if(link = document.getElementById('warninglink'))
		link.user.focus();
}

function showMe(id) {
	obj = document.getElementById(id);
	obj.style.display='';
}


/* SPECIFIC GOVOTE FUNCTIONS */

function enterEmail() {
	//get the ajax object
	var ajax = ajaxFunction();
	var form = document.getElementById('emailreminders');
	var email = form.getElementsByTagName('input')[0];
	var button = document.getElementById('emailButton');

	if(ajax==null)
		return false;

	ajax.onreadystatechange=function() {

		if(ajax.readyState==2) {
			form.innerHTML = "...";
		}
		else if(ajax.readyState==4) {
			button.style.display = 'none';
			form.innerHTML = "Thanks!";
		}
	}

	ajax.open("GET","/scripts/eaddress.php?e="+email.value+"&r="+getURLParam('r'),true);
	ajax.send(null);

}


//set state given the destination page
function setState(dest) {
	//create a new region list box if it doesn't exist
	var newDiv = document.getElementById('modal2');

	if(!newDiv) {
		newDiv = document.createElement('div');
		newDiv.id = 'modal2';
		document.getElementById('modaltext').appendChild(newDiv);
	}

	//change it's info using ajax
	ajaxChangeModal('regionlist.php?s='+document.getElementById('stateSelect').value+'&p='+dest, 'modal2');
}


//set zipcode given the destination page
function setZip(dest) {
	//create a new region list box if it doesn't exist
	var newDiv = document.getElementById('modal2');

	if(!newDiv) {
		newDiv = document.createElement('div');
		newDiv.id = 'modal2';
		document.getElementById('modaltext').appendChild(newDiv);
	}

	//change it's info using ajax
	ajaxChangeModal('regionlinks.php?z='+document.getElementById('zippy').value+'&p='+dest, 'modal2');
}

//opens a google maps tab searching for the information
//in the given input field
function mapIt(input) {
	window.open('http://maps.google.com/maps?hl=en&tab=wl&q=notary%2C '+document.getElementById(input).value, '_blank');
	//newWindow.focus();
}


/* GENERIC AJAX FUNCTIONS */

function ajaxFunction()
{
var xmlHttp;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }

return xmlHttp;
}


function getURLParam(strParamName){
  var strReturn = "";
  var strHref = window.location.href;
  if ( strHref.indexOf("?") > -1 ){
    var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
    var aQueryString = strQueryString.split("&");
    for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
      if (
aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1 ){
        var aParam = aQueryString[iParam].split("=");
        strReturn = aParam[1];
        break;
      }
    }
  }
  return unescape(strReturn);
}


/* GENERIC MODAL FUNCTIONS */

//this function creates a modal dialogue given the id of the
//hidden div and the type of modal box
function makeModal(id, type) {
	var overlay = document.getElementById('overlay');
	var modalBox = document.getElementById('modalBox');
	var modalStyle = modalBox.getElementsByTagName('div')[0];
	var info = document.getElementById('modaltext');
	var close = document.getElementById('close');

	//make all other elements unusable by using overlay
	setOverlaySize(overlay);
	overlay.style.display = 'block';

	//decide the style of the closebox
	setModalBoxType(type, overlay, modalStyle);

	//make modal box appear and add the information
	info.innerHTML = document.getElementById(id).innerHTML;
	modalStyle.appendChild(info);

	modalBox.style.display = 'block';

	//move modalBox to the center
	centerDiv(modalBox);

	close.href = "JavaScript:closeModal('modaltext')"

}


//this function closes a modal dialogue given the id of the object it holds
function closeModal(id) {
	var modalBox = document.getElementById('modalBox');

	//if there was a child, remove it
	if(id!=null) {
		var child = document.getElementById(id);
		document.getElementById('hideMe').appendChild(child);
	}

	//close the overlay
	document.getElementById('overlay').style.display = 'none';

	//close the dialogue
	modalBox.style.display = 'none';
}


//this function uses ajax to grab html from a script and
//display it in a modal dialogue of the given type
function ajaxModal(script, type) {
	//get the ajax object
	var ajax = ajaxFunction();

	if(ajax == null) {
		return;
	}

	ajax.onreadystatechange=function() {

		if(ajax.readyState==1) {
			//display overlay
			var over = document.getElementById('overlay');
			setOverlaySize(over);
			over.style.display = 'block';
		}
		else if(ajax.readyState==4) {
			//display the dialogue
			var modalBox = document.getElementById('modalBox');
			var modalStyle = modalBox.getElementsByTagName('div')[0];
			var innerBox = document.getElementById('modaltext');

			//decide the style of the closebox
			setModalBoxType(type, document.getElementById('overlay'), modalStyle)
	
			innerBox.innerHTML = ajax.responseText;
			
			modalStyle.appendChild(innerBox);

			modalBox.style.display = 'block';

			//move to the center
			centerDiv(modalBox);

			document.getElementById('close').href = "javascript:closeModal('modaltext')";
		}
	}

	ajax.open("GET","/scripts/"+script,true);
	ajax.send(null);
}


//this function changes the inner html of the modal
//dialogue box with ajax given a script and the name
//of the inner box (modal1)
function ajaxChangeModal(script, boxId) {
	//get the ajax object
	var ajax = ajaxFunction();

	if(ajax == null) {
		return;
	}

	ajax.onreadystatechange=function() {
		if(ajax.readyState==4) {
			//display the dialogue
			document.getElementById(boxId).innerHTML = ajax.responseText;
			centerDiv(document.getElementById('modalBox'));
		}
	}

	ajax.open("GET","/scripts/"+script,true);
	ajax.send(null);
}


//this function sets a div to be in the center depending
//on its size
function centerDiv(div) {
	//set modal box size
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	div.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 4 - div.offsetHeight) / 2) + 'px');
	div.style.left = (((arrayPageSize[0] - 4 - div.offsetWidth) / 2) + 'px');
}


//this function sets the overlay size to the full size of the page
function setOverlaySize(overlay) {
	var pageSize = getPageSize();

	overlay.style.width = (pageSize[2]) + 'px';
	overlay.style.height = (pageSize[1]) + 'px';
}


//this function sets the modal box type
function setModalBoxType(type, overlay, modal) {
	switch (type) {
		//normal
		case 1:
			modal.setAttribute("id",'modalDefault');
			overlay.onclick = function() {closeModal(null)}
			break;
		//warning
		case 2:
			modal.setAttribute("id",'modalWarning');
			overlay.onclick = function() {closeModal(null)}
			break;
		//district
		case 3:
			break;
		//district choose
		case 4:
			modal.setAttribute("id",'modalDistrict');
			break;

		default:
			modal.setAttribute("id",'modalDefault');
			break;
	}
}

//this function sets the close box type
function setCloseBoxType(type, overlay) {
	switch (type) {
		//normal
		case 1:
			document.getElementById('closeBox').setAttribute("class",'closeNormal');
			break;
		//warning
		case 2:
			document.getElementById('closeBox').setAttribute("class",'closeWarning');
			overlay.onclick = function() {closeModal(null)}
			break;
		//normal with no close
		case 3:
			document.getElementById('closeBox').setAttribute("class",'closeNone');
			break;
		//district choose
		case 4:
			document.getElementById('closeBox').setAttribute("class",'closeDistrict');
			break;

		default:
			document.getElementById('closeBox').setAttribute("class",'closeNormal');
			break;
	}
}


// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}


// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}


//
// addLoadEvent()
// Adds event to window.onload without overwriting currently assigned onload functions.
// Function found at Simon Willison's weblog - http://simon.incutio.com/
//
function addLoadEvent(func)
{	
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
    	window.onload = func;
	} else {
		window.onload = function(){
		oldonload();
		func();
		}
	}

}




