///Check Check Box
// If not, it displays the error message,
// focuses the control  and returns false
 
function checkBox(obj, msg, req) {
  if (req == "yes") {
    if (obj.checked==false) {
      alert(msg);
      obj.focus();
      return false;
    }
  }
  return true;
}
  

// Check For Numbers
// If not, it displays the error message,
// focuses the control  and returns false
 
function checkNumbers(obj,msg,req) {
  if (!checkNumChar(obj,req)){
    msg += '\nBe sure you are only using numbers';
    alert(msg);
    obj.focus();
    return false;
  };
  return true;
};
  
function checkNumChar(obj,req) {
  var valid = 1
  var hkGoodNumbers = "0123456789,."
  var i = 0

  if (obj.value=="" && req == "yes") {
    valid = 0
  }

  for (i =0; i <= obj.value.length -1; i++) {
    if (hkGoodNumbers.indexOf(obj.value.charAt(i)) == -1) {
      valid = 0
    }
  }
  return valid
}

  
// this function checks to see if the valid form of email address has been entered,
// If not, it displays the error message,
// focuses the control  and returns false
// 

function checkEmail(obj,msg,req) {
  if (req == "yes" || obj.value != ""){
    if (!checkEmailChar(obj)){
      alert(msg);
      obj.focus();
      return false;
    }
  }
  return true;
}

function checkEmailChar(obj) {
  invalidChars=" /:,;";
  for (i=0; i<invalidChars.length;i++){
    badChar = invalidChars.charAt(i);
      if (obj.value.indexOf(badChar,0) !=-1) {
        return false;
      };
  };
  atPos = obj.value.indexOf("@", 1);
  if (atPos == -1){
    return false;
  };
  if (obj.value.indexOf("@",atPos+1)!= -1){
    return false;
  };
  dotPos = obj.value.indexOf(".", atPos);
  if (dotPos <=0){
    return false;
  };
  if (dotPos+3 > obj.value.length){
    return false;
  };

  return true;
};

// this function checks to see if a radio-button has not been selected
// If not, it displays the error message,
// focuses the control  and returns true

function checkRadio(obj,msg, req) {
  if (req == "yes")
  {
    var itemchecked = false;
    for(var j = 0 ; j < obj.length ; ++j) 
    { 
      if(obj[j].checked) 
      { 
        itemchecked = true;
        break;
      }
    }
    
    if(!itemchecked)
    {   
      alert(msg);
      //obj.focus();
      return false;
    }
  } 	  
  
  return true;

};

// this function checks to see if a pull-down has not been selected
// past the first item OR if the value option selected is '0' or '',
// If not, it displays the error message,
// focuses the control  and returns false

function checkPullDown(obj,msg,req) {
  if (req == "yes") {
    if (obj.selectedIndex==0 || obj.options[obj.selectedIndex].value=='0' || obj.options[obj.selectedIndex].value=='') {
      alert(msg);
      obj.focus();
      return false;
    }
  }
  return true;
};

// this function checks to see whether a text input or textarea
// is blank or is all white space.
// If so, it displays the error message,
// focuses the control  and returns false

function checkText(obj,msg,req) {
  var str = obj.value+'';
  var allWhite = true;

  for(var c=0; c<str.length; c++) {
    if (!isWhite(str.charAt(c))) allWhite=false;
  };
  if (req == "yes"){
    if (allWhite || str=='') {
      obj.value="";
      alert(msg);
      obj.focus();
      return false;
    }
  }
  remove_XS_whitespace(obj);
  return true;
};

// this function checks to see whether a text input or textarea
// is blank or is all white space.
// If it is all white space, it cleans it out and return true

function cleanNonRequiredText(obj) {
  var str = obj.value+'';
  var allWhite = true;
  for(var c=0; c<str.length; c++) {
    if (!isWhite(str.charAt(c))) {
      allWhite=false; 
    };
  }
  if (allWhite || str=='') {
    obj.value="";
    return true;
  }else{
    remove_XS_whitespace(obj);
    return false;   
  };
};

// this function checks to see whether a text input or textarea
// is blank or is all white space.
// If so, it displays the error message,
// focuses the control  and returns true
// It also checks the number of chars and displays the too many
// chars message if that is exceeded
function checkTextArea(obj,msg,max,msgmax,req) {

  if (req == "yes" || obj.vaulue != ""){
    var str = obj.value+'';
    if (str.length>max) {
      alert(msgmax + ' [There are currently '+str.length+' characters]');
      obj.focus();
      return false;
    };
  }
  var allWhite = true;
  for(var c=0; c<str.length; c++) {
    if (!isWhite(str.charAt(c))) allWhite=false;
  }
  if (allWhite || str=='') {
    obj.value="";
    alert(msg);
    obj.focus();
    return false;
  };
  return true;
};

// this function checks to see whether a text input 
// contains " or '.
// If so, it displays the error message,
// focuses the control  and returns false

function checkQuoteMark(obj, msg) {
  if ((obj.value.indexOf('"') >= 0) || (obj.value.indexOf("'") >= 0)) {
    alert(msg);
    obj.focus();
    return false;
  };

  return true;
};

function checkMax(obj,max,msgmax) {
  var str = obj.value+'';
  if (str.length>max) {
    alert(msgmax + ' [There are currently '+str.length+' characters]');
    obj.focus();
    return false;
  };
  return true;
};

// These functions check to see if the text field 
// contains a valid date
// If not, they display an error message,
// focus the control and return false.
 
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
  var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
  var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
       var c = s.charAt(i);
       if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

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 DaysArray(n) {
  for (var i = 1; i <= n; i++) {
    this[i] = 31
    if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
    if (i==2) {this[i] = 29}
    } 
    return this
}

function checkDate(obj,req){

  if (req == "yes" || obj.value != "") {
    sDateString = obj.value
    var daysInMonth = DaysArray(12)
    var pos1=sDateString.indexOf(dtCh)
    var pos2=sDateString.indexOf(dtCh,pos1+1)
    /// Use This for the States
    var strMonth=sDateString.substring(0,pos1)
    var strDay=sDateString.substring(pos1+1,pos2)
    /// Use This for Europe
    //var strMonth=sDateString.substring(pos1+1,pos2)
    //var strDay=sDateString.substring(0,pos1)

    var strYear=sDateString.substring(pos2+1)
    strYr=strYear
    if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
    if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)

    for (var i = 1; i <= 3; i++) {
      if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
    }

    month=parseInt(strMonth)
    day=parseInt(strDay)
    year=parseInt(strYr)

    if (pos1==-1 || pos2==-1){
      alert("Please enter a valid date format.  The date format should be : mm/dd/yyyy");
      obj.focus();
      return false
    }

    if (strMonth.length<1 || month<1 || month>12){
      alert("Please enter a valid month");
      obj.focus();
      return false
    }

    if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
      alert("Please enter a valid day");
      obj.focus();
      return false
    }

    if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
      alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear);
      obj.focus();
      return false
    }

    if (sDateString.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(sDateString, dtCh))==false){
      alert("Please enter a valid date");
      obj.focus();
      return false
    }
  }
  return true
}

function isWhite(ch) {
  if (ch==' ' || ch=='  ' || ch=="\n" || ch=="\r") return true;
  return false;
}


function remove_XS_whitespace(obj) {
  var tmpStr = "";
  var obj_length = obj.value.length;
  var obj_length_minus_1 = obj.value.length - 1;
  for (i = 0; i < obj_length; i++) {
    if (obj.value.charAt(i) != ' ') {
      tmpStr += obj.value.charAt(i);
    }
    else {
      if (tmpStr.length > 0) {
        if (obj.value.charAt(i+1) != ' ' && i != obj_length_minus_1) {
          tmpStr += obj.value.charAt(i);
        }
      }
    }
  }
  obj.value = tmpStr;
}

function checkPhone (s) {
  var stripped = s.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters

  if (isNaN(parseInt(stripped))) {
     alert("The phone number contains illegal characters.");
     return false;
  }

  if (!(stripped.length == 10)) {
    alert("The phone number is the wrong length. Make sure you included an area code.");
    return false;
  }
  return true;
}



// Check RSVP Signup Form
function checkRSVPForm(theForm) {

  if (!checkRadio(theForm.Attend,"Please let us know whether you will attend.", "yes"))  {
    return false;
  }  
  
  if (!checkText(theForm.txtFirstName,"Please tell us your first name.", "yes"))  {
    return false;
  }
  if (!checkText(theForm.txtLastName,"Please tell us your last name.", "yes"))  {
    return false;
  }
  if (!checkText(theForm.txtGoesByName,"Please enter a value for Goes By Name.", "yes"))  {
    return false;
  }
  if (!checkText(theForm.txtJobTitle,"Please enter your job title.", "yes"))  {
    return false;
  }
  if (!checkText(theForm.txtCompany,"Please tell us your company name.", "yes"))  {
    return false;
  }
  if (!checkText(theForm.txtAddress1,"Please tell us your street address.", "yes"))  {
    return false;
  }
  if (!checkText(theForm.txtCity,"Please tell us your city.", "yes"))  {
    return false;
  }
  
  if (theForm.selCountry.options[theForm.selCountry.selectedIndex].value == 'USA') {
  		if (!checkPullDown(theForm.selState,"Please tell us your state.", "yes"))  {    			
   			return false;	
  		}
  }
  
  if (!checkPullDown(theForm.selCountry,"Please tell us your country.", "yes"))  {
    return false;
  }   
  
  if (!checkText(theForm.txtZip,"Please tell us your postal code.", "yes"))  {
    return false;
  }

  if (!checkText(theForm.txtPhone,"Please tell us your phone number.", "yes"))  {
    return false;
  }
  
  if(window.location.search.substring(1) == "section=media_lab_tour")
  {
	  if (!checkText(theForm.txtMobile,"Please tell us your mobile phone number.", "yes"))  {
	    return false;
	  }
  }
  
  if (!checkEmail(theForm.txtEmail,"Please enter a valid email address.", "yes"))  {
    return false;
  }
  
  if(window.location.search.substring(1) == "section=designing_for_growth")
  {
  //if (theForm.selCountry.options[theForm.AttendSpouse].value == 'yes') {

	  
	  if (theForm.AttendSpouse[0].checked) {
  		if (!checkText(theForm.txtSpouseDietaryRestrictions,"Please list your spouse's dietary restrictions.", "yes"))  {
		  return false;
		}
	  }
	  
	  if (!checkText(theForm.txtDietaryRestrictions,"Please list your dietary restrictions.", "yes"))  {
	    return false;
	  } 
  
  
	  
	  if (theForm.AttendChildren[0].checked) {
  		if (!checkText(theForm.txtChildFirstName1,"Please list your child(ren)'s first name(s).", "yes"))  {
		  return false;
		}
	  }
	  
	  if (!checkText(theForm.txtChildAge1,"Please list your child(ren)'s age(s).", "yes"))  {
	    return false;
	  }

  }


  
  if (!checkText(theForm.txtInvitedBy,"Please tell us who invited you to the DiamondExchange.", "yes"))  {
    return false;
  }
  
  if(window.location.search.substring(1) == "section=media_lab_tour")
  {
  	if(!checkRadio(theForm.AttendApril2008Dinner,"Please let us know if you can attend the dinner.", "yes"))
  	{
	    return false;
	}
  }

  return true;
}


// Check Activity Signup Form
function checkActivityForm(theForm) {
  if (!checkText(theForm.txtName,"Please tell us your name.", "yes"))  {
    return false;
  }
  if (!checkEmail(theForm.txtEmail,"Please enter a valid email address.", "yes"))  {
    return false;
  }
  if (!checkText(theForm.txtCompany,"Please tell us your company.", "yes"))  {
    return false;
  }
  if (!checkText(theForm.txtShirt,"Please tell us your shirt size.", "yes"))  {
    return false;
  }  
  if (checkText(theForm.Activity)) {  
  return true;
  }
  
  if (!checkText(theForm.HanP,"Please tell us your handicap.", "yes"))  {
    return false;
   
	  if (theForm.SOAttendYes.checked == true) {
  		if (!checkText(theForm.txtFamilyName,"Please tell us your guest's name.", "yes"))  {  		
    	return false;
  		}  		
  		if (!checkText(theForm.txtFamilyShirt,"Please tell us your guest's shirt size.", "yes"))  {  		
    	return false;
  		}
    	if (!checkText(theForm.HanS,"Please tell us your guest's handicap.", "yes"))  {
		return false;
		}  		
  	  return true;
	  }	
  }
  return true;
}


// Check Flight Information Form
function checkFlightForm(theForm) {

  if (!checkText(theForm.txtName,"Please tell us your name.", "yes"))  {
    return false;
  }
  if (!checkEmail(theForm.txtEmail,"Please enter a valid email address.", "yes"))  {
    return false;
  }
  if (!checkText(theForm.txtCompany,"Please tell us your company.", "yes"))  {
    return false;
  }
  
  if(theForm.chkDriving.checked == false) {
  
	  if (!checkPullDown(theForm.selArrivalDate,"Please choose your arrival date.", "yes"))  {
	    return false;
	  }
	  
	  if (!checkPullDown(theForm.selDepartureDate,"Please choose your departure date.", "yes"))  {
	    return false;
	  }
	  
	  if (!checkText(theForm.txtArrTime,"Please enter your arrival time.", "yes"))  {
	     return false;
	  }
	  
	  if (!checkText(theForm.txtDepTime,"Please enter your departure time.", "yes"))  {
	     return false;
	  }
	  
	  if (!checkText(theForm.txtArrFlight,"Please enter your arrival carrier & flight #.", "yes"))  {
	     return false;
	  }
	  
	  if (!checkText(theForm.txtDepFlight,"Please enter your departure carrier & flight #.", "yes"))  {
	    return false;
	  }

	  
	  if (!checkPullDown(theForm.selArrivalAirport,"Please choose your arrival airport.", "yes"))  {
	    return false;
	  }
	  
	  if (!checkPullDown(theForm.selDepartureAirport,"Please choose your departure airport.", "yes"))  {
	    return false;
	  }
	  
	  if(theForm.selArrivalAirport.value == "Other")
	  {
	  	if (!checkText(theForm.txtArrivalAirport,"Please specify your arrival airport.", "yes"))  {
	     return false;
		}
	  }
	  
	  if(theForm.selDepartureAirport.value == "Other")
	  {
	  	if (!checkText(theForm.txtDepartureAirport,"Please specify your departure airport.", "yes"))  {
	     return false;
		}
	  }
	
	  if(theForm.GuestDiffFlightYes.checked == true)
	  {
		if (!checkText(theForm.txtFamilyName,"Please enter your spouse/significant other's name.", "yes"))  {
	     return false;
		}
		
		//more guest validation here
		  if (!checkPullDown(theForm.selArrivalDateG,"Please choose your guest's arrival date.", "yes"))  {
		    return false;
		  }
		  
		  if (!checkPullDown(theForm.selDepartureDateG,"Please choose your guest's departure date.", "yes"))  {
		    return false;
		  }
		  
		  if (!checkText(theForm.txtArrTimeG,"Please enter your guest's arrival time.", "yes"))  {
		     return false;
		  }
		  
		  if (!checkText(theForm.txtDepTimeG,"Please enter your guest's departure time.", "yes"))  {
		     return false;
		  }
		  
		  if (!checkText(theForm.txtArrFlightG,"Please enter your guest's arrival carrier & flight #.", "yes"))  {
		     return false;
		  }
		  
		  if (!checkText(theForm.txtDepFlightG,"Please enter your guest's departure carrier & flight #.", "yes"))  {
		     return false;
		  }
		  
		  if (!checkPullDown(theForm.selArrivalAirportG,"Please choose your guest's arrival airport.", "yes"))  {
		    return false;
		  }
		  
		  if (!checkPullDown(theForm.selDepartureAirportG,"Please choose your guest's departure airport.", "yes"))  {
		    return false;
		  }
		  
		  if(theForm.selArrivalAirportG.value == "Other")
		  {
		  	if (!checkText(theForm.txtArrivalAirportG,"Please specify your guest's arrival airport.", "yes"))  {
		     return false;
			}
		  }
		  
		  if(theForm.selDepartureAirportG.value == "Other")
		  {
		  	if (!checkText(theForm.txtDepartureAirportG,"Please specify your guest's departure airport.", "yes"))  {
		     return false;
			}
		  }

		//end guest info
		
	  } else {
	  	//do nothing
	  }
  
  } else {

     if (!checkPullDown(theForm.selDrivingArrDate,"Please let us know what date you will be on the property.", "yes"))  {
	    return false;
	 }
	 
	 if (!checkText(theForm.txtDrivingArrTime,"Please enter your arrival time.", "yes"))  {
	     return false;
	 }
	 
	 if (!checkPullDown(theForm.selDrivingDepDate,"Please let us know what date you will be depart the property.", "yes"))  {
	    return false;
	 }
	 
	 if (!checkText(theForm.txtDrivingDepTime,"Please enter your departure time.", "yes"))  {
	     return false;
	 }


  }
  
  

  
  return true;
}

