function cityTimer(fieldname){
	if(typeof(citytimer)!='undefined'){
		clearTimeout(citytimer);
	}
	citytimer = setTimeout('cityLookup("'+fieldname+'")',500);
}

function cityLookup(fieldname){
	var fieldname_arr = fieldname.split('_');
	var zipcode = $("input[name="+fieldname_arr[0]+"]").val();
	if($("input[name="+fieldname_arr[1]+"]").val()==''){
		$.ajax({
				async: false,
		    url: '../../include/citylookup.php',
		    type: 'POST',
		    data: 'zipcode='+zipcode,
		    dataType: 'xml',
		    error: function(){
		      alert('Please try again or refresh the page to see if you are still logged in.');
		    },
		    success: function(xml){
		    	if($("error",xml).text()=='FALSE'){
	   				$("input[name="+fieldname_arr[1]+"]").val($("city",xml).text());
	   				$("select[name="+fieldname_arr[2]+"]").val($("state",xml).text());
	   			}
	   		}
		});
	}
}



//functions to set items visible or hidden
beenhere = false;
function showhide(id)
{
	if(beenhere===true){xinha_editors.contracttext.deactivateEditor();}
	if(document.getElementById(id).style.display != "block")
	{
		if(beenhere===false)
		{
			xinha_editors.contracttext.generate();
			beenhere = true;
		}
		setVisible(id);
	}else
	{
		setHidden(id);
	}
	//if(beenhere===true){xinha_editors.contracttext.activateEditor();}
	return true;
}
function setVisible(id)
{
	document.getElementById(id).style.display = "block";
}
function setHidden(id)
{
	document.getElementById(id).style.display = "none";
}

//function to suggest format in input boxes, converse of suggest_format
//clear_format()
function clear_format(obj, format, color)
{
	if(obj.value == format)
	{
		obj.value = '';
		obj.style.color = color;
	}
}

//function to suggest format in input boxes, converse of clear_format
//suggest_format()
function suggest_format(obj, format, color)
{
	if(obj.value == '' || obj.value == format)
	{
		obj.value = format;
		obj.style.color = color;
	}}

//checks or unchecks a checkbox control
function click_checkbox(obj)
{
	if(obj.checked == true)
	{
		obj.checked = false;
	}else
	{
		obj.checked = true;
	}
	return true;
}

//changes url by choosing an option from the dropdown box
// ddown_url(name of form, url text prior to value chosen in dropdown box)
function ddown_url(formname, urltext)
{
	var ddurl = formname.options[formname.selectedIndex].value;
	ddurl = ddurl.replace("&","%26");
	location.href = urltext + ddurl;
	return true;
}

//Utility function to determine if a select object has an options array
// hasOptions(obj)
function hasOptions(obj)
{
	if (obj!=null && obj.options!=null) { return true; }
	return false;
}
	
//Swap positions of two options in a select list
// swapOptions(select_object,option1,option2)
function swapOptions(obj,i,j)
{
	var o = obj.options;
	var i_selected = o[i].selected;
	var j_selected = o[j].selected;
	var temp = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
	var temp2= new Option(o[j].text, o[j].value, o[j].defaultSelected, o[j].selected);
	o[i] = temp2;
	o[j] = temp;
	o[i].selected = j_selected;
	o[j].selected = i_selected;
}
	
//Move selected option in a select list up one
// moveOptionUp(select_object)
function moveOptionUp(obj)
{
	if (!hasOptions(obj)) { return; }
	for (i=0; i<obj.options.length; i++)
	{
		if (obj.options[i].selected)
		{
			if (i != 0 && !obj.options[i-1].selected)
			{
				swapOptions(obj,i,i-1);
				obj.options[i-1].selected = true;
			}
		}
	}
}

//Move selected option in a select list down one
// moveOptionDown(select_object)
function moveOptionDown(obj)
{
	if (!hasOptions(obj)) { return; }
	for (i=obj.options.length-1; i>=0; i--)
	{
		if (obj.options[i].selected)
		{
			if (i != (obj.options.length-1) && ! obj.options[i+1].selected)
			{
				swapOptions(obj,i,i+1);
				obj.options[i+1].selected = true;
			}
		}
	}
}

//selects all options in a list box
// selectAll(name of list box)
function selectAll(box)
{
    for(var i=0; i<box.length; i++)
    {
    	box[i].selected = true;
    }
}

//adds to or changes query string of urls
//query_change('url or none','variable1','value1','variable2',...)
function query_change()
{
	//define variables
	//get number of and list arguments
	var argv = query_change.arguments;
  	var argc = argv.length;
  	var vararr = new Array();
  	var valuearr = new Array();
  	var urlvars = new Array();
  	var urlvalues = new Array();
   	//get url
   	if(argv[0]=='none')
   	{
		var url = window.location.href;
	}else
	{
		var url = argv[0];
	}
  	for(var i=1; i<argc; i++)
  	{
    	if(i%2)
		{
			vararr[(i-1)/2]=argv[i];
		}else
		{
			valuearr[(i-2)/2]=argv[i];
   		}
   	}
	//check for a query string
	if(url.indexOf("?") != -1)
	{
		//get query string
		var query = url.substring((url.indexOf('?')) + 1);
		//remove query string from url
		url = url.substring(0,url.indexOf('?'))
		i=0;
		//seperate variables and values
  		while(query.length > 0)
  		{
	  		//get variable
  			urlvars[i] = query.substring(0,query.indexOf('='));
  			query = query.substring((query.indexOf('=')) + 1);
  			//get value
  			//check for end of query string
  			if(query.indexOf('&') > -1)
  			{
  				urlvalues[i] = query.substring(0,query.indexOf('&'));
  				query = query.substring((query.indexOf('&')) + 1);
			}else
			{
  				urlvalues[i] = query;
  				query = '';
			}
  		  	i++;
		}
	}
	//make query ready for it's new value
	query = '?';
	//check for variable matches and set new values
	for(i=0; i<vararr.length; i++)
	{
		for(var j=0; j<urlvars.length; j++)
		{
			//variable match found, enter new value
			if(vararr[i]==urlvars[j])
			{
				urlvalues[j]=valuearr[i];
				vararr[i]='';
			}
		}
	}
	//build query string
	for(i=0; i<urlvars.length; i++)
	{
		query = query + urlvars[i] + '=' + urlvalues[i] + '&';
	}
	//add new variables and values to the query string
	for(i=0; i<vararr.length; i++)
	{
		//check to see if the variable should be added
		if(vararr[i])
		{
			query = query + vararr[i] + '=' + valuearr[i] + '&';
		}
	}
	//remove last ampersand
	query = query.substring(0, query.lastIndexOf('&'));
	//build new url
	url = url + query;
	//go to url
	window.location.href=url;
	return true;
}

