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



//Code to validate a string.

//Global variable set at start of script

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

function validateString(myfield, s) {
	if (notNull(myfield.value)&& notBlank(myfield.value)) 
		return true
	else {
		myfield.focus()
		alert("The " + s + emptyString + s)
		return false
	}
}


//Functions for validating numerical input.

function validateNumber(myfield, s) {
	if (isDigits(myfield.value) && isInRange(myfield.value,0, 999999))
		return true

	else {
		myfield.focus()
		alert("The " + s + emptyString + 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 validateList (mylist, s) {
	//('000');
		
	if (mylist.selectedIndex != 0)
		return true
	else
	{	
		mylist.focus()
		  alert("The " + s + " has not been selected. Please select " + s)
		return false
	}
}
function validateCurrency(myfield, s)
{
    rexCurrency = /\d\.\d/;
    if (rexCurrency.exec(myfield.value) )
		return true

	else {
		myfield.focus()
		alert("The " + s + emptyString + s)
		return false
	}
}
function validatePassowrdLength(myfield,str1,length)
{
//alert ('aaaaaa');
	if (myfield.value.length < length)
		{
		alert(str1+" should be "+length+" characters long.");
		return false;
		}
	else
		return true;


}

function validateCheckBoxes(frm,str1)
{


	//alert ('oooo');
	var e = 0;
	for (var c=0;c<document.frm.elements.length;c++)
		{
		//alert ('oooo');
			if (document.frm.elements[c].type == 'checkbox' && document.frm.elements[c].checked ==true)
			{//alert ('oooo');
			e=e+1;
			}
    		}
	if (e==0)
		{
		//alert ('oooo');
			alert('No '+str1+' selected. Please select a '+str1);
			return false;
		}
	else
	return true;
}
function validateCheckList(frm,intial,message)
{
	var re = new RegExp(intial+".*");
	for(i=0;i<frm.length;i++)
	{
		elem=frm[i];
		
		if(elem.type == 'checkbox' && elem.name.match(re))
			if(elem.checked)		
				return true;
	}
	alert(message+" is not selected. Please select atleast one "+message);
	return false;	
}
function validateRadio(obj,message)
{
	for(i=0;i<obj.length;i++)
		if(obj[i].checked)		
			return true;
	alert(message+" is not selected. Please select "+message);
	return false;	
}
function selectAll(chkSelectAll,initial)
{	
   formObj=chkSelectAll.form;
   var re = new RegExp(initial+".*");
   var isInverse="";
   if (chkSelectAll.checked==true){
     isInverse=0;
   }else{
       isInverse=1;
   }
      
   for (var i=0;i < formObj.length;i++)
   {
      fldObj = formObj.elements[i];
      if (fldObj.type == 'checkbox' && fldObj.name.match(re))
      {
         if(isInverse)
            fldObj.checked = (fldObj.checked) ? false : true;
         else fldObj.checked = true;
       }
   }
}

function winOpen(URL, windowName, width, height, resizable, location, menubar, scrollbars, status, toolbar){
	var windowFeatures;
	windowFeatures = '';
	if (width != '' && width != null){
		windowFeatures = windowFeatures+'width='+width+',';
	}
	if (height != '' && height != null){
		windowFeatures = windowFeatures+'height='+height+',';
	}
	if (resizable){
		windowFeatures = windowFeatures+'resizable,';
	}
	if (location){
		windowFeatures = windowFeatures+'location,';
	}
	if (menubar){
		windowFeatures = windowFeatures+'menubar,';
	}
	if (scrollbars){
		windowFeatures = windowFeatures+'scrollbars,';
	}
	if (status){
		windowFeatures = windowFeatures+'status,';
	}
	if (toolbar){
		windowFeatures = windowFeatures+'toolbar,';
	}
	window.open(URL, windowName, windowFeatures);
}


function createWindow(what) {
    var URL = what.URL.value;

    var windowName = what.windowName.value;

    var features =
        'width='        + what.width.value +
        ',height='      + what.height.value +
        ',directories=' + (what.directories.checked - 0) +
        ',location='    + (what.location.checked - 0) +
        ',menubar='     + (what.menubar.checked - 0) +
        ',scrollbars='  + (what.scrollbars.checked - 0) +
        ',status='      + (what.status.checked - 0) +
        ',toolbar='     + (what.toolbar.checked - 0) +
        ',resizable='   + (what.resizable.checked - 0);

    window.open (URL, windowName, features);
}

function submitForm() {
window.open ("/image/downloadAlert", "NewWindow","menubar=0,resizable=1,width=550,height=350");
document.downloadform.submit();
}
