//Various string-checking functions.

//Usage Example
//
//
//<SCRIPT language="JavaScript" src = 
//	"validate.js">
//</SCRIPT>
//
//<SCRIPT>
//function checkform(form) {
//return (validateString(form.firstname, "first name") &&
//	validateString(form.lastname, "last name") &&
//	validateMemNum(form.membernum, "member number") &&
//	validateState(form.state) && 
//	validateZip(form.zip) &&
//	validatePledge(form.pledge))
//}
//</SCRIPT>
//
//<INPUT type = "submit" value = "Submit"..//	onclick = "return checkform(this.form)">
//


function notNull(str) {
	if (str.length == 0 )
		return false
	else 
		return true
}

function notBlank(str) 
{
	for (i = 0; i < str.length; i++) {
		if (str.charAt(i) != " ")
			return true
	}
	return false
}

function isSize(str, size) {
	if (str.length == size) 
		return true
	else
		return false
}


//Validation functions for numerical data.

function isDigits(str) {
	var i
	for (i = 0; i < str.length; i++) {
		mychar = str.charAt(i)
		if (mychar < "0" || mychar > "9")
			return false
	}
	return true
}

function isNumber(str) {
	numdecs = 0
	for (i = 0; i < str.length; i++) {
		mychar = str.charAt(i)
		if ((mychar >= "0" && mychar <= "9") || mychar 
			== ".") {
			if (mychar == ".")
				numdecs++
		}
		else 
			return false
	}

	if (numdecs > 1)
		return false;
		
	if ( str.length == 0)
		return false;
	
return true
}

function isInRange(str, num1, num2) {
	var i = parseInt(str)
	return((i >= num1) && (i <= num2))

}


//Function to strip all non-digits from a string.

function stripNonDigits(str) {
	var i
	var newstring = ""
	for (i = 0;  i < str.length; i++) {
		mychar = str.charAt(i)
		if (isDigits(mychar)) 
			newstring += mychar
	}
	return newstring
}

function stripChars(str, chars) {
	var i
	var newstring = ""
	for (i = 0;  i < str.length; i++) {
		mychar = str.charAt(i)
		if (chars.indexOf(mychar) == -1)
			newstring += mychar
	}
	return newstring
}



function isPhoneNumber(str) {
	
	numplus = 0;
	for (i = 0; i < str.length; i++) {
		mychar = str.charAt(i)
				
		if ((mychar >= "0" && mychar <= "9") || mychar == "+") {
			if (mychar == "+")
				numplus=i;
		}
		else 
		{
			alert("Phone number is not in correct format.");
			return false;
		}
	}

	if (numplus > 0)
	{
		alert("'+' can only be at beginning of number");
		return false;
	}	

	if ( (str.length) != 11  )
	{
		alert("Phone number should be of 11 digits including + sign");
		return false;
	}
	
return true;
}

//Code to validate a string.
//Global variable set at start of script

var emptyString = " field is blank. Please enter a "
var emptyNumeric = " field is invalid numeric. Please enter valid " 


function CheckStringOnly(myfield) {
	if (notNull(myfield.value)&& notBlank(myfield.value)) 
		return true
	else 
		return false
	
}


function validateString(myfield, s) {
	if (notNull(myfield.value)&& notBlank(myfield.value)) 
		return true
	else {
		myfield.focus()
		alert("The " + s + emptyString + s)
		return false
	}
}

function validateString1(myfield, s) {
	if (notNull(myfield)&& notBlank(myfield)) 
		return true
	else {
		myfield.focus()
		alert("The " + s + emptyString + s)
		return false
	}
}


function validatePhone(myfield, s) {
	var str = myfield.value;
	
	if ( str.length == 0)
	{
		alert("The " + s + emptyString + s);
		return false;
	}


	if (isPhoneNumber(myfield.value) ) 
		return true;
	else {
		myfield.focus();
		return false;
	}
}

//Functions for validating numerical input.

function validateNumber(myfield, s) {
	if (isNumber(myfield.value) ) //&& isInRange(myfield.value,0, 999999))
		return true

	else {
		myfield.focus()
		//alert("The " + s + emptyString + s)
		alert("The " + s + emptyNumeric + s)
		return false
	}
}

function validateAmount(myfield) {
	if (notNull(myfield.value)) {
		newstring = stripChars(myfield.value, "$")
		if  (isNumber(newstring))
			return true
		else {
			myfield.focus()
			alert("Invalid amount. Please enter a valid dollar amount.")
		}
	}
	return false
}

//Code to validate state and ZIP input.

var STATECODES = "AL/AK/AZ/AR/CA/CO/CT/DE/DC/FL/GA/HI/ID/IL/IN/IA/KS/LA/ME/MD/MA/MI/MN/MS/MO/MT/NV/NH/NJ/NM/NY/NC/ND/OH/OK/OR/PA/PR/RI/SC/TN/TX/UT/VT/VA/WA/WV/WI/WY"

function isStateCode (str) {
	var newstring = str.toUpperCase()
	if (STATECODES.indexOf(newstring) != -1 && str.indexOf("../") == -1)
		return true
	else 
		return false
}

function validateState(myfield) {
	if (notNull(myfield.value) && isSize(myfield.value, 2) && isStateCode(myfield.value))
		return true
	else {
		myfield.focus()
		alert("Invalid state code. Please enter 2-letter state postal abbreviation.")
		return false
	}
}

function validateZip(myfield) {
	if (notNull(myfield.value)) {
		newstring = stripNonDigits(myfield.value)
		if (isSize(newstring,5) || isSize(newstring, 9)) 
			return true
	}
	myfield.focus()
	alert("Invalid zip code. Please enter 5-digit or 9-digit zip code.")
	return false
}

function validateEmail (myfield, s) {
	
	if(!validateString(myfield, s))
		return false
	var newstring = myfield.value
	if (newstring.indexOf("@") != -1 && newstring.indexOf(".") != -1 && newstring.charAt(0) != "@" && newstring.charAt(newstring.length) != ".")
		return true
	else 
	{ 
		myfield.focus()
		alert("Invalid email address. Please enter correct email address.")
		return false
	}
}

function validatePassword (password1, s1, password2, s2) {
	
	if(!validateString(password1, s1))
		return false
		
	if(!validateString(password2, s2))
		return false
	
	if (password1.value == password2.value)
		return true
	else
	{	
		password1.focus()
		alert("Your password does not match. Please enter same password twice")
		return false
	}
}

function validateBothEmail(Email1, s1, Email2, s2) {
	
	if(!validateEmail(Email1, s1))
		return false
		
	if(!validateEmail(Email2, s2))
		return false
	
	if (Email1.value == Email2.value)
		return true
	else
	{	
		Email1.focus()
		alert("Your email does not match. Please enter same email twice")
		return false
	}
}


function validateList(mylist, s) {
	
	if (mylist.selectedIndex != 0)
		return true
	else
	{	
		mylist.focus()
		  alert("The " + s + " has not been selected. Please select " + s)
		return false
	}
}


function validateRadios(myfield,s)
 { 
  var checked = false; 
  
   for (var i = 0; i < myfield.length; i++) 
  {
  if (myfield[i].checked) 
  checked = true; 
  }	
  
  if (checked==false)
   { 
   alert("The " + s + " has not been selected. Please select " + s);
   return false; 
  } 
  return true; 
 } 


function validateCheckBoxes(myfield,s)
 { 
  var checked = false; 
  

  for (var i = 0; i < myfield.length; i++) 
   if (myfield[i].checked) 
    		checked = true; 
	
	
  if (!checked)
   { 
   alert("No " + s + " has not been selected. Please select atleast one " + s);
   return false; 
  } 
  return true; 
 } 




function comparetimes(myfield1,myfield2)
{
value1=myfield1.value.split(":");
value2=myfield2.value.split(":");

var time1 =value1[0]; 
var time2 =value2[0]; 
var time3 =value1[1]; 
var time4 =value2[1]; 
//alert (time1);
//alert (time2);
//alert (time3);
//alert (time4);
		if (time1>time2)
		 { 
			alert('End time cannot be before Start time.'); 
			myfield2.focus();
			return false;
		} 
		
		if (time1==time2 && time3==time4)
		 { 
			alert('Start Time & End Time are equal.'); 
			myfield2.focus();
			return false;
		} 
		
			return true;
			
			
}


function comparevalues(myfiled1,s1,myfiled2,s2)
{
//alert('in');
   // if(!validateString(myfiled1, s1))
	//	return false
		
//	if(!validateString(myfiled2, s2))
	//	return false
	//alert(myfiled1);
	//alert(myfiled2);
	//alert(myfiled1.value);
	//alert(myfiled2.value);
	if (myfiled1.value>myfiled2.value)
		{
		//alert(myfiled1.value);
			myfiled2.focus();
			alert(s2+' cannot be less than '+s1);
			return false;
		}
		return true
	
}


function validateDateFormat(myfield1)
{
	value1=myfield1.value.split("-");
		if (value1.count=0)
		{
					value1=myfield1.value.split("/");
					if (value1.count=0)
					{
						alert("Date must be in dd/mm/yyyy or dd-mm-yyyy format");
						myfield1.focus();
						return false;
					}
					
		} 	
		return true;
}
function comparedates(myfield1,myfield2)
{
//alert(myfield1.value);
//alert(myfield2.value);
value1=myfield1.value.split("-");
value2=myfield2.value.split("-");


var date1 =value1[0]; 
var date2 =value2[0]; 
var date3 =value1[1]; 
var date4 =value2[1]; 
var date5 =value1[2]; 
var date6 =value2[2]; 
//alert(value1[0]+value1[1]+value1[2]);
//alert(value2[0]+value2[1]+value2[2]);
//alert (time1);
//alert (time2);
//alert (time3);
//alert (time4);
		/*if (date1==date2 && date3==date4 && date5==date6 )
		 { 
			alert('Start Date & End Date are equal.'); 
			myfield2.focus();
			return false;
		} 
		*/
		if (date5>date6)
		 { 
			alert('End date cannot be before Start date.'); 
			myfield2.focus();
			return false;
		} 
		
		if (date1>date2)
		 { 
			alert('End date cannot be before Start date.'); 
			myfield2.focus();
			return false;
		}
		if (date3>date4)
		 { 
			alert('End date cannot be before Start date.'); 
			myfield2.focus();
			return false;
		} 
		 
		
			return true;
			
			
}


	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(pos1+1,pos2)
	            var strDay=dtStr.substring(0,pos1)
	            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 : dd-mm-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
            }

            function CheckDate(obj){
	            var dt=obj
	            if (isDate(dt.value)==false){
		            dt.focus()
		            return false
	            }
                return true
            }
