// JavaScript Document
 function f_formValidate(f) {
  // validate entries
  with(f) {  
// if (Empty(fname,"Please enter your first name","text",1,30)) return false; 
// if (Empty(lname,"Please enter your last name","text",1,30)) return false; 
 if (Empty(vuser,"Please enter your Mileage Plus Number","numbers",11,11)) return false; 
 if(!f_checkDigit(vuser)) return false;

 if (Empty(email,"Email Address is required","text, numbers, and other",6,256)) return false; 
 if (!ValidEmail(email)) return false;

// if (Empty(x_phone1,"Please enter your phone number in case we need to reach you","numbers",3,3)) return false; 
// if (Empty(x_phone2,"Please enter your phone number in case we need to reach you","numbers",3,3)) return false; 
// if (Empty(x_phone3,"Please enter your phone number in case we need to reach you","numbers",4,4)) return false; 

//of age
  if (x_minAge.checked == false) {
   alert("You must certify that you are 18 years of age or older");
   return false;
  }

//agree to terms
  if (x_confirm.checked == false) {
   if (confirm ("You must check the box to indicate that you have read and agree with the \nTerms and Conditions. Do you agree?")) {
	 x_confirm.checked = true;
   } else {
     alert ("Darn. No skiing for you. Maybe you will change your mind later.")
     return false;
   }
  }

  // generate canswer ID
//  var t_canswer = "phone|" 
//    + escape(f.x_phone1.value) + "-"  
//	+ escape(f.x_phone2.value) + "-"
//	+ escape(f.x_phone3.value) +
var t_canswer =	 "agreetoreceive|"
	+ x_vail_optin.checked
	+ "|minage|" + x_minAge.checked
	canswer.value = t_canswer;
  }//end of with

  // disable submit button
  if(parseInt(navigator.appVersion) < 5 && navigator.appName.indexOf("Netscape") != -1)f_submitOnce(f); 

  
  // assemble dynamic destination 
  // build destination base
  var urlString = f.action + "?";

  for (i=0; i< f.length; i++) {
    var g = f.elements[i];
      if (g.name.charAt(0) != "x" && g.value != null && g.value != "") { 
        if(g.name == "fname" 
           || g.name == "lname"
           || g.name == "targetURL"
           || g.name == "failureURL"
           || g.name == "email") 
        {
          urlString += g.name+ "=" + escape(g.value) + "&";
        } else if (g.type == "submit") { // do nothing here
        } else { // add to the string all the hidden values
          urlString += g.name +  "=" + g.value + "&";
        }
     }
  }
  // clean up URL string (remove last "&"
  urlString = urlString.substring(0,urlString.length-1);
  alert("here is url string: "+urlString);
  // submit window to destination
  top.location=urlString;
  return false;
}

function SelectMe(fld) {
  fld.focus();
  if (fld.type != "select-one") fld.select();
}

function Empty(fld,alertMsg,aType,minLen,maxLen) {
  // determine the Valid Characters
  var strValidChars = " ";
  if (aType.indexOf("text") != -1) {
    strValidChars += "abcdefghijklmnopqrstuvwxyz";
  } 
  if (aType.indexOf("numbers") != -1) {
    strValidChars += "1234567890";
  }
  if (aType.indexOf("other") != -1) {
    strValidChars += "@_().-/ ";
  }

  with(fld) {
    // test if we're just off really simply
    if (value.length != 0 && value != " ") { 
      // here, there's some characters and it's not a single space
      //let's test further...assuming all things start off good:
      var valid = true;
      // let's test for length first - there's special cases
      // ...for exact length (i.e. the ARC Number),
      if (minLen == maxLen && value.length != minLen) {
        valid = false;
        alertMsg +=". It should be a " + minLen + " digit number";
      }
      // ...for too short,
      if (value.length < minLen && valid == true) {
        valid = false;
        alertMsg +=". It should be at least " + minLen + " characters long";
      }
      // ...and too long.
      if (value.length > maxLen && valid == true) {
        valid = false;
        alertMsg +=". It should be at no more than " + maxLen + " characters long";
      }      
      // Now, set up a flag for looping
      var spaces = 0;      
      // now loop thru the string given and see what's there
      for(i=0; i < value.length && valid == true; i++) {
        aChar = value.charAt(i).toLowerCase();
        if (strValidChars.indexOf(aChar) == -1) {
          valid=false;
          alertMsg += ", and should contain only " + aType;
        }
        if (aChar == " ") {
          spaces++;
          if(spaces == value.length) {
              valid=false;
              alertMsg += ", and cannot consist of all spaces"
          } else {
           // Special case, text only fields can not have more than 2 spaces
            if (type == "text") {
              if(spaces > 5) {
                valid=false;
                alertMsg += ", and cannot contain extra spaces"
              }
            }
          }
        }  
      }      
      // we're done looping - are things good or not?
      if(!valid) {
        alert(alertMsg + ".");
        setTimeout("SelectMe(document.forms['" + 
  		    fld.form.name + "'].elements['" + fld.name + "'])", 0);
  	    return true;
      } else { 
        return false;
      }
  	} else { // we made a simple stupid error - give me basic messaging
      alert(alertMsg + ".");
      setTimeout("SelectMe(document.forms['" + 
  		  fld.form.name + "'].elements['" + fld.name + "'])", 0);
  	  return true;
    } 
  } 
}

function ValidEmail(address) {
  with (address)  {
    apos=value.indexOf("@"); 
    dotpos=value.lastIndexOf(".");
    lastpos=value.length-1;

    if (apos<1 || dotpos-apos<2 || lastpos-dotpos>3 || lastpos-dotpos<2) {
      alert('Email address requires the @ symbol, a . (dot), and at least two letters after the dot.');
      setTimeout("SelectMe(document.forms['" + 
  		address.form.name + "'].elements['" + address.name + "'])", 0);
      return false;

    } else {
      return true;
    }
  } 
}

function ValidTerms(fld,alertMsg) {
  with(fld) {
    if (checked == false) {
      alert(alertMsg);
      setTimeout("SelectMe(document.forms['" + 
  		fld.form.name + "'].elements['" + fld.name + "'])", 0);    
      return false;
    } else return true;
  } 
}

function f_submitOnce(aForm){
  // Controls Button Change //
  if (document.all||document.getElementById){
    for (i=0; i < aForm.length; i++){
      var t_obj=aForm.elements[i];
      if(t_obj.type.toLowerCase()=="submit"){
        t_obj.value="Sending...";
        t_obj.disabled=true;  
      }
    }
  }
}
function f_checkDigit(fld) {
  var mpNum = fld.value;
    if (mpNum.search(/^\d{11}$/) == -1) return false;
    var factor = [5,4,3,2,7,6,5,4,3,2];
    var check = 0;
    for (var digit = 0; digit < 10; digit++)
        check += mpNum.charAt(digit) * factor[digit];
    check = (((11 - (check % 11)) % 11) % 10);
    // return (check == mpNum.charAt(10));
    
    if(check == mpNum.charAt(10)) {
      return true;
    } else {
      alert("The Mileage Plus number you entered is invalid, please try again.");
      setTimeout("SelectMe(document.forms['" + 
  		  fld.form.name + "'].elements['" + fld.name + "'])", 0);
      return false;
    }
}


matchHeight=function(){ 
 var divs,contDivs,maxHeight,divHeight,d,requirementHeight=0; 
 // get all <div> elements in the document 
 divs=document.getElementsByTagName('div'); 
 contDivs=[]; 

 // initialize maximum height value 
 maxHeight=0; 
 // iterate over all <div> elements in the document 

 for(var i=0;i<divs.length;i++){ 
  // make collection with <div> elements with class attribute 'container' 
  if(/\bcontainer\b/.test(divs[i].className)){ 
   d=divs[i]; 
   contDivs[contDivs.length]=d; 
   // determine height for <div> element 
   divHeight= (d.offsetHeight) ? d.offsetHeight : d.style.pixelHeight

   // calculate maximum height 
   maxHeight=Math.max(maxHeight,divHeight)
  } 
 } 

 // assign maximum height value to all of container <div> elements 
 for(var i=0;i<contDivs.length;i++){ 
  contDivs[i].style.height=maxHeight;  
 } 
} 

