//14:30 15/03/2007 - AJAX functions

var ajaxTown = createXmlHttpRequestObject();

var townLoc, countyLoc;

townLoc = "town";
countyLoc = "county";

function processTownCode(townVal, countyVal)
{


var url = "towncountyxml.asp?town=" + townVal + "&county=" + countyVal;

	ajaxTown.open("GET",url, true);
	ajaxTown.onreadystatechange = townServerResponse;
		
	ajaxTown.send(null);
}


function townServerResponse()
{

	var townCode, countyCode;
townCode = "";
countyCode = "";


	if(ajaxTown.readyState == 4)
	{
		if(ajaxTown.status == 200)
		{
			//extract the XML
			if(ajaxTown.responseXML.getElementsByTagName("towncode")[0].firstChild != null)
			{
				townCode = ajaxTown.responseXML.getElementsByTagName("towncode")[0].firstChild.data;
			}

			if(ajaxTown.responseXML.getElementsByTagName("countycode")[0].firstChild!=null)
			{
				countyCode = ajaxTown.responseXML.getElementsByTagName("countycode")[0].firstChild.data;
			}

			if(townCode != "" && document.getElementById(townLoc))
			{
				//set the county
				//first, get the type

//alert("UPDATE " + townLoc + " to " + townCode)
				if((document.getElementById(townLoc).type == 'select-multiple') || (document.getElementById(townLoc).type == 'select-one'))
				{
					//document.getElementById(townLoc).selectedIndex = document.getElementById(townLoc).options[townCode];
					document.getElementById(townLoc).options("town_code"+townCode).selected = true;

				}
				else
				{
					document.getElementById(townLoc).value = townVal;
				}
			}
			else
			{
				//alert("can't update");
			}

			if(countyCode != "" && document.getElementById(countyLoc))
			{
				//set the county
				//first, get the type


				if((document.getElementById(countyLoc).type == 'select-multiple') || (document.getElementById(countyLoc).type == 'select-one'))
				{
					//document.getElementById(countyLoc).selectedIndex = 5;//document.getElementById(countyLoc).options[countyCode];
					document.getElementById(countyLoc).options("county_code"+countyCode).selected = true;///
//alert(document.getElementById(countyLoc).options("county_codeWEMI"))

				}
				else
				{
				//	document.getElementById(countyLoc).value = countyVal;
				}
			}

			
		}
	}

}


function createXmlHttpRequestObject()
{
	var xmlHttp;
	
	//IE
	if(window.ActiveXObject)
	{
		try
		{
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e)
		{
			xmlHttp = false;
		}
	}
	else
	{
		//Mozilla etc
		try
		{
			xmlHttp = new XMLHttpRequest();
		}
		catch(e)
		{
			xmlHttp = false;
		}
	}
	if(xmlHttp)
	{
		return xmlHttp;
	} 

}

//10:02 08/03/2007 JC - function to add the popup QAS page
 function QAS_Popup(sAddress, sTown, sCounty, sPostcode)
		  {	
			var sParam;
			sParam = "?address=" + sAddress;
			sParam += "&town=" + sTown;
			sParam += "&county=" + sCounty
			sParam += "&postcode=" + sPostcode;
//aegis 0900062 kc 07/04/2009 - upgrade to QAS ProWeb version 6
//		QAS_PRO = window.open("address_from_postcode.asp" + sParam,"QAS", "scrollbars=yes,resizable=yes,width=650,height=400"); 
QAS_PRO = window.open("FlatPrompt.aspx", "QAS", "scrollbars=yes,resizable=yes,width=830,height=500,left=300,top=200"); 
		  }

//16:40 07/03/2007 JC - hacks to fix the issue with the Milonic code
function hackMenu(id)
{

	var obj;
	var objStr = "menutabspan" + id;
	obj = document.getElementById(objStr)
	//alert(obj.innerHTML)
	obj.style.color = 'white';

	objStr = "menutabspana" + id;
	obj = document.getElementById(objStr)
	//alert(obj.innerHTML)
	obj.style.color = 'white';
}

function hackOutMenu(id, colour)
{
var obj;
	var objStr = "menutabspan" + id;
	obj = document.getElementById(objStr)
	//alert(obj.innerHTML)
	obj.style.color = colour;

	objStr = "menutabspana" + id;
	obj = document.getElementById(objStr)
	//alert(obj.innerHTML)
	obj.style.color = colour;

}

var tieHandlers;
tieHandlers = new Object();

//global variable for pop-up window
var popWin;
var profWin;

function addTieHandler(startObj, endObj, eventString)
{
	tieHandlers[startObj] = new TieHandler(endObj, eventString);
	
	document.getElementById(startObj).tieHandler = tieHandlers[startObj];
	document.getElementById(startObj).onclick = clickTiedElement;
	document.getElementById(startObj).style.cursor = "hand";
}

function getTieHandler(id)
{
	return tieHandlers[id];
}


function tieObject(objRef, targetRef)
{
	if((document.getElementById(objRef) != null) && (document.getElementById(targetRef) != null))
	{
		document.getElementById(objRef).tiedObject = targetRef;
		return true;
	}
	return false;
}

function tieRadioObject(objRef, targetRef, ind)
{
	if((document.getElementById(objRef) != null) && (document.getElementById(targetRef) != null))
	{
		document.getElementById(objRef).tiedObject = targetRef;
		return true;
	}
	return false;
}

function tieClick(objRef, targetRef)
{
	if(tieObject(objRef, targetRef))
	{
		document.getElementById(objRef).onclick = document.getElementById(targetRef).onclick
		addTieHandler(objRef, targetRef, "onclick");
	}	
}

function tieRadioClick(objRef, targetRef, ind)
{
	if(tieRadioObject(objRef, targetRef, ind))
	{
		document.getElementById(objRef).onclick = document.getElementById(targetRef).onclick
		addTieHandler(objRef, targetRef, "onclick", ind);
	}
}


function TieHandler(endObj, eventString)
{
	this.endObj = endObj;
	this.eventString = eventString;
	this.run = TieHandler_run;
}

function TieHandler_run()
{
	var th = getTieHandler();
	clickElement(th.endObj);
}

function setElement(objRef, value)
{


}

function speakerAdded(val)
{
	//var csvAry = opener.document.getElementById("speakers").value.split(",");
	var csvAry = csvify(opener.document.getElementById("speakers").value);

	for(var i = 0; i < csvAry.length; i++)
	{
		if(csvAry[i] == val)
		{
			return true;
		}
	}
	return false;
}

function passSpeakerValues(val, speaker, contactCounter)
{
	//alert("VAL : " + val)
	//alert("SPEAKER: " + speaker)
	//alert("CC : " + contactCounter)
	//get reference to the section to write to
	var namesField = opener.document.getElementById("speakers_names");
	var hiddenField = opener.document.getElementById("speakers");

	//if it's not in the hidden field CSV already, add it in
	if(speakerAdded(val) != true)
	{
		var docRef;
		docRef = opener.document;

		var csvAry = csvify(hiddenField.value);

		csvAry[csvAry.length] = val;
		csvAry[csvAry.length] = contactCounter;

		hiddenField.value = csvAry.join(",");
		var namesAry = csvify(namesField.value);
		namesAry[namesAry.length] = speaker;

		namesField.value = namesAry.join(",");

		showSpeakers(docRef);
	}
	window.close();
}

function removeSpeaker(val)
{
	//alert("REMOVE FROM " + document.getElementById("speakers").value)
	//remove from csv
	var id_newAry;
	id_newAry = new Array();
	
	var name_newAry;
	name_newAry = new Array();

	var id_csvAry = csvify(document.getElementById("speakers").value);
	
	var name_csvAry = csvify(document.getElementById("speakers_names").value);

	for(var i = 0; i < id_csvAry.length; i=i+2)
	{
		//alert(id_csvAry[i] + "==" + val + "?")
		if(id_csvAry[i] != val)
		{
			id_newAry[id_newAry.length] = id_csvAry[i];
			id_newAry[id_newAry.length] = id_csvAry[i+1];
			name_newAry[name_newAry.length] = name_csvAry[(i/2)];
		}
		else
		{
				//alert("REMOVE VAL : " + id_csvAry[i])
	//alert("REMOVE SPEAKER: " + name_csvAry[(i/2)])
	//alert("REMOVE CC : " + id_csvAry[i+1])
		}
	}
	document.getElementById("speakers").value = id_newAry.join(",");
	document.getElementById("speakers_names").value = name_newAry.join(",");

	var docRef;
	docRef = document;

	showSpeakers(docRef);
}

function csvify(str)
{
	if(str.length == 0)
	{
		return new Array();
	}
	else
	{
		return str.split(",");
	}
}

function showSpeakers(docRef)
{
//alert("showspeakers")
	var html;
	html = "<ul>";

	//get the CSV, and just display them as a list
	var namesAry = csvify(docRef.getElementById("speakers_names").value);
	var idsAry = csvify(docRef.getElementById("speakers").value);
	
	for(var i = 0; i < namesAry.length; i++)
	{
		html += "<li>" + namesAry[i];
		html += "<span onclick=\"openSpeakerProfileWindow('" + idsAry[(i*2)] + "')\"><b>EDIT PROFILE</b></span>" + " "; 

		html += "<span onclick=\"removeSpeaker('" + idsAry[(i*2)] + "')\"><b>REMOVE SPEAKER</b></span></li>"

	}
	html += "</ul>";

	if(namesAry.length > 0)
	{
		docRef.getElementById("speakers_div").innerHTML = html;
	}
	else
	{
		docRef.getElementById("speakers_div").innerHTML = "";
	}

}


function writeSpeakerOption(speaker, val)
{
	var html;
	html = "<li id='" + val + "'>" + speaker;

	// now add the removal option

	html = html + " <span onclick=\"removeSpeaker('" + val + "')\"><b>REMOVE SPEAKER</b></span>"

	html = html + "</li>"

	return html;
}


function openPopUpWindow(url, name, features)
{
	popWin = window.open(url, name, features)
	popWin.focus();
}

function openAddSpeakerWindow()
{
	openPopUpWindow("add_speaker.asp","speaker","")
}

function openEditSpeakerWindow()
{
	openPopUpWindow("edit_speaker.asp","speaker","")
}


function openSpeakerProfileWindow(memberCode)
{
	if(profWin == this)
	{
		profWin = null;
	}
	profWin = window.open("speaker_profile.asp?membercode=" + memberCode, "speakerprofile", "")
	profWin.focus();

}


function clickTiedElement()
{
	var th = getTieHandler(this.id);

	if(!(document.getElementById(th.endObj).type=="radio" && document.getElementById(th.endObj).checked))
	{
		document.getElementById(th.endObj).checked = !document.getElementById(th.endObj).checked;
		document.getElementById(th.endObj).fireEvent("onclick");
	}
}


function FormValidator()
{
	this.requiredFields = new Array();
	this.addRequiredField = FormValidator_addRequiredField;
	this.conditionalChecks = new Array();
	this.addConditionalCheck = FormValidator_addConditionalCheck;
	this.execute = FormValidator_execute;
}

function FormValidator_addConditionalCheck(functionText, msg)
{
	this.conditionalChecks[this.conditionalChecks.length] = new ConditionalCheck(functionText, msg);
}

function ConditionalCheck(functionText, msg)
{
	this.functionText = functionText;
	this.msg = msg;
	this.execute = ConditionalCheck_execute;
}

function ConditionalCheck_execute()
{
	//alert("EXECUTING CC")
	var result;
	result = eval(this.functionText);
	return result;
}

/*
function testCondition()
{
	if(document.getElementById("field1").value != document.getElementById("field2").value)
	{
		return false;
	}
	else
	{
		return true;
	}
}
*/

//var months = new Array("January","February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")

function validateDate(dateStr)
{
	//var basicError = "Please enter a valid date";

	if(dateStr.length > 0)
	{

		var dateAry;
		dateAry = dateStr.split("/");
		if(dateAry.length < 3)
		{
			return false;
		}

		var parseDay, parseMonth, parseYear;
		parseDay = parseInt(dateAry[0]);
		parseMonth = parseInt(dateAry[1]);
		parseYear = parseInt(dateAry[2]);
		
		if(isNaN(parseDay))
   		{
    			return false;	
   		}
		if(isNaN(parseMonth))
   		{
    			return false;	
   		}
		if(isNaN(parseYear))
   		{
    			return false;	
   		}  

		if(parseMonth > months.length)
		{
			return false;
		}

		if(parseYear < 1800)
		{
			return false;
		}
		if(parseYear > 9999)
		{
			return false;
		}

		var testDate = new Date(parseDay + " " + months[parseMonth] + " " + parseYear);

		if(parseMonth != testDate.getMonth())
		{
			return false;
		}	
		return true;		


	}
	return false;
}






function RequiredField(id, msg)
{
	this.id = id;
	this.msg = msg;
}

function FormValidator_addRequiredField(id, msg)
{
	this.requiredFields[this.requiredFields.length] = new RequiredField(id, msg);
}

function FormValidator_execute()
{
	var errorsArray;
	errorsArray = new Array();

	for(var i = 0; i < this.requiredFields.length; i++)
	{
		if(document.getElementById(this.requiredFields[i].id).value == "")
		{
			errorsArray[errorsArray.length] = this.requiredFields[i].msg;
		}
	}
	//alert("now conditional checks")

	//now the conditional checks
	for(i = 0; i < this.conditionalChecks.length; i++)
	{
		//alert("cc " + i)
//this.conditionalChecks[i].execute()
		if(this.conditionalChecks[i].execute() == false)
		{
			errorsArray[errorsArray.length] = this.conditionalChecks[i].msg;
		}
	}

	if(errorsArray.length > 0)
	{
		var errorMsg;
		errorMsg = "";
		for(i = 0; i < errorsArray.length; i ++)
		{
			if(i > 0)
			{
				errorMsg += "\n";
			}
			errorMsg += errorsArray[i];		
		}		
		alert(errorMsg);
		return false;
	}
	else
	{
		return true;
	}
}

/*
remove whitespace
*/
function trim(val)
{
	var result;
	result = val;
	
	result = ltrim(rtrim(result));
	return result;
}

function ltrim(val)
{
	var result;
	result = val;
	for(var i = 0; i < result.length; i++)
	{
		if(result.charAt(i) != " ")
		{
			return result.substr(0);
		}
	}
	return "";
}

function rtrim(val)
{
	var result;
	result = val;
	//for(var i = 0; i < result.length; i++)
	for(var i = result.length-1; i >= 0; i--)
	{
		if(result.charAt(i) != " ")
		{
			return result.substr(0);
		}
	}
	return "";
}

function validateDateOfBirth(dobfield)
{
	var dob = document.getElementById(dobfield);

	var splitAry;
	splitAry = dob.value.split("/");

	if(splitAry.length != 3)
	{
		return false;
	}
	else
	{
		var len = splitAry.length;
		for(var i = 0; i < len ; i++)
		{
			switch(i)
			{
				case 0:
					//if(trim(splitAry[i]).toString().length > 2)
					if(splitAry[i].toString().length > 2)
					{
						return false;
					}
				break;

				case 1:
					if(splitAry[i] > 12)
					{
						return false;
					}		
				break;

				case 2:
					if(splitAry[i].toString().length != 4)
					{
						return false;
					}
				break;
			}
		}
	}

	return true;
}

function goToQuickLink(selectID)
{
	var selectObj;
	selectObj = document.getElementById(selectID);
	alert(selectObj.value);
	//if

	document.location.href = val;
}


