
/* This is the standard javascript file for public output */

// Glbal variable for highlighting and lowlighting object's colour
var storedMenuColor="";
var storedBackColor="";
var storedColor="";

function highlight(obj){
	// changes obj color to highlight colour
	storedMenuColor=obj.style.color;
	storedBackColor=obj.style.backgroundColor;
	obj.style.color="#d9d9d9";
	obj.style.backgroundColor="#111755";
	}
	
function lowlight(obj){
	// reverts object colour to original colour
	obj.style.color=storedMenuColor;
	obj.style.backgroundColor=storedBackColor;
	}	

function texthighlight(obj){
	// changes obj text to bold
	storedColor=obj.style.color;
	obj.style.color="#FF2222";
	}
	
function textlowlight(obj){
	// reverts object text to normal
	obj.style.color=storedColor;
	}		
	
	
function loadAdjust(){
		// Code for when page loads
		}
		
function checkEmail(obj) {
		// validates text box object's value for email address
		var str=obj.value;
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
			};
		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
			};
		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
			};
		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
			 };
		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
			 };
		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
			 };
		 if (str.indexOf(" ")!=-1){
		    return false
			 };
 		 return true					
	}

function IsNumeric(strString)
   //  check for valid numeric strings	
   {
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
   }
   
   
   
/*  cookie handlers */

function addToFavourites(idProperty){
	var cookie = readCookie('PROPERTIES');
	if(cookiesAreSwitchedOff()){
		alert("www.wendyshouses.com\r\n\r\n To use the 'my properties' facility on this website \r\n 'cookies' should be switched on in your web browser.\r\n\r\nThankyou");
		}
	else{	
		/* if there is already a cookie on the client */
		if(cookie){
			/* read cookie */
			var cookieContent="PROPERTIES="+readCookie("PROPERTIES");
			var arrProperties=new Array();
			arrProperties=parseProperties(cookieContent);
			if(!propertyExists(arrProperties,idProperty)){
				/* add property to cookie content */
				cookieContent+=","+idProperty;
				/* delete cookie */
				deleteCookie();
				/* save cookie again */
				var content=cookieContent;
				saveCookie(content);
				}
			else{
				alert("www.wendyshouses.com\r\n\r\nYou already have this property stored in your list of properties\r\n\r\n If you want to remove the property from your list, goto to 'My Properties' and click the remove button next to the property.\r\n\r\nThankyou");
				}	
			// alert(arrProperties);
			}
		else{
			/* set new cookie */
			var content = "PROPERTIES="+idProperty;
			saveCookie(content);
			}	
		}	
	}

function removeFromFavourites(idProperty){
	/* read cookie */
	var cookieContent="PROPERTIES="+readCookie("PROPERTIES");
	var arrProperties=new Array();
	arrProperties=parseProperties(cookieContent);	
	var newContent="PROPERTIES=";
	var hasContent=false;
	for(var i=0;i<arrProperties.length;i++){
		if(arrProperties[i]!=idProperty){
			if(hasContent) newContent+=",";
			else hasContent=true;
			newContent+=arrProperties[i];
			}
		}
	/* delete cookie */
	deleteCookie();	
	/* save new cookie  */
	saveCookie(newContent);	
	}
	
	
function saveCookie(content){
	var date = new Date();
	date.setTime(date.getTime()+(3650*24*60*60*1000));
	var expires = "; expires="+date.toGMTString();
	document.cookie = content+expires+"; path=/";	
	}
	
function readCookie(name) {
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(name+"=") == 0) return c.substring(name.length+1,c.length);
	}
	return null;
	}	

function deleteCookie(){
	document.cookie =  "PROPERTIES=;-1;path=/";	
	}	

function cookiesAreSwitchedOff(){
	var date = new Date();
	var rtn=false;
	date.setTime(date.getTime()+(3650*24*60*60*1000));
	var expires = "; expires="+date.toGMTString();
	document.cookie =  "testCookies=on;"+expires+";path=/";
	if(readCookie("testCookies")!="on") rtn=true;
	document.cookie =  "testCookies=;-1;path=/";	
	return rtn;
	}
	
function parseProperties(wrd){
	var thisWord=""
	var rtn=new Array();
	var start=wrd.indexOf("=")+1;
	for(var i=start;i<wrd.length;i++){
		if(wrd.substring(i,i+1)==","){
			rtn.push(thisWord);
			thisWord="";
			}
		else{
			thisWord+=wrd.substring(i,i+1);
			}	
		}
	rtn.push(thisWord);
	return rtn;	
	}	

function propertyExists(arrProperties,num){
	for(var i=0;i<arrProperties.length;i++){
		var thisProperty=arrProperties[i];
		if(num==parseFloat(arrProperties[i])) return true
		}
	return false;	
	}
	

