//detect browser to show or hide the Iframe hack
var browserName=navigator.userAgent.toLowerCase();
if (browserName.indexOf('opera') != -1) {
	showFrameHack = false;
} else {
	showFrameHack = true;
}

//used for IE bug where select boxes show up
document.write('<iframe id="div_make_an_enquiry_frame" class="enquiryFrame" src="javascript:;" scrolling="no" frameborder="0" style="position:absolute;top:0px;left:0px;visibility:hidden;display:none"></iframe>');
document.write('<iframe id="div_book_test_drive_frame" class="testdriveFrame" src="javascript:;" scrolling="no" frameborder="0" style="position:absolute;top:0px;left:0px;visibility:hidden;display:none"></iframe>');
document.write('<iframe id="div_trade_up_frame" class="tradeupFrame" src="javascript:;" scrolling="no" frameborder="0" style="position:absolute;top:0px;left:0px;visibility:hidden;display:none"></iframe>');
document.write('<iframe id="div_email_a_friend_frame" class="emailFriendFrame" src="javascript:;" scrolling="no" frameborder="0" style="position:absolute;top:0px;left:0px;visibility:hidden;display:none"></iframe>');

function ShowFormDiv(show_div,hide_div1,hide_div2,hide_div3) {
	if (hide_div1) {
		HideDiv(hide_div1);
		if (showFrameHack==true) {
			var IfrRef = new getObj(hide_div1+"_frame");
			IfrRef.style.visibility = 'hidden';
			IfrRef.style.display = 'none';
		}
	}

	if (hide_div2) {
		HideDiv(hide_div2);
		if (showFrameHack==true) {
			var IfrRef = new getObj(hide_div2+"_frame");
			IfrRef.style.visibility = 'hidden';
			IfrRef.style.display = 'none';
		}
	}
	
	if (hide_div3) {
		HideDiv(hide_div3);
		if (showFrameHack==true) {
			var IfrRef = new getObj(hide_div3+"_frame");
			IfrRef.style.visibility = 'hidden';
			IfrRef.style.display = 'none';
		}
	}

	var div_object = new getObj(show_div)
	if (div_object.obj)
	{
		// For positioning it relative to the top of window, regardless of scrolling
		var DivTop = Math.ceil(windowState.getScrollY()+120);//120
		var DivLeft = Math.ceil((windowState.getWidth()/2)-280); //use 390 if you want it to align to the left of the enquiry box
		div_object.style.top = DivTop + 'px';
		div_object.style.left = DivLeft + 'px';
		div_object.style.zIndex = "999999";
	}
	ShowDiv(show_div);
	
	if (showFrameHack==true) {
		var IfrRef = new getObj(show_div+"_frame");
		IfrRef.style.top = DivTop + 'px';
		IfrRef.style.left = DivLeft + 'px';
		IfrRef.style.zIndex = "100";
		IfrRef.style.visibility = 'visible';
		IfrRef.style.display = 'block';
	}
	
}
function HideFormDiv(hide_div) {

	HideDiv(hide_div);
	
	if (showFrameHack==true) {
		var IfrRef = new getObj(hide_div+"_frame");
		IfrRef.style.visibility = 'hidden';
		IfrRef.style.display = 'none';
	}
	
}

/* Function to set */
function setEnquiryName(form, enquiry_name)
{
	var form_object = new getObj(form);
	if (form_object.obj)
	{
		form_object.obj.txtFullVehicleName.value = enquiry_name;
		
		//var div_vehicle_name = new getObj("div_vehicle_name");
		var div_vehicle_name = new getObj("div_" + form);
		
		if (div_vehicle_name.obj)
		{
			div_vehicle_name.obj.innerHTML = enquiry_name;
		}
	}
}

/* Function for Make an Enquiry Form */
function frm_make_an_enquiry(check)
{
	errorDetails = '';
	errorControl = '';
	resetTextBoxCSS(check.txtName);
	resetTextBoxCSS(check.txtEmail);
	resetTextBoxCSS(check.txtPhone);
	
	
	if (check.txtName.value == "")
	{
		applyErrorTextBoxCSS(check.txtName);
		if (errorDetails == '')
		{
			errorControl = check.txtName;
		}
		errorDetails = errorDetails + " - The Name field cannot be blank\n";
		
	}
	if (check.txtEmail.value == "")
	{
		applyErrorTextBoxCSS(check.txtEmail);
		if (errorDetails == '')
		{
			errorControl = check.txtEmail;
		}
		errorDetails = errorDetails + " - The Email field cannot be blank\n";
	}
	if (isValidEmail(check.txtEmail.value)==false)
	{
		applyErrorTextBoxCSS(check.txtEmail);
		if (errorDetails == '')
		{
			errorControl = check.txtEmail;
		}
		errorDetails = errorDetails + " - Please enter a valid email address\n";
	}
	if (check.txtPhone.value == "")
	{
		applyErrorTextBoxCSS(check.txtPhone);
		if (errorDetails == '')
		{
			errorControl = check.txtPhone;
		}
		errorDetails = errorDetails + " - The Phone field cannot be blank\n";
		
	}
	
	if (errorDetails == '')
	{
		return true;
	}
	else
	{
		alert('To submit the form you need to enter the following mandatory fields \n' + errorDetails);
		if (errorControl != '')
			errorControl.focus();
			
		return false;
	}
}

/* Function for Book a Test Drive Form */
function frm_book_test_drive(check)
{
	errorDetails = '';
	errorControl = '';
	resetTextBoxCSS(check.txtDate);
	resetTextBoxCSS(check.txtTime);
	resetTextBoxCSS(check.txtOtherTime);
	resetTextBoxCSS(check.txtName);
	resetTextBoxCSS(check.txtEmail);
	resetTextBoxCSS(check.txtPhone);
	
	
	if (check.txtDate.value == "")
	{
		applyErrorTextBoxCSS(check.txtDate);
		if (errorDetails == '')
		{
			errorControl = check.txtDate;
		}
		errorDetails = errorDetails + " - The Date field cannot be blank\n";
		
	}
	
	if (check.txtTime.selectedIndex == 0 && check.txtOtherTime.value == "")
	{
		if (check.txtTime.selectedIndex == 0)
			applyErrorTextBoxCSS(check.txtTime);
		if (check.txtOtherTime.value == "")
			applyErrorTextBoxCSS(check.txtOtherTime);
		if (errorDetails == '')
		{
			if (check.txtTime.selectedIndex == 0)
				errorControl = check.txtTime;
			else
				errorControl = check.txtOtherTime;
			
		}
		errorDetails = errorDetails + " - The Time was not selected or the Other time is blank \n";
		
	}
	if (check.txtName.value == "")
	{
		applyErrorTextBoxCSS(check.txtName);
		if (errorDetails == '')
		{
			errorControl = check.txtName;
		}
		errorDetails = errorDetails + " - The Name field cannot be blank\n";
		
	}
	if (check.txtEmail.value == "")
	{
		applyErrorTextBoxCSS(check.txtEmail);
		if (errorDetails == '')
		{
			errorControl = check.txtEmail;
		}
		errorDetails = errorDetails + " - The Email field cannot be blank\n";
	}
	if (isValidEmail(check.txtEmail.value)==false)
	{
		applyErrorTextBoxCSS(check.txtEmail);
		if (errorDetails == '')
		{
			errorControl = check.txtEmail;
		}
		errorDetails = errorDetails + " - Please enter a valid email address\n";
	}
	if (check.txtPhone.value == "")
	{
		applyErrorTextBoxCSS(check.txtPhone);
		if (errorDetails == '')
		{
			errorControl = check.txtPhone;
		}
		errorDetails = errorDetails + " - The Phone field cannot be blank\n";
		
	}
	
	if (errorDetails == '')
	{
		return true;
	}
	else
	{
		alert('To submit the form you need to enter the following mandatory fields \n' + errorDetails);
		if (errorControl != '')
			errorControl.focus();
			
		return false;
	}
}

/* Function for Trade Up Form */
function frm_trade_up(check)
{
	errorDetails = '';
	errorControl = '';
	resetTextBoxCSS(check.txtName);
	resetTextBoxCSS(check.txtEmail);
	resetTextBoxCSS(check.txtPhone);
	resetTextBoxCSS(check.txtVehicleMake);
	resetTextBoxCSS(check.txtVehicleModel);
	resetTextBoxCSS(check.txtYear);
	resetTextBoxCSS(check.txtKilometers);
	
	if (check.txtName.value == "")
	{
		applyErrorTextBoxCSS(check.txtName);
		if (errorDetails == '')
		{
			errorControl = check.txtName;
		}
		errorDetails = errorDetails + " - The Name field cannot be blank\n";
		
	}
	if (check.txtEmail.value == "")
	{
		applyErrorTextBoxCSS(check.txtEmail);
		if (errorDetails == '')
		{
			errorControl = check.txtEmail;
		}
		errorDetails = errorDetails + " - The Email field cannot be blank\n";
	}
	if (isValidEmail(check.txtEmail.value)==false)
	{
		applyErrorTextBoxCSS(check.txtEmail);
		if (errorDetails == '')
		{
			errorControl = check.txtEmail;
		}
		errorDetails = errorDetails + " - Please enter a valid email address\n";
	}
	if (check.txtPhone.value == "")
	{
		applyErrorTextBoxCSS(check.txtPhone);
		if (errorDetails == '')
		{
			errorControl = check.txtPhone;
		}
		errorDetails = errorDetails + " - The Phone field cannot be blank\n";
		
	}
	if (check.txtVehicleMake.value == "")
	{
		applyErrorTextBoxCSS(check.txtVehicleMake);
		if (errorDetails == '')
		{
			errorControl = check.txtVehicleMake;
		}
		errorDetails = errorDetails + " - The Vehicle make field cannot be blank\n";
		
	}
	if (check.txtVehicleModel.value == "")
	{
		applyErrorTextBoxCSS(check.txtVehicleModel);
		if (errorDetails == '')
		{
			errorControl = check.txtVehicleModel;
		}
		errorDetails = errorDetails + " - The Vehicle Model field cannot be blank\n";
		
	}
	if (check.txtYear.value == "")
	{
		applyErrorTextBoxCSS(check.txtYear);
		if (errorDetails == '')
		{
			errorControl = check.txtYear;
		}
		errorDetails = errorDetails + " - The Year field cannot be blank\n";
		
	}
	if (check.txtKilometers.value == "")
	{
		applyErrorTextBoxCSS(check.txtKilometers);
		if (errorDetails == '')
		{
			errorControl = check.txtKilometers;
		}
		errorDetails = errorDetails + " - The Kilometers field cannot be blank\n";
		
	}
	
	if (errorDetails == '')
	{
		return true;
	}
	else
	{
		alert('To submit the form you need to enter the following mandatory fields \n' + errorDetails);
		if (errorControl != '')
			errorControl.focus();
			
		return false;
	}
}


/* Function for Email to a Friend */
function frm_email_a_friend(check)
{
	errorDetails = '';
	errorControl = '';
	resetTextBoxCSS(check.txtName);
	resetTextBoxCSS(check.txtFriendName);
	resetTextBoxCSS(check.txtEmail);
	resetTextBoxCSS(check.txtFriendEmail);
	
	
	if (check.txtName.value == "")
	{
		applyErrorTextBoxCSS(check.txtName);
		if (errorDetails == '')
		{
			errorControl = check.txtName;
		}
		errorDetails = errorDetails + " - The Name field cannot be blank\n";
		
	}
	if (check.txtEmail.value == "")
	{
		applyErrorTextBoxCSS(check.txtEmail);
		if (errorDetails == '')
		{
			errorControl = check.txtEmail;
		}
		errorDetails = errorDetails + " - The Email field cannot be blank\n";
	}
	if (isValidEmail(check.txtEmail.value)==false)
	{
		applyErrorTextBoxCSS(check.txtEmail);
		if (errorDetails == '')
		{
			errorControl = check.txtEmail;
		}
		errorDetails = errorDetails + " - Please enter a valid email address\n";
	}
	if (check.txtFriendName.value == "")
	{
		applyErrorTextBoxCSS(check.txtFriendName);
		if (errorDetails == '')
		{
			errorControl = check.txtFriendName;
		}
		errorDetails = errorDetails + " - The Friends name field cannot be blank\n";
		
	}
	if (check.txtFriendEmail.value == "")
	{
		applyErrorTextBoxCSS(check.txtFriendEmail);
		if (errorDetails == '')
		{
			errorControl = check.txtFriendEmail;
		}
		errorDetails = errorDetails + " - The Friends email field cannot be blank\n";
	}
	if (isValidEmail(check.txtFriendEmail.value)==false)
	{
		applyErrorTextBoxCSS(check.txtFriendEmail);
		if (errorDetails == '')
		{
			errorControl = check.txtFriendEmail;
		}
		errorDetails = errorDetails + " - Please enter a valid Friends email address\n";
	}
	
	
	if (errorDetails == '')
	{
		return true;
	}
	else
	{
		alert('To submit the form you need to enter the following mandatory fields \n' + errorDetails);
		if (errorControl != '')
			errorControl.focus();
			
		return false;
	}
}
