// JavaScript Document

function clearValue(thisid, origValue, newclass){
	if(document.getElementById(thisid).value=origValue){
		document.getElementById(thisid).value='';
		if(newclass){
			document.getElementById(thisid).className = newclass;
		}
	}
}
	
function switchCheck(theID){
	if(theID){
		if(document.getElementById(theID).disabled==false){
			document.getElementById(theID).checked = !document.getElementById(theID).checked;
		}
	}	
}

function CheckEnable(theCheck, theField){
	if(theField){
		document.getElementById(theField).disabled = !document.getElementById(theCheck).checked;
		document.getElementById(theField).focus();
	}
}

function startPolling(theID){
	var myinterval=setInterval(
		function(){
			if(poll(theID))
				clearInterval(myinterval);
			},200);
	}

function poll(theFrameID){
	var position = document.getElementById(theFrameID).scrollTop;
	var maxPosition = document.getElementById(theFrameID).scrollHeight;
	
	//var browserMax = (BrowserDetect.browser=="MSIE")? maxPosition: maxPosition-position;

	if (position > (.925 * maxPosition)){
		document.getElementById('checkAccept').disabled = false;
		return true;
	}else{
		return false;
	}
}

function getFileData(path,ext) {
	if(!ext){ext="php";}
	var oRequest = new XMLHttpRequest();
	var sURL  = "https://" + self.location.hostname + path + "." + ext;
	
	oRequest.open("GET",sURL,false);
	oRequest.setRequestHeader("User-Agent",navigator.userAgent);
	oRequest.send(null);
	
	if (oRequest.status==200){
		return oRequest.responseText;
	}else{
		return "<strong>Error executing XMLHttpRequest call!</strong><br /> Tried: '"+sURL+"'";
	}
}

function loadTerms(theDiv,PathToTerms,ext){
		str = getFileData(PathToTerms);
		document.getElementById(theDiv).innerHTML = str;
}
function loadFrame(theDiv,PathToTerms,ext){
		(!ext) ? ext="php": ext=ext;
		//str = getFileData(PathToTerms);
		document.getElementById(theDiv).src = "https://cpawatchdog.com" + PathToTerms + "." + ext;
}
	

function clearRow(id){
	//delete the row id
	
	var tb 	= 	document.getElementById('AffReffListTable');
	var b	=	tb.getElementsByTagName('tbody')[0];
	var rw	=	document.getElementById(id);
	var rowIndex	=	document.getElementById(id).rowIndex;
	b.removeChild(rw);
	
	//clear the inputs
		
		// Name cell
		var fnSaveID = 'AffRef' + (rowIndex) + '_FirstName';
		var lnSaveID = 'AffRef' + (rowIndex) + '_LastName';
		document.getElementById(fnSaveID).value = null;
		document.getElementById(lnSaveID).value = null;

		// Email cell
		var emailSaveID = 'AffRef' + (rowIndex) + '_Email';
		document.getElementById(emailSaveID).value = null;

		// Phone cell
		var phoneSaveID = 'AffRef' + (rowIndex) + '_Phone';
		document.getElementById(phoneSaveID).value = null;
		var phoneExtSaveID = 'AffRef' + (rowIndex) + '_PhoneExt';
		document.getElementById(phoneExtSaveID).value = null;

		// Company cell
		var companySaveID = 'AffRef' + (rowIndex) + '_Company';
		document.getElementById(companySaveID).value = null;
		
		//bump 2 to 1 if 1 was cleared
		if(rowIndex==1){
			// Name cell
			document.getElementById('AffRef1_FirstName').value = document.getElementById('AffRef2_FirstName').value;
			document.getElementById('AffRef1_LastName').value = document.getElementById('AffRef2_LastName').value;
			document.getElementById('AffRef2_FirstName').value = null;
			document.getElementById('AffRef2_LastName').value = null;
			
			// Email cell
			document.getElementById('AffRef1_Email').value = document.getElementById('AffRef2_Email').value
			document.getElementById('AffRef2_Email').value = null;
	
			// Phone cell
			document.getElementById('AffRef1_Phone').value = document.getElementById('AffRef2_Phone').value
			document.getElementById('AffRef1_PhoneExt').value = document.getElementById('AffRef2_PhoneExt').value
			document.getElementById('AffRef2_Phone').value = null;
			document.getElementById('AffRef2_PhoneExt').value = null;
	
			// Company cell
			document.getElementById('AffRef1_Company').value = document.getElementById('AffRef2_Company').value
			document.getElementById('AffRef2_Company').value = null;
			}
	}


function addReference(FirstName,LastName,Phone,PhoneExt,Email,Company,TheRefTableID){
	//if(document.getElementById('AffRefListBox').style.display=="none"){document.getElementById('AffRefListBox').style.display="block";}
	//alert("Attempting to Add: " + FirstName +", "+LastName+", "+Phone+", "+PhoneExt+", "+Company+", "+TheRefTableID);
	if(!FirstName.length || !LastName.length || !Phone.length || !Email.length || !Company.length || !TheRefTableID.length){
		//alert("Missing a field: FN(" + FirstName +"), LN("+LastName+"), P("+Phone+"), Pe("+PhoneExt+"), E("+Email+"), C("+Company+"), tbl("+TheRefTableID+")");
		return false;
	}else{
		//alert("Passed to Add: " + FirstName +", "+LastName+", "+Phone+", "+PhoneExt+", "+Email+", "+Company+", "+TheRefTableID);
		//see if this is the first add, by checking for the nullTableID, then delete it
		
		var vFirstName = document.getElementById(FirstName).value;
		var vLastName = document.getElementById(LastName).value;
		var vCompany = document.getElementById(Company).value;
		var vEmail = document.getElementById(Email).value;
		var vPhone = document.getElementById(Phone).value;
		var vPhoneExt = document.getElementById(PhoneExt).value;
		
		var tbl = document.getElementById(TheRefTableID);
		var lastRow = tbl.rows.length;
		var iteration = lastRow;
		var rand = Math.ceil(100000*Math.random());
		if(lastRow==3){
			alert("You have already added 2 References, please go to the next section.");
			return false;
			}
		
		var row = tbl.insertRow(lastRow);
		row.id = "tr_" + rand;
		var textNode = "";
		
		

		
		// Edit Cell
		var cell_a = row.insertCell(0);
		var EditNode = "<a href=\"javascript:void(0);\" onclick=\"clearRow('tr_" + rand + "');\" >Remove<a/>";
		cell_a.innerHTML = EditNode;
		
		// Name cell
		var cell_b = row.insertCell(1);
		textNode = document.createTextNode(vFirstName+" "+vLastName);
		cell_b.appendChild(textNode);
		var fnSaveID = 'AffRef' + (iteration) + '_FirstName';
		var lnSaveID = 'AffRef' + (iteration) + '_LastName';
		document.getElementById(fnSaveID).value = vFirstName;
		document.getElementById(lnSaveID).value = vLastName;

		// Email cell
		var cell_c = row.insertCell(2);
		textNode = document.createTextNode(vEmail);
		cell_c.appendChild(textNode);
		var emailSaveID = 'AffRef' + (iteration) + '_Email';
		document.getElementById(emailSaveID).value = vEmail;

		// Phone cell
		var cell_d = row.insertCell(3);
		var vPhoneAll = (vPhoneExt!='') ? vPhone +" x"+vPhoneExt:vPhone;
		textNode = document.createTextNode(vPhoneAll);
		cell_d.appendChild(textNode);
		var phoneSaveID = 'AffRef' + (iteration) + '_Phone';
		document.getElementById(phoneSaveID).value = vPhone;
		var phoneExtSaveID = 'AffRef' + (iteration) + '_PhoneExt';
		document.getElementById(phoneExtSaveID).value = vPhoneExt;


		// Company cell
		var cell_e = row.insertCell(4);
		textNode = document.createTextNode(vCompany);
		cell_e.appendChild(textNode);
		var companySaveID = 'AffRef' + (iteration) + '_Company';
		document.getElementById(companySaveID).value = vCompany;
		
	}
}

function CreateBookmarkLink(pageTitle,pageURL) {
	var title = pageTitle; 
	var url = pageURL;

	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url,"");
	} else if( window.external ) { // IE Favorite
		window.external.AddFavorite( url, title); }
	else if(window.opera && window.print) { // Opera Hotlist
		return true; }
}

 
function urlencode(str) {
	str = escape(str);
	str = str.replace('+', '%2B');
	str = str.replace('%20', '+');
	str = str.replace('*', '%2A');
	str = str.replace('/', '%2F');
	str = str.replace('@', '%40');
	return str;
}

	function openMap(address){
		addr = urlencode(address);
		var url = "http://maps.google.com/maps?f=q&hl=en&geocode=&q=" + addr + "&ie=UTF8&z=17&iwloc=addr"
		var opts = "menubar=1,status=0,width=800,height=600";
		//http://maps.google.com/maps?f=q&hl=en&geocode=&q=332+Marine+Avenue,+Balboa+Island,+CA+92662&sll=37.0625,-95.677068&sspn=49.757664,114.257812&ie=UTF8&z=17&iwloc=addr
		window.open(url);
	}
	

