var stored_colour = '';

var site_url = location.protocol + "//" + document.domain;
var newsletter_validate = {'Name':'text','Email':'email'};

function newsletter_signup(){
	var frmObj = document.forms['newsletter_signup_form'];
	if(frmObj['Name'].value == 'Your Name') frmObj['Name'].value = ''; 
	if(frmObj['Email'].value == 'someones@email.com') frmObj['Email'].value = ''; 
	if(verifylist(frmObj,newsletter_validate,'newsletter_error')){
		var params_obj = {};
		params_obj.name = frmObj['Name'].value;
		params_obj.email = frmObj['Email'].value;
		params_obj.sucesstext = "<div class='message'>Thank you for signing up.<\/div>";
		params_obj.ac = 'subscribe';
		var params = make_url_params(params_obj,1);
		var call = site_url+'/cgi-bin/htmlemailer/newsletter_emailer.pl'+params;
		ajax_request(call,"ajax_fill_data('newsletter_signup_div',http_request.responseText);");
	}
}


function $(id){
	return document.getElementById(id);
}

function submit_form(json){
	var frmObj = $(json.frmname);
	if(frmObj){
		for(var n in json){
			var obj = $(n);
			//alert("Set "+n+" in "+json.frmname+" to "+json[n]);
			if(obj) obj.value = json[n];
		}
		//Check to see if validation is required
		if(json.val_list){
			if(verifylist(frmObj,json.val_list,json.error_panel)){
				frmObj.submit();
			}
		}else{
			frmObj.submit();
		}
	}else{
		alert("Can't find form with name "+json.frmname);
	}
}

				
function price_list_update_control(fill_id,script_url,jsonstr,obj){
	var json = eval("jsonstr;");
	if(json.sort == 1){
		//alert("CHANGE SORT OPTIONS");
		json = price_list_sort(json);
	}
	if(obj){
		json.currency = obj.value;
	}
	var call = script_url + "?page=" + json.page + "&site=" + json.site + "&development=" + json.development + "&currency=" + json.currency + "&order_by=" + json.order_by + "&order_type=" + json.order_type;
	//alert(call);
	ajax_request(call,"ajax_fill_data('"+fill_id+"',http_request.responseText)");
}

function price_list_sort(json){
	if(json.sqlfield){
		json.order_type = (json.order_type != 'ASC')?'ASC':'DESC';
		json.order_by = json.sqlfield;
		return json;
	}else{
		alert("No sql field name given.");
	}
}

function pager_submit(v,prefix){
	var obj=$(prefix+'_pagestart');
	if (obj) {
		obj.value = v;
		document.forms['pagingform'].submit();
	}else{
		alert("For paged listings you must have a form with some hidden variables, pagestart, sortby, sortdes etc\\nEither include one given here or make your own");
	}
}


function swap_pic(contianer,num){	
	//alert('swap pic = '+nm);
	var c = $(contianer);
	if (c){
		var divs = c.getElementsByTagName('div');

		if (divs){
			for (var x=0;x<divs.length ;x++ ){
				divs[x].style.display = (x==num)?"block":"none";
			}
		}else{
			alert ("divs not found inside the div named '"+contianer+"'")
		}	
	}else{
		alert ("div '"+contianer+"' not found")
	}

}

function dynamic_contact(model){
	if (model != null){
		location.href = "/dyn/pages/brochure-request/index.shtml?Model="+escape(model);
		//alert(location.href);
	}else{
		//alert("currentlayer="+currentlayer );
		if (currentlayer != ''){
			var mobj=$('model'+currentlayer);
			if (mobj){
				//alert("modelobj="+mobj );
				//alert(obj.value);
				location.href = "/dyn/pages/brochure-request/index.shtml?Model="+escape(mobj.value);
			}else{
				alert("Error Hidden field 'model"+currentlayer+"' not found\nCould be the ssi is not updated to latest version")
			}
		}else{
			//alert("Error Currentlayer not A or B")
		}
	}
}

function load_page(path){
	if (path != null && location.href != path){
		location.href = path;
	}
}

function arrayrandomise(arr,n){
// Will randomise a single dimension array,of integers or strings.
	var l=arr.length;
	if (n!=null){
		for (w=0;w<n;w++)arr[w]=w;
		l=n;
	}
	//alert("Lenght of array "+l);
	for (var w=0; w<l;w++){

		var r = parseInt(Math.random()*l);
		var t = arr[r];
		arr[r]=arr[w];
		arr[w]=t ;
	}
	//for (w=0; w<l;w++){

	//	alert(arr[w]);
	//}
}

// Select 3 random adds
function rand_small_ads(){
	var w=0;
	while ($("smallad_"+w)){
		w++;
	}
	var arr=new Array();
	arrayrandomise(arr,w);
	var s="";
	var t=(w>6)?6:w;
	for (var w=0; w<t;w++){
		s+=$("smallad_"+arr[w]).innerHTML;
	}
	$("smallads").innerHTML=s;
}

// Use list item 0 then select 2 random adds from rest of list (used on slmvauxhall)
function rand_small_ads2(){
	var w=0;
	while ($("smallad_"+w)){
		w++;
	}
	var arr=new Array();
	arrayrandomise(arr,w);
	var s="";
	var t=(w>6)?6:w;

	for (var x=0; x<w;x++){
		if(arr[x]==0){
			arr[x] = arr[0];
			arr[0] = 0;
			break;
		}
	}

	for (var x=0; x<t;x++){
		s+=$("smallad_"+arr[x]).innerHTML;
	}
	$("smallads").innerHTML=s;
}

function more_details(path){
	if (path != null){
		location.href = path;
	}
}

function copyToField(sourcefield,destfieldname){
	//alert(sourcefield+" <=> "+destfieldname);
	var sourceObj = $(sourcefield);
	var destObj = $(destfieldname);
	if(destObj && sourceObj){
		destObj.value = sourceObj.value;
	}
}

function show_or_hide_other(value,othervalue,element){
	if(value == othervalue){
		show_elements(element);
	}else{
		hide_elements(element);
	}
}

function show_elements(ids){
	var elements = ids.split(',');
	for(var x=0;x<elements.length;x++){
		var obj = $(elements[x]);
		if(obj) obj.style.display = 'block';
	}
}

function hide_elements(ids){
	var elements = ids.split(',');
	for(var x=0;x<elements.length;x++){
		var obj = $(elements[x]);
		if(obj) obj.style.display = 'none';
	}
}

function togglePanels(ids){
	var elements = ids.split(',');
	for(var x=0;x<elements.length;x++){
		var obj = $(elements[x]);
		if(obj) obj.style.display = (obj.style.display == 'none')?'block':'none';
	}
}

function bounce_secure(){
	var href = location.href;
	if(href.search('https://') == -1){
		//location.href = href.replace('http://','https://');
	}
}

function popup_control(url,type,title,width,height){
	//alert("URL: "+url+"  Window Type: "+windowtype);
	if(url != 0 && url != '' && location.href != url){
		if(type == 'gallery'){
			OpenWindow(new Array(0,url,title,width,height,0,1,0,0,0,0,0));
		}
	}
}
function mOvr(src,colour)	{
	if(src.style.backgroundColor){ stored_colour = src.style.backgroundColor; }
	else if(src.backgroundColor){ stored_colour = src.backgroundColor; }
	src.style.backgroundColor=colour;
	src.backgroundColor=colour;
}

function mOut(src)
{ 
	src.style.backgroundColor=stored_colour;
	src.backgroundColor=stored_colour;
}
function OpenWindow(action) {
	//alert(action[1]);
	var wf = "";
	wf = wf + "top=10,left=10";
	wf = wf + ",width=" + action[3];
	wf = wf + ",height=" + action[4];
	wf = wf + ",resizable=" + (action[5] ? "yes" : "no");
	wf = wf + ",scrollbars=" + (action[6] ? "yes" : "no");
	wf = wf + ",menubar=" + (action[7] ? "yes" : "no");
	wf = wf + ",toolbar=" + (action[8] ? "yes" : "no");
	wf = wf + ",directories=" + (action[9] ? "yes" : "no");
	wf = wf + ",destination=" + (action[10] ? "yes" : "no");
	wf = wf + ",status=" + (action[11] ? "yes" : "no");
	return window.open(action[1],action[2],wf);
}

function display_image(img_filename){
	toggle_masker(1);
	var obj = $('masker');
	if(obj){
		var frame = $('image_frame');
		//alert('upload file passbackfield = '+passbackfield+ ' fextns = '+fextns+' expath = '+expath);
		if(frame) frame.innerHTML = "<img src='"+img_filename+"' style='border:10px solid #ffffff;'><div><a href='javascript:close_popup();' style='background-color:#ffffff;padding:5px;'><b>Click here to close this popup</b></a></div>";
		window.scrollTo(0,0);
		toggle_image_frame(1);	//OPEN IMAGE FRAME
	}else{
		alert("No masker layer was found.");
	}
}

function close_popup(){
	toggle_masker(0);
	toggle_image_frame(0);	//SHUT IMAGE FRAME
	var obj = $('masker');
	if(obj)obj.innerHTML = "";
}

function toggle_image_frame(onoff){
	var obj = $('image_frame');
	if(obj){
		obj.style.visibility = (onoff == 1)?'visible':'hidden';
	}
}

function toggle_masker(onoff){
	var obj = $('masker');
	if(obj){
		setOpacity(obj, 70);
		obj.style.visibility = (onoff == 1)?'visible':'hidden';
		//alert(window.scrollMaxY);
		
		var sizes = getPageSize();
		obj.style.width = sizes[2];
		obj.style.height = sizes[3];
		
		if(window.scrollMaxY){
			obj.style.height = (window.scrollMaxY + sizes[3]) + 'px';
		}else if(document.body.scrollHeight){
			obj.style.height = ((document.body.scrollHeight / 1.5) + sizes[3]) + 'px';
		}
	}
}

function setOpacity(obj, opacity) {
	opacity = (opacity == 100)?99.999:opacity;
	// IE/Win
	obj.style.filter = "alpha(opacity:"+opacity+")";
	// Safari<1.2, Konqueror
	obj.style.KHTMLOpacity = opacity/100;
	// Older Mozilla and Firefox
	obj.style.MozOpacity = opacity/100;
	// Safari 1.2, newer Firefox and Mozilla, CSS3
	obj.style.opacity = opacity/100;
}

function getPageSize(){

	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else if (document.documentElement && document.documentElement.scrollHeight > document.documentElement.offsetHeight){ // Explorer 6 strict mode
		xScroll = document.documentElement.scrollWidth;
		yScroll = document.documentElement.scrollHeight;
	} else { // Explorer Mac...would also work in Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) { // all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}
	
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}
	
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
	return arrayPageSize;
}	