﻿
var emptyString = " field is blank. Please enter a "


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 validateInteger(control,msg)
{
	var x =control.value;
	var filter =/^[1-9][0-9]*$/;
	if (!filter.test(x)) 
		{
			alert("The " + msg + emptyString + msg)
			control.focus();
			return false;
		}
	else
		return true;
}

function validateDropdown(control,msg)
{
	var x = control.value;
	if(x == "0"){
	    alert("please select " + msg)
	    return false;   
	}else{
	    return true;
	}
}


//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
}



//Code to validate a string.

//Global variable set at start of script



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
	}
}



//Functions for validating numerical input.

function validateNumber(control, msg) {
	var x = control.value;
	var filter  = /^[0-9]+?$/;
	if (!filter.test(x)) 
		{
			alert(msg);
			control.focus();
		 	return false;
		}
	else
		return true;
}

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(control,msg)
{
	var x =control.value;
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(x)) 
		{
			alert(msg);
			control.focus();
		 	return false;
		}
	else
		return true;
}

function validateCellNoWithCode(control,msg)
{
	var x = trim(control.value);
	var filter  = /^([0-9]{4}\s[0-9]{7})|([0-9]{4}[0-9]{7})$/;
	if (!filter.test(x)) 
		{
			alert(msg);
			control.focus();
		 	return false;
		}
	else
		return true;
}

function validateCellNoWithOutCode(control,msg)
{
	var x = trim(control.value);
	var filter  = /^([0-9]{7,13})$/;
	if (!filter.test(x)) 	
		{
		 	alert(msg);
			control.focus();
			return false;
		}
	else{
			return true;		
	}
}


function trim(str) {     if(!str || typeof str != 'string')         return null;     return str.replace(/^[\s]+/,'').replace(/[\s]+$/,'').replace(/[\s]{2,}/,' '); }

function validateUrl(control,msg)
{
	var x =control.value;
	var filter  = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
	if (!filter.test(x)) 
		{
			alert(msg);
			control.focus();
		 	return false;
		}
	else
		return true;
}


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 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 mm/dd/yyyy or mm-dd-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;
}

function Confirmation(){
    return confirm("Delete Record y/n ?.");
}
