function trim(s) {

    if((s==null)||(typeof(s)!='string')||!s.length) {
        return '';
    }
    return s.replace(/^\s+/,'').replace(/\s+$/,'');

}

function check_blank(field) {

    if (field == '') {
        return false;
    }
    if (trim(field) == '') {
        return false;
    }
    return true;
}

function isDigit(num) {
    
    if (num.length>1) { 
        return false;
    }
    var string="1234567890";
    if (string.indexOf(num)!=-1) { 
        return true;
    }
    return false;
}

function isInteger(val){
    
    for(var i=0;i<val.length;i++) {
	if(!isDigit(val.charAt(i))) {
	    return false;
	}
    }
    return true;
}

function upper_postal() {
    
    var postal = document.form1.postalcode.value;

    document.form1.postalcode.value = postal.toUpperCase();
}

function validate_postal(postal) {

var regex = /^\D{1}\d{1}\D{1}\ ?\d{1}\D{1}\d{1}$/;

return regex.test(postal);
}

function validate_alberta(postal) {
    
if ((postal.charAt(0) != 'T') && (postal.charAt(0) != 't') ) {
return false;
}
else {
return true;
   }
}

function isExpiryDate(year, month) {

    var today = new Date();
    var expiry = new Date(year, month);

    if (today.getTime() > expiry.getTime()) {
    	return true;
    }
    
    else {
	return false;
    }
}

function testCreditCard (ccnum, cctype, ccexpirymonth, ccexpiryyear) {

  if (isExpiryDate(ccexpiryyear, ccexpirymonth)) {
      return "expired";
  }
  else if (checkCreditCard (ccnum, cctype)) {
      return "valid";
  } 
  else {
      return "invalid";
  }
}

function validate_area(area) {

    if ((area.length == 3) && (isInteger(area))) {
	return true;
    }
    else {
	return false;
    }
}

function validate_phone(phone) {

    var regex  = /^\d{3}\-\d{4}$/;

    return regex.test(phone);
}

function echeck(str) {

      var at="@";
      var dot=".";
      var lat=str.indexOf(at);
      var lstr=str.length;
      var ldot=str.indexOf(dot);

      if (str.indexOf(at)==-1){
	   return false;
      }

      if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
          return false;
      }

      if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
 	  return false;
      }

      if (str.indexOf(at,(lat+1))!=-1){
	  return false;
      }

      if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	  return false;
      }

      if (str.indexOf(dot,(lat+2))==-1){
          return false;
      }
		
      if (str.indexOf(" ")!=-1){
	  return false;
      }

      return true;					
}

function validate_tickets1() {
    
    var postal = document.form1.postalcode.value;

    if (!check_blank(postal)) {
        alert("Your postal code is required.");
        return false;
    }

    else {

        if (validate_postal(postal)) {
	    if (validate_alberta(postal)) {
		return true;
	    }
	    else {
		alert("Sorry, only residents of Alberta are allowed to participate.");
		return false;
	    }
	}
	else {
	    alert("Your postal code is invalid. It must appear in the form T2A 2T3.");
	    return false;
	}
    }
}

function validate_tickets2() {

    var firstname = document.form1.firstname.value;
    var lastname = document.form1.lastname.value;
    var address = document.form1.address1.value;
    var city = document.form1.city.value;
    var province = document.form1.province.value;
    var postal = document.form1.postalcode.value;
    var email = document.form1.email.value;
    var areahome = document.form1.areahome.value;
    var phonehome = document.form1.phonehome.value;
    var areabus = document.form1.areabus.value;
    var phonebus = document.form1.phonebus.value;
    var ccnum = document.form1.ccnum.value;
    var ccname = document.form1.ccname.value;
    var cctype = document.form1.cctype.value;
    var ccexpirymon = document.form1.ccexpirymon.value;
    var ccexpiryyear = document.form1.ccexpiryyear.value;
    var singles = document.form1.singles.value;
    var threes = document.form1.threes.value;
    var fives = document.form1.fives.value;
    var eights = document.form1.eights.value;

    if (!check_blank(firstname)) {
	alert("Your first name is required.");
	return false;
    }

    else if (!check_blank(lastname)) {
	alert("Your last name is required.");
	return false;
    }
 
    else if (!check_blank(address)) {
	alert("Your address is required");
	return false;
    }

    else if (!check_blank(city)) {
	alert("Your city is required.");
	return false;
    }

    else if (!check_blank(province)) {
	alert("Your province is required.");
	return false;
    }

    else if ((province.toUpperCase() != "ALBERTA") && (province.toUpperCase() != "AB")) {
	alert("Sorry, only residents of Alberta are allowed to participate."); 
	return false;
    }

    else if (!check_blank(postal)) {
        alert("Your postal code is required.");
        return false;
    }

    else if (!validate_postal(postal)) {
	alert("Your postal code is invalid. It must appear in the form T2A 2T3.");
	return false;
    }

    else if (!validate_alberta(postal)) {
	alert("Sorry, only residents of Alberta are allowed to participate.\nYour postal code is not from Alberta.");
	return false;
    }

    else if (!check_blank(email)) {
	alert("Your email address is required.");
	return false;
    }

    else if (!echeck(email)) {
	alert ("Your email address is invalid.");
	return false;
    }

    else if (!check_blank(ccnum)) {
	alert ("Your credit card number is required.");
	return false;
    }

    else if (!check_blank(ccname)) {
	alert ("Your card holder name is required.");
	return false;
    }

    if (!check_blank(areahome)) {
        alert("You home area code is required.");
	return false;
    }

    else if (!validate_area(areahome)) {
        alert ("Your home area code is not valid.\nIt must be 3 digits (i.e. 780).");
	return false;
    }

    else if (!check_blank(phonehome)) {
	alert ("Your home phone number is required.");
	return false;
    }

    else if (!validate_phone(phonehome)) {
	alert ("Your home phone number is not valid.\nIt must appear in the form 999-9999.");
	return false;
    }

    else if ((check_blank(areabus)) && (!validate_area(areabus))) {
	alert ("Your business area code is not valid.\nIt must be 3 digits (i.e. 780).");
	return false;
    }
 
    else if ((check_blank(phonebus)) && (!check_blank(areabus))) {
	alert ("You must provide an area code with your business phone number.");
	return false;
    }

    else if ((check_blank(phonebus)) && (!validate_phone(phonebus))) {
	alert ("Your business phone number is not valid.\nIt must appear in the form 999-9999.");
	return false;
    }

    else if (cctype == "- Select Card Type -") {
	alert ("You must select a credit card type.");
	return false;
    }

    else if (ccexpirymon == "- Month -") {
	alert ("You must select a credit card expiry month.");
	return false;
    }

    else if (ccexpiryyear == "- Year -") {
	alert ("You must select a credit card expiry year.");
	return false;
    }

    var valid = testCreditCard(ccnum, cctype, ccexpirymon, ccexpiryyear);
    if (valid == "expired") {
        alert ("Your credit card is expired.");
    	return false;
    }
    else if (valid == "invalid") {
        alert ("Invalid credit card number for " + cctype);
	return false;
    }
    else if (valid == "valid") {

	if ((singles == "none") && (threes == "none") && (fives == "none") && (eights == "none")) {
            alert ("You must choose at least one ticket package.");
	    return false;
    	}
    	else if (singles > 2) {
	    if (confirm("You have ordered multiple single tickets. Packages are available in denominations of 3, 5, and 8. Select OK to continue or Cancel to edit your order.")) {
             	return true;
       	    }
       	    else {
             	return false;
       	    }
    	}
    	else {
	    return true;
    	}
    }
    else {
	alert ("Unknown error validating credit card number.");
	return false;
    }
}

function changeTotal() {

    var singles, threes, fives, eights, totalSingles, totalThrees, totalFives, totalEights;
    var total = 0;

    var singlesString = document.form1.singles.value;
    var threesString = document.form1.threes.value;
    var fivesString = document.form1.fives.value;
    var eightsString = document.form1.eights.value;

    if (singlesString == "none") {
	singles = 0;	
    }
    else {
	singles = singlesString;
    }

    if (threesString == "none") {
	threes = 0;
    }
    else {
	threes = threesString;
    }

    if (fivesString == "none") {
	fives = 0;
    }
    else {
	fives = fivesString;
    }

    if (eightsString == "none") {
	eights = 0;
    }
    else {
	eights = eightsString;
    }

    totalSingles = singles*100;
    totalThrees = threes*250;
    totalFives = fives*375;
    totalEights = eights*525;

    total = totalSingles + totalThrees + totalFives + totalEights;

    document.getElementById("total").innerHTML = '$' + total; 

}

function validate_winner(namenumber) {
    
    if (!check_blank(namenumber)) {
	alert('Please enter your LAST NAME or TICKET NUMBER in the space provided.');
	return false;
    }

    return true;
}
