var digits = "0123456789";
// whitespace characters
var whitespace = " \t\n\r";

// non-digit characters which are allowed in 
// Social Security Numbers
var SSNDelimiters = "- ";

// characters which are allowed in Social Security Numbers
var validSSNChars = digits + SSNDelimiters;

// U.S. Social Security Numbers have 9 digits.
// They are formatted as 123-45-6789.
var digitsInSocialSecurityNumber = 4;
var digitsInME = 5;

function EditClass(dtTimeID){
if (dtTimeID != 0){
	var scWidth=650
	var scHeight=370

	var strOptions='dialogWidth=' + scWidth + 'px;dialogHeight=' + scHeight + 'px;help=no;status=no;center=yes'
	var strUrl = '../Classes/EditClassWindow.asp?ClassID=' + dtTimeID

	var strID=window.showModalDialog(strUrl,"ModalWindow",strOptions); 
	if (strID=1){
		document.location.reload();	
		}
	}
}

function OpenWindowX(strURL,lgSize){

	var lgIndex=lgSize.indexOf('x')
	var scWidth=lgSize.slice(0,lgIndex)
	var scHeight=lgSize.slice(lgIndex+1,lgSize.length)
	sx=screen.availWidth-30
	sy=screen.availHeight-30
	if (sx>1270){
		sx=1270
		}
	var posTop=(sy-scHeight)/2
	var posLeft=(sx-scWidth)/2
	var strOptions=',,status=no,,,scrollbars=no,resizable=no,width=' + scWidth + ',height=' + scHeight + ',top=' + posTop + ',left=' + posLeft

	window.open(strURL,"child2",strOptions); 
	//return true;
}

function OpenForPrint(strURL){

	sx=screen.availWidth
	sy=screen.availHeight
	if (sx>1280){
		sx=1280
		}
	var strOptions=',,status=no,menubar=yes,scrollbars,toolbar=yes,resizable=yes,width=' + sx + ',height=' + sy + ',top=0,left=0'

	window.open(strURL,"child3",strOptions); 
	//return true;
}

function window_onblur(src) {
var x=window.event.clientX;
var y=window.event.clientY;
var docH=document.documentElement.offsetHeight
var docW=document.documentElement.offsetWidth
if ((x<0) || (x>docW) || (y<0) || (y>docH)){
	src.focus()
	}
}

function OpenCal(strField,strFrame){

	var scWidth=170;
	var scHeight=170;
	var posTop=window.event.screenY - scHeight/2;
	var posLeft=window.event.screenX - scWidth/2;
	scWidth=scWidth + 'px';
	scHeight=scHeight + 'px';	
	var strOptions=',,status=no,,,scrollbars=no,resizable=no,width=' + scWidth + ',height=' + scHeight + ',top=' + posTop + ',left=' + posLeft
	var strDate=document.forms[0][strField].value
	if (isDate(strDate)){
		var tmpDate=strDate;
		var lgLength=tmpDate.length;
		var lgPos2 = tmpDate.lastIndexOf("/");
		var lgPos1 = tmpDate.indexOf("/");
		if(lgPos1 == -1){
			var lgPos2 = tmpDate.lastIndexOf("-");
			var lgPos1 = tmpDate.indexOf("-");
		}
		if(lgPos1 == -1){
		}
		var year=tmpDate.substring((lgPos2+1),lgLength);
		var day=tmpDate.slice((lgPos1+1),lgPos2);
		var month=tmpDate.substring(0,lgPos1);
		strDate='&Month=' + month + '&Year=' + year
	}
	else{
		strDate=''
	}
	var strURL='../Common/SmallCal.asp?FieldName=' + strField + strDate
	window.open(strURL,"child2",strOptions); 
}

function PickDate(strField){
	var strDate=document.forms[0][strField].value
	var scWidth=176;
	var scHeight=200;
	var posTop=window.event.screenY;
	var posLeft=window.event.screenX;
	scWidth=scWidth + 'px';
	scHeight=scHeight + 'px';	
	posTop=posTop + 'px';
	posLeft=posLeft + 'px';
	strURL='../Common/SmallCal.htm'
	var strOptions='center=off;dialogTop=' + posTop + ';dialogLeft=' + posLeft + ';dialogWidth=' + scWidth + ';dialogHeight=' + scHeight + ';help=no;status=no'
	//alert(strOptions);
	var strDate=window.showModalDialog(strURL,strDate,strOptions); 
	if(typeof(strDate) != "undefined"){
		document.forms[0][strField].value=strDate
		return strDate;
	}
	else{
		return -1;
	}
	
}

function GetClientID(strURL,lgSize){

	var lgIndex=lgSize.indexOf('x')
	var scWidth=lgSize.slice(0,lgIndex)
	var scHeight=lgSize.slice(lgIndex+1,lgSize.length)
	sx=screen.availWidth
	sy=screen.availHeight
	if (sx>1280){
		sx=1280
		}
	var posTop=(sy-scHeight)/2
	var posLeft=(sx-scWidth)/2
	var strOptions='dialogWidth=' + scWidth + 'px;dialogHeight=' + scHeight + 'px;help=no;status=no;center=yes'
	var strID=window.showModalDialog(strURL,"ModalWindow",strOptions); 
	if(strID != -1){
		document.form1.ClID.value=strID
		//document.form1.submit()
		document.ClientFrame.location = '../Clients/ClientInfoFrame.asp?ClID=' + strID;
		return true;
	}
	else{
		return false;
	}
}

function FindClientID(FieldName,FrameName){
//Opens Dialog to find client
	var strOptions='dialogWidth=765px;dialogHeight=350px;help=no;status=no;center=yes;scroll=no;resizable=no'
	var strID=window.showModalDialog('../Clients/FindClientWindow.asp',"ModalWindow",strOptions); 
	if(strID != -1){
		document.form1[FieldName].value=strID
		document.frames[FrameName].location = '../Clients/ClientInfoFrame.asp?ClID=' + strID;
		return true;
	}
	else{
		return false;
	}
}

function FindProID(FieldName,FrameName){
//Opens Dialog to find Professor
	var strOptions='dialogWidth=690px;dialogHeight=350px;help=no;status=no;center=yes;scroll=no;resizable=no'
	var strID=window.showModalDialog('../Professors/FindProfessorWindow.asp',"ModalWindow",strOptions); 
	if(strID != -1){
		document.form1[FieldName].value=strID
		document.frames[FrameName].location = '../Professors/ProfessorInfoFrame.asp?ProID=' + strID;
		return strID;
	}
	else{
		return -1;
	}
}

function ClearProID(FieldName,FrameName){
//Clears Professor ID
		document.form1[FieldName].value=""
		document.frames[FrameName].location = '../Common/Blank.htm';
}

function NewClientID(FieldName,FrameName){
	var strOptions='dialogWidth=640px;dialogHeight=440px;help=no;status=no;center=yes;scroll=no;resizable=no'
	var ClID=form1[FieldName].value
	var strQueryString=''
	if (!isNaN(ClID) & ClID != -1){
		strQueryString='?ClientID=' + ClID
	}
	var strID=window.showModalDialog('../Clients/ClientEditWindow.asp' + strQueryString,"ModalWindow",strOptions);
	if(strID != -1 & FieldName !='' & FrameName!= ''){
		document.form1[FieldName].value=strID
		document.frames[FrameName].location = '../Clients/ClientInfoFrame.asp?ClID=' + strID;
		return true;
	}
	else{
		return false;
	}
}

//Is a valid Social Security Number
function isSSN (s)
{   if (isEmpty(s)) 
       if (isSSN.arguments.length == 1) return false;
       else return (isSSN.arguments[1] == true);
	s=stripAlphas(s);
    return (isInteger(s) && ((s.length == digitsInME) || s.length == digitsInSocialSecurityNumber))
}

//clean up all text fields in a form
function cleanFields(){
	for(var i = 0; i < document.forms[0].length; i++) {
        var e = document.forms[0].elements[i];
        if (((e.type == "text") || (e.type == "textarea"))) {
            e.value = "";
        }
	}
}	

//check if the field value is present
function isPresent(fd){
	if ((fd == null) || (fd == "") || (isblank(fd))){
		return false;
	}
	else{
		return true;
	}
}

//check if the field was filled with blank spaces
function isblank(s){
    for(var i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
    }
    return true;
}

//check for valid email address
function isEmail(s)
{   if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return false;
       else return (isEmail.arguments[1] == true);
   
    if (isWhitespace(s)) return false;
    
    var i = 1;
    var sLength = s.length;

    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}

function isDate(dateVar) {
	var tmpDate=dateVar;
	var lgLength=tmpDate.length;
	var lgPos2 = tmpDate.lastIndexOf("/");
	var lgPos1 = tmpDate.indexOf("/");
	if(lgPos1 == -1){
		var lgPos2 = tmpDate.lastIndexOf("-");
		var lgPos1 = tmpDate.indexOf("-");
	}
	if(lgPos1 == -1){
		return false;
	}
	var year=tmpDate.substring((lgPos2+1),lgLength);
	var day=tmpDate.slice((lgPos1+1),lgPos2);
	var month=tmpDate.substring(0,lgPos1);
    if (! (isYear(year, false) && isMonth(month, false) && isDay(day, false))){
		return false;
	}
	var daysInMonth = new Array(12);
	daysInMonth[1] = 31;
	daysInMonth[2] = daysInFebruary (year);
	daysInMonth[3] = 31;
	daysInMonth[4] = 30;
	daysInMonth[5] = 31;
	daysInMonth[6] = 30;
	daysInMonth[7] = 31;
	daysInMonth[8] = 31;
	daysInMonth[9] = 30;
	daysInMonth[10] = 31;
	daysInMonth[11] = 30;
	daysInMonth[12] = 31;
	
    var intYear = parseInt(year);
    var intMonth = parseInt(month);
    var intDay = parseInt(day);

    if (intDay > daysInMonth[intMonth]) return false; 

    if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) return false;

    return true;
}

function isTime(strTime) {
	var temp=strTime;

	var lgLength=temp.length;
	var lgPos1 = temp.indexOf(":");
		if (lgPos1==-1){
			return false;
		}	

		var strHour=(temp.substring(0,lgPos1));
		if (!isIntegerInRange(strHour, 0, 23)){
			return false;
		}
		var strMinutes=(temp.substring((lgPos1+1),lgLength));
		if (!(isIntegerInRange(strMinutes, 0, 59) && (strMinutes.length)==2)){
			return false;
		
		}
   return true;
}

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 isYear (s)
{   if (isEmpty(s)) 
       if (isYear.arguments.length == 1) return false;
       else return (isYear.arguments[1] == true);
    if (!isNonnegativeInteger(s)) return false;
    return ((s.length == 2) || (s.length == 4));
}

function isMonth (s)
{   if (isEmpty(s)) 
       if (isMonth.arguments.length == 1) return false;
       else return (isMonth.arguments[1] == true);
    return isIntegerInRange (s, 1, 12);
}

function isDay (s)
{   if (isEmpty(s)) 
       if (isDay.arguments.length == 1) return false;
       else return (isDay.arguments[1] == true);   
    return isIntegerInRange (s, 1, 31);
}
function isWhitespace(s){
    var i;
    if (isEmpty(s)) 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;
    }
    return true;
}

//check for valid credit card number
function isCreditCard(st) {
  if (st.length > 19)
    return (false);

  sum = 0; mul = 1; l = st.length;
  for (i = 0; i < l; i++) {
    digit = st.substring(l-i-1,l-i);
    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)
    return (true);
  else
    return (false);
}

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}



function isInteger (s)

{   var i;

    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isInteger.arguments[1] == true);
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);

        if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}

function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}

// Removes all characters which appear in string strBag from string strWord.
function CleanString(strWord, strBag)
{   var i;
    var returnString = "";

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function isNonnegativeInteger (s)
{   var secondArg = false;

    if (isNonnegativeInteger.arguments.length > 1)
        secondArg = isNonnegativeInteger.arguments[1];
    return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s) >= 0) ) );
}

function isSignedInteger (s){
   if (isEmpty(s)) 
       if (isSignedInteger.arguments.length == 1) return false;
       else return (isSignedInteger.arguments[1] == true);
    else {
        var startPos = 0;
        var secondArg = false;
        if (isSignedInteger.arguments.length > 1)
            secondArg = isSignedInteger.arguments[1];
        if ( (s.charAt(0) == "-") || (s.charAt(0) == "+") )
           startPos = 1;    
        return (isInteger(s.substring(startPos, s.length), secondArg))
    }
}

//Form validation function
function FormCheck(){
	var Alen=fElem.length
	for (i = 0; i < Alen; i++){
		var strField=fElem[i];
		var lgLength = strField.length;
		var lgPos1 = strField.indexOf(";");
		var lgPos2 = strField.indexOf(";",(lgPos1+1));
		var lgPos3 = strField.indexOf(";",(lgPos2+1));
		var lgPos4 = strField.indexOf(";",(lgPos3+1));
		if (lgPos4 != -1){
			var Fparameter=',' + strField.substring((lgPos4+1),lgLength);
		}
		else{
			var Fparameter=''
			lgPos4=lgLength
		}		
	
		var Ffield=strField.substring(0,lgPos1);
		var Ffunction=strField.substring((lgPos2+1),lgPos3);
		var Frequired=strField.substring((lgPos3+1),lgPos4);
		var Flabel=strField.slice((lgPos1+1),lgPos2);
		var Fvalue=document.forms[0][Ffield].value;

		var strEval= Ffunction + '("' + Fvalue+ '"' + Fparameter + ')';
		if(!(Frequired==0 && Fvalue=='')){
			if (eval(strEval) == false){
				var strType=document.forms[0][Ffield].type
				if (strType != 'hidden') document.forms[0][Ffield].focus();
				strMessage='Please enter a valid "' + Flabel + '" before proceeding';
				alert(strMessage);
				return false;
			}
		}
	} 
	return true;
}

function mOvr(src){
	src.style.cursor = 'hand';
	src.style.backgroundColor = 'Silver';
}

function mOut(src){
	  src.style.cursor = 'default';
	  src.style.backgroundColor = 'White';
}

function mHand(src){
	src.style.cursor = 'hand';
}

function mNoHand(src){
	  src.style.cursor = 'default';
}

function isIntegerInRange (s, a, b)
{  
	if (!isPresent(s)){
		return false;
	}
    if (!isInteger(s, false)) return false;

    var num = parseInt (s);
    return ((num >= a) && (num <= b));
}

function isNumberInRange (s, a, b)
{  
	if (!isPresent(s)){
		return false;
	}
    if (isNaN(s)) return false;

    var num = s;
    return ((num >= a) && (num <= b));
}

function formatCurrency(s){

s = Math.round(s*100)/100;

sValue = s.toString();

	if (sValue.indexOf(".") == -1){
	    sValue = sValue + ".00";
	}
	else{
		if (sValue.indexOf(".") == sValue.length - 1){
			sValue = strBalance + "00";
		}else if (sValue.indexOf(".") == sValue.length - 2){
			sValue = sValue + "0";
		}
	}
	    
	//  Add commas if necessary
	if (sValue.indexOf(".") > 3){
	    sValue = sValue.substring(0,sValue.indexOf(".") - 3) + ","
	        + sValue.substring(sValue.indexOf(".") - 3,sValue.length);
	}

	sValue = "$" + sValue;
	return sValue;

}

//Remove all non numeric characters from string
function stripAlphas(s){
	return stripCharsNotInBag (s, digits)
}

// Removes all characters which do NOT appear in string bag 
// from string s.

function stripCharsNotInBag (s, bag)

{   var i;
    var returnString = "";

    // Search through string's characters one by one.
    // If character is in bag, append to returnString.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) != -1) returnString += c;
    }

    return returnString;
}
