// Function for making the AJAX object
function createRequestObject() {
   var request;
	// Firefox, Safari, Opera... 
	if(window.XMLHttpRequest){ 
		request = new XMLHttpRequest();
	// Internet Explorer 5+ 
	} else if(window.ActiveXObject) {
		request = new ActiveXObject("Msxml2.XMLHTTP");
	// There is an error or an old browser is being used.
	} else if(window.ActiveXObject) {
		request = new ActiveXObject("Microsoft.XMLHTTP");
	// There is an error or an old browser is being used.
	}else {
		request = null;
		alert('Problem creating the XMLHttpRequest object');
	}
	return request;
} 

var http = createRequestObject(); 
var timestamp = new Date().getTime();
var strDiv = '';

function sendRequest(act, Div, iCountry, iRegion, bRed) {
	strDiv = Div;
   strURL = '/ajax.asp?act='+act+'&iCountry='+escape(iCountry)+'&iRegion='+escape(iRegion)+'&bRed='+bRed+'&timestamp='+timestamp
   //alert(strURL);
   http.open('get', strURL); 
   // When ready, handle the response
   http.onreadystatechange = handleResponse;
   // Send nothing (GET has already passed values)
   http.send(null);
}

function handleResponse(){
	if(http.readyState == 4){
		if(http.status == 200){ 
			var response = http.responseText; 
			//alert(response);
			//alert(strDiv);
			if (response == 'redirect'){
				CheckForm(0);
			}else if(response == 'redirectall'){
				CheckForm(1);
			}else{
				document.getElementById(strDiv).innerHTML = response; //response.substr(response.length);
			}
		}
	}
}

function populateDD(strDD, bRed){
	var oform = document.getElementById("myform");
	strCountry = oform.CountryID.options[oform.CountryID.options.selectedIndex].value;
	strCounty = "";
	if(oform.iRegion && oform.iRegion.options.selectedIndex > 0){
		strCounty = oform.iRegion.options[oform.iRegion.options.selectedIndex].value;
	}
	strTown = "";
	if(oform.iTown && oform.iTown.options.selectedIndex > 0){
		strTown = oform.iTown.options[oform.iTown.options.selectedIndex].value;
	}
	
	if(strDD == 'regionDD'){
		//clear other dd's
		if(document.getElementById('townDD')){
			document.getElementById('townDD').innerHTML = '';
		}
		sendRequest('regions', 'regionDD', strCountry, '', bRed);
	}else if(strDD == 'TownDD'){
		sendRequest('towns', 'townDD', strCountry, strCounty, bRed);
	}
}

function CheckForm(iAll){
	var oform = document.getElementById("myform");
	iCountry = oform.CountryID.options[oform.CountryID.options.selectedIndex].value;
	strCountry = oform.CountryID.options[oform.CountryID.options.selectedIndex].text;
	strCountry = strCountry.replace(/ /g, "_");

	if(iAll == 1){
		strCounty = "All_Regions";
	}else{
		strCounty = "";
		if(oform.iRegion && oform.iRegion.options.selectedIndex > 0){
			strCounty = oform.iRegion.options[oform.iRegion.options.selectedIndex].text;
			strCounty = strCounty.replace(/ /g, "_");
		}else if(document.getElementById('sRegion')){
			strCounty = document.getElementById('sRegion').value;
		}
	}
	
	strTown = "";
	if(oform.iTown && oform.iTown.options.selectedIndex > 0){
		strTown = oform.iTown.options[oform.iTown.options.selectedIndex].text;
		strTown = strTown.replace(/ /g, "_");
	}
	
	//set cookies
	if(oform.iWorkstations){
		//alert(oform.iWorkstations.value)
		createCookie('Workstations',oform.iWorkstations.value);
	}
	if(oform.iRequiredByDate){
		//alert(oform.iRequiredByDate.value)
		createCookie('RequiredByDate',oform.iRequiredByDate.value);
	}
	
	if(iCountry != ""){
		//country is selected
		if(strCounty == "" && strTown == ""){
			//redirect to selected county to pick region
			var sURL = "/" + strCountry + "/";
			//alert(sURL);
			document.location.href = sURL; 
		}else if(strCounty != "" && strTown == ""){
			//redirect to search
			var sURL = "/offices/" + strCountry + "/" + strCounty + "/" ;
			//alert(sURL);
			document.location.href = sURL; 
			
		}else if(strCounty != "" && strTown != ""){
			//redirect to search
			var sURL = "/offices/" + strCountry + "/" + strCounty + "/" + strTown + "/" ;
			//alert(sURL);
			document.location.href = sURL; 
		}
		
	
	}
	
	
}


$(document).ready(function() {
	$("a.iframe").fancybox({
	'width': 600,
	'height': 480,
	'hideOnContentClick'    : false					   
	});
});

function requestView(officeID, href){
	//fudge to create a cookie for checking
	var officeIDs = '';
	officeIDs = readCookie('OfficeIDs');
	if(officeIDs==null || officeIDs=='null' || officeIDs==''){
		//alert('null');
		officeIDs = ',';
		createCookie('OfficeIDs',officeIDs);
	}
	//alert(officeIDs);
	
	
	var myProspectID = readCookie('myProspectID');
	if (myProspectID) {
		// send request using the magic image request
		//alert('doing hidden thing');
		if(document.images){
			//alert("requestDetails_process.asp?v=" + href + "&OfficeID=" + officeID);
			new Image().src= "requestDetails_process.asp?v=" + href + "&OfficeID=" + officeID;
		}
			
		//alert(href);
		if (href == 1){ 
			LinkText = "Viewing Requested";
			divName = "View";
		}else if(href == 0){
			LinkText = "Details Requested";
			divName = "Details";
		}
		//alert(divName);

		//EWC do the office record
		if(document.getElementById(divName + '_' + officeID)){
			document.getElementById(divName + '_' + officeID).innerHTML = '<span style="color:red;">' + LinkText + '</span>';
		}
		//EWC do the prime office record
		if(document.getElementById(divName + '_' + officeID + '_prime')){
			document.getElementById(divName + '_' + officeID + '_prime').innerHTML = '<span style="color:red;">' + LinkText + '</span>';
		}
		
		//alert('finish doing hidden thing');
	}else{
		var j1 = document.getElementById('hiddenTrigger');
		j1.href = 'requestDetails.asp?v=' + href + '&id=' + officeID;
		$('#hiddenTrigger').trigger('click');
	}
}
