//global variables and what not
var targetForCheck
requiredFieldNames = new Array();

function isPictureFileNotBlank(){
	var str = targetForCheck.value.toString()
	if(str != ""){
		return isPictureFile();
	}
	else{
		return true;
	}
}

function isPictureFile(required){
	
	var str = targetForCheck.value.toString()
	
	if(!required && str == ""){
		return true;
	}
	
	found = false;
	if(str.indexOf(".gif") !=  -1){
		found = true;
	}
	if(str.indexOf(".jpg") !=  -1){
		found = true;
	}
	if(str.indexOf(".bmp") !=  -1){
		found = true;
	}
	if(str.indexOf(".png") !=  -1){
		found = true;
	}	
	return found;
}



function isCreditCard() {
	var number = targetForCheck.value.toString()
	var newNumber = number;
	
	if(number.charAt(1) == "*") {
		return true;
	}	
	
	if(number.length < 15){
		return false;
	}
	

	
	var total = 0;
	var flag = 0;
	for (var i=(number.length - 1);i>=0; i--) {
		//make sure this is a number and not a "-" or whatever
		if(number.charAt(i) >= "0" && number.charAt(i) <= "9") {
			if (flag == 1) {
				var digits = number.charAt(i) * 2;
				if (digits > 9) digits -= 9;
				total += digits;
				flag = 0;
			} else {
				total = total + parseInt(number.charAt(i));
				flag = 1;
			}
		}
		else{
			newNumber = newNumber.replace(number.charAt(i),"");
		}
	}
	if ((total%10) == 0) {
		//replace number with stripped down number
		targetForCheck.value = newNumber
		return true;
	} else {
		return false;
	}
}




function isDouble(){
    var strVal = targetForCheck.value.toString()
	var foundPeriod = false;
	
	
    if (strVal.length == 0)
        return false;
    for (var i=0; i < strVal.length; i++){
        var ch = strVal.charAt(i)
        if ((ch < "0" || ch > "9") && ch != "."  & ch != "$" & ch != "%")
            return false;
			
		
    }
    return true
}


function isInteger(){
    var strVal = targetForCheck.value.toString()
    if (strVal.length == 0)
        return false;
    for (var i=0; i < strVal.length; i++){
        var ch = strVal.charAt(i)
        if ((ch < "0" || ch > "9")  & ch != "$")
            return false
    }
    return true
}


function minLength(targetLength){
	if(targetForCheck.value.length < targetLength){
		return false;
	}
	else{
		return true;
	}
}


function isShortDate() {
	dateStr = targetForCheck.value
	 var datePat = /^(\d{1,2})(\/|-)(\d{1,2})$/;
	 var matchArray = dateStr.match(datePat); // is the format ok?
	
	 if (matchArray == null) {
	 	//alert("Please enter date as either mm/dd/yyyy or mm-dd-yyyy.");
	 	return false;
	 }
	
	 month = matchArray[1]; // p@rse date into variables
	 year = matchArray[3];
	
	 if (month < 1 || month > 12) { // check month range
	 	alert("Month must be between 1 and 12.");
	 	return false;
	 }
	
	 return true; // date is valid
 }






// ********************************************************
// This function accepts a string variable and verifies if
// it is in the proper format for an e-mail address or not.

// The function returns true if the format is valid, false
// if not.
// ********************************************************

function isEmail(required) {
	var email = targetForCheck.value.toString();
	var i = 0;
    invalidChars = " ~\'^\`\"*+=\\|][(){}$&!#%/:,;";

    // Check for null
    if (required && email == "") {
        return false;
    }

    // Check for invalid characters as defined above
    for (i=0; i<invalidChars.length; i++) {
        badChar = invalidChars.charAt(i);
        if (email.indexOf(badChar,0) > -1) {
            return false;
        }
    }

    lengthOfEmail = email.length;
    if ((email.charAt(lengthOfEmail - 1) == ".") || (email.charAt(lengthOfEmail - 2) == ".")) {
        return false;
    }

    Pos = email.indexOf("@",1);
    if (email.charAt(Pos + 1) == ".") {
        return false;
    }

    while ((Pos < lengthOfEmail) && ( Pos != -1)) {
        Pos = email.indexOf(".",Pos);
        if (email.charAt(Pos + 1) == ".") {
            return false;
        }
        if (Pos != -1) {
            Pos++;
        }
    }

    // There must be at least one @ symbol
    atPos = email.indexOf("@",1);
    if (atPos == -1) {
        return false;
    }

    // But only ONE @ symbol
    if (email.indexOf("@",atPos+1) != -1) {
        return false;
    }

    // Also check for at least one period after the @ symbol
    periodPos = email.indexOf(".",atPos);
    if (periodPos == -1) {
        return false;
    }
    if (periodPos+3 > email.length) {
        return false;
    }
    return true;
}





 // ******************************************************************
 // This function accepts a string variable and verifies if it is a
 // proper date or not. It validates format matching either
 // mm-dd-yyyy or mm/dd/yyyy. Then it checks to make sure the month
 // has the proper number of days, based on which month it is.

 // The function returns true if a valid date, false if not.
 // ******************************************************************


function isDate() {
	dateStr = targetForCheck.value
	 var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
	 var matchArray = dateStr.match(datePat); // is the format ok?
	
	 if (matchArray == null) {
	 alert("Please enter date as either mm/dd/yyyy or mm-dd-yyyy.");
	 return false;
	 }
	
	 month = matchArray[1]; // p@rse date into variables
	 day = matchArray[3];
	 year = matchArray[5];
	
	 if (month < 1 || month > 12) { // check month range
	 alert("Month must be between 1 and 12.");
	 return false;
	 }
	
	 if (day < 1 || day > 31) {
	 alert("Day must be between 1 and 31.");
	 return false;
	 }
	
	 if ((month==4 || month==6 || month==9 || month==11) && day==31) {
	 alert("Month "+month+" doesn`t have 31 days!")
	 return false;
	 }
	
	 if (month == 2) { // check for february 29th
	 var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
	 if (day > 29 || (day==29 && !isleap)) {
	 alert("February " + year + " doesn`t have " + day + " days!");
	 return false;
	 }
	 }
	 return true; // date is valid
 }

//used to determine if a field is empty, if not run eval on str
function ifNotEmpty(evalStr){
	var str = targetForCheck.value.toString()
	if(str != ""){
		return eval(evalStr);
	}
	else{
		return true;
	}
}


//used to get rid of characters in a string
	function stripCharacter(words,character) {
	//documentation for this script at http://www.shawnolson.net/a/499/
	  var spaces = words.length;
	  for(var x = 1; x<spaces; ++x){
	   words = words.replace(character, "");   
	 }
	 return words;
    }


