function notNull(str) {
      //alert("The value of str in notNull function is" + str)
	if (str.length == 0 )
		return false
	else 
		return true
}

function notBlank(str) {
      //alert("The value of myfield in notBlank function is" + str)
	for (i = 0; i < str.length; i++) {
		if (str.charAt(i) != " ")
			return true
	}
	return false
}

 function notRegExp(str) {
  //alert("Inside the isemail call")
  //var emailreg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$"
  var textreg = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
  var regex = new RegExp(textreg);
  return regex.test(str);
 }
 

function isSize(str, size) {
      //alert("The value of str in issize function is" + str + " and size is " + size)

	if (str.length == size) 
     
		return true
	else
          
		return false
}

function isDigits(str) {
      //alert("The value of str in isdigits function is" + str )
	var i
	for (i = 0; i < str.length; i++) {
		mychar1 = str.charAt(i)
            //alert("The value of mychar in isDigits function is " + mychar1)
		if (mychar1 < "0") {
                //alert("The value of str is less than 0 :" + str)
                return false
                        }
            if (mychar1 > "9") {
                  //alert("The value of str is greater than 9:" + str)
			return false
                        }
	}
	return true
}

function isNumber(str) {
      //alert("The value of str in isNumber function is" + 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 isNbr(str) {
      //alert("The value of str in isNumber function is" + str)

	for (i = 0; i < str.length; i++) {
		var mychar = str.substring(i, i + 1)
		if (mychar < "0" || mychar > "9"){
                  return false
                }
        }
return true
}

function isInRange(str, num1, num2) {
	var i = parseInt(str)
	return((i >= num1) && (i <= num2))

}

function isEmail(str) {
  //alert("Inside the isemail call")
  //var emailreg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$"
  var emailreg = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
  var regex = new RegExp(emailreg);
  return regex.test(str);
 }
 
 function isPassword(str) {
 alert("Inside the isPassword call" + str)
  var pwreg = /^[A-Za-z]\w{6,}[A-Za-z]$/;
  var regex = new RegExp(pwreg);
  return regex.test(str);
 }



/* PURPOSE:  Since we are using the single tick mark as the
	string delimiter to construct our SQL queries, a string with
	a tick mark in it will cause a SQL error.  Therefore we replace
	all "'" with "''", which eliminates the possibility of a SQL error.
*/

function replaceAll (s, fromStr, toStr)
{
	var new_s = s;
	for (i = 0; i < 100 && new_s.indexOf (fromStr) != -1; i++)
	{
		new_s = new_s.replace (fromStr, toStr);
		//alert("The value of new s is" + new_s);
	}
	return new_s;
}

function sqlSafe (s)
{
	var new_s = s;
	//alert("The sqlSafe value of s is" + s);
	new_s = replaceAll (new_s, "'", "|");
	new_s = replaceAll (new_s, "|", "''");
	new_s = replaceAll (new_s, "\"", "|");
	new_s = replaceAll (new_s, "|", "''");
	//	alert("Done with the sqlSafe value of s is" + s);

	return new_s;
}



function makeSafe (i)
{
	//alert("The value of i is" + i);
	//i.value = sqlSafe (i.value);
	i.value = sqlSafe(i);
	//alert("The new value of i is" + i);
}

function stripNonDigits(str) {
      //alert("The value of str in strip non digits function is" + str )
	var i
	var newstring = ""
	for (i = 0;  i < str.length; i++) {
		mychar = str.charAt(i)
            //alert("The value of mychar(stripNonDigits) is " + str )
		if (isDigits(mychar)) 
	         newstring += mychar
             else
               i = str.length;
      }
      //alert("The final value of newstring is " + newstring)
	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
}

//Global variable set at start of script
var emptyString = " field is blank. Please enter a "
var numfield = " field contains an invalid numeric value. Please do not enter non numeric characters in the "

function validateString(myfield, s) {
       // alert("The value of myfield is " + myfield)
	if (notNull(myfield) && notBlank(myfield)) 
		return true
	else {
		//myfield.focus()
		alert("The " + s + emptyString + s)
		return false
	}
}

function validateString2(myfield, s) {
       // alert("The value of myfield is " + myfield)
	if (notNull(myfield) && notBlank(myfield)) {
        if  (notRegExp(myfield))
			return true
		else {
		//	myfield.focus()
			alert("Invalid characters in text. Please remove any quotes.")
			}
		}
	else {
		//myfield.focus()
		alert("The " + s + emptyString + s)
		return false
	}
}

function validateMemNum(myfield) {
	if (isDigits(myfield.value) && isInRange(myfield.value,100, 999))
		return true

	else {
	//	myfield.focus()
		alert("Invalid customer number. Please enter a three digit number between 100 and 999")
		return false
	}
}

function validatePledge(myfield) {
	if (notNull(myfield.value)) {
		newstring = stripChars(myfield.value, "$")
		if  (isNumber(newstring))
			return true
		else {
		//	myfield.focus()
			alert("Invalid pledge amount. Please enter a valid dollar amount.")
		}
	}
	return false
}

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 validateemail2(myfield) {
      alert("The value of myfield in validateemail function is" + myfield)

	if (notNull(myfield)) {
		 //alert("After the isemail call")
			return true
	}
	//Personal.zip.focus() Not Working properly WHY?
	alert("Invalid email address. ")
	return false
}

function validateEmail(myfield) {
//alert("The value of myfield in validateemail function is" + myfield)

if (notNull(myfield) && notBlank(myfield)) {
       if (isEmail(myfield)) {
		 // alert("After the isemail call")
		  return true
		 }else {
		 //myfield.focus()
		 alert("The email address you have entered is invalid. Please try again. ex. someone@somewhere.com")
		 return false
		 }
	}
}

function validateUserId(myfield) {
//alert("The value of myfield in validateemail function is" + myfield)

if (notNull(myfield) && notBlank(myfield)) {
       if (isEmail(myfield)) {
		 // alert("After the isemail call")
		  return true
		 }else {
		 //myfield.focus()
		 alert("The UserId you have entered is invalid. Please make it at least 8 characters long. Please try again. ")
		 return false
		 }
	}
}

function validatePassword(myfield) {
if (notNull(myfield) && notBlank(myfield)) {
      alert("After the not null check")
		if (isPassword(myfield)) {
		  //alert("After the isemail call")
		  return true
		 }else {
		 //myfield.focus()
		 alert("The password you have entered is invalid. The password must be at least 8 characters. Please try again.")
		 //alert("test123")
		 return false
		 }
	}
}

function confirmPassword(pw,cpw) {
 
if (pw != cpw) {
        //alert("After the not null check")
     alert("The password and password confirmation fields do not match. Please try again.")
	 Personal.pw2.focus()
	 return false
		 }else {
		  //alert("the passwords matched")
		  return true
		 }
}

function validatePhone(myfield) {
      //alert("The value of myfield in validatezip function is" + myfield)

	if (notNull(myfield)) {
		newstring = stripNonDigits(myfield)
		if (isSize(newstring,10)) 
			return true
	}
	//Personal.zip.focus() Not Working properly WHY?
	alert("Invalid phone number. Please enter 10-digit phone number. Example: 6142191001")
	return false
}

function validateFax(myfield) {
      //alert("The value of myfield in validatezip function is" + myfield)

	if (notNull(myfield)) {
		newstring = stripNonDigits(myfield)
		if (isSize(newstring,10)) 
			return true
	}
	//Personal.zip.focus() Not Working properly WHY?
	alert("Invalid fax number. Please enter 10-digit fax number. Example: 6142191001")
	return false
}

function validatePrice(myfield, s) {
      //alert("The value of myfield in validatePrice function is" + myfield)

	if (notNull(myfield)) {
		//newstring = stripNonDigits(myfield)
		if (isNbr(myfield)) 
			return true
	}
        alert("The " + s + numfield + s)
	return false
}

function validateNumeric(myfield, s) {
      //alert("The value of myfield in validateNumeric function is" + myfield)

	if (notNull(myfield)) {
		newstring = stripNonDigits(myfield)
		if (isNbr(myfield)) 
			return true
	}
        alert("The " + s + emptyString + s)
	return false
}

function validateCCNum(myfield) {
      //alert("The value of myfield in validateCCNum function is" + myfield)

	if (notNull(myfield)) {
		newstring = stripNonDigits(myfield)
		if (isSize(newstring,15) || isSize(newstring, 16)) 
			return true
	}
	//Personal.zip.focus() Not Working properly WHY?
	alert("Invalid credit card number. Please enter 15 to 16-digit credit card number.")
	return false
}

function validateExpDate(myfield) {
      //alert("The value of myfield in validateCCNum function is" + myfield)

	if (notNull(myfield)) {
		newstring = stripNonDigits(myfield)
		if (isSize(newstring,4)) 
			return true
	}
	//Personal.zip.focus() Not Working properly WHY?
	alert("Invalid expiration date. Please enter 4-digit expiration date.")
	return false
}

function validatephotopath(myfield) {
      //alert("The value of myfield in validatephotopath function is" + myfield)

	if (notNull(myfield)&& notBlank(myfield)) {
		jpegstring = "jpeg"
		if (myfield.search(jpegstring) > -1 ) 
			return true
            else {
               jpegstring = "jpg"
               if (myfield.search(jpegstring) > -1 ) 
			return true
               else { 
                  alert("Invalid file format.  The file type must be jpeg.")
                  return false
                  }
               }

     } else {
        alert("A valid file path must be entered into at least one entry field.")
        return false
        }
	}

