/*<![CDATA[*/
// ########### getSearchAsArray() USEFULL FOR OTHER PROJECTS #################
function getSearchAsArray(){//FUNCTION SPLITS GET VARIABLES INTO ARRAYS.
	var results = new Array();
	var input = unescape(location.search.substr(1));//TAKE ALL TEXT AFTER ? IN URL
	if(input){//IF GET VARIABLES EXIST
		var srchArray = input.split("&");//SEPARATE DIFFERENT VALUE / NAME PAIRS
		var tempArray = new Array();
		for(var i = 0;i<srchArray.length;i++){//LOOP THROUGH ALL PAIRS
			tempArray = srchArray[i].split("=");// SPLIT VALUES AT = SIGN
			abc = results[tempArray[0]]=tempArray[1];//PRODUCE ARRAY ARRAY[NAME] = VALUE
		}
	}
	return results; //RETURN VALUES ARRAY
}

// ########### random cookie enabled banner swaper #################
		
function setCookie(c_name,c_value,c_expiredays){
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+c_expiredays);
	document.cookie=c_name+ "=" +escape(c_value)+
	((c_expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}
		
function getCookie(c_name)	{
	if (document.cookie.length>0){
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1){ 
			c_start=c_start + c_name.length+1; 
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		} 
	}
	return "";
}
		
function make_list_from_cookie(){
	cookie_crumbs = getCookie('chosen_array')
	if(cookie_crumbs!="") cookie_crumbs = cookie_crumbs.split(",");
	
	if(cookie_crumbs.length>0){
		for(i=0;i<cookie_crumbs.length;i++){
			picturesChosen.push(cookie_crumbs[i]);
		}
	}
}
function remove_used_images(){
	for(i=0;i<picturesToChooseFrom.length;i++){
		for(j=0;j<picturesChosen.length;j++){
			if(picturesChosen[j] == picturesToChooseFrom[i]){
				picturesToChooseFrom.splice(i,1);
				remove_used_images();
			}
		}
	}
}
function add_used_image(r){
	picturesChosen.push(picturesToChooseFrom[r]);
}
function get_random_image(){
	random = Math.random()*(picturesToChooseFrom.length-1);
	return Math.floor(random);
}
		
function make_cookie_from_list(){			// SET NEW COOKIE VALUE 
	setCookie("chosen_array",picturesChosen.toString(),365);
}
		
function outputResponse(r){
	document.write("<h1>"+picturesChosen[picturesChosen.length-1]+"</h1>");
	document.write(getCookie('chosen_array'));
	document.write("<hr>items in picturesChosen Array j["+picturesChosen.length+"]<ul>");
	for(i=0;i<picturesChosen.length;i++){
		document.write("<li>item["+i+"] = "+picturesChosen[i]+"</li>")
	}
	document.write("</ul>");

	document.write("<hr>items in picturesToChooseFrom Array i["+picturesToChooseFrom.length+"]<ul>");
	for(i=0;i<picturesToChooseFrom.length;i++){
		document.write("<li>item["+i+"] = "+picturesToChooseFrom[i]+"</li>")
	}
	document.write("</ul>");
}
		
//                                          #####################
//                                          ### MAIN FUNCTION ###
//											#####################

picturesChosen = new Array();
picturesToChooseFrom = new Array();

function getRandomBanner(){ // returns the value in the main list array randomly 
	
	//NEWID Y LEIN YMA I ADIO'R LLYNIA
//			for(i=1;i<7;i++){
	for(i=1;i<15;i++){
		picturesToChooseFrom.push("images/banners/"+i+".jpg");
	}
	
	/*
	picturesToChooseFrom.push("images/banners/1.jpg");
	picturesToChooseFrom.push("images/banners/2.jpg");
	picturesToChooseFrom.push("images/banners/3.jpg");
	picturesToChooseFrom.push("images/banners/4.jpg");
	*/
	
	make_list_from_cookie();//PRODUCES A LIST OF USED IMAGES
	if(picturesChosen.length==picturesToChooseFrom.length){
		picturesChosen.length = 0;
		setCookie("chosen_array","",365)
	}
	remove_used_images();//  REMOVES THE ANY IMAGES IN FULL LIST THAT ARE IN THE USED LIST
	random_image = get_random_image(); //GET RANDOM IMAGE IN LIST
	
	add_used_image(random_image); //ADD LAST IMAGE USED TO LIST OF USED IMAGES.	
	remove_used_images();// REMOVES THE ANY IMAGES IN FULL LIST THAT ARE IN THE USED LIST
	make_cookie_from_list();
	//outputResponse(random_image); 
	return  picturesChosen[picturesChosen.length-1];
	//return false;
}		

// ########### end of cookie enabled banner swaper #################
$(document).ready(function(){
	$('#menu').accordion({ 
		autoHeight: false,
		navigation: true,
		alwaysOpen: false,
		header: ".navHead",
		animated: false
	});
	$('#menu li a.navHead').click(function(){
		window.location.href=$(this).attr("href");
		return false;
	})
	var srchArray = getSearchAsArray();
	if(!srchArray["id"]){
		$('#menu').accordion("activate",-1);
	}
	if(srchArray["id"] == "10" || srchArray["id"] == "11" || srchArray["id"] == "12"){ // this was a quick fix for the menu to show the sub categories for the directory search
		$('#menu').accordion("activate",6); // on test site lpoc.co.uk/mx10 and on live site use 6!!!!!!!
		//alert("ID = "+srchArray["id"]);
	}
	if(srchArray["passed"] == "5"){ // this was a quick fix for the menu to show the sub categories for the owning/buying a listed property
		$('#menu').accordion("activate",4);
		//alert("ID = "+srchArray["passed"]);
	}
	$("#header").css({"background-image":"url("+getRandomBanner()+")"});
	
 	$(".was_thickbox").colorbox({
		transition:"elastic",
		speed:1000,
		iframe:true,
		height:"90%",
		width:"75%"
	});
 });

function MM_openBrWindow(theURL,winName,features) { //v2.0
	window.open(theURL,winName,features);
}

function formCheck(formobj){
	// Enter name of mandatory fields
	var fieldRequired = Array("title", "f_name", "l_name", "email", "addy1", "addy_town", "addy_count", "addy_pcode");
	// Enter field description to appear in the dialog box
	var fieldDescription = Array("Title", "First Name", "Surname", "E-Mail Address", "Address Line 1", "Town", "County", "Post Code");
	// dialog message
	var alertMsg = "Please complete the following fields:\n";
	
	var l_Msg = alertMsg.length;
	
	for (var i = 0; i < fieldRequired.length; i++){
		var obj = formobj.elements[fieldRequired[i]];
		if (obj){
			switch(obj.type){
			case "select-one":
				if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "select-multiple":
				if (obj.selectedIndex == -1){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "text":
			case "textarea":
				if (obj.value == "" || obj.value == null){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			default:
			}
			if (obj.type == undefined){
				var blnchecked = false;
				for (var j = 0; j < obj.length; j++){
					if (obj[j].checked){
						blnchecked = true;
					}
				}
				if (!blnchecked){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
			}
		}
	}

	if (alertMsg.length == l_Msg){
		if(checkmail(formobj)==true){
			return true;		
		}else{
			alert("Please enter a valid email address.");
			return false;
		}			
	}else{
		alert(alertMsg);
		return false;
	}
}

function checkmail(e) {
	var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i
	var returnval=emailfilter.test(e.email.value)
	return returnval
}
/*]]*/