function popWindow(thisWindow, thisTop, thisLeft, thisWidth, thisHeight)
// Function to open a parsed file name.
{
	window.open(thisWindow, "thisTitle", "toolbar=no,left="+thisLeft+",top="+thisTop+",width="+thisWidth+",height="+thisHeight+",directories=no,status=no,scrollbars=yes,resizable=no,menubar=no");
}

function popTermsWindow(thisWindow, thisTitle, thisTop, thisLeft, thisWidth, thisHeight)
// Function to open a parsed file name.
{
	window.open(thisWindow, thisTitle, "toolbar=no,left="+thisLeft+",top="+thisTop+",width="+thisWidth+",height="+thisHeight+",directories=no,status=no,scrollbars=yes,resizable=no,menubar=no");
}

function showVehiclePhoto(vehicleInfo)
// Function to pop up a vehicle photo window.
{
	window.open("popupwindow.cfm?vehicleID=" + vehicleInfo, "VehiclePhoto", "toolbar=no,left=50,top=0,width=675,height=560,directories=no,status=no,scrollbars=yes,resizable=yes,menubar=no");
}

function showVehicleFuelLabel(vehicleInfo)
// Function to pop up a vehicle fuel label window.
{
	window.open("fuelLabel.cfm?vehicleID=" + vehicleInfo, "" +vehicleInfo+ "", "toolbar=no,left=50,top=0,width=720,height=560,directories=no,status=no,scrollbars=no,resizable=yes,menubar=no");
}

//-----------------------------------------------------------

function showIndividualVehicleDetails(vehicleInfo)
// Function to pop up a vehicle photo window.
{
	window.open("viewdetailedindividual.cfm?pVehicleID=" + vehicleInfo, "VehicleDetails", "toolbar=no,left=50,top=0,width=600,height=570,directories=no,status=no,scrollbars=no,resizable=no,menubar=no");
}

//-----------------------------------------------------------

function showIndividualVehicleDetailsPriceUpdate(vehicleInfo)
// Function to pop up a vehicle photo window.
{
	window.open("Prices/updateIndividualVehiclePrice.cfm?pVehicleID=" + vehicleInfo, "VehicleDetails", "toolbar=no,left=50,top=0,width=600,height=570,directories=no,status=no,scrollbars=no,resizable=no,menubar=no");
}

//-----------------------------------------------------------

function editVehicle(vehicleInfo)
// Function to pop up a vehicle photo window.
{
	window.open("editvehicle.cfm?VehicleID=" + vehicleInfo, "VehicleDetails", "toolbar=no,left=50,top=0,width=600,height=570,directories=no,status=no,scrollbars=no,resizable=no,menubar=no");
}

//-----------------------------------------------------------

function testForDynamicURL()
{
// Function to grab a url and test if record reference has been passed
// in it to display search results records in the main frame.
// The URL needs to be: "http://www.nzan.co.nz/fordwholesale/fordbuyonline/index.cfm?id=12345drm"
// Where id is the VIN number in the database to retrieve and drm
// represents the end of the record number in case something else is
// tacked on the end in error.

var dynamicURL = parent.location.href;

// Test to see if this is a standard index load, or contains a record
// number.

if ((dynamicURL != "http://www.nzan.co.nz/FordWholesale/FordBuyOnline") &&
	(dynamicURL != "http://www.nzan.co.nz/FordWholesale/FordBuyOnline/") && 
	(dynamicURL != "http://www.nzan.co.nz/FordWholesale/FordBuyOnline/index.cfm") && 
	(dynamicURL != "http://nzanwebt/FordWholesale/FordBuyOnline") && 
	(dynamicURL != "http://nzanwebt/FordWholesale/FordBuyOnline/") &&
	(dynamicURL != "http://nzanwebt/FordWholesale/FordBuyOnline/index.cfm") &&
	(dynamicURL != "http://nzanweb/FordWholesale/FordBuyOnline") &&
	(dynamicURL != "http://nzanweb/FordWholesale/FordBuyOnline/") &&
	(dynamicURL != "http://nzanweb/FordWholesale/FordBuyOnline/index.cfm"))
	{
	// Locate the record component of the URL.
	var VINStart = dynamicURL.search("vin=");
	var VINEnd = dynamicURL.search("&eol");
	
	if (VINEnd != -1)
		{
		// Then "drm" has been found, so continue.
		var pVIN = dynamicURL.slice(VINStart + 4, VINEnd);
		//searchMessage();
		document.SearchCriteriaForm.action="viewdetailedindividual.cfm?pVIN=" + pVIN;
		return document.SearchCriteriaForm.submit();
		}
		
	else
		{
		// Then "drm" has not been found, so simply load the index, as
		// it is unclear what may have been tacked on the end, or missed off.
		alert("Sorry, but the Ford Fixed Price System was unable to ascertain\nthe correct 'id' from the given URL address.\n\nPlease try copying and pasting the URL address into the\nbrowser address field instead.\n\nThank-you.");
		return false;
		}
	}
}

//-----------------------------------------------------------

function printPage()
// Function to print the current web page.
{
	if (window.print != null)
		{
		window.print();
		}
	else
		{
		alert('Unfortunately, your browser does not support this shortcut.  Please select Print from the File menu.');
		}
}

//-----------------------------------------------------------

function ConfirmCloseSite(Status)
{
	if(confirm("Are you sure you want to "+ Status+ " the user site?"))
		{
			window.location.href="Index.cfm?layout=Open_Close_Main&sublayout=Open_Close_MainAction&OpenStatus=" +Status+""
		}
}
//-----------------------------------------------------------

function check_date(field)
{
	var checkstr = "0123456789";
	var seperator = ".";
	var day;
	var month;
	var year;
	var leap = 0;
	var err = 0;
	var i;

   err = 0;
   /* year is wrong if year = 0000 */
   year = openclose.StartYear.value;
   if (year == 0) {
      err = 20;
   }
   /* Validation of month*/
   month = openclose.StartMonth.value;
   if ((month < 1) || (month > 12)) {
      err = 21;
   }
   /* Validation of day*/
   day = openclose.StartDay.value;
   if (day < 1) {
     err = 22;
   }
   /* Validation leap-year / february / day */
   if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) {
      leap = 1;
   }
   if ((month == 2) && (leap == 1) && (day > 29)) {
      err = 23;
   }
   if ((month == 2) && (leap != 1) && (day > 28)) {
      err = 24;
   }
   /* Validation of other months */
   if ((day > 31) && ((month == "1") || (month == "3") || (month == "5") || (month == "7") || (month == "8") || (month == "10") || (month == "12"))) {
      err = 25;
   }
   if ((day > 30) && ((month == "4") || (month == "6") || (month == "9") || (month == "11"))) {
      err = 26;
   }
   /* if 00 ist entered, no error, deleting the entry */
   if ((day == 0) && (month == 0) && (year == 00)) {
      err = 0; day = ""; month = ""; year = ""; seperator = "";
   }
   /* if no error, write the completed date to Input-Field (e.g. 13.12.2001) */
   if (err == 0) {
      //DateField.value = day + seperator + month + seperator + year;
   }
   /* Error-message if err != 0 */
   else {
      alert("Sorry, but the date you have selected is invalid.");
   }
}
//-----------------------------------------------------------


function check_date2(field)
{
	var checkstr = "0123456789";
	var seperator = ".";
	var day;
	var month;
	var year;
	var leap = 0;
	var err = 0;
	var i;

   err = 0;

   /* year is wrong if year = 0000 */
   year = openclose.EndYear.value;
   if (year == 0) {
      err = 20;
   }
   /* Validation of month*/
   month = openclose.EndMonth.value;
   if ((month < 1) || (month > 12)) {
      err = 21;
   }
   /* Validation of day*/
   day = openclose.EndDay.value;
   if (day < 1) {
     err = 22;
   }
   /* Validation leap-year / february / day */
   if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) {
      leap = 1;
   }
   if ((month == 2) && (leap == 1) && (day > 29)) {
      err = 23;
   }
   if ((month == 2) && (leap != 1) && (day > 28)) {
      err = 24;
   }
   /* Validation of other months */
   if ((day > 31) && ((month == "1") || (month == "3") || (month == "5") || (month == "7") || (month == "8") || (month == "10") || (month == "12"))) {
      err = 25;
   }
   if ((day > 30) && ((month == "4") || (month == "6") || (month == "9") || (month == "11"))) {
      err = 26;
   }
   /* if 00 ist entered, no error, deleting the entry */
   if ((day == 0) && (month == 0) && (year == 00)) {
      err = 0; day = ""; month = ""; year = ""; seperator = "";
   }
   /* if no error, write the completed date to Input-Field (e.g. 13.12.2001) */
   if (err == 0) {
      //DateField.value = day + seperator + month + seperator + year;
   }
   /* Error-message if err != 0 */
   else {
      alert("Sorry, but the date you have selected is invalid.");
   }
}
//-----------------------------------------------------------

function check_OpenReportDate(field)
{
	var checkstr = "0123456789";
	var seperator = ".";
	var day;
	var month;
	var year;
	var leap = 0;
	var err = 0;
	var i;

   err = 0;

   /* year is wrong if year = 0000 */
   year = salesreport.StartYear.value;
   if (year == 0) {
      err = 20;
   }
   /* Validation of month*/
   month = salesreport.StartMonth.value;
   if ((month < 1) || (month > 12)) {
      err = 21;
   }
   /* Validation of day*/
   day = salesreport.StartDay.value;
   if (day < 1) {
     err = 22;
   }
   /* Validation leap-year / february / day */
   if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) {
      leap = 1;
   }
   if ((month == 2) && (leap == 1) && (day > 29)) {
      err = 23;
   }
   if ((month == 2) && (leap != 1) && (day > 28)) {
      err = 24;
   }
   /* Validation of other months */
   if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) {
      err = 25;
   }
   if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) {
      err = 26;
   }
   /* if 00 ist entered, no error, deleting the entry */
   if ((day == 0) && (month == 0) && (year == 00)) {
      err = 0; day = ""; month = ""; year = ""; seperator = "";
   }
   /* if no error, write the completed date to Input-Field (e.g. 13.12.2001) */
   if (err == 0) {
      //DateField.value = day + seperator + month + seperator + year;
   }
   /* Error-message if err != 0 */
   else {
      alert("Sorry, but the date you have selected is invalid.");
   }
}


//-----------------------------------------------------------

function check_CloseReportDate(field)
{
	var checkstr = "0123456789";
	var seperator = ".";
	var day;
	var month;
	var year;
	var leap = 0;
	var err = 0;
	var i;

   err = 0;

   /* year is wrong if year = 0000 */
   year = salesreport.EndYear.value;
   if (year == 0) {
      err = 20;
   }
   /* Validation of month*/
   month = salesreport.EndMonth.value;
   if ((month < 1) || (month > 12)) {
      err = 21;
   }
   /* Validation of day*/
   day = salesreport.EndDay.value;
   if (day < 1) {
     err = 22;
   }
   /* Validation leap-year / february / day */
   if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) {
      leap = 1;
   }
   if ((month == 2) && (leap == 1) && (day > 29)) {
      err = 23;
   }
   if ((month == 2) && (leap != 1) && (day > 28)) {
      err = 24;
   }
   /* Validation of other months */
   if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) {
      err = 25;
   }
   if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) {
      err = 26;
   }
   /* if 00 ist entered, no error, deleting the entry */
   if ((day == 0) && (month == 0) && (year == 00)) {
      err = 0; day = ""; month = ""; year = ""; seperator = "";
   }
   /* if no error, write the completed date to Input-Field (e.g. 13.12.2001) */
   if (err == 0) {
      //DateField.value = day + seperator + month + seperator + year;
   }
   /* Error-message if err != 0 */
   else {
      alert("Sorry, but the date you have selected is invalid.");
   }
}

//-----------------------------------------------------------

function CheckInternalUser(thisCheckBox)
{// Function to manage non-allowance of joint Admin and Super Admin selection,
// and to select 'InternalUser' if 'Admin' or 'SuperAdmin' are selected.
	switch(thisCheckBox.name)
		{
		case "InternalUser":
			{// If Admin or Super are selected, then auto-select 'Internal User'
			if ((document.UserForm.AccessLevel.checked == true) | (document.UserForm.SuperAccessLevel.checked == true))
				document.UserForm.InternalUser.checked = true;
			break;
			}
		
		case "AccessLevel":
			{//Auto-select 'Internal User' if selected and de-select 'Super Admin'
			if (thisCheckBox.checked == true)
				{
				document.UserForm.InternalUser.checked = true;
				document.UserForm.SuperAccessLevel.checked = false;
				}
			break;
			}
		
		case "SuperAccessLevel":
			{//Auto-select 'Internal User' if selected and de-select 'Admin'
			if (thisCheckBox.checked == true)
				{
				document.UserForm.InternalUser.checked = true;
				document.UserForm.AccessLevel.checked = false;
				}
			break;
			}
		}
}

//-----------------------------------------------------------

// Functions created by Fireworks for image roll-over and preloading.
  
function MM_findObj(n, d)
{ //v3.0
  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); return x;
}

/* Functions that swaps images. */
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];}
}

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;
}

/* Functions that handle preload. */
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 login(){
	window.open('login.cfm', '','status=no,toolbar=no,location=no,resizable=no,scrollbars=no,left=194,top=169,height=180,width=300');
}

//-----------------------------------------------------------

function loginAlert(){
	alert('CAR SELECT:\n\nRegistration functionality coming soon.\nPlease try again next time.\n\n                                            Thank you');
}

//-----------------------------------------------------------

function logout(){
	window.open('logout.cfm', '','status=no,toolbar=no,location=no,resizable=no,scrollbars=no,left=220,top=180,height=150,width=300');
}

//-----------------------------------------------------------

// Check for a valid email address.
function IsEmailAddr(email)
{
  var result = false
  var theStr = new String(email)
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  return result;
}

//-----------------------------------------------------------

// Check all required fields have been entered on the Contact Us Form.
function ValidateContact() 
{
	  if ((FeedbackForm.name.value == "") || (FeedbackForm.name.value == "(required)"))
	    {
	    alert("Please enter your Name");
	    FeedbackForm.name.focus();
	    return (false);
	    }

	  if ((FeedbackForm.email.value == "") || (FeedbackForm.email.value == "(required)"))
	    {
	    alert("Please enter your Email Address");
	    FeedbackForm.email.focus();
	    return (false);
	    }
		
	  if (!IsEmailAddr(FeedbackForm.email.value))
	    {
	    alert("Please enter a complete Email Address i.e. yourname@yourdomain.com");
	    FeedbackForm.email.focus();
	    return (false);
	    }

	  if (FeedbackForm.comments.value == "(required)" || FeedbackForm.comments.value == "")
	    {
	    alert("Please enter your Comments");
	    FeedbackForm.comments.focus();
	    return (false);
	    }

      else
        {
        return (true);
        }
}  

//-----------------------------------------------------------

//Check that both Region and Dealer haven't been selected together.
function CheckSelection()
{
	var selectedRegion = document.SearchCriteriaForm.RegionCode.options[document.SearchCriteriaForm.RegionCode.selectedIndex].value;
	var selectedDealer = document.SearchCriteriaForm.Dealer.options[document.SearchCriteriaForm.Dealer.selectedIndex].value;
	//var selectedDealerName = document.SearchCriteriaForm.Dealer.options[document.SearchCriteriaForm.Dealer.selectedIndex].DealerName;	
	//var selectedDealerCode = document.SearchCriteriaForm.Dealer.options[document.SearchCriteriaForm.Dealer.selectedIndex].DealerCode;
	
	if (selectedRegion != "All"  && selectedDealer != "All")
		{
		alert('DEALER / REGION SELECTION:\n\nPlease refine your search so that either a Dealer\nor a Region is selected - but not both together.\n\n                                                            Thank you');
		document.SearchCriteriaForm.RegionCode.focus();
		return false;
		}
	else
		{
		return true;
		}
}

//-----------------------------------------------------------

//Change the Dealer Name and Code to that selected.
function ChangeValues()
{
	var selectedDealerName = document.SearchCriteriaForm.Dealer.options[document.SearchCriteriaForm.Dealer.selectedIndex].DealerName;	
	var selectedDealerCode = document.SearchCriteriaForm.Dealer.options[document.SearchCriteriaForm.Dealer.selectedIndex].DealerCode;
	
	document.SearchCriteriaForm.SelectedDealerName.value = selectedDealerName;
	document.SearchCriteriaForm.SelectedDealerCode.value = selectedDealerCode;
	return true;
}
