﻿//<!--


function Blur(elem, word) {
   var txt = document.getElementById(elem);
   if (txt.value.toLowerCase() == '') { txt.value = word; }
}

function Focus(elem, word) {
   var txt = document.getElementById(elem);
   if (txt.value.toLowerCase() == word.toLowerCase()) { txt.value = ''; }
}

function dateBlur(elem, word) {
   var txt = document.getElementById(elem);
   if (txt.value.toLowerCase() == '') { txt.value = word; } else { checkDate(); }
}

function checkDate() {
   var RegularExpression = new RegExp("(^((((0[1-9])|([1-2][0-9])|(3[0-1]))|([1-9]))\x2F(((0[1-9])|(1[0-2]))|([1-9]))\x2F(([0-9]{2})|(((19)|([2]([0]{1})))([0-9]{2}))))$)");
   
   var date = document.getElementById("txtBday");
   if (!RegularExpression.test(date.value)) {
      alert('Invalid date, please correct it');
      date.focus();
      return false;
   }
   else {
      return true;
   }
}

function swapImg(id) {
   var img = document.getElementById(id);

   if (img.src.indexOf('up') == -1) {
      img.src = "images/ck_up.gif";
   }
   else {
      img.src = "images/ck_down.gif";
   }
}


function validateSubmit() {
   var fn = document.getElementById("txtFname");
   var ln = document.getElementById("txtLName");
   var email = document.getElementById("txtEmail");
   var bday = document.getElementById("txtBday");
   var cardnum = document.getElementById("txtCardNum");
   var caption = document.getElementById("txtCaption");
   var terms = document.getElementById("imgTerms");

   if (terms.src.indexOf("up") > 1) {
      alert('You must agree to the terms and conditions');
      return false;
   }

   if (fn.value.replace(/^\s+|\s+$/g, "") == "") {
      Alert("first name", fn);
      return false;
   }
   if (ln.value.replace(/^\s+|\s+$/g, "") == "") {
      Alert("last name", ln);
      return false;
   }
   if (!checkDate()) {      
      return false;
   }
   if (valEmail(email)) {
      return false;
   }
   if (cardnum.value.replace(/^\s+|\s+$/g, "") == "") {
      Alert("membership number", cardnum);
      return false;
   }
   if (caption.value.replace(/^\s+|\s+$/g, "") == "") {
      Alert("caption", caption);
      return false;
   }   

   return true;   
}

function Alert(control, field) {   
   alert('Please enter your ' + control);
   field.focus();
}

function valEmail(el) {
   var invalidChars = " /:,;";
   if (el.value == "") {
      alert("Please enter an email address.");
      el.focus();
      return true;
   }
   for (i = 0; i < invalidChars.length; i++) {
      badChar = invalidChars.charAt(i);
      if (el.value.indexOf(badChar, 0) != -1) {
         alert("Your email address contains an invalid character, please correct it.");
         el.focus();
         return true;
      }
   }
   atPos = el.value.indexOf("@", 1);
   if (atPos == -1) {
      alert("Your email address must contain an @ character.");
      el.focus();
      return true;
   }
   if (el.value.indexOf("@", atPos + 1) != -1) {
      alert("Your email address must have letters before the @ character.");
      el.focus();
      return true;
   }
   periodPos = el.value.indexOf(".", atPos);
   if (periodPos == -1) {
      alert("Your email address must contain a . character.");
      el.focus();
      return true;
   }
   if (periodPos + 3 > el.value.length) {
      alert("Your email address must have letters after the . character.");
      el.focus();
      return true;
   }
   return false;
}

function getHTTPObject() {
   var xmlhttp;
   /*@cc_on
   @if (@_jscript_version >= 5)
   try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
   } catch (e) {
      try {
         xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
         xmlhttp = false;
      }
   }
   @else
      xmlhttp = false;
      @end
   @*/
   if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
      try {
         xmlhttp = new XMLHttpRequest();
      } catch (e) {
         xmlhttp = false;
      }
   }
   return xmlhttp;
}
var http = getHTTPObject(); // We create the HTTP Object


function setGameDetails(distance, gender) {
   distance;
   gender;

   var url = "setscore.ashx?gender=" + gender + "&score=" + distance;
   http.open("GET", url, true);
   http.onreadystatechange = function() {
      if (http.readyState == 4) {
         window.location = "entry.aspx";
      }
   };
        
   http.send(null);   
}

function openWindow(url, title) {
   window.open(url, null, "resizable=no,width=500,height=500");
}
//-->