﻿// JavaScript File

    function MM_findObj(n, d) { //v4.01
      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);
      if(!x && d.getElementById) x=d.getElementById(n); return x;
    }
    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;
    }

    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 validateList(nameregex,max,current){
      var counter =0;
      var name="";
     
      for(i = 0; i < document.forms[0].elements.length; i++)
      {
        elm = document.forms[0].elements[i]
        if (elm.type == 'checkbox' && elm.checked)
        {
            name=elm.name
            if (name.indexOf(nameregex)!= -1)
            {
                 if (max==1){
                    clearCheckBoxes(nameregex)
                    current.checked = true;
                    }
                 else{
                       counter+=1;
                       }
            }
         }
      }
      if (counter > max){
        current.checked = false;
        alert("Cannot select more than " + max + " item(s)");
      }
    }
    
    function clearCheckBoxes(nameregex){
     for(i = 0; i < document.forms[0].elements.length; i++)
      {
        elm = document.forms[0].elements[i]
        if (elm.type == 'checkbox' && elm.checked)
        {
            name=elm.name
            if (name.indexOf(nameregex)!= -1)
            {
        
                elm.checked=false;
            }
         }
      }
    }
    
    function SetUniqueRadioButton(nameregex, current)
{
      re = new RegExp(nameregex);
      for(i = 0; i < document.forms[0].elements.length; i++)
      {
            elm = document.forms[0].elements[i]
            if (elm.type == 'radio')
            {
                  if (re.test(elm.name))
                  {
                          elm.checked = false;
                  }
             }
      }
      current.checked = true;
      
}

var isNN = (navigator.appName.indexOf("Netscape")!=-1);

function autoTab(input,len, e) {
  var keyCode = (isNN) ? e.which : e.keyCode; 
  var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
  if(input.value.length >= len && !containsElement(filter,keyCode)) {
    input.value = input.value.slice(0, len);
    input.form[(getIndex(input)+1) % input.form.length].focus();
  }

  function containsElement(arr, ele) {
    var found = false, index = 0;
    while(!found && index < arr.length)
    if(arr[index] == ele)
    found = true;
    else
    index++;
    return found;
  }

  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;
}

 function cancelPostBack()
    {
        // add any condition here that checks for a value
        if (1 == 2)       
            return true;
        else
            return false;
    }

function DisplayCreditForm()
{
    document.getElementById('payment').style.display='block';


}

function HideCreditForm()
{
    document.getElementById('payment').style.display='none';


}

function ValidateChkOutForm(){
  
   var elm = document.getElementById('ctl00_ContentPlaceHolder1_RdoPayment_1')
   
   if (elm.checked == true){
        var ct =  document.getElementById('ctl00_ContentPlaceHolder1_DDcardType')
        if (ct.selectedIndex == '0'){
            alert("Please select a card type");
           return false;
        }
        var cn = document.getElementById('ctl00_ContentPlaceHolder1_txtCardNumber')
        var re16digit=/^\d{16}$/
        if (cn.value.search(re16digit) == -1){
            alert("Please enter a valid Card Number");
            return false;
        }
        var cm = document.getElementById('ctl00_ContentPlaceHolder1_DDCardMonth')
        var cy = document.getElementById('ctl00_ContentPlaceHolder1_DDCardYear')
        if (cm.selectedIndex == '0' || cy.selectedIndex == '0'){
            alert("Please select the expiration month and year of your card");
           return false;
        }
        var cm = document.getElementById('ctl00_ContentPlaceHolder1_DDCardMonth')
        var cy = document.getElementById('ctl00_ContentPlaceHolder1_DDCardYear')
        if (cm.selectedIndex == '0' || cy.selectedIndex == '0'){
            alert("Please select the expiration month and year of your card");
           return false;
        }
        var cvc = document.getElementById('ctl00_ContentPlaceHolder1_txtCVC')
        if (ct.options[ct.selectedIndex].value == "American Express"){
            len = 4;
         }
         else{
            len= 3;
         }
        if (cvc.value.length < len ){
            alert("Please enter a valid CVC");
           return false;
        }
         var ba = document.getElementById('ctl00_ContentPlaceHolder1_txtAddress1')
         if (ba.value.length < 1){
            alert("Please enter a billing address.");
            return false;
         }
       
        var zip = document.getElementById('ctl00_ContentPlaceHolder1_txtZip')
        var re5digit=/^\d{5}$/
        if (zip.value.search(re5digit) == -1){
              alert("Please enter a valid billing zip code");
           return false;
        }
   }
   
   
   return true;

}

function checkGratuityInput(e){

    if (((event.keyCode < 48) || (event.keyCode > 57)) && (event.keyCode != 46))
    event.returnValue = false;
}

function addGratuity(val, total){
   var sVal = val;
  var sTotal;

    if (sVal== '' || sVal=="."){
        sVal='0';
        }
      sTotal= (parseFloat(total) + parseFloat(sVal));
  
   if (sVal != '0'){ 
        document.getElementById('ctl00_ContentPlaceHolder1_txtGratuity').value =sVal
        }
      document.getElementById('ctl00_ContentPlaceHolder1_txtTotal').value = "$" + sTotal.toFixed(2);
  
}
function setForDeletion(val){
    document.getElementById('ctl00_ContentPlaceHolder1_DeletionRequest').value = val;
    
}
function addNewAddress(val){
    //alert(val);
    document.getElementById('ctl00_ContentPlaceHolder1_AddNewAddress').value = val;
    
}

 function overlay() {
	//el = document.getElementById("overlay");
	//el.style.visibility = (el.style.visibility == "visible") ? "hidden" : "visible";
	Modalbox.hide();
}

function makeRequest() {
	
	var url;
	var email = document.getElementById('email').value
	//alert(email);
	url = "sendpassword.aspx?email=" + email;
        var httpRequest;

        if (window.XMLHttpRequest) { // Mozilla, Safari, ...
            httpRequest = new XMLHttpRequest();
            if (httpRequest.overrideMimeType) {
                httpRequest.overrideMimeType('text/xml');
                // See note below about this line
            }
        } 
        else if (window.ActiveXObject) { // IE
            try {
                httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
            } 
            catch (e) {
                try {
                    httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                } 
                catch (e) {}
            }
        }

        if (!httpRequest) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }
        httpRequest.onreadystatechange = function() { alertContents(httpRequest); };
        httpRequest.open('GET', url, true);
        httpRequest.send('');

    }

    function alertContents(httpRequest) {

        if (httpRequest.readyState == 4) {
            if (httpRequest.status == 200) {
                var msg = httpRequest.responseText
               alert(msg);
               if(msg.indexOf("has been sent") != -1){
                    //overlay();
                    $.modal.close();
                   }
                else{
                    return false;
                    }
			   
            } else {
                alert('There was a problem with the request.');
                overlay();
            }
        }

    }
