// JavaScript Document

function RTrim(VALUE){
var w_space = String.fromCharCode(32);
var v_length = VALUE.length;
var strTemp = "";
if(v_length < 0){
return"";
}
var iTemp = v_length -1;

while(iTemp > -1){
if(VALUE.charAt(iTemp) == w_space){
}
else{
strTemp = VALUE.substring(0,iTemp +1);
break;
}
iTemp = iTemp-1;

} //End While
return strTemp;

} //End Function
function validsearch()
{
	//alert("test")
	var year = document.form1.year.value;
	//alert(year)
	var make = RTrim(document.form1.make.value);
	//alert(make)
	MakeOne = make.replace(/\s/g,"-");
	//alert(MakeOne)
	URL = 'http://www.discountcompressor.com/selectmodel/'+year+'_'+MakeOne+'.html';
	//alert(URL)
	window.location.replace(URL);
	return false;
}

function chkdiv()
	{
		if (document.getElementById('forgotpass').style.display == "none")
			document.getElementById('forgotpass').style.display='block';
		else
			document.getElementById('forgotpass').style.display='none';
	}

function validsearch1()
{
	//alert("s")
	var year1 = document.searchform.year1.value;
	//alert(year1)
	var make1 = RTrim(document.searchform.make1.value);
	//alert(make1)
	MakeOne1 = make1.replace(/\s/g,"-");
	URL1 = 'http://www.discountcompressor.com/selectmodel/'+year1+'_'+MakeOne1+'.html';
	//alert(URL1)
	window.location.replace(URL1);
	return false;
}

function FnEvents()
	{
	var eve;
	eve = document.parts.descriptform.value;
	if (eve == 1)
	{
	alert("Please Select a Part");
	document.parts.descriptform.focus();
	}
	else
	document.location.href=eve;
	}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

	function discountvalidation()
	{
		if(document.discount.promocode.value=="")
		 {
		 	alert("Please enter the Coupon Code");
			document.discount.promocode.select();
			return false;
		}
		var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";
		for(var i=0;i<document.discount.promocode.value.length;i++)
		{
			if(iChars.indexOf(document.discount.promocode.value.charAt(i)) != -1)
			{
				alert("Coupon Code Has Special Characters. \nThese Are Not Allowed.\n Please Remove Them And Try Again.");
				document.discount.promocode.select();
				return false;
			}
		}
	  return true;		
	}


/*Login scripts*/

function formvalidate()
	{
		if (document.loginform.username.value=="")
			{
		    alert("Please Enter User Name");
		    document.loginform.username.select();
		    document.loginform.username.focus();
		    return false;
			}
		if (document.loginform.password.value=="")
			{
		    alert("Please Enter Password");
		    document.loginform.password.select();
		    document.loginform.password.focus();
		    return false;
			}
	}


// ----------------------------------------------------------------------------
// DATE VALIDATION
// ----------------------------------------------------------------------------
function checkValidDate(dateStr) {
    // dateStr must be of format month day year with either slashes
    // or dashes separating the parts. Some minor changes would have
    // to be made to use day month year or another format.
    // This function returns True if the date is valid.
    var slash1 = dateStr.indexOf("/");
    if (slash1 == -1) { slash1 = dateStr.indexOf("-"); }
    // if no slashes or dashes, invalid date
    if (slash1 == -1) { return false; }
    var dateMonth = dateStr.substring(0, slash1)
    var dateMonthAndYear = dateStr.substring(slash1+1, dateStr.length);
    var slash2 = dateMonthAndYear.indexOf("/");
    if (slash2 == -1) { slash2 = dateMonthAndYear.indexOf("-"); }
    // if not a second slash or dash, invalid date
    if (slash2 == -1) { return false; }
    var dateDay = dateMonthAndYear.substring(0, slash2);
    var dateYear = dateMonthAndYear.substring(slash2+1, dateMonthAndYear.length);
    if ( (dateMonth == "") || (dateDay == "") || (dateYear == "") ) { return false; }
    // if any non-digits in the month, invalid date
    for (var x=0; x < dateMonth.length; x++) {
        var digit = dateMonth.substring(x, x+1);
        if ((digit < "0") || (digit > "9")) { return false; }
    }
    // convert the text month to a number
    var numMonth = 0;
    for (var x=0; x < dateMonth.length; x++) {
        digit = dateMonth.substring(x, x+1);
        numMonth *= 10;
        numMonth += parseInt(digit);
    }
    if ((numMonth <= 0) || (numMonth > 12)) { return false; }
    // if any non-digits in the day, invalid date
    for (var x=0; x < dateDay.length; x++) {
        digit = dateDay.substring(x, x+1);
        if ((digit < "0") || (digit > "9")) { return false; }
    }
    // convert the text day to a number
    var numDay = 0;
    for (var x=0; x < dateDay.length; x++) {
        digit = dateDay.substring(x, x+1);
        numDay *= 10;
        numDay += parseInt(digit);
    }
    if ((numDay <= 0) || (numDay > 31)) { return false; }
    // February can't be greater than 29 (leap year calculation comes later)
    if ((numMonth == 2) && (numDay > 29)) { return false; }
    // check for months with only 30 days
    if ((numMonth == 4) || (numMonth == 6) || (numMonth == 9) || (numMonth == 11)) { 
        if (numDay > 30) { return false; } 
    }
    // if any non-digits in the year, invalid date
    for (var x=0; x < dateYear.length; x++) {
        digit = dateYear.substring(x, x+1);
        if ((digit < "0") || (digit > "9")) { return false; }
    }
    // convert the text year to a number
    var numYear = 0;
    for (var x=0; x < dateYear.length; x++) {
        digit = dateYear.substring(x, x+1);
        numYear *= 10;
        numYear += parseInt(digit);
    }
    // Year must be a 2-digit year or a 4-digit year
    if ( (dateYear.length != 2) && (dateYear.length != 4) ) { return false; }
    // if 2-digit year, use 50 as a pivot date
    if ( (numYear < 50) && (dateYear.length == 2) ) { numYear += 2000; }
    if ( (numYear < 100) && (dateYear.length == 2) ) { numYear += 1900; }
    if ((numYear <= 0) || (numYear > 9999)) { return false; }
    // check for leap year if the month and day is Feb 29
    if ((numMonth == 2) && (numDay == 29)) {
        var div4 = numYear % 4;
        var div100 = numYear % 100;
        var div400 = numYear % 400;
        // if not divisible by 4, then not a leap year so Feb 29 is invalid
        if (div4 != 0) { return false; }
        // at this point, year is divisible by 4. So if year is divisible by
        // 100 and not 400, then it's not a leap year so Feb 29 is invalid
        if ((div100 == 0) && (div400 != 0)) { return false; }
    }
    // date is valid
    return true;
}
// ----------------------------------------------------------------------------
// DATE VALIDATION FINISHED
// ----------------------------------------------------------------------------



function e(s) {
	rex=true;
	if (window.RegExp)
	{
		st="a";ex=new RegExp(st);
		if (st.match(ex)) {
		r1=new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
		r2=new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
		b=(!r1.test(s)&&r2.test(s));
		}
		else
		{
			rex=false;
		}
	}
	else
	{
		rex=false;
	}
	if(!rex) b=(s.indexOf("@")>0 && s.indexOf(".")>0 && s!="" && s!="enter e-mail");
	return (b);
}

function CheckALL(refControlArray,strAny){
	var formIndex;
	var controlMax = refControlArray.length
	for (formIndex = 0; formIndex < controlMax; formIndex++)
	{
		if (refControlArray[formIndex].value==strAny){
		refControlArray[formIndex].checked = true;
		}
		else{
		refControlArray[formIndex].checked = false;
		}
	}
}

function UnCheckLATER(refControlArray,strAny){
	var formIndex;
	var controlMax = refControlArray.length
	for (formIndex = 0; formIndex < controlMax; formIndex++)
	{
		if (refControlArray[formIndex].value==strAny){
		refControlArray[formIndex].checked = false;
		}
	}
}

function isAlphanumeric(string, ignoreWhiteSpace) {
	if (string.search) {
		if ((ignoreWhiteSpace && string.search(/[^\w\s][_]/) != -1) || (!ignoreWhiteSpace && string.search(/\W/) != -1)) return false;
	}
	return true;
}


function openMailbox(){

  var app=navigator.appName, popurl="../mailbox/mailhome.asp";
  if (app.indexOf('Microsoft') == 0) {
  	winpops=window.open(popurl,"","scrollbars=yes,width=600,height=350,left=0,top=195")
  }else if (app.indexOf('Netscape') == 0) {
	winpops=window.open(popurl,"","scrollbars=yes,width=600,height=350,screenX=0,screenY=195")
  } 
}

function get_cookie(Name) {
  var search = Name + "="
  var returnvalue = "";
  if (document.cookie.length > 0) {
    offset = document.cookie.indexOf(search)
    if (offset != -1) { // if cookie exists
      offset += search.length
      // set index of beginning of value
      end = document.cookie.indexOf(";", offset);
      // set index of end of cookie value
      if (end == -1)
         end = document.cookie.length;
      returnvalue=unescape(document.cookie.substring(offset, end))
      }
   }
  return returnvalue;
}

function loadMailOnce(){
	if (get_cookie('MailBoxLoaded')==''){
		openMailbox()
		document.cookie="MailBoxLoaded=yes"
	}
}


function formmailchk()
{
	
	if (document.form1.email.value=="")
	{
		alert("Please Enter Email Id");
		document.form1.email.select();
		document.form1.email.focus();
		return false;
	}
	if(!e(document.form1.email.value))
		{
			alert("\Please enter a proper e-mail address, of the form id@domain.zzz or id@domain.xx.zzz.");
			document.form1.email.focus();
			return false;
		}
		return true;
	}
	
	
/*User account script	*/

function accountform()
{
	if (document.custdetails.bfirstname.value=="")
	{
		alert("Please Enter FirstName");
		document.custdetails.bfirstname.select();
		document.custdetails.bfirstname.focus();
		return false;
	}
	if (document.custdetails.blastname.value=="")
	{
		alert("Please Enter LastName");
		document.custdetails.blastname.select();
		document.custdetails.blastname.focus();
		return false;
	}
	if (document.custdetails.baddress1.value=="")
	{
		alert("Please Enter Address1");
		document.custdetails.baddress1.select();
		document.custdetails.baddress1.focus();
		return false;
	}
	if (document.custdetails.bcity.value=="")
	{
		alert("Please Enter A City");
		document.custdetails.bcity.select();
		document.custdetails.bcity.focus();
		return false;
	}
	if (document.custdetails.bstate.value=="")
	{
		alert("Please Enter State");
		document.custdetails.bstate.select();
		document.custdetails.bstate.focus();
		return false;
	}
	if (document.custdetails.bcountry.value=="")
	{
		alert("Please Enter Country");
		document.custdetails.bcountry.select();
		document.custdetails.bcountry.focus();
		return false;
	}
	if (document.custdetails.bzip.value=="")
	{
		alert("Please Enter Zip Code");
		document.custdetails.bzip.select();
		document.custdetails.bzip.focus();
		return false;
	}
	if (document.custdetails.bphone.value=="")
	{
		alert("Please Enter A Phone Number");
		document.custdetails.bphone.select();
		document.custdetails.bphone.focus();
		return false;
	}
	if (document.custdetails.sfirstname.value=="")
	{
		alert("Please Enter FirstName");
		document.custdetails.sfirstname.select();
		document.custdetails.sfirstname.focus();
		return false;
	}
	if (document.custdetails.slastname.value=="")
	{
		alert("Please Enter LastName");
		document.custdetails.slastname.select();
		document.custdetails.slastname.focus();
		return false;
	}
	if (document.custdetails.saddress1.value=="")
	{
		alert("Please Enter Address");
		document.custdetails.saddress1.select();
		document.custdetails.saddress1.focus();
		return false;
	}
	if (document.custdetails.scity.value=="")
	{
		alert("Please Enter A City");
		document.custdetails.scity.select();
		document.custdetails.scity.focus();
		return false;
	}
	if (document.custdetails.sstate.value=="")
	{
		alert("Please Enter State");
		document.custdetails.sstate.select();
		document.custdetails.sstate.focus();
		return false;
	}
	if (document.custdetails.scountry.value=="")
	{
		alert("Please Enter Country");
		document.custdetails.scountry.select();
		document.custdetails.scountry.focus();
		return false;
	}
	if (document.custdetails.szip.value=="")
	{
		alert("Please Enter Zip Code");
		document.custdetails.szip.select();
		document.custdetails.szip.focus();
		return false;
	}
	if (document.custdetails.sphone.value=="")
	{
		alert("Please Enter A Phone Number");
		document.custdetails.sphone.select();
		document.custdetails.sphone.focus();
		return false;
	}
	if (document.custdetails.email.value=="")
	{
		alert("Please Enter Email Id");
		document.custdetails.email.select();
		document.custdetails.email.focus();
		return false;
	}
		if(!e(document.custdetails.email.value))
		{
			alert("\Please enter a proper e-mail address, of the form id@domain.zzz or id@domain.xx.zzz.");
			document.custdetails.email.focus();
			return false;
		}
		return true;
	}
	
	function shipSameAsBill()
{
	
	if (document.custdetails.shipsameasbill.checked == true)
	{
		document.custdetails.sfirstname.value=document.custdetails.bfirstname.value;
		document.custdetails.slastname.value=document.custdetails.blastname.value;
		document.custdetails.saddress1.value=document.custdetails.baddress1.value;
		document.custdetails.saddress2.value=document.custdetails.baddress2.value;
		document.custdetails.scity.value=document.custdetails.bcity.value;
		document.custdetails.sstate.value=  document.custdetails.bstate.value;
		document.custdetails.sphone.value=  document.custdetails.bphone.value;
		document.custdetails.szip.value = document.custdetails.bzip.value;
		document.custdetails.scountry.value=  document.custdetails.bcountry.value;
	}
	else
	{
		document.custdetails.sfirstname.value="";
		document.custdetails.slastname.value="";
		document.custdetails.saddress1.value="";
		document.custdetails.saddress2.value="";
		document.custdetails.scity.value="";
		document.custdetails.sstate.value =  "";
		document.custdetails.sphone.value="";
		document.custdetails.szip.value = "";
		document.custdetails.scountry.value =  "";
	}	
	
}


/*change pasword*/

function passwordchk()
{
	if (document.pwdform.oldpwd.value=="")
	{
		alert("Please Enter Your Old Password!");
		document.pwdform.oldpwd.select();
		document.pwdform.oldpwd.focus();
		return false;
	}
	if (document.pwdform.newpwd.value=="")
	{
		alert("Please Enter A New Password!");
		document.pwdform.newpwd.select();
		document.pwdform.newpwd.focus();
		return false;
	}
	if (document.pwdform.confpwd.value=="")
	{
		alert("Please Enter Confirm Your New Password!");
		document.pwdform.confpwd.select();
		document.pwdform.confpwd.focus();
		return false;
	}
	if (document.pwdform.newpwd.value!=document.pwdform.confpwd.value)
	{
		alert("The New Password and Confirmation Mismatch!");
		document.pwdform.confpwd.select();
		document.pwdform.confpwd.focus();
		return false;
	}
	}
	
function contactvalid()
{
 if(document.contactus.custname.value == "")
 {
  alert("Please Enter the Name");
  document.contactus.custname.select();
  return false;
 }
id1=document.contactus.email.value.indexOf("@")
id2=document.contactus.email.value.indexOf(".")
if (document.contactus.email.value=="")
{
 alert("Please enter the Email ID");
 document.contactus.email.focus();
 return false;
}

 if(id1==-1)
  {
  alert("Invalid Email Id:");
  document.contactus.email.focus();
  return false;
  } 
 if(id2==-1)
  {
  alert("Please Enter Domain Name Also:");
  document.contactus.email.focus();
  return false;
  }
var iChars = "!#$%^&*()+=-[]\\\';,/{}|\":<>?";

  for (var i = 0; i < document.contactus.email.value.length; i++) {
   if (iChars.indexOf(document.contactus.email.value.charAt(i)) != -1) {
   alert ("Your username has special characters. \nThese are not allowed.\n Please remove them and try again.");
 document.contactus.email.focus();
   return false;
   }
  }
 /*if(document.contactus.company.value == "")
 {
  alert("Please Enter the Company Name");
  document.contactus.company.select();
  return false;
 }*/
 if(document.contactus.city.value == "")
 {
  alert("Please Enter the City Name and State Name");
  document.contactus.city.select();
  return false;
 }
 if(document.contactus.phone.value == "")
 {
  alert("Please Enter the Phone Number");
  document.contactus.phone.select();
  return false;
 }
var num=document.contactus.phone.value;
 for(i=0;i<num.length;i++)
 { 
  var number=num.substring(i,i+1); 
  if(!((((number>=0) && (number<=9)) || (number=='-'))))
  {
   alert("Enter only numbers in Phone Number");
   document.contactus.phone.focus();
   return false;
  }
 }
 if(document.contactus.year.value == "")
 {
  alert("Please Enter the Year");
  document.contactus.year.select();
  return false;
 }
var num=document.contactus.year.value;
 for(i=0;i<num.length;i++)
 { 
  var number=num.substring(i,i+1); 
  if(!((((number>=0) && (number<=9)))))
  {
   alert("Enter only numbers in Year");
   document.contactus.year.focus();
   return false;
  }
 }
 if(document.contactus.make.value == "")
 {
  alert("Please Enter the Make Name");
  document.contactus.make.select();
  return false;
 }
 if(document.contactus.carinfo.value == "")
 {
  alert("Please Enter the Carinfo");
  document.contactus.carinfo.select();
  return false;
 }
 if(document.contactus.part.value == "")
 {
  alert("Please Enter the Part Name");
  document.contactus.part.select();
  return false;
 }
return true;  
}

	