// image popup

function viewimage(URL,productid) {
	var newwinwidth = 790;
	var newwinheight = 590;
	window.open(URL+'pop_viewimage.cfm?productid=' + productid, "ViewPhoto", "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=yes,resizable=1,width=" +newwinwidth+",height=" +newwinheight+",top=0,left=0");
	}	


function popBranchGoogleMap (dealerid) {
	window.open('/pop_googlemap.cfm?dealerid=' + dealerid, "ViewMap", "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=yes,resizable=1,width=600,height=600,top=0,left=0");
}

function ChangeCurrentRegion(region) {
	document.getElementById('CurrentDealerRegion').innerHTML = '<h2 class="small">' + region + ' BRANCHES</h2>';
	document.getElementById('DealerDetailsOutput').innerHTML = '';
	populatedealers(region);
	
}

function ActivateBranchName () {
	document.getElementById('branchtable-inital').style.visibility='hidden';
	document.getElementById('branchtable').style.visibility='visible';
}

function ChangeCurrentLinkRegion(region) {
	document.getElementById('CurrentLinkRegion').innerHTML = '<h2 class="small">' + region + ' Links</h2>';
	//document.getElementById('LinkDetailsOutput').innerHTML = '';
	populatelinks(region);
	
}

function ActivateBranchName () {
	document.getElementById('branchtable-inital').style.visibility='hidden';
	document.getElementById('branchtable').style.visibility='visible';
}

function refer() {
	var TheUrl = siteurl;
	new_win = window.open(TheUrl + 'pop_refer.cfm',"new_win",'toolbar=0,location=0,directories=0,status=0,menubar=1,scrollbars=1,resizable=1,width=450,height=500');
}	

// Contact Us Form Validation
	function contactsubmit()
	
	{
		if (FRMSubmit.firstname.value == '')
		{
			alert("Please enter your first name");
			FRMSubmit.firstname.focus();
			return false;
		}
		if (FRMSubmit.surname.value == '')
		{
			alert("Please enter your surname");
			FRMSubmit.surname.focus();
			return false;
		}		
		if (FRMSubmit.contact_Email.value == '')
		{
			alert("Please enter your email address");
			FRMSubmit.contact_Email.focus();
			return false;
		}			
		if (FRMSubmit.contact_subject.value == '')
		{
			alert("Please enter a subject for the enquiry");
			FRMSubmit.contact_subject.focus();
			return false;
		}
		if (FRMSubmit.contact_comments.value == '')
		{
			alert("Please enter the enquiry.");
			FRMSubmit.contact_comments.focus();
			return false;
		}
		
	return true;
	}

// search form validation
function searchsubmit() {
	if (FRMsearch.keyword.value == '')
		{
			alert("Please enter a keyword to search for");
			FRMsearch.keyword.focus();
			return false;
		}
}

// Extranet Login Form Validation
	function extranetloginsubmit()
	
	{
		if (FRMLogin.user_email.value == '')
		{
			alert("Please enter your username");
			FRMLogin.user_email.focus();
			return false;
		}
		if (FRMLogin.user_password.value == '')
		{
			alert("Please enter your password");
			FRMLogin.user_password.focus();
			return false;
		}
		
	return true;
	}

	function leftloginsubmit()
	
	{
		if (FRMLeftLogin.user_email.value == '')
		{
			alert("Please enter your username");
			FRMLeftLogin.user_email.focus();
			return false;
		}
		if (FRMLeftLogin.user_password.value == '')
		{
			alert("Please enter your password");
			FRMLeftLogin.user_password.focus();
			return false;
		}
		
	return true;
	}

// Email Password Form Validation
	
	function passreqsubmit()
	
	{
		if (FRMPasswordReq.email.value == '')
		{
			alert("Please enter your email address");
			FRMPasswordReq.email.focus();
			return false;
		}
		
	return true;
	}	
	
// Extranet Registration Form Validation
	function extranetregistrationsubmit()
	{	
			
		if (FRMRegister.Fname.value == '')
		{
			alert("Please enter your first name");
			FRMRegister.Fname.focus();
			return false;
		}
		if (FRMRegister.Sname.value == '')
		{
			alert("Please enter your surname");
			FRMRegister.Sname.focus();
			return false;
		}
		/*
		if (FRMRegister.occupation.value = '')
		{
			alert("Please enter your occupation");
			FRMRegister.occupation.focus();
			return false;
		}		
		*/
		//basic email address syntax validation
		if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(FRMRegister.email.value) == false) {
			alert("Invalid E-mail Address! Please re-enter.");
			return false;
		}
		
		/*
		if (FRMRegister.companyname.value == '')
		{
			alert("Please enter your companyname name");
			FRMRegister.companyname.focus();
			return false;
		}
		*/	
		if (FRMRegister.address1.value == '')
		{
			alert("Please enter your address");
			FRMRegister.address1.focus();
			return false;
		}			
		if (FRMRegister.city.value == '')
		{
			alert("Please enter your city");
			FRMRegister.city.focus();
			return false;
		}					
				
		if (FRMRegister.user_password.value == '')
		{
			alert("Please enter your password")
			FRMRegister.user_password.focus();
			return false;
		}
		
		if (FRMRegister.user_password.value.length < 3) {
			alert("Please enter a password that is more than 3 characters long.")
			FRMRegister.user_password.focus();
			return false;
		}
		if (FRMRegister.user_password_confirm.value == '')
		{
			alert("Please enter your password confirmation");
			FRMRegister.user_password_confirm.focus();
			return false;
		}
		if (FRMRegister.user_password.value != FRMRegister.user_password_confirm.value  ) {
			alert("Your password and password confirmation do not match.\nPlease make sure your password and password confirmation are the same");
			FRMRegister.user_password_confirm.focus();
			return false;
		}
		/*
		if (FRMRegister.disclaimer.checked == false) {
			alert("You must accpet the site Terms & Conditions to proceed.");
			FRMRegister.disclaimer.focus();
			return false;
		}
		*/		
		
	return true;
	}

// Extranet Registration Form Validation
	function extranetregistrationupdate()
	{	
			
		if (FRMRegister.Fname.value == '')
		{
			alert("Please enter your first name");
			FRMRegister.Fname.focus();
			return false;
		}
		if (FRMRegister.Sname.value == '')
		{
			alert("Please enter your surname");
			FRMRegister.Sname.focus();
			return false;
		}
			
		
		//basic email address syntax validation
		if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(FRMRegister.email.value) == false) {
			alert("Invalid E-mail Address! Please re-enter.");
			return false;
		}
		
		
		if (FRMRegister.address1.value == '')
		{
			alert("Please enter your address");
			FRMRegister.address1.focus();
			return false;
		}			
		if (FRMRegister.city.value == '')
		{
			alert("Please enter your city");
			FRMRegister.city.focus();
			return false;
		}					
				
		if (FRMRegister.user_password.value == '')
		{
			alert("Please enter your password")
			FRMRegister.user_password.focus();
			return false;
		}
		
		if (FRMRegister.user_password.value.length < 3) {
			alert("Please enter a password that is more than 3 characters long.")
			FRMRegister.user_password.focus();
			return false;
		}
		if (FRMRegister.disclaimer.checked == false) {
			alert("You must accpet the site Terms & Conditions to proceed.");
			FRMRegister.disclaimer.focus();
			return false;
		}		
		
	return true;
	}	
	
//Standard Functions
function open_new(url) {
	new_win = window.open(url,"new_win",'toolbar=0,location=0,directories=0,status=0,menubar=1,scrollbars=1,resizable=1');
	}			
function open_email(url) {
	new_email = window.open(url,"new_email",'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0, width=450, height=500');
	}
function open_print(url) {
	new_print = window.open(url,"new_print",'toolbar=1,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0, width=630, height=600');
	}	
function open_subscription(url) {
	new_sub = window.open(url,"new_sub",'toolbar=0,location=0,directories=0,status=0,menubar=1,scrollbars=1,resizable=0,width=450,height=500');
	}	
	
// navigation
function sendToPage(toLocation) {
	if (toLocation != "")
		top.location.href=toLocation;
	}

// open view photo window

function viewphoto (photoid,winwidth,winheight,viewthumb) {
	var TheUrl = siteurl;
	var newwinwidth = winwidth + 30;
	var newwinheight = winheight + 30;
	//alert (viewthumb);
	if (viewthumb == 1) {
		window.open(TheUrl + 'pop_viewphoto.cfm?photoid=' + photoid+'&viewthumb=yes', "ViewPhoto", "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=yes,resizable=1,width=" +newwinwidth+",height=" +newwinheight+",top=0,left=0");
	}
	else {
		window.open(TheUrl + 'pop_viewphoto.cfm?photoid=' + photoid, "ViewPhoto", "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=yes,resizable=1,width=" +newwinwidth+",height=" +newwinheight+",top=0,left=0");
	}
}

// open view photo window

function viewslideshow (photoid,winwidth,winheight,categoryid,viewthumb) {
	
	var TheUrl = siteurl;
	var newwinwidth = winwidth + 50;
	var newwinheight = winheight + 50;
	//alert (TheUrl);
	if (viewthumb == 1) {
		window.open(TheUrl + 'pop_viewslideshow.cfm?photoid=' + photoid+'&categoryid='+categoryid+'&viewthumb=yes', "ViewPhoto", "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=yes,resizable=1,width=" +newwinwidth+",height=" +newwinheight+",top=0,left=0");
	}
	else {
		window.open(TheUrl + 'pop_viewslideshow.cfm?photoid=' + photoid+'&categoryid='+categoryid, "ViewPhoto", "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=yes,resizable=1,width=" +newwinwidth+",height=" +newwinheight+",top=0,left=0");
	}
}

//existing site functions
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_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_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];}
}



// XMLHttpRequest fix for Internet Explorer

if(!window.XMLHttpRequest) {
 window.XMLHttpRequest = function() {
  var types = [
   'Microsoft.XMLHTTP',
   'MSXML2.XMLHTTP.5.0',
   'MSXML2.XMLHTTP.4.0',
   'MSXML2.XMLHTTP.3.0',
   'MSXML2.XMLHTTP'
  ];

  for(var i = 0; i < types.length; i++) {
   try {
    return new ActiveXObject(types[i]);
   } catch(e) {}
  }

  return false; // XMLHttpRequest not supported
 };
}


function updateAd() { // Checks if the top advertisement should be hidden or visible
 var ad = document.getElementById('adspot-a');

 if(ad != null) {
  if(adHiders[0] || adHiders[1] || adHiders[2])
   ad.style.visibility = 'hidden';
  else
   ad.style.visibility = 'visible';
 }
}


// get dealers

function populatedealers(region) {
	document.getElementById('dealerlist-divider').className = '';
	ajax.requestFile = '/include/inc_dealers_region_return.cfm?region='+region;	// Specifying which file to get
	ajax.onCompletion = populatedealerhtml;	// Specify function that will be executed after file has been found
	ajax.runAJAX();		// Execute AJAX function
}


function populatedealerhtml () {
	var obj = document.getElementById('DealerOutput');
	eval(ajax.response);	// Executing the response from Ajax as Javascript code	
}


function populatedealerdetails(dealerid) {
	ajax.requestFile = '/include/inc_dealer_return.cfm?dealerid='+dealerid;	// Specifying which file to get
	ajax.onCompletion = populatedealerdetailshtml;	// Specify function that will be executed after file has been found
	ajax.runAJAX();		// Execute AJAX function
}


function populatedealerdetailshtml () {
	document.getElementById('dealerlist-divider').className = 'dealerlist-divider';
	var obj2 = document.getElementById('DealerDetailsOutput');
	eval(ajax.response);	// Executing the response from Ajax as Javascript code	
}

// regional links

function populatelinks(region) {
	ajax.requestFile = '/include/inc_links_region_return.cfm?region='+region;	// Specifying which file to get
	ajax.onCompletion = populatelinkhtml;	// Specify function that will be executed after file has been found
	ajax.runAJAX();		// Execute AJAX function
}


function populatelinkhtml () {
	var obj = document.getElementById('linkOutput');
	eval(ajax.response);	// Executing the response from Ajax as Javascript code	
}


/************** jQuery code start ***********/

$(document).ready(function()
{
	/*
	*	Calculator functions
	*/
	$(".calcresult").attr("readonly","readonly");
	
	$(".calcvalue").keyup(function()
	{
		$thevalue = $.trim($(this).val());
		$thevalue = $thevalue.replace(/[^0-9\.]/g,"");
		$thevalue = $thevalue.replace(/(\.){2}/g,".");
		$(this).val($thevalue);
		
		$result = $(this).parent().next().find(".calcresult");
		
		if($thevalue != "")
		{
			$thevalue = parseFloat($thevalue);
			$themultiplier = parseFloat($(this).attr("multiplier"));
			$result.val( ($thevalue*$themultiplier) );
		}
		else
		{
			$result.val("");
		}
	});
	
	
	/*
	*	Make the Left menu extend to the bottom 
	*/
	
	if(!$.browser.msie)
	{
	
		$lhsdivheight = ($("#lhsdiv").height() == null)?0:$("#lhsdiv").height();
		$middledivheight = ($("#middlediv").height() == null)?0:$("#middlediv").height();
		$rightdivheight = ($("#rightdiv").height() == null)?0:$("#rightdiv").height();
		
		$maxheight = [$lhsdivheight,$middledivheight,$rightdivheight];
		$maxheight.sort(function(a,b){return b - a}); //sorts numerically in descending order
		
		$("#lhsdiv").height($maxheight[0]); //pick the greatest
	}
	
	
	/*
	*	FILE MANAGER
	*	Popup 'pop_getfile.cfm' for file downloads
	*	Anchors: href="ENCRYPTEDFILEPATH" class="filedownload"
	*/
	
	$("a.filedownload").click(function(e)
	{
		e.preventDefault();
		$type = $(this).attr("type");
		$href = $(this).attr("href");
		switch($type)
		{
			case "cms":
				window.open("/pop_getfile.cfm?getfile="+$href,"win","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,width=400,height=400");
				break;
			case "filemanager":
				window.open("/pop_getfile_filemanager.cfm?getfile="+$href,"win","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,width=400,height=400");
				break;
		}
	});
	
});

/*************** jQuery code end ************/



/********** Event Registration **************/
function eventsubmit()
	
	{
		if (document.FRMEvent.firstname.value == '')
		{
			alert("Please enter your first name.");
			document.FRMEvent.firstname.focus();
			return false;
		}
		if (document.FRMEvent.surname.value == '')
		{
			alert("Please enter your surname.");
			document.FRMEvent.surname.focus();
			return false;
		}		
		if (document.FRMEvent.companyname.value == '')
		{
			alert("Please enter your company name.");
			document.FRMEvent.companyname.focus();
			return false;
		}
		if (document.FRMEvent.emailaddress.value == '')
		{
			alert("Please enter your email address.");
			document.FRMEvent.emailaddress.focus();
			return false;
		}		
		if (document.FRMEvent.phone.value == '')
		{
			alert("Please enter your phone number.");
			document.FRMEvent.phone.focus();
			return false;
		}	
		
	return true;
	}
	
	
/*$(function()
{
	// Use this example, or...
	$("a.lightbox").lightBox();
	
	// for mini gallery
	$("div.scrollable").scrollable({
		size:3
	});
	
	$("a.replacemain").click(function(e)
	{
		e.preventDefault();
		$href = $(this).attr("href");
		$large = $(this).attr("large");
		$img = $(this).attr("img");
		
		$mainimage = $("#img_"+$img);
		$mainimagelink = $("#img_"+$img).parent("a");
		
		$mainimage.stop().animate({
			opacity:0
		}, function()
		{
			$(this).attr("src",$href);
			$mainimagelink.attr("href",$large);
			$(this).animate({opacity:1})
		});
	});
	
	$("a.jtools[rel]").overlay({ 
 
		effect:"apple",
		expose: {
			color:"#fff",
			opacity:0.7
		},
		left:"center",
		top:"center",
		onBeforeLoad: function() { 
			// grab wrapper element inside content 
			var wrap = this.getContent().find(".contentWrap"); 
			// load the page specified in the trigger 
			
			$thehref = this.getTrigger().attr("href").toLowerCase();
			
			wrap.css({width:"",height:"",overflow:"auto", "background-image":"/images/template/overlay/white.png"});
			
			if($thehref.indexOf('.html') >= 0 || $thehref.indexOf('.htm') >= 0 || $thehref.indexOf('.HTML') >= 0 || $thehref.indexOf('.HTM') >= 0 || $thehref.indexOf('.cfm') >= 0 || $thehref.indexOf('.CFM') >= 0)
			{
				wrap.css({width:"640px",height:"480px"});
				wrap.load(this.getTrigger().attr("href")); 
			}
			else if($thehref.indexOf('.jpg') >= 0 || $thehref.indexOf('.jpeg') >= 0 || $thehref.indexOf('.png') >= 0 || $thehref.indexOf('.gif') >= 0)
			{
				$img = $("<img>").attr("src",$thehref);
				wrap.css({width:"auto",height:"auto","background-image":""});
				wrap.html($img);
			}
		} 
	}); 
	
	
	$(function()
	{
		$(".catname").click(function(e)
		{
			e.preventDefault();
			$(this).next(".catspecs").slideToggle("fast");
		});
	});
	
	$(function()
	{
		$(".question").click(function(e)
		{
			e.preventDefault();
			$(this).next(".answer").slideToggle("fast");
		});
	});
	
	$("input.numbersonly").livequery("keypress", function(e)
	{
		if( e.which!=8 && e.which!=0 && (e.which < 48 || e.which > 57))
		{	
			$(this).css({backgroundColor:"#ff0000",color:"#ffffff"}).stop().animate({backgroundColor:"#ffffff",color:"#000"},10000);
			return false;
		}
		else
			return true;
	});


});*/



/*******************************
***** CHECK EMAIL VIA AJAX *****
*******************************/

$(function()
{
	var checkemailtimer;
	$(".checkemail").keyup(function(e)
	{
		$theemail = $(this).val();
		$thisparent = $(this).parent();
		
		$("div#prompt",$($thisparent)).remove();
		
		if(checkemailtimer)
		{
			clearTimeout(checkemailtimer);
		}
		checkemailtimer = setTimeout(function()
		{
			if($.trim($theemail) != "")
			{
				if($("#customeremail").size() > 0)
				{
					if($.trim($theemail) != $.trim($("#customeremail").val()))
					{
						$.post(
							"/_checkemail.cfm",
							{
								emailaddress:$theemail,
								returnpath:$("#returnpath").val()
							},
							function(data)
							{
								$thisparent.append(data);
							}
						);
					}
				}
				else
				{
					$.post(
						"/_checkemail.cfm",
						{
							emailaddress:$theemail,
							returnpath:$("#returnpath").val()
						},
						function(data)
						{
							$thisparent.append(data);
						}
					);
				}
				
			}
		},500);
	});
	
	/*$(".autopasswordrequest").livequery("click",function(e)
	{
		e.preventDefault();
		$.post(
			"/_autopasswordrequest.cfm",
			{
				autopasswordrequest:$(this).attr("id"),
				returnpath:$("#returnpath").val()
			},
			function(data)
			{
				$("div#prompt").removeClass("notice").addClass("success").html(data);
			}
		);
	});
	
	$(".closeprompt").livequery("click",function(e)
	{
		e.preventDefault();
		$("div#prompt").remove();
	});*/
});
