/****************************************************************
 * contest.js
 * This has all the error checking functions used in the travel section.
 *
 * <SCRIPT language="JavaScript1.1" src="contest.js"></SCRIPT>
 *
 * Functions found in this script file:
 *
 * strcheck
 * e.g. strcheck(document.form.field.value,1,80,"Name")
 *
 * radiocheck
 * e.g. radiocheck(document.form.field)
 *
 * datecheck
 * e.g. datecheck(document.form.field.value,"Label")
 *
 * emailcheck
 * e.g. emailcheck (document.form.field.value)
 *
 * phonecheck
 * e.g. phonecheck (document.form.field.value)
 *
 * numcheck
 * e.g. numcheck (document.form.field.value,"Price")
 *
 * is18
 * e.g. is18 (document.form.field.value)
 *
 * isLegal
 * e.g. isLegal (document.form.field.value,age)
 ****************************************************************/

// check to see if the text field is not in bound
function strcheck (field_handle,min,max,field_name) {
  if ((field_handle.length < min) || (field_handle.length > max)) {
    alert (field_name + " must be completed " +
      "and must be fewer than " + (max+1) + " characters.");
    return false;
  }
  return true;
}

// check to see if radio value is checked
function radiocheck (field_handle,field_name) {
	for (i=0;i<field_handle.length;i++) {
		if (field_handle[i].checked) {
			return true;
		}
	}
    alert (field_name + " must be selected.");	
	return false;
}

// check for the syntax of email
// emailcheck(strvalue)
// Format e.g.: my_account234@domain.org.hk
// Matches any character including alphanumeric, underscore, hyphen, %, and dot
// before @ (but the last character must be alphanumeric) and at
// least 2 alphnumeric characters and at most 3 characters at the end
function emailcheck (myStr)
{
   re = /^[\w%~\.\-]*(\w)+@([\w\-]+\.)+\w{2,3}$/;
   if (myStr.search(re) == -1) {
	alert ("Invalid E-mail format.");
	return false;
   } else return true;
}

// check for the syntax of phone
// phonecheck(strvalue)
// Format e.g.: ddd-ddd-dddd
function phonecheck (myStr)
{
   re = /^\d\d\d-\d\d\d-\d\d\d\d$/;
   if (myStr.search(re) == -1) {
	alert ("Invalid phone format. Must be xxx-xxx-xxxx");
	return false;
   } else return true;
}

// check to see if the text field is numeric
// isNaN = is not a number
function numcheck (field_handle,field_name) {
  if (field_handle.length == 0) {
    alert (field_name + " must be a number.");
    return false;
  }
  for (var j=0; j<field_handle.length; j++) {
    if (isNaN(parseInt(field_handle.substring(j,j+1))) &&
        (field_handle.substring(j,j+1) != ".")) {
      alert (field_name + " must be a number.");
      return false;
    }
  }
  return true;
}

// check for corrent informix date format mm/dd/yyyy
function datecheck (field_handle,field_name) {
   var allpass = true
   // if the string is less than 10 chars, no need to proceed further validation
   if (field_handle.length > 0) {
   if (field_handle.length < 10) {
      alert (field_name + ": incorrect format.  Must be mm/dd/yyyy.");
      allpass = false;
   }
   else {
     // separate out the elements
     var leapyear = false;
     var year = field_handle.substring(6,10);
     var month = field_handle.substring(0,2);
     var day = field_handle.substring(3,5);
     var firstslash = field_handle.substring(2,3);
     var secondslash = field_handle.substring(5,6);
     if ((firstslash != '/') || (secondslash != '/')) {
	alert (field_name + ": incorrect format.  Must be mm/dd/yyyy.");
	allpass = false;
     }
     else {
     if ((numcheck(year,"year") == false) || (numcheck(month,"month") == false) || (numcheck(day,"day") == false))
        allpass = false;
     else {
       if (isLeapYear(year)) 
          leapyear = true;
       if ( (month < 1) || ( month > 12)) {  
	  alert (field_name + ": month must be between 1 and 12.");
          allpass = false;
       }
	if ((day < 1) || (year < 1)) {
	  alert (field_name + ": day or year must be greater than 0");
          allpass = false;
	}
       if ((month == 1) || (month == 3) || (month == 5) || (month == 7) || (month == 8) || (month == 10) || (month ==12)) {  
        if (day > 31) {  
	  alert (field_name + ": cannot have more than 31 days in this month.");
          allpass = false;
        }
       }
       else {  
        if (month == 2) {  
	 if (leapyear) { 
	  if (day > 29) { 
	   alert (field_name + ": cannot have more than 29 days in this month.");
           allpass = false;
          }
         }
         else {
          if (day > 28) {
           alert (field_name + ": cannot have more than 28 days in this month.");
           allpass = false;
          }
         }
        }
        else {
         if (day > 30) {
          alert (field_name + ": cannot have more than 30 days in this month.");
          allpass = false;
         }
        }
       } //end of day, month, and year are numbers = true
     } //end of month check
    } //end of slashes check
   } // end of field length check
   }
   return allpass;
}  // end of datecheck

// check to see if the text field is empty
function isEmpty (field_handle,field_name) {
  if (field_handle.length < 1) {
    alert (field_name + " cannot be empty ");
    return true;
  }
  return false;
}

// check for the appearance of a particular phrase or symbol
// in string.
// symbolcheck(value,what_symbol,musthave, field_name)
function symbolcheck (field_handle,symbol,musthave,field_name) {
var my_str;
  if (field_handle.length > 0) {
   if (musthave == true) {
    if (field_handle.indexOf(symbol) == -1) {
      alert ("Invalid format: " + field_name + " must have this: " + symbol);
      return false;
    }
    return true;
   }
   else {
    if (field_handle.indexOf(symbol) == -1) return true;
    switch (symbol) {
      case " " :
           my_str = " must not have a space.";
           break;
      case "\n" :
           my_str = " must not have a carriage return.";
           break;
      default : my_str = " must not have this: " + symbol + ".";
   }
    alert ("Invalid format: " + field_name + my_str);
    return false;
   }
  } // end of not empty
  return true;
}

// get number of days in month
// this won't work with NS 3
function getDaysInMonth(month,year)  {
    var days
    switch (month) {
      case 4 :
      case 6 :
      case 9 :
      case 11 :
                days=30;
                break;
      case 2 :
                if (isLeapYear(year)) { days=29; }
                else  { days=28; }
                break;
      default : days=31;
    }
    return (days);
}

// check for leap year
function isLeapYear (Year) {
    if (((Year % 4)==0) && ((Year % 100)!=0) || ((Year % 400)==0)) {
        return (true);
    }
    else {
        return (false);
    }
}

//generate 4 distinct random numbers between 1 and 82.
function genRan() {
  var ran = new Array(4);
  for (var i=1; i<=4; i++) {
    ran[i]=(Math.round(Math.random()*100000))%82+1;
    var pass=0;
    if (i>1) {
      while (!pass) {
        pass = 1;
        for (var j=1; j<i; j++) {
          if (ran[j] == ran[i]) {
			  ran[i]=(Math.round(Math.random()*100000))%82+1;
    		  pass = 0;
          }
        }
      }
    }
  }
  for (var j=1; j<=4; j++) {
    document.writeln("<td><img src=\"/images/contest/"+ran[j]+".gif\" width=49 height=49 alt="+ran[j]+"</td>");
  }
    document.writeln("<input type=hidden name=r1 value="+ran[1]+">");
    document.writeln("<input type=hidden name=r2 value="+ran[2]+">");
    document.writeln("<input type=hidden name=r3 value="+ran[3]+">");
    document.writeln("<input type=hidden name=r4 value="+ran[4]+">");
  return true;
}

// generate a random number
function genNextWin() {
  var ran=(Math.round(Math.random()*100000))%25+1;
  document.writeln("<input type=hidden name=nextWin value="+ran+">");
  return true;
}

//compare today's date with the birthdate and errors out below 18 years old
function is18(myBD) {
	// get today's date and calculate who reaches 18 years old
	var now = new Date();
	var month = now.getMonth() + 1;
	var day = now.getDay();
	var year = now.getYear() - 18;
	if (year < 1000) { year += 1900; }

	year18 = new Date(month + "/" + day + "/" + year + " 00:00:00");

   	bday = new Date(myBD + " 00:00:00");

	diff = year18.getTime() - bday.getTime();

	if (diff < 0) {
		alert ("You must be 18 years of age or older.");
		return false;
	}
	else {
		return true;
	}

}

//compare today's date with the birthdate and errors out below the specified age
function isLegal(myBD,age) {
	// get today's date and calculate who reaches the legal age
	var now = new Date();
	var month = now.getMonth() + 1;
	var day = now.getDay();
	var year = now.getYear() - age;
	if (year < 1000) { year += 1900; }

	legalAge = new Date(month + "/" + day + "/" + year + " 00:00:00");

	bday = new Date(myBD + " 00:00:00");

	diff = legalAge.getTime() - bday.getTime();

	if (diff < 0) {
		alert ("You must be " + age + " years of age or older.");
		return false;
	}
	else {
		return true;
	}

}

