

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


function newWin(){
	if(document.frmRefineSearch) {
		theForm = document.frmRefineSearch;
		str="Form Elements: "
		if(theForm){
			for (i=0; i<theForm.length; i++) {
				if( theForm.elements[i].type == "checkBox" ){
					theForm.elements[i].checked = true;
				}
				
			}
		}
	}
} 


function submitCountries() {
	
	if(document.frmRefineSearch) {
		
		iChecked = 0;
		cChecked = 0;
		
		countryDivs = getElementsByClass("nonFlashRegionName", "div")
		
		for (i=0; i<countryDivs.length; i++) { 
		
			if(countryDivs[i].style.display == "block") { // check whether a country div has been displayed - if not, we want the form to submit anyway.
				cChecked = 1; // but if it has, we will run a further check to make sure a checkbox has been selected
			}
		}
		
	
		inputs = document.frmRefineSearch.Country;
		
		for (i=0; i<inputs.length; i++) { 
		
			if(inputs[i].checked) {
				iChecked = 1;
			}
		}
		
		//alert("bChecked is: " + bChecked);
		
		if(iChecked == 0 && cChecked == 1) { // no country form checkboxes have been checked AND there is a country Div displayed
			// show dialog with OK button 
			alert("Please select one or more countries from the list.")
			return false;
		} else {
			//alert("else");
			return true;
		}
	}
}



function test(id){
	elems = document.getElementsByTagName("DIV");
	for (i=0; i< elems.length; i++){
		if(elems[i].id == id){
			elems[i].style.display = "block";
		}		
	}
}


// functions which show and hide a html element when you pass it in as follow:
// onclick="showById('id_of_element');"
function showByID(id) {
	elem = document.getElementById(id);
	elem.style.display = "block";
}

function hideByID(id) {
	elem = document.getElementById(id);
	elem.style.display = "none";
}

function toggle(id){
	elem = document.getElementById(id);
	if(elem.style.display == 'none') {
		elem.style.display = 'block'
	} else {
		elem.style.display = 'none'
	}
}


// functions which show and hide a group of html elements when you pass in their classname as follows:
// onclick="showById('id_of_element');"
function showByClass(elemType, elementClass) {
	
	aElems = document.getElementsByTagName(elemType);
	
	for(i=0; i < aElems.length; i++) {
		if(aElems[i].getAttribute("className")){
			thisClass = aElems[i].getAttribute("className");
		} else {// firefox uses .class
			thisClass = aElems[i].getAttribute("class");
		}
		
		if(thisClass == elementClass) {
			aElems[i].style.display = "block";	
		}
	}
}

function hideByClass(elemType, elementClass) {
	
	aElems = document.getElementsByTagName(elemType);
	
	for(i=0; i < aElems.length; i++) {
		if(aElems[i].getAttribute("className")){
			thisClass = aElems[i].getAttribute("className");
		} else {// firefox uses .class
			thisClass = aElems[i].getAttribute("class");
		}
		
		if(thisClass == elementClass) {
			aElems[i].style.display = "none";	
		}
	}
}

function toggleRegion(elemType, elementClass) { 
	
	var thisClass = "";
	var myElementClass = "region_" + elementClass;
	var myTitleClass = "title_" + elementClass;

	aElems = document.getElementsByTagName(elemType);
	
	for(i=0; i < aElems.length; i++) {

		if(aElems[i].getAttribute("className")){
			thisClass = aElems[i].getAttribute("className");
		} else {// firefox uses .class
			thisClass = aElems[i].getAttribute("class");
		}
		
		if(thisClass) {
			if(thisClass.match("region_")) {
				aElems[i].style.display = "none";	
			}
			
			if(thisClass == myElementClass) {
				aElems[i].style.display = "block";	
			}	
			
			if(thisClass.match("title_")) {
				aElems[i].style.fontWeight = "normal";	
			}

			if(thisClass == myTitleClass) {
				aElems[i].style.fontWeight = "bold";	
			}	
			
			var match = "details_" + elementClass;
			if(thisClass.match(match)) { // matches all details_{$ThisRegionName}_x divs
				
				var thisDiv = document.getElementsByTagName("DIV");

				for(i=0; i < thisDiv.length; i++) {
					if(thisDiv[i].className.match("details_")) {
						if(thisDiv[i].style.display == "block") {
							thisDiv[i].style.display = "none";
							var networkFillerDiv = document.getElementById("networkFiller");
							networkFillerDiv.style.display = "block";
						}	
					}	
				}	
			}
		}
	}	
}


function toggleDetails(elemType, elementClass){
	
	var thisClass = "";
	var myElementClass = "details_" + elementClass;

	aElems = document.getElementsByTagName(elemType);
	
	for(i=0; i < aElems.length; i++) {
		
		if(aElems[i].getAttribute("className")){
			thisClass = aElems[i].getAttribute("className");
		} else {// firefox uses .class
			thisClass = aElems[i].getAttribute("class");
		}
		
		if(thisClass) {
			if(thisClass.match("details_")) {
				aElems[i].style.display = "none";		
			}
			
			if(thisClass == myElementClass) {
				aElems[i].style.display = "block";	
				document.getElementById("networkFiller").style.display = "none";
			}	
		}
	}	
}

function deleteFields(id, numberOfcountries, countryName) {	
	
	// for some insane reason this syntax does not work:
	// "document.forms['frmRefineSearch'].Country"
	// so use:
	// "document.forms[1].elements[4]"
	// instead. I don't know why.
	
	countryValue = document.forms[1].elements[4].value;
	myStr = id + "|";
	
	if (countryValue.match(myStr)) {
		newcountryValue = countryValue.replace(myStr,"");
		countryValue = newcountryValue;	
		document.forms[1].elements[4].value = countryValue;
	}
	
	deletes = getElementsByClass("deleteCountryButton", "div")
	bDisplay = "none";
	numberOfDeletedItems = 0;
	
	for (i=0; i <deletes.length; i++) { 
		if (deletes[i].style.display == "none") {
			numberOfDeletedItems = numberOfDeletedItems + 1;
		}
	}
	
	if(numberOfcountries == numberOfDeletedItems) {
		document.getElementById("allCountriesDeleted").style.display = "block";
	} 

	checkboxClass = "checkbox" + countryName;
	regions = getElementsByClass(checkboxClass, "input")
	
	for(i=0; i<regions.length; i++) {
		regions[i].checked = false;
	}	
}


function checkAllCountriesDeleted(url, mode) {
	
	if(mode != 'Match' && mode !='InvestmentType') {
	var url = location.href;
	url = url.toString();
	var myMatch = "Country=&Lifestyle";
	
	if(url.match(myMatch)) {
		document.getElementById("allCountriesDeleted").style.display = "block";
	} 
	
	deleteButtons = getElementsByClass("deleteCountryButton", "div")
	
	//alert("delete= " + deleteButtons.length);
	if (deleteButtons.length == 0) {
		document.getElementById("allCountriesDeleted").style.display = "block";
	}
	
	for(i=0; i<deleteButtons.length; i++) {
		
		if(deleteButtons[i].style.display == "block") {
			//don't display the allCountriesDeleted text since at least one of them is on.
		} else {
			document.getElementById("allCountriesDeleted").style.display = "block";
		}
		
		
	}
	}
	
	
}



function toggleChecked(countryName) { 
	//alert(countryName);
	
	if(document.frmRefineSearch.elements['all' + countryName]){
	
		bChecked = document.frmRefineSearch.elements['all' + countryName].checked; 
		
		inputs = document.getElementsByTagName("INPUT");
		//alert(inputs.length);
		
		for (i=0; i <inputs.length; i++) {
			if(inputs[i].getAttribute("type") == "checkbox") {
				
				str = inputs[i].id;
				//alert(str);
				if (str.match(countryName)) {
					document.getElementById(str).checked = bChecked;
				}
			}
		}
	}
	
} 



function toggleIndeterminate(countryName)  { 
	//alert(countryName);
	
	bChecked = true; 
	nChecked = 0; 
	checkBoxGroup = "checkbox" + countryName
	
	inputs = getElementsByClass(checkBoxGroup, "input")
	
	if(inputs.length) { 
		bChecked = true; 
		nChecked = 0; 
					
		for(i = 1; i < inputs.length; i++) {
			
			str = inputs[i].id;
						
			if(document.getElementById(str).checked) {
				nChecked++; 
			}
							
			if(nChecked < inputs.length - 1) { 
	    	
				if(nChecked) { 
					bChecked = false 
				} else { 
					bChecked = false;
				} 
						    
			} else  {
				bChecked = true
			}
			
			document.frmRefineSearch.elements['all' + countryName].checked = bChecked; 	
						
		}	
	}
}

function toggleCheckedHome(countryName) { 
	//alert(countryName);
	
	if(document.frmRefineSearch.elements['all' + countryName]){ //check the all field for the country exists
	
		bChecked = document.frmRefineSearch.elements['all' + countryName].checked; // get the checked value of the field
		
		inputs = document.getElementsByTagName("INPUT"); // get all the inputs
		//alert(inputs.length);
		
		for (i=0; i <inputs.length; i++) { // loop through inputs
			if(inputs[i].getAttribute("type") == "checkbox") { // until you find a checkbox
				
				str = inputs[i].id; // get the id value of the checkbox
				//alert(countryName+ " match: " + str);
				if (str.match(countryName)) {  //if it is the same as the countryName
					//alert(countryName+ " match: " + str);
					document.getElementById(str).checked = bChecked; //set it to the value of it's All field (grabbed above line 378)
				}
			}
		}
	}
	
} 


function toggleIndeterminateHome(countryName)  { 
	//alert(countryName);
	
	bChecked = true; 
	nChecked = 0; 
	checkBoxGroup = "checkbox" + countryName
	
	inputs = getElementsByClass(checkBoxGroup, "input")
	
	if(inputs.length) { 
		bChecked = true; 
		nChecked = 0; 
					
		for(i = 1; i < inputs.length; i++) {
			
			str = inputs[i].id;
			
			if(document.getElementById(str).checked) {
				nChecked++; 
			}
							
			if(nChecked < inputs.length - 1) { 
	    	
				if(nChecked) { 
					bChecked = false 
				} else { 
					bChecked = false;
				} 
			
			} else  {
				bChecked = true
			}
			
			document.frmRefineSearch.elements['all' + countryName].checked = bChecked; 	
						
		}	
	}
}


function checkBoxes(countryName) {
	checkBoxGroup = "checkbox" + countryName
	inputs = getElementsByClass(checkBoxGroup, "input")
	for (i=0; i <inputs.length; i++) { 
		inputs[i].checked = true;
	}
}

function uncheckBoxes(countryName) {
	
	checkBoxGroup = "checkbox" + countryName
	inputs = getElementsByClass(checkBoxGroup, "input")
	for (i=0; i <inputs.length; i++) { 
		inputs[i].checked = false;
	}
}

function toggleCheckedType(oElement) { 
	oForm = oElement.form; 
	oElement = oForm.elements[oElement.name]; 
	if(oElement.length) { 
		bChecked = oElement[0].checked; 
		for(i = 1; i < oElement.length; i++) {
			oElement[i].checked = bChecked; 
		}
	}
} 


function toggleIndeterminateType(oElement)  { 
	oForm = oElement.form; 
	oElement = oForm.elements[oElement.name]; 
	
	if(oElement.length) { 
					
		bChecked = true; 
		nChecked = 0; 
					
		for(i = 1; i < oElement.length; i++) {
				
			if(oElement[i].checked) {
				nChecked++; 
			}
							
			if(nChecked < oElement.length - 1) { 
	    	
				if(nChecked) { 
					bChecked = false 
				} else { 
					bChecked = false;
				} 
						    
			} else  {
				bChecked = true
			}
			
			oElement[0].checked = bChecked; 				
		}	
				
	} 
}

function getElementsByClass(searchClass,tag) {

	var classElements = new Array();
	var els = document.getElementsByTagName(tag); 
	var elsLen = els.length;
	var pattern = new RegExp("\\b"+searchClass+"\\b");

	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}

	return classElements;

}

function OpenPropertyEnquiryForm(propertyid){
	newWindow = window.open('ContactUsPopUp.aspx?propertyid=' + propertyid, 'ContactUs', 'scrollbars,width=450,height=600');
	newWindow.focus();
}

function OpenRegisterPopup(){
	newWindow = window.open('EmailNewsletterRegistration.aspx', 'EmailRegistration', 'scrollbars,width=450,height=325');
	newWindow.focus();
}

function OpenPropertyAlertRegistration(){
	newWindow = window.open('PropertyAlertRegister.aspx', 'PropertyAlertRegistration', 'scrollbars = 1,width=450,height=650');
	//newWindow.focus();
}

function OpenRegistration(partnersiteurl, propertyId){

	newWindow = window.open('Register.aspx?partnersiteurl=' + partnersiteurl + '&amp;propertyid=' + propertyId, 'PropertyAlertRegistration', 'scrollbars = 1, resizable = 1,width=435,height=296');
	newWindow.focus();
}

function OpenContactUs(){
	newWindow = window.open('ContactUs.aspx', 'ContactUs', 'width=800,height=600');
	newWindow.focus();
}

function OpenRegistrationForm(partnersiteurl, propertyId){
	/*Have removed Property Id from this, suspect will need to add this in*/
	newWindow = window.open('Register.aspx?partnersiteurl=' + partnersiteurl + '&amp;propertyid=' + propertyId, 'PropertyAlertRegistration', 'scrollbars = 1, resizable = 1,width=435,height=296');
	newWindow.focus();
}

function OpenContactUsPartner(networkid){
	
	newWindow = window.open('ContactUsPartner.aspx?networkid=' + networkid,  'ContactUsPartner', 'scrollbars,width=450,height=600');
	newWindow.focus();
}

//TermsAndConditions.htm
function OpenTermsAndConditions(){
	newWindow = window.open('html/TermsAndConditions.htm',  'TermsAndConditions', 'scrollbars,width=389,height=600');
	newWindow.focus();
}

function clearform(formIdent) {  
	var form, elements, elemonts, i, elm, elmo; 
	form = document.getElementById? document.getElementById('formIdent') : document.forms[formIdent]; 
	if (document.getElementsByTagName) 
	{ 
		elements = document.getElementsByTagName('input'); 
		for( i=0, elm; elm=elements.item(i++); ) 
		{ 
			if (elm.getAttribute('type') == "text") 
			{ 
				elm.setAttribute('value',''); 
			} 
		} 
		
		elemonts = document.getElementsByTagName('textarea');
		for (i = 0, elmo; elmo=elemonts.item(i++);){
			elmo.setAttribute('value', '');
		}
	} 
	// Actually looking through more elements here 
	// but the result is the same. 
	else 
	{ 
		elements = form.elements; 
		for( i=0, elm; elm=elements[i++]; ) 
		{ 
			if (elm.type == "text") 
			{ 
				elm.value =''; 
			} 
		} 
	} 
} 

function SubmitAndValidate(){
	var blnOk = true;
	
	var strFirstName = document.getElementById("firstname").value;
	var stringLastName = document.getElementById("lastname").value;
	var stringEmailAddress = document.getElementById("emailaddress").value;
	
	if(isOkString(strFirstName, 30) == false){
		alert("You must provide your first name, which must be less than 30 characters");
		blnOk = false;
		return false;
	}
	if(isOkString(stringLastName, 30) == false){
		alert("You must provide your last name, which must be less than 30 characters");
		blnOk = false;
		return false;
	}
	if(isOkEmail(stringEmailAddress) == false){
		alert("You must provide a valid email address");
		blnOk = false;
		return false;
	}
	if(blnOk = true){
		//window.location = "AboutContact.aspx?firstname=" + strFirstName + "&lastname=" + stringLastName + "&email=" + stringEmailAddress;
		//window.location.reload();
		return true;
	}
	else{
		return false;
	}
	
}

function isOkString(strString, maxValue){
	var blnResult = false;
	if(strString != ""){
		if(strString.length > 0 && strString.length <= maxValue){
			blnResult = true;
		}
	}
	return blnResult;
}

function isOkEmail(strEmail){
				
		var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; //' not valid
		var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; //' valid
				
		if (!reg1.test(strEmail) && reg2.test(strEmail)){
			return true;
		}
		return false;
	}


