function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g, '');
}

function gameover(item,action)
{ 
	if(action==1)
	{
	document.getElementById(item).className="gameHighlight";
	}
	else
	{
	document.getElementById(item).className="gamenoHighlight";
	}
}


function givethekey(e){
	if(window.event){
		var entered = event.keyCode;
	}
	else{
		var entered = e.keyCode;
	}
	
	if(entered == 13){
		validateheader();
	}
}
function clear(side){
	if(side=='login'){	
		document.myform.loginemail="";
		document.myform.loginpassword="";
	}else{
		document.myform.newemail="";
		document.myform.newpassword="";
		document.myform.newrepassword="";
	}	
}
function validateheader(){
	var msg = "";
	var missed_answers = "";
	var empty_fields ="";
	var errors = "";
	
	cur_value=document.loginfrm.loginemail.value
	if((cur_value == null) || (cur_value == "") || (isBlank(cur_value))){
		empty_fields += "\n     " + "email";
	}else{
		//making sure that email entry is valid
		if(!validemail(cur_value)){
			errors += "  - Your email address is invalid. \n\n";
		}
	}
	
	cur_value = document.loginfrm.loginpassword.value;
	if((cur_value == null) || (cur_value == "") || (isBlank(cur_value))){
		empty_fields += "\n     " + "Password";
	}
	
	if (!empty_fields && !errors && !missed_answers){
			return true;
	}else{    
  		//otherwise...compose message  
 		msg  = "_____________________________________________________\n\n";
 	 	msg += "We are sorry, but we cannot submit your Info without\n";
 	 	msg += "the following fields being filled out correctly.\n";
 	 	msg += "Please correct these error(s) and re-submit.\n";
 	 	msg += "_____________________________________________________\n\n";
  	 
 		 if(empty_fields){
    		msg += "- The following required field(s) are empty:"
      	 	+ empty_fields + "\n";
    		if(errors){ msg += "\n\n";}	
		 }	
 
		if(errors){
     	  	 msg += " - The following required field(s) are filled out incorrectly: \n\n"
     	  	+ errors + "\n";
			if(missed_answers){ msg += "\n";}
		}
		if(missed_answers){
			msg += " - You have not answered to the following questions: \n"
			+ missed_answers +"\n";
		}	
  		alert(msg);
 		return false; 
	}
}


function validate(){
	if(document.myform.loginemail.value=="" && document.myform.newemail.value==""){
		return validatelogin();
	}else if(!document.myform.loginemail.value=="") {
		return validatelogin();
	}else if(!document.myform.newemail.value==""){
		return validatenew();
	}
}

	var missed_answers = "";
	var empty_fields = "";
	var errors = "";
   function isDate(dateStr) {
//joel modified feb9
  var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
  var matchArray = dateStr.match(datePat); // is format OK?

  if (matchArray == null) {
    //alert("Please enter date as either mm/dd/yyyy or mm-dd-yyyy.");
    return "Please enter date as either mm/dd/yyyy or mm-dd-yyyy.";
  }

  // parse date into variables
  month = matchArray[1];
  day = matchArray[3];
  year = matchArray[5];

  if (month < 1 || month > 12) { // check month range
    //alert("Month must be between 1 and 12.");
    return "Month must be between 1 and 12.";
  }

  if (day < 1 || day > 31) {
   // alert("Day must be between 1 and 31."");
    return "Please enter date as either mm/dd/yyyy or mm-dd-yyyy.";
  }

  if ((month==4 || month==6 || month==9 || month==11) && day==31) {
  //  alert("Month " + month + " doesn't have 31 days!")
    return "Month " + month + " doesn't have 31 days!";
  }

  if (month == 2) { // check for february 29th
    var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
    if (day > 29 || (day==29 && !isleap)) {
     // alert("February " + year + " doesn't have " + day + " days!");
      return "February " + year + " doesn't have " + day + " days!";
    }
  }
return ''  // date is valid
}    


function validatenew(){	
	//declaring method variables
	var msg = "";
	var missed_answers = "";
	var empty_fields ="";
	var errors = "";
	var cur_value = "";
	//checking for first name entry	
	var invalidChars = "!#$%^&*/:,;()[+=|?><]'{}`~";
	
	
	cur_value=trim(document.myform.newemail.value);
	if((cur_value == null) || (cur_value == "") || (isBlank(cur_value))){
		empty_fields += "\n     " + "email";
	}else{
		//making sure that email entry is valid
		if(!validemail(cur_value)){
			errors += "  - Your email address is invalid. \n\n";
		}
	}
	pass1 = document.myform.newpassword.value;
	if((pass1 == null) || (pass1 == "") || (isBlank(pass1))){
		empty_fields += "\n     " + "Password";
	}
	if(!document.myform.chkterms.checked){
		errors += "  - You must agree to the Terms and Conditions to proceed. \n\n";
	}
	cur_value = document.myform.billzip.value;
	if((cur_value == null) || (cur_value == "") || (isBlank(cur_value))){ 
		empty_fields += "\n     " + "Zip/Postal Code";
	}
	if(cur_value.len > 10)
	{
			errors += "  - Zip code must be less then 10 characters \n\n";

	}
	
	var oselect = document.myform.selgender;
	cur_value = oselect.options[oselect.options.selectedIndex].value;
	if((cur_value == null) || (cur_value == "") || (isBlank(cur_value)) || (cur_value == 0)){ 
		empty_fields += "\n     " + "Gender";
	}
	var emptydate = 0;
	var bdate = new Array('bmonth','bday','byear')
	for(i=0;i<bdate.length;i++){
		var celement = bdate[i];
		var shname = document.myform.elements[celement];
		cur_value = shname.options[shname.options.selectedIndex].value;
			if((cur_value == null) || (cur_value == "") || (isBlank(cur_value)) || (cur_value == 0)){ 
				empty_fields += "\n     " + "BirthDate";
				emptydate = 1;
				break;
			}
	}
	if(emptydate == 0){
	var yearSelected=new String(document.myform.byear.options[document.myform.byear.selectedIndex].value);
	    var monthSelected = new String(document.myform.bmonth.options[document.myform.bmonth.selectedIndex].value);
        var daySelected = new String(document.myform.bday.options[document.myform.bday.selectedIndex].value);

	var bdate=monthSelected +  '/' + daySelected + '/' + yearSelected;
	if (isDate(bdate).length > 0)
		{
		errors += ' - ' + isDate(bdate) + '\n\n';
		}
	
		
	}
	
	
	
	
	
	
	
	if (!empty_fields && !errors && !missed_answers){
			return true;
	}else{    
  		//otherwise...compose message  
 		msg  = "_____________________________________________________\n\n";
 	 	msg += "We are sorry, but we cannot submit your Info without\n";
 	 	msg += "the following fields being filled out correctly.\n";
 	 	msg += "Please correct these error(s) and re-submit.\n";
 	 	msg += "_____________________________________________________\n\n";
  	 
 		 if(empty_fields){
    		msg += "- The following required field(s) are empty:"
      	 	+ empty_fields + "\n";
    		if(errors){ msg += "\n\n";}	
		 }	
 
		if(errors){
     	  	 msg += " - The following required field(s) are filled out incorrectly: \n\n"
     	  	+ errors + "\n";
			if(missed_answers){ msg += "\n";}
		}
		if(missed_answers){
			msg += " - You have not answered to the following questions: \n"
			+ missed_answers +"\n";
		}	
  		alert(msg);
 		return false; 
	}
	

}

function validatelogin(){	
	//declaring method variables
	var msg = "";
	var missed_answers = "";
	var empty_fields ="";
	var errors = "";
	var cur_value = "";
	//checking for first name entry	
	var invalidChars = "!#$%^&*/:,;()[+=|?><]'{}`~";
	
	
	cur_value=trim(document.myform.loginemail.value);
	if((cur_value == null) || (cur_value == "") || (isBlank(cur_value))){
		empty_fields += "\n     " + "email";
	}else{
		//making sure that email entry is valid
		if(!validemail(cur_value)){
			errors += "  - Your email address is invalid. \n\n";
		}
	}
	cur_value = document.myform.loginpassword.value;
	if((cur_value == null) || (cur_value == "") || (isBlank(cur_value))){
		empty_fields += "\n     " + "Password";
	}
	
	if (!empty_fields && !errors && !missed_answers){
			document.myform.submit()
	}else{    
  		//otherwise...compose message  
 		msg  = "_____________________________________________________\n\n";
 	 	msg += "We are sorry, but we cannot submit your Info without\n";
 	 	msg += "the following fields being filled out correctly.\n";
 	 	msg += "Please correct these error(s) and re-submit.\n";
 	 	msg += "_____________________________________________________\n\n";
  	 
 		 if(empty_fields){
    		msg += "- The following required field(s) are empty:"
      	 	+ empty_fields + "\n";
    		if(errors){ msg += "\n\n";}	
		 }	
 
		if(errors){
     	  	 msg += " - The following required field(s) are filled out incorrectly: \n\n"
     	  	+ errors + "\n";
			if(missed_answers){ msg += "\n";}
		}
		if(missed_answers){
			msg += " - You have not answered to the following questions: \n"
			+ missed_answers +"\n";
		}	
  		alert(msg);
 		return false; 
	}
}




//checks for blank fields ------------------------------
function isBlank(s){
	for(var i=0;i<s.length;i++){
      	var c = s.charAt(i);
      	if ((c != ' ')&&(c != '\n')&&(c != '\t'))
        	return false;
 	}
 	return true;
}

function hasNumber(s){
	var flag = 0;
	for(var i=0; i<s.length;i++){
		var c = s.charAt(i);
		if(isNaN(c) || isBlank(c)){
			
		}else{
			flag = 1;
		}
	}
	if(flag){
		return true;
	}else{
		return false;
	}
}						

//checks for valid email address
function validemail(emailStr){
  	var checkTLD=1;
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum|COM|NET|ORG|EDU|INT|MIL|GOV|ARPA|BIZ|AERO|NAME|COOP|INFO|PRO|MUSEUM)$/;
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)><@,;:`~!#$%^&*+=|//}{?\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray=emailStr.match(emailPat);
	
	var userpat2=emailStr.indexOf('www.');
	if (userpat2 > -1)
	{
		return false;
	}
	var userpat3=emailStr.indexOf('gamefiesta');
	if (userpat3 > -1)
	{
		return false;
	}
	
	if (matchArray==null){
		return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];
	for (i=0; i<user.length; i++){
		if (user.charCodeAt(i)>127){
			return false;
   		}
	}
	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127){
			return false;
   		}
	}
	if (user.match(userPat)==null){
		return false;
	}
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null){
		for (var i=1;i<=4;i++){
			if (IPArray[i]>255){
				return false;
   			}
		}
		return true;
	}
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++){
		if (domArr[i].search(atomPat)==-1){
			return false;
   		}
	}
	if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1) {
		return false;
	}
	if (len<2){
		return false;
	}
	return true;
}