function giveFocus(formName, formObject){
	if(parent.document.getElementById(formName+"_"+formObject) != null){
		parent.document.getElementById(formName+"_"+formObject).focus();
	}
}
function printWindow(){
	window.print();
}


/* This is just some xmlhttprequest testing TODO: remove */

var url = "/ioReceive.php"; // The server-side script
var countyUrl = "/getCountyXML.php?stateId=";
var countyKey = ""; //global key, for use in handleCountyXMLResponse
var contactUrl = "/api/getContactXML.php?contactId=";

function handleHttpResponse() {
	if (http.readyState == 4) {

		if (http.responseText.indexOf('invalid') == -1) {

      // Split the comma delimited response into an array
			results = http.responseText;
			parent.document.getElementById('ioForm_out').value = results;

			isWorking = false;

			parent.document.getElementById('preview').innerHTML = "<img src=\"" + results + "\">";
		}
	}
}

function handleCountyXMLResponse(){
	if (http.readyState == 4) {
		if (http.responseText.indexOf('invalid') == -1) {
			var xmlDocument = http.responseXML;

			//erase the previous entries
			purgeSelectOptions(countyKey);
			for(var i=0;;i++){
				if(xmlDocument.getElementsByTagName('name').item(i) == null){
					break;
				}

				var county = xmlDocument.getElementsByTagName('name').item(i).firstChild.data;
				var countyId = xmlDocument.getElementsByTagName('id').item(i).firstChild.data;
				//document.getElementById('city').value = city;
				//document.getElementById('state').value = state;



				addSelectOption(countyKey,county,countyId);
			}

			//isWorking = false;
		}
	}
}

function pushCountyData(stateId){
	//if (!isWorking && (http != false)) {
	http.open("GET", countyUrl + escape(stateId), true);
	http.onreadystatechange = handleCountyXMLResponse;
		//isWorking = true;
	http.send(null);
	//}
}

function pushData(){

	var tmp = parent.document.getElementById("ioForm_myFile").value;
	http.open("POST",url,false);
	/*http.setRequestHeader('Content-Type','multipart/form-data');*/
	/*http.send(parent.document.getElementById('ioForm_myFile'));*/
	//http.setRequestHeader('Content-Type','text/xml');
	http.setRequestHeader('Content-Type','application/x-www-form-urlencoded');


	http.onreadystatechange = handleHttpResponse;
	isWorking = true;
	//http.send(null);
	http.send(parent.document.getElementById('ioForm_in'));
}

function getHTTPObject() {
	var xmlhttp;
  /*@cc_on
	@if (@_jscript_version >= 5)
	try {
	xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
	try {
	xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
	xmlhttp = false;
}
}
	@else
	xmlhttp = false;
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
			//ensures data read as xml
			xmlhttp.overrideMimeType("text/xml");
		} catch (e) {
			xmlhttp = false;
		}
	}
	return xmlhttp;
}



function addField (form, fieldType, fieldName, fieldItr, fieldValue, cnt) {
	if(cnt == null){
		cnt = 1;
	}
	for(i=1;i<=cnt;i++){
		var input = document.createElement('INPUT');
		input.setAttribute('type', fieldType);
		input.setAttribute('name', fieldName+"[]");
		input.setAttribute('value', fieldValue);
		document.getElementById('imageForm_imageKeyword[0]').appendChild(input);
	}
}

function addImageKeyword(){

	var input = document.createElement('INPUT');
	input.setAttribute('type', 'input');
	input.setAttribute('name', 'imageKeyword'+"[]");
	input.setAttribute('value', '');
	input.setAttribute('class', 'forminput');

	/*var str = '<tr><td align="right" class="fieldtitle">keyword</td><td align="left"  style="width:350px;">';
	str += '<input name="imageKeyword[]">';
	str += '</td><td align="left"> RIGHT </td>\n</tr>\n';



	document.getElementById('imageForm_keywords').innerHTML += str;*/

	var subB = document.getElementById('imageForm_submit');

	var table = document.getElementById('field_keywordField');
	//var tr = table.insertBefore(document.createElement('tr'),subB);
	var tr = table.appendChild(document.createElement('tr'));
	var a = tr.appendChild(document.createElement('td'));
	a.appendChild(document.createTextNode('Keyword'));
	var b = tr.appendChild(document.createElement('td'));
	//b.appendChild(document.createTextNode('*'));
	b.appendChild(input);
	//b.appendChild(document.appendChild(input));
	var c = tr.appendChild(document.createElement('td'));
}


function addSelectOption(selectKey, option, value){
	selectObj = parent.document.getElementById(selectKey);
	var optionObj = document.createElement('option');
	optionObj.text = option;
	optionObj.value = value;
	try{
		selectObj.add(optionObj,null); //should work in most browsers that support good dom
	} catch(ex) {
		selectObj.add(optionObj); //should work in ie, if above fails
	}

}

function purgeSelectOptions(selectKey){
	selectObj = parent.document.getElementById(selectKey);
	selectObj.length = 0;
}

function getCountyFromState(stateKey,countyLocalKey){
	stateObj = parent.document.getElementById(stateKey);
	var stateId = stateObj.options[stateObj.selectedIndex].value;

	//set the global key
	countyKey = countyLocalKey;

	//now that we have the stateId, we can push this to our php page for the county list
	pushCountyData(stateId);
}

//changes the id=srcKey's src to dstURL
function changeImage(srcKey,dstURL){
	parent.document.getElementById(srcKey).src = dstURL;
}

function nextImage(srcKey){
	if(currentImageId < imagesBrowse.length-1){
		var dstURL = imagesBrowse[++currentImageId];
		changeImage(srcKey,dstURL);
	}
}

function prevImage(srcKey){
	if(currentImageId > 0){
		var dstURL = imagesBrowse[--currentImageId];
		changeImage(srcKey,dstURL);
	}
}

function jumpImage(srcKey,id){
	currentImageId=id;
	var dstURL = imagesBrowse[currentImageId];
	changeImage(srcKey,dstURL);
}

function popUp(url,width,height){
	newwindow=window.open(url,'name','height='+height+',width='+width+',status=no,location=no');
	if (window.focus) {newwindow.focus()}
	return false;
}
function newWindow(url, ref, options) {
	newwindow=window.open(url, ref, options);
	if( window.focus ) {newwindow.focus()}
	return false;
}
function mapload(){
	var map = new GMap(document.getElementById("map"));
	map.addControl(new GSmallMapControl());
	map.centerAndZoom(new GPoint(-122.1419, 37.4419), 4);
}

/*
function getCountyFromState(stateKey,countyLocalKey){
	stateObj = parent.document.getElementById(stateKey);
	var stateId = stateObj.options[stateObj.selectedIndex].value;

	//set the global key
	countyKey = countyLocalKey;

	//now that we have the stateId, we can push this to our php page for the county list
	pushCountyData(stateId);
}
*/

function handleContactXMLResponse(){
	if (http.readyState == 4) {
		if (http.responseText.indexOf('invalid') == -1) {
			var xmlDocument = http.responseXML;
			
			//var contact = xmlDocument.getElementsByTagName('contact').item(0).firstChild.data;
			//var contact = xmlDocument.getElementsByTagName('contact');
			
			//var test = contact.getElementsByTagName('contactFirstName');
			//var test = contact.item(0);
			
			//i'm pretty sure a better way exists than this, but whatever
			var pieces = Array();
			var i=0;
			pieces[i++] = 'contactFirstName';
			pieces[i++] = 'contactLastName';
			pieces[i++] = 'contactAddress1';
			pieces[i++] = 'contactAddress2';
			pieces[i++] = 'contactCity';
			pieces[i++] = 'contactZip';
			pieces[i++] = 'contactPhoneHome';
			pieces[i++] = 'contactPhoneWork';
			pieces[i++] = 'contactPhoneMobile';
			pieces[i++] = 'contactFax';
			pieces[i++] = 'contactEmail';
			pieces[i++] = 'stateId';
			
			var x = "";
			var curKey = "";
			
			for(var j=0;j<i;j++){
				x = "";
				if(xmlDocument.getElementsByTagName(pieces[j]) != null 
					&& xmlDocument.getElementsByTagName(pieces[j]).item(0) != null
					&& xmlDocument.getElementsByTagName(pieces[j]).item(0).firstChild != null){
					x = xmlDocument.getElementsByTagName(pieces[j]).item(0).firstChild.data;
				}
				if(parent.document.getElementById(pieces[j]) != null){
					if(pieces[j] == 'stateId' && x == ""){
						x = 0;
					}
					parent.document.getElementById(pieces[j]).value = x;
					if(pieces[j] == 'stateId'){
						x = "";
						if(xmlDocument.getElementsByTagName('state') != null 
							&& xmlDocument.getElementsByTagName('state').item(0) != null
							&& xmlDocument.getElementsByTagName('state').item(0).firstChild != null){
							x = xmlDocument.getElementsByTagName('state').item(0).firstChild.data;
						}
					}
					
					parent.document.getElementById('contactOriginal_'+pieces[j]).innerHTML = x;
				}
			}


			//isWorking = false;
		}
	}
}

function pushContactData(contactId){
	//if (!isWorking && (http != false)) {
	http.open("GET", contactUrl + escape(contactId), true);
	http.onreadystatechange = handleContactXMLResponse;
		//isWorking = true;
	http.send(null);
	//}
}


function updateContactLead(contactKey){
	contactObj = parent.document.getElementById(contactKey);
	var contactId = contactObj.options[contactObj.selectedIndex].value;
	pushContactData(contactId);
}

function confirmBox(msg,linkURL){
	input_box=confirm(msg);
	if (input_box==true){
		window.location = linkURL;
	}
}



function swapCells (cell1, cell2) {
  var dummyCell;
  if (document.createElement &&
      (dummyCell = document.createElement('tr'))) {
    cell1.parentNode.replaceChild(dummyCell, cell1);
    cell2.parentNode.replaceChild(cell1, cell2);
    dummyCell.parentNode.replaceChild(cell2, dummyCell);
    
    alert (cell1.getAttribute("id"));
    cell1.setAttribute("class", "contactlist");
    
    /*
    if(cell1.className == "contactliston"){
    	cell1.className = "contactlist";
    } else {
    	cell1.className = "contactliston";
    	
    }
    
    if(cell2.className == "contactliston"){
    	cell2.className = "contactlist";
    } else {
    	cell2.className = "contactliston";
    }*/
    	
  }
}

/*function doCellSwap (form, tableId) {
  var table;
  if (document.getElementById &&
      (table = document.getElementById(tableId))) {
    var rowIndex1 = form.elements.rowIndex1.value;
    var cellIndex1 = form.elements.cellIndex1.value;
    var rowIndex2 = form.elements.rowIndex2.value;
    var cellIndex2 = form.elements.cellIndex2.value;
    if (table.rows) {
      var cell1 = table.rows[rowIndex1].cells[cellIndex1];
      var cell2 = table.rows[rowIndex2].cells[cellIndex2];
      swapCells(cell1, cell2);
    }
  }
}*/















