// JavaScript Document

function isDigit (c){
    return ((c >= '0') && (c <= '9'));}

function isNum (s){
    for (var i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if (!isDigit(c))
		return false;
    }
    return true;
}

function nonBlank (s){
    if (s != null)
        for (var i = 0; i < s.length; i++) {
            var c = s.charAt(i);
            if ((c != ' ') && (c != '\n') && (c != '\t'))
                return true;
        }
    return false;
}

function verifyFilled (field, s){
    if (!nonBlank(s)) {
        if (verifyStrict["Filled"]) {
            alert("Please complete the following field:\n" + field + " ");
            return false;
        } else {
            return confirm("The " + field + " field is empty." + verifyConfirmMsg);
        }
    }
    return true;
}

function verifyNumber (field, n, blank){
    if (!blank && !verifyFilled(field, n))
        return false;

    if (!isNum(n)) {
        if (verifyStrict["Number"]) {
            alert("Value given for " + field + " field is\nnot a number.");
            return false;
        } else {
            return confirm("Value given for " + field + " field (" + n + ") is\nnot a number." + verifyConfirmMsg);
        }
    }
    return true;
}

function verifyDate (field, d, blank){
    var accept = true;

    if (!blank && !verifyFilled(field, d))
        return false;

    if (d.length > 0) {
        accept = (d.length == 10);
        for (var i = 0; i < d.length; i++) {
            var c = d.charAt(i);
            if ((i == 2) || (i == 5)) {
                if (c != '/')
                    accept = false;
            } else  {
                if (!isDigit(c))
                    accept = false;
            }
        }
    }

    if (accept) {
        var month = d.substring(0,2).valueOf();
        var day = d.substring(3,5).valueOf();
        var year = d.substring(6,10).valueOf();
        if ((month < 1) || (month > 12))
            accept = false;
        if ((month == 1) || (month == 3) || (month == 5) || (month == 7) ||
             (month == 8) || (month == 10) || (month == 12)) {
            if ((day < 1) || (day > 31))
               accept = false;
        } else if (month == 2) {
            if ((day < 1) || (day > 29))
               accept = false;
            /* FIX: discern leap/nonleap case */
        } else {
            if ((day < 1) || (day > 30))
                accept = false;
        }
        if (year < 1910)  /* arbitrary */
            accept = false;
    }

    if (!accept) {
        if (verifyStrict["Date"])
            alert("Value given in " + field + " field does not\nhave a valid form (MM/DD/YYYY).\nPlease use zero as necessary.");
        else
            accept = confirm("Value given in " + field + " field (" + d + ") does not\nhave a valid form (MM/DD/YYYY).\nPlease use zero as necessary." + verifyConfirmMsg);
    }
    return accept;
}

function verifyTime (field, t, blank){
    var accept = true;

    if (!blank && !verifyFilled(field, t))
        return false;

    if (t.length > 0) {
        accept = (t.length == 5);
        for (var i = 0; i < t.length; i++) {
            var c = d.charAt(i);
            if (i == 2) {
                if (c != ':')
                    accept = false;
            } else  {
                if (!isDigit(c))
                    accept = false;
            }
        }
    }

    if (accept) {
        var hr = t.substring(0,2).valueOf();
        var min = t.substring(3,5).valueOf();
        if ((hr < 0) || (hr > 23))
            accept = false;
        if ((min < 0) || (min > 59))
            accept = false;
    }

    if (!accept) {
        if (verifyStrict["Time"])
            alert("Value given in " + field + " field does not\nhave a valid form (HH:MM).\nPlease use 24-hour format.");
        else
            accept = confirm("Value given in " + field + " field (" + t + ") does not\nhave a valid form (HH:MM).\nPlease use 24-hour format." + verifyConfirmMsg);
    }

    return accept;
}

function verifyTelephone (field, p, blank)
{
    var accept = true;
    if (!blank && !verifyFilled(field, p))
        return false;
    if (p.length > 0) {
        accept = (p.length == 12);
        for (var i = 0; i < p.length; i++) {
            var c = p.charAt(i);
            if ((i == 3) || (i == 7)) {
                if (c != '-')
                    accept = false;
            } else  {
                if (!isDigit(c))
                    accept = false;
            }
        }
    }

    if (!accept) {
        if (verifyStrict["Telephone"])
            alert("Value given in " + field + " field does not\nhave a valid form (NNN-NNN-NNNN).");
        else
            accept = confirm("Value given in " + field + " field (" + p + ") does not\nhave a valid form (NNN-NNN-NNNN)." + verifyConfirmMsg);
    }
    return accept;
}

function verifyState (field, s, blank){
    var accept = true;

    if (!blank && !verifyFilled(field, s))
        return false;

    if (s.length > 0) {
        accept = (s.length == 2);
        for (i = 0;  i < s.length; i++) {
            var c = s.charAt(i);
            if ((c < 'A') || (c > 'Z')) {
                accept = false;
                break;
            }
        }
    }
    if (!accept) {
        if (verifyStrict["State"])
            alert("Value given in " + field + " field is not a valid\nUS state (make sure you are using capitals).");
        else
            accept = confirm("Value given in " + field + " field (" + s + ") is not a valid\nUS state (make sure you are using capitals)." + verifyConfirmMsg);
    }
    return accept;
}

function verifyZipcode  (field, z, blank){
    if (!blank && !verifyFilled(field, z))
        return false;

    if ((z.length > 0) && !((z.length == 5) && isNum(z))) {
        if (verifyStrict["Zipcode"]) {
            alert("The value given in the " + field + " field\nis not valid.");
            return false;
        } else {
            return confirm("The value given in the " + field + " field (" + z + ")\nis not valid." + verifyConfirmMsg);
        }
    }
    return true;
}

function verifyEmail (field, e, blank){
    var accept = true;

    if (!blank && !verifyFilled(field, e))
        return false;

	accept = echeck(e);

    if (!accept) {
        if (verifyStrict["email"])
            alert("Value given in " + field + " field is not a valid\nE-mail address (user@host.domain).\n" + e + "\n!");
        else
            accept = confirm("Value given in " + field + " field (" + e + ") is not a valid\nE-mail address (user@host.domain)." + verifyConfirmMsg);
    }
    return accept;
}

function echeck(str) {
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   /* alert("Invalid E-mail ID") */
		   return false
		}
		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   /* alert("Invalid E-mail ID") */
		   return false
		}
		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}
		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }
		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }
		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		 if (str.indexOf(" ")!=-1){
		    return false
		 }
 		 return true					
	}
	
function verifyButton (formobj, field, e, blank) {
	var fieldRequired = Array(e);
	// field description to appear in the dialog box
	var fieldDescription = Array(field);
	// dialog message
	var alertMsg = "Please complete the following fields:\n";
	var l_Msg = alertMsg.length;
	
	for (var i = 0; i < fieldRequired.length; i++){
		var obj = formobj.elements[fieldRequired[i]];
		if (obj){
			if (obj.type == null){
				var blnchecked = false;
				for (var j = 0; j < obj.length; j++){
					if (obj[j].checked){
						blnchecked = true;
					}
				}
				if (!blnchecked){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				continue;
			}
			switch(obj.type){
			case "select-one":
				if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "select-multiple":
				if (obj.selectedIndex == -1){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "text":
			case "textarea":
				if (obj.value == "" || obj.value == null){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			default:
			}
		}
	}
	if (alertMsg.length == l_Msg){
		return true;
	}else{
		alert(alertMsg);
		return false;
	}
}

	
function verifyTitle (field, e, blank){
	var accept = true;
	if (!document.sendform.title[0].checked &&
    	!document.sendform.title[1].checked &&
    	!document.sendform.title[2].checked) {
    	accept = false;
    }
	if (!accept) {
        if (verifyStrict["Title"])
            alert("Please choose a " + field + " .");
        else
            accept = confirm("You did not choose a title " + field + " ." + verifyConfirmMsg);
    }
	if (document.sendform.title[0].checked) {
			document.sendform.title1.value = "Mr"
			}
	if (document.sendform.title[1].checked) {
			document.sendform.title1.value = "Mrs"
			}
	if (document.sendform.title[2].checked) {
			document.sendform.title1.value = "Ms"
			}
	return accept;
}

/**********************************************************************/ 
/*Function name :isDigit(theDigit) */ 
/*Usage of this function :test for an digit */ 
/*Input parameter required:thedata=string for test whether is digit */ 
/*Return value :if is digit,return true */ 
/* else return false */ 
/**********************************************************************/ 
function isDigit2(theDigit) 
{ 
var digitArray = new Array('0','1','2','3','4','5','6','7','8','9'),j; 

for (j = 0; j < digitArray.length; j++) 
{if (theDigit == digitArray[j]) 
return true 
} 
return false 

} 
/*************************************************************************/ 
/*Function name :isPositiveInteger(theString) */ 
/*Usage of this function :test for an +ve integer */ 
/*Input parameter required:thedata=string for test whether is +ve integer*/ 
/*Return value :if is +ve integer,return true */ 
/* else return false */ 
/*function require :isDigit */ 
/*************************************************************************/ 
function isPositiveInteger(theString) 
{ 
var theData = new String(theString) 

if (!isDigit(theData.charAt(0))) 
if (!(theData.charAt(0)== '+')) 
return false 

for (var i = 1; i < theData.length; i++) 
	if (!isDigit(theData.charAt(i))) 
return false 
return true 
} 
/**********************************************************************/ 
/*Function name :isDate(s,f) */ 
/*Usage of this function :To check s is a valid format */ 
/*Input parameter required:s=input string */ 
/* f=input string format */ 
/* =1,in mm/dd/yyyy format */ 
/* else in dd/mm/yyyy */ 
/*Return value :if is a valid date return 1 */ 
/* else return 0 */ 
/*Function required :isPositiveInteger() */ 
/**********************************************************************/ 
function isDate(s,f) 
{var a1=s.split("/"); 
var a2=s.split("-"); 
var e=true; 
if ((a1.length!=3) && (a2.length!=3)) 
{ 
e=false; 
} 
else 
{if (a1.length==3) 
var na=a1; 
if (a2.length==3) 
var na=a2; 
if (isPositiveInteger(na[0]) && isPositiveInteger(na[1]) && isPositiveInteger(na[2])) 
{ if (f==1) 
{var d=na[1],m=na[0]; 
} 
else 
{var d=na[0],m=na[1]; 
} 
var y=na[2]; 
if (((e) && (y<1000)||y.length>4)) 
e=false 
if (e) 
{ 
v=new Date(m+"/"+d+"/"+y); 
if (v.getMonth()!=m-1) 
e=false; 
} 
} 
else 
{ 
e=false; 
} 
} 
return e 
} 

function CheckDate(v)
{ 
var s=v.a.value; 
if (isDate(s,0)) //dd/mm/yyyy format 
	alert("The inputted date value is valid!"); 
else 
	alert("The inputted date value is not valid!"); 
return false; 
} 