var locations = new Array();

function IsAllSpace(strValue) {
    var re = /^\s*$/;
    return re.test(strValue);
}

function IsNumber(strValue) {
    var re = /^\d*$/;
    return re.test(strValue);
}

function pj_set_address4() {
    if (document.form1.apt_code[3].checked) {
        document.form1.apt_code_ext.value = "";
        document.form1.city.focus();
    } else {
        document.form1.apt_code_ext.focus();
    }

}



function IsAllZero(s){
    var i;
    var counter = 0;
    for (i = 0; i < s.length; i++)
    {
        var c = s.charAt(i);
        if (c == "0") {
          counter++;
        }
    }
    if (counter >= s.length) {
       return true;
    }
    return false;
}

function isEmailValid(emailAddress)
{
    var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
    var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,8}|[0-9]{1,3})(\\]?)$");
    return (!r1.test(trim(emailAddress)) && r2.test(trim(emailAddress)));
}

function isSpaceFoundInEmail(strValue)
{
    // check for a space. if any space is found ( after we trim front and back spaces ) is  not valid
    var retValue = trim(strValue);
    var i;
    var counter = 0;
    for (i = 0; i < retValue.length; i++)
    {
        var c = retValue.charAt(i);
        if (c == " ")
        {
          counter++;
        }
    }
    if ( counter > 0)
    {
        return true;
    }

    return false;
}

function trim(inputString)
{
    // Removes leading and trailing spaces from the passed string. Also removes
    // consecutive spaces and replaces it with one space. If something besides
    // a string is passed in (null, custom object, etc.) then return the input.
    if (typeof inputString != "string") {
        return inputString;
    }
    var retValue = inputString;
    var ch = retValue.substring(0, 1);
    while (ch == " ") { // Check for spaces at the beginning of the string
        retValue = retValue.substring(1, retValue.length);
        ch = retValue.substring(0, 1);
    }
    ch = retValue.substring(retValue.length - 1, retValue.length);
    while (ch == " ") { // Check for spaces at the end of the string
        retValue = retValue.substring(0, retValue.length - 1);
        ch = retValue.substring(retValue.length - 1, retValue.length);
    }
    while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
        retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ") + 1, retValue.length); // Again, there are two spaces in each of the strings
    }
    return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function

//
//
//
function validateCustomerInformation()
{
    if (document.form1.email.value.length < 6 || IsAllSpace(document.form1.email.value)) {
        document.form1.email.focus();
        alert("Please confirm that your\n Email Address is correct");
        return false;
    }

    if (isEmailValid(document.form1.email.value) == false) {
        alert("Your email address is not properly formed.  Please change it and try again.");
        return false;
    }

    if (document.form1.agecheck)
    {
        //if (!document.form1.agecheck.checked)
        if (!validateAgeInput() )
        {
            //document.form1.agecheck.focus();
            //alert("Sorry, you must be at least 13 years old to order from papajohns.com");
            return false;
        }
        if (document.form1.password.value.length < 6 || IsAllSpace(document.form1.password.value)) {
            document.form1.password.focus();
            alert("Your Password should be at least 6 characters.");
            return false;
        }
    } else if (document.form1.password.value.length < 6 && !IsAllSpace(document.form1.password.value)) {
        document.form1.password.focus();
        alert("Your Password should be at least 6 characters.");
        return false;
    }

    if (IsAllSpace(document.form1.firstname.value)) {
        document.form1.firstname.focus();
        alert("Please enter your First Name.");
        return false;
    }
    if(checkForAscii(document.form1.firstname.value)){
        document.form1.firstname.focus();
        alert("First name includes invalid characters")
        return false;
    }

    if (IsAllSpace(document.form1.lastname.value)) {
        document.form1.lastname.focus();
        alert("Please enter your Last Name.");
        return false;
    }

    if(checkForAscii(document.form1.lastname.value)){
        document.form1.lastname.focus();
        alert("Last name includes invalid characters")
        return false;
    }

    if (document.form1.password.value.toUpperCase() == document.form1.email.value.toUpperCase()) {
        alert("Your Password cannot be the same as your email");
        return false;
    }

    if (document.form1.verify_password.value != document.form1.password.value) {
        document.form1.verify_password.focus();
        alert("The passwords you entered do not match. Please re-enter your password in both fields.")
        return false;
    }

    if (document.form1.email_confirm) {
        if (document.form1.email_confirm.value != document.form1.email.value) {
            document.form1.email_confirm.focus();
            alert("The emails you entered do not match. Please re-enter the email")
            return false;
        }
    }


    // age cannot be > 30 characters
      var   addr = document.form1.address1.value;
      if (addr.length > 30)
      {
        document.form1.address1.focus();
        alert("Address cannot be greater than 30 characters.");
        return false;
      }

    // cellphone number check
    // cellphone removed - now done in customer_change_phone.jsp (And now its back for SMS fun)
    var cell1 = "";
    var cell2 = "";
    var cell3 = "";
    var cell1b = "";
    var cell2b = "";
    var cell3b = "";
    if(document.form1.cellphonearea) {
      cell1 = trim(document.form1.cellphonearea.value);
    }
    if(document.form1.cellphonefirst) {
      cell2 = trim(document.form1.cellphonefirst.value);
    }
    if(document.form1.cellphonelast) {
      cell3 = trim(document.form1.cellphonelast.value);
    }
    if(document.form1.cellphonearea2) {
      cell1b = trim(document.form1.cellphonearea2.value);
    }
    if(document.form1.cellphonefirst2) {
      cell2b = trim(document.form1.cellphonefirst2.value);
    }
    if(document.form1.cellphonelast2) {
      cell3b = trim(document.form1.cellphonelast2.value);
    }
    var wholecell = cell1 + cell2 + cell3;
    var wholecell2 = cell1b + cell2b + cell3b;
    if (wholecell != wholecell2)
    {
        alert ("Your cellphone numbers do not match.");
        return false;
    }

    return true;
}

// Validate location information from a form
function validateLocationInput() {
    if (IsAllSpace(document.form1.location_name.value)) {
        document.form1.location_name.focus();
        alert("Please enter Location.");
        return false;
    }

    if (IsAllSpace(document.form1.address1.value)) {
        document.form1.address1.focus();
        alert("Please enter your address.");
        return false;
    }

    if (!document.form1.apt_code[3].checked && IsAllSpace(document.form1.apt_code_ext.value)) {
        document.form1.apt_code_ext.focus();
        alert("Please enter Apt./Suite/Floor.");
        return false;
    }

    // Make sure an apt code has been selected
    var aptCodeIdnex = -1;
    for (i = 0; i < document.form1.apt_code.length; i++) {
        if (document.form1.apt_code[i].checked) {
            aptCodeIdnex = i;
        }
    }

    if (!(document.form1.apt_code_ext.value == "") && aptCodeIdnex < 0) {
        document.form1.apt_code.focus();
        alert("Please check Apt, Suite or Floor.");
        return false;
    }

    if (IsAllSpace(document.form1.city.value))
    {
        document.form1.city.focus();
        alert("Please enter your City.");
        return false;
    }

    if (IsAllSpace(document.form1.state.value)) {
        document.form1.state.focus();
        alert("Please enter your State.");
        return false;
    }

    if (!IsNumber(document.form1.postal_code.value) || document.form1.postal_code.value.length != 5) {
        document.form1.postal_code.focus();
        alert("Please enter valid Zip Code.");
        return false;
    }

    // Phone number check
    if (!IsNumber(document.form1.phonearea.value) || document.form1.phonearea.value.length != 3) {
        document.form1.phonearea.focus();
        alert("Please enter valid Phone Number.");
        return false;
    }

    if (!IsNumber(document.form1.phonefirst.value) || document.form1.phonefirst.value.length != 3) {
        document.form1.phonefirst.focus();
        alert("Please enter valid Phone Number.");
        return false;
    }

    if (!IsNumber(document.form1.phonelast.value) || document.form1.phonelast.value.length != 4) {
        document.form1.phonelast.focus();
        alert("Please enter valid Phone Number.");
        return false;
    }


    if (IsAllZero(document.form1.phonearea.value) &&
        IsAllZero(document.form1.phonefirst.value) &&
        IsAllZero(document.form1.phonelast.value)) {
        document.form1.phonearea.focus();
        alert("Phone Number cannot be zero.");
        return false;
    }


    if (IsAllSpace(document.form1.email.value))
    {
        document.form1.email.focus();
        alert("Please enter your Email address.");
        return false;
    }
    else if ( isSpaceFoundInEmail(document.form1.email.value) )
    {
         document.form1.email.focus();
         alert("Email address with a space is not valid.");
         return false;

    }
    else if (!isEmailValid(document.form1.email.value))
    {
        document.form1.email.focus();
        alert("Please enter a valid Email Address");
        return false;
    }
	// unique name check
	if(locations.length > 0) {
	    // count locations with same name
	    var sameCount = 0;
	    var selObj = document.getElementById('location_id');
	    for(var ndx=0; ndx < locations.length; ndx++) {
	    	if( equalsIC(locations[ndx].pLabel 
	    	        , document.form1.location_name.value )) {
	    	        sameCount++;
	    	}
	    }
	    if(equalsIC(selObj[selObj.selectedIndex].text 
	    	        , document.form1.location_name.value )) {
	      sameCount--;
	    }
	    // alert('comparing ' + document.form1.location_name.value);
	    // alert('same count is ' + sameCount + ' ' + selObj[selObj.selectedIndex].text);
	    if(sameCount > 0) {
	    		    alert('Location name "' + document.form1.location_name.value
	    		    	+ '" is already used.  Please select another.');
	    		    document.form1.location_name.focus();
	    	 		return false;
	    }
	}


    return true;
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function equalsIC(item1,item2) {
  var same = false;
  if( trim(item1).toLowerCase() == trim(item2).toLowerCase() ) {
    same = true;
  }
  return same;
}

function validateCreditCardPage() {

    if (IsAllSpace(document.form2.cardNumber.value)) {
        document.form2.cardNumber.focus();
        alert("Please enter a credit card number.");
        return false;
    }

    if(document.form2.expirationMonth.value == -1)
    {
        document.form2.expirationMonth.focus();
        alert("Please select an expiration month.");
        return false;
    }

    if(document.form2.expirationYear.value == -1)
    {
        document.form2.expirationYear.focus();
        alert("Please select an expiration year.");
        return false;
    }

    if (IsAllSpace(document.form2.cardName.value)) {
        document.form2.cardName.focus();
        alert("Please enter the name on your credit card.");
        return false;
    }

    if (IsAllSpace(document.form2.cardId.value)) {
        document.form2.cardName.focus();
        alert("Please enter the id number from your credit card.");
        return false;
    }

    if(document.form2.billSelection[1].checked)
    {
      if (IsAllSpace(document.form2.name.value)) {
          document.form2.name.focus();
          alert("Please enter the name for your billing address.");
          return false;
      }

      if (IsAllSpace(document.form2.addr1.value)) {
          document.form2.addr1.focus();
          alert("Please enter the address line 1 for your billing address.");
          return false;
      }

      if (IsAllSpace(document.form2.city.value)) {
          document.form2.city.focus();
          alert("Please enter the city for your billing address.");
          return false;
      }

      if (document.form2.state.value == -1) {
          document.form2.state.focus();
          alert("Please select the state for your billing address.");
          return false;
      }

      if (IsAllSpace(document.form2.zip.value)) {
          document.form2.zip.focus();
          alert("Please enter the zip code for your billing address.");
          return false;
      }

      if (IsAllSpace(document.form2.phonearea.value)) {
          document.form2.phonearea.focus();
          alert("Please enter the phone number for your billing address.");
          return false;
      }

      if (IsAllSpace(document.form2.phonefirst.value)) {
          document.form2.phonefirst.focus();
          alert("Please enter the phone number for your billing address.");
          return false;
      }

      if (IsAllSpace(document.form2.phonelast.value)) {
          document.form2.phonelast.focus();
          alert("Please enter the phone number for your billing address.");
          return false;
      }
    }

    return true;
}

function validatePapaCardPage()
{

    if (IsAllSpace(document.form2.gcNumber.value)) {
        document.form2.gcNumber.focus();
        alert("Please enter a papa card number.");
        return false;
    }

    if (IsAllSpace(document.form2.gcPinNumber.value)) {
        document.form2.gcPinNumber.focus();
        alert("Please enter a papa card pin number.");
        return false;
    }

    return true;
}

function checkBlankSpaces() {
    var Ret = 0
    var validFlag = true
    var deliveryoption= -1

    if (document.form1.email.value == "") {
        validFlag = false;
        Ret = Ret + 1;
        alert("Please enter your email.");
        document.form1.email.focus();
        return validFlag;
    }

    if (document.form1.lastname.value == "") {
        validFlag = false;
        Ret = Ret + 1;
        alert("Please enter your Last Name.");
        document.form1.lastname.focus();
        return validFlag;
    }

    var validEmail = isEmailValid(document.form1.email.value);

    if (validEmail == false) {
        validFlag = false;
        Ret = Ret + 1;
        alert("Your email address is not properly formed.  Please change it and try again.");
        document.form1.email.focus();
        return validFlag;
    }

    return validFlag;
}

function checkBlankSpacesCustomer() {
    var Ret = 0
    var validFlag = true
    var deliveryoption= -1


    if ( trim(document.form1.email.value) == "") {
        validFlag = false;
        Ret = Ret + 1;
        alert("Please enter your email.");
        document.form1.email.focus();
        return validFlag;
    }

    if ( trim(document.form1.password.value) == "") {
        validFlag = false;
        Ret = Ret + 1;
        alert("Please enter your password.");
        document.form1.email.focus();
        return validFlag;
    }

    var validEmail = isEmailValid(document.form1.email.value);
    if (validEmail == false) {
        validFlag = false;
        Ret = Ret + 1;
        alert("Your email address is not properly formed.  Please change it and try again.");
        document.form1.email.focus();
        return validFlag;
    }

    return validFlag;
}

// if year is evenly divisible by 4 it is a leap year
function isLeapYear(year,day)
{
    var tmp = year / 4;
    var i;
    var counter = 0;
    tmp = tmp+'';          // converts number to a string

    for (i = 0; i < tmp.length; i++)
    {
        var c = tmp.charAt(i);
        if (c == ".")
        {
          counter++;
        }
    }
    if (counter > 0)
    {
       return false;
    }
    return true;
}
 //
// validate the birthdate input  ( must be > 13 years old )
//
function validateAgeInput()
{
    //document.form1.birthMonth.value = "";   // this blanks out this field - handy dandy
    var mymonth =   trim(document.form1.birthmonth.value);
    var myday =   trim(document.form1.birthday.value);
    var myyear =   trim(document.form1.birthyear.value);


    if ( mymonth == -1 )
    {
        alert("Please enter the month you were born.");
        document.form1.birthmonth.focus();
        return false;
    }
    else if  ( myday == "Day" || myday == "-1")
    {
        alert("Please enter the day you were born.");
        document.form1.birthday.focus();
        return false;
    }
    else if  ( myyear == -1 )
    {
        alert("Please enter the year you were born.");
        document.form1.birthyear.focus();
        return false;
    }

    if ( mymonth == "02" )
    {
        if ( myday == "31" || myday == "30" )
        {
            alert("Invalid day selected for this month");
            document.form1.birthday.focus();
            return false;
        }

        if ( myday == "29" )
        {
            if (! isLeapYear(myyear, myday) )
            {
                 alert( " February 29 is not valid for this year.  Please select another date. ");
                 return false
            }
        }
    }


    if ( mymonth == "04" || mymonth == "06" || mymonth == "09" || mymonth == "11")
      {
          if ( myday == "31" )
          {
              alert("Invalid day selected for this month");
              document.form1.birthday.focus();
              return false;
          }
      }

    return true;
}

function addFebuaryDays()
{
      document.form1.birthday.options[0] = new Option("Day", "-1");
      document.form1.birthday.options[1] = new Option("01", "01");
      document.form1.birthday.options[2] = new Option("02", "02");
      document.form1.birthday.options[3] = new Option("03", "03");
      document.form1.birthday.options[4] = new Option("04", "04");
      document.form1.birthday.options[5] = new Option("05", "05");
      document.form1.birthday.options[6] = new Option("06", "06");
      document.form1.birthday.options[7] = new Option("07", "07");
      document.form1.birthday.options[8] = new Option("08", "08");
      document.form1.birthday.options[9] = new Option("09", "09");
      document.form1.birthday.options[10] = new Option("10", "10");
      document.form1.birthday.options[11] = new Option("11", "11");
      document.form1.birthday.options[12] = new Option("12", "12");
      document.form1.birthday.options[13] = new Option("13", "13");
      document.form1.birthday.options[14] = new Option("14", "14");
      document.form1.birthday.options[15] = new Option("15", "15");
      document.form1.birthday.options[16] = new Option("16", "16");
      document.form1.birthday.options[17] = new Option("17", "17");
      document.form1.birthday.options[18] = new Option("18", "18");
      document.form1.birthday.options[19] = new Option("19", "19");
      document.form1.birthday.options[20] = new Option("20", "20");
      document.form1.birthday.options[21] = new Option("21", "21");
      document.form1.birthday.options[22] = new Option("22", "22");
      document.form1.birthday.options[23] = new Option("23", "23");
      document.form1.birthday.options[24] = new Option("24", "24");
      document.form1.birthday.options[25] = new Option("25", "25");
      document.form1.birthday.options[26] = new Option("26", "26");
      document.form1.birthday.options[27] = new Option("27", "27");
      document.form1.birthday.options[28] = new Option("28", "28");
      document.form1.birthday.options[29] = new Option("29", "29");

 }

  function doMonthChanges()
  {


    //get the month user selected
    var mymonth =   document.form1.birthmonth.value;


     // first set the days to 0
     document.form1.birthday.options.length = 0;

    // day option box should be dynamic depending on the month
    if ( mymonth == "02")
    {
        addFebuaryDays();
     }
    else if ( mymonth == "04" || mymonth == "06" || mymonth == "09" || mymonth == "11")
    {
        addFebuaryDays();
        document.form1.birthday.options[30] = new Option("30", "30");
    }
    else
    {
        addFebuaryDays();
        document.form1.birthday.options[30] = new Option("30", "30");
        document.form1.birthday.options[31] = new Option("31", "31");
    }


  }

  function checkForAscii(strValue){
      var r1 = new RegExp("[^A-Za-z0-9\\-\\.\\_\\'\\ ]")
      return r1.test(strValue);
    }

function autoTab(input,len) {
  if(input.value.length >= len) {
    input.value = input.value.slice(0, len);
    input.form[(getIndex(input)+1) % input.form.length].focus();
  }

  function getIndex(input) {
    var index = -1, i = 0, found = false;
    while (i < input.form.length && index == -1)
    if (input.form[i] == input)index = i;
    else i++;
    return index;
  }
  return true;
}

// ---------------------------------------------
//validation for dropdown menus on homepage
// ---------------------------------------------

var submitcount=0;

	function checkSubmit() {
		if (submitcount == 0) {
			if (checkBlankSpaces()) {
			  submitcount++;
			  return true;
		  } else {
	  return false;
  }//end if
	  }else{
		return false;
	  }//end if

   }

function checkBlankSpaces() {
	var Ret = 0;
	var validFlag = true;
	var deliveryoption= -1;
	document.form1.javascript.value = 'enabled';
	if (document.form1.email.value == "") {
		  validFlag = false;
		  Ret = Ret + 1;
		  alert("Please enter your email.");
		  document.form1.email.focus();
		  return validFlag;
	}

	if (document.form1.password.value == "") {
		  validFlag = false;
		  Ret = Ret + 1;
		  alert("Please enter your Password.");
		  document.form1.email.focus();
		  return validFlag;
	}

	  var validEmail = isEmailValid(document.form1.email.value)

	  if (validEmail == false) {
		  validFlag = false;
		  Ret = Ret + 1;
		  alert("Your email address is not properly formed.  Please change it and try again.");
		  document.form1.email.focus();
		  return validFlag;
	  }

	  return validFlag;
  }
  
//========================================================================   

//RestLocator form2 addresss and zip code validator


function validateRestLocator(form2){

    

	if(IsAllSpace(form2.city.value) || IsAllSpace(form2.state.value)) {
	    	if (IsAllSpace(form2.zip.value) || !IsNumber(form2.zip.value)) {
      			alert("Please enter a valid City/State or Zip Code.");
          		return false;
          	}
		else if (form2.zip.value.length != 5) {
          		form2.zip.focus();
          		alert("Please enter a 5 digit Zip Code.");
          		return false;
      	}
      }

   

     return true;
}

//========================================================================   

//DirectionsOrigin form2 addresss and zip code validator


function validateDirectionsOrigin(form2){

    

	if(IsAllSpace(form2.origcity.value) || IsAllSpace(form2.origstate.value)) {
	    	if (IsAllSpace(form2.origzip.value) || !IsNumber(form2.origzip.value)) {
      			alert("Please enter a valid City/State or Zip Code.");
          		return false;
          	}
		else if (form2.origzip.value.length != 5) {
          		form2.origzip.focus();
          		alert("Please enter a 5 digit Zip Code.");
          		return false;
      	}
      }

   

     return true;
}

//========================================================================   

//DirectionsDestination form2 addresss and zip code validator


function validateDirectionsDestination(form2){

    

	if(IsAllSpace(form2.destcity.value) || IsAllSpace(form2.deststate.value)) {
	    	if (IsAllSpace(form2.destzip.value) || !IsNumber(form2.destzip.value)) {
      			alert("Please enter a valid City/State or Zip Code.");
          		return false;
          	}
		else if (form2.destzip.value.length != 5) {
          		form2.destzip.focus();
          		alert("Please enter a 5 digit Zip Code.");
          		return false;
      	}
      }

   

     return true;
}

