var ranNum = Math.round(Math.random()*100000000000000000);
var digits = "0123456789";
var phoneNumberDelimiters = "()- ";
var validWorldPhoneChars = phoneNumberDelimiters + "+";
var minDigitsInIPhoneNumber = 10;
var dtCh= "/";
var minYear=1900;
var myDate = new Date();
maxYear = (myDate.getFullYear() - 13);

function get_random17Integers() {
    var ranNum= Math.round(Math.random()*100000000000000000);
    return ranNum;
}

function ValidateHebrewName( name ) {
	if ((name.toLowerCase().indexOf(" ben ") > -1) || (name.toLowerCase().indexOf(" bas ") > -1) || (name.toLowerCase().indexOf(" bat ") > -1) ) { return true; }
	else { return false; }
}

function ValidateDate() {
	var dt=document.frmRegister.TR_Birthday
	if (isDate(dt.value)==false) {
		dt.focus();
		return false;
	}
	return true;
}

function checkInternationalPhone(strPhone) {
	s=stripCharsInBag(strPhone,validWorldPhoneChars);
	return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

function ValidatePhone() {
	if ((document.frmRegister.TR_Phone.value==null)||(document.frmRegister.TR_Phone.value=="")) {
		document.frmRegister.TR_Phone.focus();
		return false
	}
	if (checkInternationalPhone(document.frmRegister.TR_Phone.value)==false) {
		document.frmRegister.TR_Phone.value="";
		document.frmRegister.TR_Phone.focus();
		return false
	}
	return true;
}
            
function isInteger(s) {
	var i;
	for (i = 0; i < s.length; i++){   
		var c = s.charAt(i);
		if (((c < "0") || (c > "9"))) return false;
	}
        return true;
}

function stripCharsInBag(s, bag) {
	var i;
	var 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) {
	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) {
	// The date format should be : mm/dd/yyyy
		return false;
	}
	if (strMonth.length<1 || month<1 || month>12) {
        // Please enter a valid month
		return false;
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]) {
	// Please enter a valid day
		return false;
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear) {
        // 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) {
	// The date format should be : mm/dd/yyyy
		return false
	}
	return true
}

function isValidEmail(email, required) {
    if (required==undefined) {   // if not specified, assume it's required
        required=true;
    }
    if (email==null) {
        if (required) {
            return false;
        }
        return true;
    }
    if (email.length==0) {
        if (required) {
            return false;
        }
        return true;
    }
    if (!allValidChars(email)) {  // check to make sure all characters are valid
        return false;
    }
    if (email.indexOf("@") < 1) { //  must contain @, and it must not be the first character
        return false;
    } else if (email.lastIndexOf(".") <= email.indexOf("@")) {  // last dot must be after the @
        return false;
    } else if (email.indexOf("@") == email.length) {  // @ must not be the last character
        return false;
    } else if (email.indexOf("..") >=0) { // two periods in a row is not valid
	return false;
    } else if (email.indexOf(".") == email.length) {  // . must not be the last character
	return false;
    }
    return true;
}

function allValidChars(email) {
	var parsed = true;
	var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
	for (var i=0; i < email.length; i++) {
	var letter = email.charAt(i).toLowerCase();
	if (validchars.indexOf(letter) != -1)
		continue;
		parsed = false;
		break;
	}
	return parsed;
}

function Objlostfocus(obj, message) {
	if(obj.value=="" || obj.value==null) obj.value = message;
}

function Objonfocus(obj, message) {
    if (obj.value == message) obj.value="";
}
