// JavaScript Document
//=============== isDate ========================

var dtCh= "/";
var minYear=1900;
var maxYear=2100;

	function isInteger(s) 
	{
		var i;
		for (i = 0; i < s.length; i++){   
			// Check that current character is number.
			var c = s.charAt(i);
			if (((c < "0") || (c > "9"))) return false;
		}
		// All characters are numbers.
		return true;
	}

	function stripCharsInBag(s, bag)
	{
		var i;
		var returnString = "";
		// Search through string's characters one by one.
		// If character is not in bag, append to returnString.
		for (i = 0; i < s.length; i++){   
			var c = s.charAt(i);
			if (bag.indexOf(c) == -1) returnString += c;
		}
		return returnString;
	}

	function daysInFebruary (year)
	{
		// February has 29 days in any year evenly divisible by four,
		// EXCEPT for centurial years which are not also divisible by 400.
		return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
	}
	function DaysArray(n)
	{
		for (var i = 1; i <= n; i++) {
			this[i] = 31
			if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
			if (i==2) {this[i] = 29}
		} 
		return this
	}

	function isDate(dtStr)
	{
		var daysInMonth = DaysArray(12)
		var pos1=dtStr.indexOf(dtCh)
		var pos2=dtStr.indexOf(dtCh,pos1+1)
		var strMonth=dtStr.substring(0,pos1)
		var strDay=dtStr.substring(pos1+1,pos2)
		var strYear=dtStr.substring(pos2+1)
		strYr=strYear
		if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
		if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
		for (var i = 1; i <= 3; i++) {
			if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
		}
		month=parseInt(strMonth)
		day=parseInt(strDay)
		year=parseInt(strYr)
		if (pos1==-1 || pos2==-1){
			alert("The date format should be : mm/dd/yyyy")
			return false
		}
		if (strMonth.length<1 || month<1 || month>12){
			alert("Please enter a valid month")
			return false
		}
		if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
			alert("Please enter a valid day")
			return false
		}
		if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
			alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
			return false
		}
		if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
			alert("Please enter a valid date")
			return false
		}
		return true
	}


	//========================== email validation function =============================
	function isEmail(fieldObj)
	{
		var str = fieldObj.value;
		var re = /^([a-zA-Z0-9-_\.]+@([a-zA-Z0-9-_]+\.)+[a-zA-Z]{2,4})$/;
		if (re.test(str)) {
			return true;
		} else {				
			return false;
		}
	
	}
	function isEmailValid(str)
	{
		var re = /^([a-zA-Z0-9-_\.]+@([a-zA-Z0-9-_]+\.)+[a-zA-Z]{2,4})$/;
		if (re.test(str)) {	
			return true; 
		} else {				
			return false;
		}
	}

//------------- us phone no check

	function validPhoneUS(fieldObj)
	{
		var str = fieldObj.value;
		re = /^\(?\d{3}\)?\s|-\d{3}-\d{4}$/;
		if (re.test(str)) {
			return true;
		} else {
			return false;
		}
	}
	function isPhoneNumber(str){
	  var stripped = str.replace('-', '');
	  	  stripped = stripped.replace('-', '');
		//strip out acceptable non-numeric characters
		if (isNaN(parseInt(stripped))) {
		  return false;
		} else if (stripped.length != 10) {
			return false;
		}
		return true;
	}
	
	function validPhone(mixnumber)
	{
		var str = mixnumber;
		if (str.length <= 0)
		{ return false }
		var i;
		for (i = 0; i < str.length; i++){   
		// Check that current character is number.
		var c = str.charAt(i);
		if (((c < 0) || (c > 9))) return false; //  && c!="-" removed
		}
		// All characters are numbers.
		return true;
	}
	function validCreditCard(mixnumber)
	{
		var str = mixnumber;
		var i;
		for (i = 0; i < str.length; i++){   
		// Check that current character is number.
		var c = str.charAt(i);
		if (((c < 0) || (c > 17))) return false; //  && c!="-" removed
		}
		// All characters are numbers.
		return true;
	}

	//------------- zip check
	function validZip(fieldObj)
	{
		var str = fieldObj.value;
		var re = /\d{5}(-\d{4})?/;
		if (!isNaN(str)===false) {
			return false;
		} else if (str.length <=3 || str.length >=10) {
			return false;
		} else {			
			return true;
		}	
	}

//==============  CC Checks ====================================

	function isVisa( cc )
	{
		if( (cc.substring(0,1) == 4) && (cc.length == 16) || (cc.length == 13) ) {
			return true; // isCreditCard( cc );
		}
		return (false);
	}
		
	function isMC( cc )
	{
		if( (cc.length == 16) && (cc.substring(0,2) == 51) || (cc.substring(0,2) == 52) || (cc.substring(0,2) == 53) || (cc.substring(0,2) == 54) || (cc.substring(0,2) == 55) ) {
			return true;//isCreditCard( cc );
		}
		return (false);
	}

	function isAmex( cc )
	{
		if( (cc.length == 15) && (cc.substring(0,2) == 34) || (cc.substring(0,2) == 37) ) {
			return true;//isCreditCard( cc );
		}
		return (false);
	}


	function isDiscover( cc ) {
		if( (cc.length == 16) && (cc.substring(0,4) == 6011) )
		{
			return true;//isCreditCard( cc );
		}
			return (false);
	}
	
	function showDOM()
	{
		for (it in document)
		{
		alert(it);
		}
	}

	function getFileSize(obj)
	{	
		var oas = new ActiveXObject("Scripting.FileSystemObject");
		var e = oas.getFile(obj.value);
		var f = e.size;
		return f;
	}



/****************************************************************/
/****************************************************************/



// whitespace characters
var whitespace = " \t\n\r";


// Check whether string s is empty.
function isEmpty(s)
{ return ((s == null) || (s.length == 0)) }



function isWhitespace (s)
{
var i;

// Is s empty?
if (isEmpty(s)) return true;

// Search through string's characters one by one
// until we find a non-whitespace character.
// When we do, return false; if we don't, return true.

for (i = 0; i < s.length; i++)
{
// Check that current character isn't whitespace.
var c = s.charAt(i);

if (whitespace.indexOf(c) == -1) return false;
}

// All characters are whitespace.
return true;
}

/****************************************************************/

function ForceEntry(val, str) {
var strInput = new String(val.value);

if (isWhitespace(strInput)) {
alert(str);
return false;
} else
return true;

}

/****************************************************************/

function ValidateRanking() {
// This function ensures document.forms[0].nRanking.value >=1 && <= 10

if (parseInt(document.forms[0].nRanking.value) >= 1 && parseInt(document.forms[0].nRanking.value) <=10)
return true;
else
return false;
}

/****************************************************************/

function ValidateData() {
var CanSubmit = false;

// Check to make sure that the full name field is not empty.
CanSubmit = ForceEntry(document.forms[0].txtName,"You supply a full name.");

// Check to make sure ranking is between 1 and 10
if (CanSumbit) CanSubmit = ValidRanking();

return CanSubmit;
}
function isLeadingWhitespace (s)
{

// Is s empty?
if (isEmpty(s)) return true;

// Search through string's characters one by one
// until we find a non-whitespace character.
// When we do, return false; if we don't, return true.
var c = s.charAt(0);
if (c == ' ') return true;           

// All characters are whitespace.
return false;
}

function confirmation(msg) {
	if (confirm(msg)===false) {
		return false;
	}
}

function isValidCreditCard(type, ccnum) {
   if (type == "Visa") {
      // Visa: length 16, prefix 4, dashes optional.
      var re = /^4\d{3}-?\d{4}-?\d{4}-?\d{4}$/;
   } else if (type == "MasterCard") {
      // Mastercard: length 16, prefix 51-55, dashes optional.
      var re = /^5[1-5]\d{2}-?\d{4}-?\d{4}-?\d{4}$/;
   } else if (type == "Discover") {
      // Discover: length 16, prefix 6011, dashes optional.
      var re = /^6011-?\d{4}-?\d{4}-?\d{4}$/;
   } else if (type == "AmEx") {
      // American Express: length 15, prefix 34 or 37.
      var re = /^3[4,7]\d{13}$/;
   } else if (type == "DinersClub") {
      // Diners: length 14, prefix 30, 36, or 38.
      var re = /^3[0,6,8]\d{12}$/;
   }
   if (!re.test(ccnum)) return false;
   // Remove all dashes for the checksum checks to eliminate negative numbers
   ccnum = ccnum.split("-").join("");
   // Checksum ("Mod 10")
   // Add even digits in even length strings or odd digits in odd length strings.
   var checksum = 0;
   for (var i=(2-(ccnum.length % 2)); i<=ccnum.length; i+=2) {
      checksum += parseInt(ccnum.charAt(i-1));
   }
   // Analyze odd digits in even length strings or even digits in odd length strings.
   for (var i=(ccnum.length % 2) + 1; i<ccnum.length; i+=2) {
      var digit = parseInt(ccnum.charAt(i-1)) * 2;
      if (digit < 10) { checksum += digit; } else { checksum += (digit-9); }
   }
   if ((checksum % 10) == 0) return true; else return false;
}
