function PopulateUserName(){
	if (document.getElementById("user_username").value == '') {
		document.getElementById("user_username").value = document.getElementById("email").value;
		
	}
}

function UpdateDealerSelection(ThisForm) {
	if (document.getElementById('DealerID').value != "") {
		ThisForm.submit();
	}
}


function UpdateForm(ThisForm) {
	ThisForm.submit();
}

function FRMChangeCountry(ThisForm) {
	if (document.getElementById('countryid').value != "") {
		ThisForm.submit();
	}
}

function FRMChangeCountry2(ThisForm) {
	if (document.getElementById('scountryid').value != "") {
		ThisForm.submit();
	}
}

function FRMChangeRegion(ThisForm) {
	if (document.getElementById('regionid').value != "") {
		ThisForm.submit();
	}
}
function FRMChangeRegion2(ThisForm) {
	if (document.getElementById('sregionid').value != "") {
		ThisForm.submit();
	}
}


function FRMSelectShippingLocation(ThisForm) {
	//if (document.getElementById('ShipToLocation').value == '2') {
		ThisForm.submit();
	//}
}
function FRMSelectPaymentMethod(ThisForm) {
	//if (document.getElementById('paymentmethod').value == '1') {
		ThisForm.submit();
	//}
}

function FRMClearLogin () {
	var loginemailvalue = document.getElementById("FRMLoginuser_email").value;
	var loginpassvalue = document.getElementById("FRMLoginuser_password").value;
	if (loginemailvalue == 'Email Address') {
		document.getElementById("FRMLoginuser_email").value = '';
	}
	if (loginpassvalue == 'enter your password') {
		document.getElementById("FRMLoginuser_password").value = '';
	}
}


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");
}
// toggle visibility code

function toggle(obj) {
 var el = document.getElementById(obj);
 el.style.display = (el.style.display != 'none' ? 'none' : '');
}

// specifications popup

function viewspecifications(URL,productid,display) {
	var newwinwidth = 620;
	var newwinheight = 500;
	window.open(URL+'pop_specifications.cfm?productid=' + productid + '&display=' + display, "ViewPhoto", "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=yes,resizable=1,width=" +newwinwidth+",height=" +newwinheight+",top=0,left=0");
	}	

// product enquiry popup

function productenquiry(URL,productid) {
	var newwinwidth = 620;
	var newwinheight = 600;
	window.open(URL+'pop_product_enquiry.cfm?productid=' + productid, "Enquiry", "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=yes,resizable=1,width=" +newwinwidth+",height=" +newwinheight+",top=0,left=0");
	}	
function productenquiryredvalue(URL,productid) {
	var newwinwidth = 620;
	var newwinheight = 600;
	window.open(URL+'pop_product_enquiry_redvalue.cfm?enquiryid=' + productid, "Enquiry", "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=yes,resizable=1,width=" +newwinwidth+",height=" +newwinheight+",top=0,left=0");
	}	
	 
	
// image popup

function viewimage(URL,productid) {
	var newwinwidth = 610;
	var newwinheight = 700;
	window.open(URL+'?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");
	}	

// print page


	function printPage() {
	if (window.print) {
		agree = confirm('Do you wish to print this page now?');
		if (agree) window.print(), initDelay(); 
		//else self.close();
	   }
	}
	function initDelay() {
		//e.g. delay the display for 0.5sec (500ms)
		setTimeout("self.close()",1000);
	}
	

var _d = document;
function showTab(tab) {
 var tb=lt.id.substring(1); // pull out previous number
 ta=tab.id.substring(1); // pull out the current number
 lt.className='tabtextover'; // change previous to class o
 tab.className='tabtextover';
 lt=tab;

 // display table
 _d.getElementById('s'+tb).style.display="none";
 _d.getElementById('s'+ta).style.display="block";
 _d.getElementById('m'+ta).className='tabtextover';
  _d.getElementById('m'+tb).className='tabtext';
}


// Contact Form Validation
	function contactsubmit()
	
	{
		if (FRMContact.contact_name.value == '')
		{
			alert("Please enter your name.");
			FRMContact.contact_name.focus();
			return false;
		}
		if (FRMContact.contact_email.value == '')
		{
			alert("Please enter your email address.");
			FRMContact.contact_email.focus();
			return false;
		}		
		if (FRMContact.contact_subject.value == '')
		{
			alert("Please enter a subject.");
			FRMContact.contact_subject.focus();
			return false;
		}
		if (FRMContact.contact_phone.value == '')
		{
			alert("Please enter your phone number.");
			FRMContact.contact_phone.focus();
			return false;
		}	
		
	return true;
	}

// Extranet Login Form Validation
	function extranetloginsubmit()
	
	{
		if (FRMLogin.user_email.value == '')
		{
			alert("Please enter your email address");
			FRMLogin.user_email.focus();
			return false;
		}
		if (FRMLogin.user_password.value == '')
		{
			alert("Please enter your password");
			FRMLogin.user_password.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.email.value == '')
		{
			alert("Please enter your email address");
			FRMRegister.email.focus();
			return false;
		}
		if (FRMRegister.email.value.indexOf("@") < 0) {
			alert("Sorry that email format is incorrect, please check and re-enter.")
			FRMRegister.email.focus();
			return false;
		}
		if (FRMRegister.email.value.indexOf(".") < 0) {
			alert("Sorry that email format is incorrect, please check and re-enter.")
			FRMRegister.email.focus();
			return false;
		}
		if (FRMRegister.email.value.length < 7) {
			alert("Sorry that email format is incorrect, please check and re-enter.")
			FRMRegister.email.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;
		}
		
		
	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=680, height=600');
	}	
	
//Activeedit JS to handle custome links

  		function aeapi_custom_DynHyperlink(aeNum) {
			ae_hot=aeNum;
			DHTMLSafe=aeObjects[aeNum];
			DHTMLSafe.focus();
			DHTMLSafe.DOM.body.focus();	
			if(DHTMLSafe.DOM.selection.type=="Control") {
				var el=DHTMLSafe.DOM.selection.createRange().commonParentElement();
				var tr = DHTMLSafe.DOM.body.createTextRange();
				tr.moveToElementText(el);
				tr.select();
			}
			if (typeof(ae_linkwin) == "undefined" || ae_linkwin.closed) { //short circuit eval
				var szURL=inc + "hyperlink.cfm";
				ae_linkwin = window.open(szURL ,"ae_linkwin","scrollbars=auto,width=360,height=220, resizable=yes",true);
			}
			ae_linkwin.focus();
  		}
		function aeapi_custom_insertFile(aeNum) {
		//if the focus never touched the control, you get an error, so lets touch the control
		DHTMLSafe=aeObjects[aeNum];
		DHTMLSafe.focus();
		if (typeof(ae_filewin) == "undefined" || ae_filewin.closed) { //short circuit eval
			//spawn image window
			var szURL;
			szURL=inc + "file.cfm?instance=" + aeNum;
			szURL+="&filedata=";
			szURL+=ae_filedata[aeNum];
			szURL+="&fileurl=";
			szURL+=ae_fileurl[aeNum];
			ae_filewin = window.open(szURL ,"ae_filewin","scrollbars=yes,status=yes,width=650,height=400, resizable=yes");
	
		}
		ae_filewin.focus();		
		
		}
		//insert the image
		function onFile(u, base_url, y, itext, num) {
			DHTMLSafe=aeObjects[num];
			var img = "<a href=\"" + y + "\" target='_blank'>" + itext + "</a>";
			DHTMLSafe.DOM.selection.createRange().pasteHTML(img);
			ae_filewin.close();
		}	
		function ae_fileProperties(aeNum) {
			DHTMLSafe=aeObjects[aeNum];
			DHTMLSafe.focus();
			if (typeof(ae_filepropwin) == "undefined" || ae_filepropwin.closed) { //short circuit eval
				//spawn image properties window
				var szURL=inc + "fileProperties.htm?instance=" + num;
				ae_filepropwin = window.open(szURL ,"ae_filepropwin","scrollbars=no,width=370,height=305, resizable=no");
		 	}
		 	ae_filepropwin.focus();
		}	
		
//

// 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");
	}
}

$(function()
{
	$("h1,h2,h3").parents("a").each(function()
	{
		$(this).css("text-decoration","none");
	});
	
	
});

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 + '</h2>';
	document.getElementById('DealerDetailsOutput').innerHTML = '';
	populatedealers(region);
	
}

function ActivateBranchName () {
	document.getElementById('branchtable-initial').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-initial').style.visibility='hidden';
	document.getElementById('branchtable').style.visibility='visible';
}

function ChangeMap(region) {
	document.NZMap.src = '/Images/dealers/' + region;
}


// 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	
}




/*******************************
***** 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();
	});
});

