
   // send image for  update time spent in classroom in DB 
   function TimeSpendPing () {
	var d;
	d = new Date();

	   s = d.getHours()+d.getSeconds()+d.getMilliseconds();

	  s=s*13;
	  document.images['images/spacer.gif'].src="time_spend.php?rand="+s;
  }


  // This function checks that a ZIP code is in the
  // correct format.
  function validateZIP(theString) {
    zipOK = false;  // zipOK is a GLOBAL variable
    // Check that the zip is the right length
    if ( theString.length != 5 ) return false;
    // don't accept non-numeric characters
    if (!numbersOnly(theString)) return false;
    return (zipOK=true);
  }

  // Test for phone number in proper format.
  // This is not particularly strict, but it's hard to do
  // real regular expression matching in JavaScript.
  function validatePhone(theString) {
     phoneOK = true; // phoneOK is GLOBAL
     var digits = 0;
     // Check that the phone number only contains
     // the characters "() -0-9" and contains the right
     // number of digits total
     for (var i=0; i < theString.length; i++) {
        var theChar = theString.charAt(i);
        if ((theChar >= "0") && (theChar <= "9")) {
           digits++;
           continue;
        }
        if (theChar == " ") continue;
        if (theChar == "-") continue;
        if (theChar == "(") continue;
        if (theChar == ")") continue;
        phoneOK = false;
     }

     // The string is OK if it contains only the allowed
     // characters and the number of digits in the
     // phone number is 10 total (area code + number)
     phoneOK = phoneOK && (digits == 10);
     return phoneOK;
  }

   // Check that a string contains only alphabetic characters numbers
   function alphaNumbers(theString) { 
     var OK = true;
     for (var i=0;i<theString.length;i++) {
        theChar = theString.charAt(i);
        if ( alphaOnly(theChar) )
           continue;
        if ( numbersOnly(theChar) )
           continue;
        OK = false;
     }
     return OK;
   }

   // Check that a string contains only alphabetic characters
   function alphaOnly(theString) { 
     var OK = true;
     for (var i=0;i<theString.length;i++) {
        theChar = theString.charAt(i);
        if ( (theChar >= "a") && (theChar <= "z") )
           continue;
        if ( (theChar >= "A") && (theChar <= "Z") )
           continue;
        OK = false;
     }
     return OK;
   }

   // Check that a field contains only numeric characters
   function numbersOnly(theString) {
     var OK = true;
     for (var i=0;i<theString.length;i++) {
        theChar = theString.charAt(i);
        if ((theChar < "0") || (theChar > "9")) {
           OK = false;
           break;
        }
     }
    return OK;
  }

  function validateEmail(theString) {
	var str = theString; // email string
	var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
	var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid
	if (!reg1.test(str) && reg2.test(str)) { // if syntax is valid
		return true;
	}
	return false;
  }

  function Trim(Untrimmed) {
	var Trimmed = ''
	for (var i = 0; i < Untrimmed.length; i++) {
		if (Untrimmed.charCodeAt(i)!=32) {
			Trimmed += Untrimmed[i]
		}
	}
	return Trimmed
  } 

  function checkFormData(items, form) {
    for(i = 0; i<items.length; i++) {
    	var item = items[i];
    	if(Trim(eval(form+'.'+item[0]+'.value')) == '') {
        	eval(form+'.'+item[0]+'.focus()');
//        	eval(form+'.'+item[0]+'.select()');
			alert('Please enter your '+item[1]);
            return false;
        } else {
			if(item[2] == 'alpha') {
				if(!alphaOnly(eval(form+'.'+item[0]+'.value'))) {
					eval(form+'.'+item[0]+'.focus()');
					eval(form+'.'+item[0]+'.select()');
					alert(item[1] + ' must contain only alphabetic characters!');
		            return false;
				}
			} else if(item[2] == 'zip') {
				if(!validateZIP(eval(form+'.'+item[0]+'.value'))) {
					eval(form+'.'+item[0]+'.focus()');
					eval(form+'.'+item[0]+'.select()');
					alert(item[1] + ' must contain five digits!');
		            return false;
				}
			} else if(item[2] == 'phone') {
				if(!validatePhone(eval(form+'.'+item[0]+'.value'))) {
					eval(form+'.'+item[0]+'.focus()');
					eval(form+'.'+item[0]+'.select()');
					alert(item[1] + ' must be in the format (555) 555-5555');
		            return false;
				}
			} else if(item[2] == 'email') {
				if(!validateEmail(eval(form+'.'+item[0]+'.value'))) {
					eval(form+'.'+item[0]+'.focus()');
					eval(form+'.'+item[0]+'.select()');
					alert(item[1] + ' must be a valid Email address');
		            return false;
				}
			} else if(item[2] == 'alphanum') {
				if(!alphaNumbers(eval(form+'.'+item[0]+'.value'))) {
					eval(form+'.'+item[0]+'.focus()');
					eval(form+'.'+item[0]+'.select()');
					alert(item[1] + '  must contain only alphabetic characters and numbers!');
		            return false;
				}
			}
		}
    }
	return true;
  }

// Form Field Validation Functions:
//
// isValidExpDate(formField,fieldLabel,required)
//   -- checks for date in the format MM/YY or MM/YYYY against the current date
// isValidCreditCardNumber(formField,ccType,fieldLabel,required)
//   -- checks for valid credit card format using the Luhn check and known digits about various cards
//

function validRequired(formField,fieldLabel)
{
	var result = true;
	
	if (formField.value == "")
	{
		alert('Please enter a value for the "' + fieldLabel +'" field.');
		formField.focus();
		result = false;
	}
	
	return result;
}


function allDigits(str)
{
	return inValidCharSet(str,"0123456789");
}

function inValidCharSet(str,charset)
{
	var result = true;
	
	for (var i=0;i<str.length;i++)
		if (charset.indexOf(str.substr(i,1))<0)
		{
			result = false;
			break;
		}
	
	return result;
}

function isValidExpDate(formField,fieldLabel,required)
{
	var result = true;
	var formValue = formField.value;

	if (required && !validRequired(formField,fieldLabel))
		result = false;

	if (result && (formField.value.length>0))
 	{
 		var elems = formValue.split("/");
 		
 		result = (elems.length == 2); // should be two components
 		var expired = false;
 		
 		if (result)
 		{
 			var month = parseInt(elems[0],10);
 			var year = parseInt(elems[1],10);
 			
 			if (elems[1].length == 2)
 				year += 2000;
 			
 			var now = new Date();
 			
 			var nowMonth = now.getMonth() + 1;
 			var nowYear = now.getFullYear();
 			
 			expired = (nowYear > year) || ((nowYear == year ) && (nowMonth > month));
 			
			result = allDigits(elems[0]) && (month > 0) && (month < 13) &&
					 allDigits(elems[1]) && ((elems[1].length == 2) || (elems[1].length == 4));
 		}
 		
  		if (!result)
 		{
 			alert('Please enter a date in the format MM/YY for the "' + fieldLabel +'" field.');
			formField.focus();
		}
		else if (expired)
		{
 			result = false;
 			alert('The date for "' + fieldLabel +'" has expired.');
			formField.focus();
		}
	} 
	
	return result;
}

function isValidCreditCardNumber(formField,ccType,fieldLabel,required)
{
	var result = true;
 	var ccNum = formField.value;

	if (required && !validRequired(formField,fieldLabel))
		result = false;

  	if (result && (formField.value.length>0))
 	{ 
 		if (!allDigits(ccNum))
 		{
 			alert('Please enter only numbers (no dashes or spaces) for the "' + fieldLabel +'" field.');
			formField.focus();
			result = false;
		}	

		if (result)
 		{ 
 			
 			if (!LuhnCheck(ccNum) || !validateCCNum(ccType,ccNum))
 			{
 				alert('Please enter a valid card number for the "' + fieldLabel +'" field.');
				formField.focus();
				result = false;
			}	
		} 

	} 
	
	return result;
}

function LuhnCheck(str) 
{
  var result = true;

  var sum = 0; 
  var mul = 1; 
  var strLen = str.length;
  
  for (i = 0; i < strLen; i++) 
  {
    var digit = str.substring(strLen-i-1,strLen-i);
    var tproduct = parseInt(digit ,10)*mul;
    if (tproduct >= 10)
      sum += (tproduct % 10) + 1;
    else
      sum += tproduct;
    if (mul == 1)
      mul++;
    else
      mul--;
  }
  if ((sum % 10) != 0)
    result = false;
    
  return result;
}



function GetRadioValue(rArray)
{
	for (var i=0;i<rArray.length;i++)
	{
		if (rArray[i].checked)
			return rArray[i].value;
	}
	
	return null;
}


function validateCCNum(cardType,cardNum)
{
	var result = false;
	cardType = cardType.toUpperCase();
	
	var cardLen = cardNum.length;
	var firstdig = cardNum.substring(0,1);
	var seconddig = cardNum.substring(1,2);
	var first4digs = cardNum.substring(0,4);

	switch (cardType)
	{
		case "VISA":
			result = ((cardLen == 16) || (cardLen == 13)) && (firstdig == "4");
			break;
		case "AMEX":
			var validNums = "47";
			result = (cardLen == 15) && (firstdig == "3") && (validNums.indexOf(seconddig)>=0);
			break;
		case "MASTERCARD":
			var validNums = "12345";
			result = (cardLen == 16) && (firstdig == "5") && (validNums.indexOf(seconddig)>=0);
			break;
		case "DISCOVER":
			result = (cardLen == 16) && (first4digs == "6011");
			break;
		case "DINERS":
			var validNums = "068";
			result = (cardLen == 14) && (firstdig == "3") && (validNums.indexOf(seconddig)>=0);
			break;
	}
	return result;
}

function validCCForm(ccTypeField,ccNumField,ccExpField)
{
//	var result = isValidCreditCardNumber(ccNumField,ccTypeField.value,"Credit Card Number",true) &&
//		isValidExpDate(ccExpField,"Expiration Date",true);
	var result = isValidCreditCardNumber(ccNumField,ccTypeField.value,"Credit Card Number",true);
	return result;
}
