/*
Back end processing javascript functions created by Amos Mah
November 27, 2006
*/
function isNum(passedVal){
for (i=0; i<passedVal.length;i++){

   if (passedVal.charAt(i) > 9){
       return false
   }
   if(passedVal.charAt(i) <0){
       return false
   }

   return true
}
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}


function isValidDate(day,month,year){

   var DaysInMonth = new Array();
        DaysInMonth[1]=31;
        
        DaysInMonth[2]=daysInFebruary(year);
        DaysInMonth[3]=31;
        DaysInMonth[4]=30;
        DaysInMonth[5]=31;
        DaysInMonth[6]=30;
        DaysInMonth[7]=31;
        DaysInMonth[8]=31;
        DaysInMonth[9]=30;
        DaysInMonth[10]=31;
        DaysInMonth[11]=30;
        DaysInMonth[12]=31;

        if(day>DaysInMonth[month] || day<1){
         return false;
        }else{
         return true
        }
}
function ValidEmail(email){
  invalidChars = "/:,;"

  if(email == ""){
     return false
  }
  for(i=0; i<invalidChars.length;i++){
  badChar = invalidChars.charAt(i)
  if(email.indexOf(badChar,0) > -1){
  return false
  }
  }
  atPos=email.indexOf("@",1)
  if(atPos == -1){
     return false
  }
  if (email.indexOf("@",atPos+1) !=-1){
     return false
  }
  periodPos = email.indexOf(".",atPos)
  if(periodPos == -1){
     return false
  }
  if(periodPos+2 > email.length){
     return false
  }
  return true
  }

function ValidWeb(webaddress){
  invalidChars = "/:,;"

  if(webaddress == "www.YourSite.com"){
     return false
  }

  for(i=0; i<invalidChars.length;i++){
  badChar = invalidChars.charAt(i)
  if(webaddress.indexOf(badChar,0) > -1){
  return false
  }
  }
  
  WebL = webaddress.toLower();
  WWWPos=webL.indexOf("www.",atPos)

  if(WWWPos == -1 ){
     return false
  }
  
  SiteName=WebL.substring(WWWPos,WebL.length-4)
  periodPos = SiteName.indexOf(".",atPos)
  if(periodPos == -1){
     return false
  }

  if(periodPos+1 > email.length){
     return false
  }

  return true
}

function SubmitIt(form){

if(form.salutation.selectedIndex ==0){
 alert("please select a salutation")
 return false
}
if(form.FName.value==""){
 alert("please enter your first name")
 form.FName.focus()
 form.FName.select()
 return false
}
if(form.LName.value==""){
 alert("please enter your last name")
 return false
}

if(form.JTitle.value==""){
 alert("please enter your Job Title")
 return false
}

if(form.Company.value==""){
 alert("please enter your company name")
 return false
}
if(form.Street.value==""){
 alert("please enter your street")
 return false
}
if(form.FCity.value==""){
 alert("please enter your city")
 return false
}

if(form.Country.selectedIndex ==0){
 alert("please select a Country")
 return false
}
if(form.Prov.selectedIndex ==0){
 alert("please select a Province")
 return false
}

if(form.Postal.value==""){
 alert("please enter your postal code")
 return false
}

if(!ValidEmail(form.email.value)){
 alert("please re-enter your email address")
 return false
}
/*
if(!ValidWeb(form.WebAdd.value)){
 alert("please re-enter your web address www.YourSite.com")
 return false
}
*/
if(form.Tel.value==""){
 alert("please enter your telephone number")
 return false
}
if(!isNum(form.Tel.value)){
 alert("please re-enter your telephone number")
 return false
}

  if (form.AppYear.selectedIndex ==0){

     form.AppYear.focus()
     alert("Please select a year")
     return false
  }
   if (form.AppMonth.selectedIndex ==0){

     form.AppMonth.focus()
     alert("Please select a month")
     return false
  }
   if (form.AppDay.selectedIndex ==0){

     form.AppDay.focus()
     alert("Please select a day")
     return false
  }


  var dd=form.AppDay.value
    var mm=form.AppMonth.value
      var yr=form.AppYear.value

  if (!isValidDate(dd,mm,yr)){

     form.AppDay.focus()
     alert("Invalid Application Date")
     return false

  }
  if (form.BannerPage.selectedIndex ==0 && form.Item[4].checked){

     form.BannerPage.focus()
     alert("Please select a banner page")
     return false
  }

  
  if (!BoxSelected){
    alert("Please select a product category")
    return false
  }

return true

}


function roundToPrecision(inputNum, desiredPrecision){
  var precisionGuide = Math.pow(10, desiredPrecision);
  return( Math.round(inputNum * precisionGuide) / precisionGuide );
}

function addZeroesToPrecision(inputNum, desiredPrecision){
  var numString = inputNum + "";
  var decimalpt=numString.search(/\./);
  var afterDecimalString;
  if(decimalpt<0){
      numString = numString + ".";
      afterDecimalString = numString.substring(numString.length + 1)
  }else{
      afterDecimalString = numString.substring(decimalpt + 1)
  }

  while (afterDecimalString.length < desiredPrecision) {
    afterDecimalString += "0";
    numString += "0";
  }
  return(numString);
}

var BoxSelected=false;
var AdSelected = new Array();

function fTotal(field){

var fTotalAd=0.00; var multiplier=0.00;
var CatSelected=0;

for(u=0; u<5; u++){

   if(listing.Item[u].checked){
      AdSelected[u]="1"

      if(u==4){
         multiplier=1.00*200.00;
      }else if(u==3){
         multiplier=1.00*150.00;
      }else{
         multiplier=1.00*50.00;
      }

    }else{
       multiplier=0.00;
       AdSelected[u]="0"
    }

     fTotalAd += multiplier;

}


  if(listing.G1.checked){
      CatSelected++;
  }  if(listing.G2.checked){
      CatSelected++;
  }  if(listing.G3.checked){
      CatSelected++;
  }  if(listing.G4.checked){
      CatSelected++;
  }  if(listing.G5.checked){
      CatSelected++;
  }  if(listing.G6.checked){
      CatSelected++;
  }  if(listing.G7.checked){
      CatSelected++;
  }  if(listing.G8.checked){
      CatSelected++;
  }  if(listing.G9.checked){
      CatSelected++;
  }  if(listing.G10.checked){
      CatSelected++;
  }  if(listing.G11.checked){
      CatSelected++;
  }  if(listing.G12.checked){
      CatSelected++;
  }  if(listing.G13.checked){
      CatSelected++;
  }  if(listing.G14.checked){
      CatSelected++;
  }  if(listing.G15.checked){
      CatSelected++;
  }  if(listing.G16.checked){
      CatSelected++;
  }  if(listing.G17.checked){
      CatSelected++;
  }  if(listing.G18.checked){
      CatSelected++;
  }  if(listing.G19.checked){
      CatSelected++;
  }  if(listing.G20.checked){
      CatSelected++;
  }  if(listing.G22.checked){
      CatSelected++;
  }  if(listing.G23.checked){
      CatSelected++;
  }  if(listing.G24.checked){
      CatSelected++;
  }  if(listing.G25.checked){
      CatSelected++;
  }  if(listing.G26.checked){
      CatSelected++;
  }  if(listing.G27.checked){
      CatSelected++;
  }  if(listing.G28.checked){
      CatSelected++;
  }  if(listing.G29.checked){
      CatSelected++;
  }  if(listing.G30.checked){
      CatSelected++;
  }  if(listing.G31.checked){
      CatSelected++;
  }  if(listing.G32.checked){
      CatSelected++;
  }  if(listing.G33.checked){
      CatSelected++;
  }  if(listing.G35.checked){
      CatSelected++;
  }  if(listing.G36.checked){
      CatSelected++;
  }  if(listing.G37.checked){
      CatSelected++;
  }  if(listing.G38.checked){
      CatSelected++;
  }  if(listing.G39.checked){
      CatSelected++;
  }  if(listing.G41.checked){
      CatSelected++;
  }
  
  if (CatSelected>0){
   fTotalAd +=(CatSelected-1)*50.00
   BoxSelected=true
  }else{
     BoxSelected=false
  }




  var temp=parseFloat(fTotalAd);
//  discount=0.5;
  discount=1.0;
  temp *= discount;
  roundedtemp=roundToPrecision(temp,5);

  listing.label1.value = "$ " + addZeroesToPrecision(roundedtemp,2);

  var SubTotal=temp*0.06;
  SubTotal=roundToPrecision(SubTotal,5);
  listing.label2.value = "$ " + addZeroesToPrecision(SubTotal,2);

  var TotalCost=roundedtemp+SubTotal;
   SubTotal=roundToPrecision(TotalCost,5);
   listing.label3.value = "$ " + addZeroesToPrecision(TotalCost,2);

  listing.label4.value = CatSelected;

  listing.AdSelected.value=AdSelected[0]+AdSelected[1]+AdSelected[2]+AdSelected[3]+AdSelected[4]

  return;

}





