var phone_exp=/^\d{10}$/g
var reWhitespace = /^\s+$/
var email_exp = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;
var zip_exp = /^\d{5}$|^\d{5}[\-\s]?\d{4}$/
var letter_exp = /[a-z'-]/ig
var noDBQuote_exp = /[a-z'-.,d]/ig
var digits_exp = /\d/ig
var date_exp = /^0?(\d{1,2})\D+0?(\d{1,2})\D+(\d{2}|\d{4})$/;
var date_out = "$1/$2/$3";
//edits by Natasha McTigue to replace all non=word characters with &#163
var simple_char=/\^w|\^W[^\,\!\?\|\\]/ig;
var single_exp1 = /[\’]/ig
var single_exp2 = /[\‘]/ig
var single_exp3 = /[\”]/ig
var single_exp4 = /[\“]/ig
var single_exp5 = /[\—]/ig
var single_exp6 = /[\…]/ig

var graphic_exp=/gif|jpg|jpeg|bmp|pic/i;
var NetscapeVerArray=false;

// Determin Netscape Browser Virsions
if (navigator.appName == "Netscape") {
    // Get Netscape 4.x
    if (navigator.appVersion.substring(0,4)=="4.03") {
       var NetscapeVerArray=true;
    }
    // Get Netscape 3.x
    else if (navigator.appVersion.substring(0,1)=="3") {
		if (confirm("To view this site your have to install a new version of Netscape Navigator. \n Would you like to upload one now?")){
			window.open("http://home.netscape.com/download/index.html?cp=djuc1")
		}
        
    }
    // Get Netscape 2.x
    else if (navigator.appVersion.substring(0,1)=="2") {
		if (confirm("To view this site your have to install a new version of Netscape Navigator. \n Would you like to upload one now?")){
			window.open("http://home.netscape.com/download/index.html?cp=djuc1")
		}
    }
 
    
}
//alert(navigator.appVersion.substring(0,4))

//Vallidate no double quotes fields
function validate_noDBquote(inputValue, fieldName){
	if((inputValue.value=="") || (isWhitespace (inputValue.value)))
	                return ERRORS+="No " + fieldName + " entered \n";
	        else { 	
					value=inputValue.value.replace(/\s/g, "")			                 
	                result=value.replace(letter_exp, "")
	                if (result.length>0)  	                
	                return ERRORS+="Invalid " +fieldName +"\nThis field must contain only letters\n";           
	            }      
	}
	

//function removes leading and ending spaces from string
function trimLeadSpace(inputValue){

	//beggining spaces 
	while(''+ inputValue.charAt(0)==' '){	
		inputValue=inputValue.substring(1,inputValue.length);
	}
	
	//ending spaces
	while(inputValue.charAt(inputValue.length-1)+''==' '){	
		inputValue=inputValue.substring(0,inputValue.length-1);
	}
	
	return inputValue;	
}



//Check for curly quotes (Microsoft Office) and replace them with straight quotes 
function replace_quotes(inputValue){
	if(!(inputValue.value=="") || !(isWhitespace (inputValue.value)))
				{
					value=inputValue.value
					replaceChar=value.replace(single_exp1, "'")					
					replaceChar1=replaceChar.replace(single_exp2, "'") 
					replaceChar2=replaceChar1.replace(single_exp3, '"') 
					replaceChar3=replaceChar2.replace(single_exp4, '"') 
					replaceChar4=replaceChar3.replace(single_exp5, "--") 
					replaceChar5=replaceChar4.replace(single_exp6, "...")
					inputValue.value=replaceChar5
	            }      
	}

//Vallidate text fields ======================================================
function validate_text(inputValue, fieldName){
	if((inputValue.value=="") || (isWhitespace (inputValue.value)))
	                return ERRORS+="No " + fieldName + " entered \n";
	        else { 	
					value=inputValue.value.replace(/\s/g, "")			                 
	                result=value.replace(letter_exp, "")
	                if (result.length>0)  	                
	                return ERRORS+="Invalid " +fieldName +"\nThis field must contain only letters\n";           
	            }      
	}

//Vallidate digits fields ======================================================
function validate_digits(inputValue, fieldName){
	if((inputValue.value=="") || (isWhitespace (inputValue.value)))
	                return ERRORS+="No " + fieldName + " entered \n";
	        else { 	
					value=inputValue.value.replace(/\s/g, "")			                 
	                result=value.replace(digits_exp, "")
	                if (result.length>0)  	                
	                return ERRORS+= fieldName +"\nthis field must be a digit input\n";           
	            }      
	}





//validate phone ======================================================
function validate_phone(inputValue, fieldName,vLink){	
	if((inputValue.value=='')||(isWhitespace (inputValue.value))){
                ERRORS+=fieldName +"<a href='"+vLink+"'> - please enter a valid phone number</a><br>";
                return false;
                }
       else {                        
            value=inputValue.value.replace(/\s/g, "")
   			var phoneNum= value.replace( /[\(\)-]/g, "" )
   			if ((isNaN(phoneNum)==true ) || (!(phoneNum.length==10))){
   				ERRORS+=(fieldName + "<a href='"+vLink+"'> - this field must be a 10 digit phone number</a><br>");
   				return false;
   				}
   				return true;
   		  }             
}   

//validate phone ======================================================      
function validate_phone555(inputValue, fieldName,vLink){	
	if((inputValue.value=='555')){
                ERRORS+=fieldName + "555 is not a valid <a href='"+vLink+"'>" + fieldName + "  entry</a><br>";
                return false;
                }
       else {return true; }             
}                       

//validate phone ======================================================
function validate_phoneByVal(inputValue, fieldName,vLink){	
	if((inputValue=='')||(isWhitespace (inputValue))){
                ERRORS+=fieldName + " - <a href='"+vLink+"'>this field is required</a><br>";
                return false;
                }
       /*
       else {                        
            value=inputValue.replace(/\s/g, "")
   			var phoneNum= value.replace( /[\(\)-]/g, "" )
   			if ((isNaN(phoneNum.substring(0,10))==true ) || (!(phoneNum.length>=10))){
   				ERRORS+=(" - <a href='"+vLink+"'>" + fieldName + "please enter a valid phone number</a><br>");
   				return false;
   				}
   				return true;
   		  }
   		  */
   		  return true;             
}     
 
//validate Email ======================================================
function validate_email(inputValue, fieldName,vLink){
	if((inputValue.value=='') ||(isWhitespace(inputValue.value))){
                ERRORS+=fieldName + " - <a href='"+vLink+"'>this field is required</a><br>";
                return false;
                }
       else {
                       if (!(isValid(email_exp, inputValue.value))) {
							ERRORS+=(fieldName + " - <a href='"+vLink+"'>please enter a valid email address <em>(name@domain.com)</em></a><br>");
							return false;
                       }
                       return true;
            }
 
}

//validate required fields ==================================================
		        
function validate_required(inputValue, fieldName,vLink){
   if((inputValue.value=='') ||(isWhitespace(inputValue.value))){
                ERRORS+=fieldName + " - <a href='"+vLink+"'>this field is required</a><br>";
                return false;
     }else{return true;}
}      

//validate pulldown box ======================================================
function validate_pulldown(inputValue, fieldName,vLink){
	if(inputValue.selectedIndex==0){
      ERRORS+=(fieldName + " - <a href='"+vLink+"'>please make a selection</a><br>");
      return false;
      }
      return true;
}      
      
//validate Date ======================================================
function validate_date(inputValue, fieldName){
	if((inputValue.value=='') ||(isWhitespace(inputValue.value)))
		return ERRORS+="No " + fieldName + " entered\n";

	else return validate_DateEntry(inputValue.value, fieldName)
			 
}

function trim(strText) { 
    // this will get rid of leading spaces 
    while (strText.substring(0,1) == ' ') 
        strText = strText.substring(1, strText.length);

    // this will get rid of trailing spaces 
    while (strText.substring(strText.length-1,strText.length) == ' ')
        strText = strText.substring(0, strText.length-1);

   return strText;
} 


// Check whether string s is empty.
function isEmpty(str)
{   return ((str == null) || (str.length == 0))
}

// Returns true if string s is empty or 
// whitespace characters only.
function isWhitespace (str)
{   // Is s empty?
    return (isEmpty(str) || reWhitespace.test(str));
}

//validate graphic file type
function validate_graphic(inputValue, fieldName){
	if ((!(inputValue.value=='')) || (!(isWhitespace(inputValue.value))))
	              { 	
					var value=inputValue.value.replace(/\s/g, "") 
	                var imageName=(value.split("\\"))[value.split("\\").length-1]
					var fileType=(imageName.split("."))[imageName.split(".").length-1]
					if (!isValid(graphic_exp, fileType)) 
					return ERRORS+="File " + imageName + " contains incorrect format \n";	               
					else return true;					 
	            }   
	// else 	return ERRORS+="No " + fieldName + " entered\n";   
	}



function isValid(pattern, str) {
        return pattern.test(str)
}

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 );
}

//content validation. In this case checks for correct dates
function validate_DateEntry(inputValue, fieldName) {	
	var value;
	var now = new Date();
	var nowDate=now.getTime();//get today's date
	var userDate=Date.parse(inputValue);//convert number in milliseconds for comparing	
	var daysInMonth = new Array(12);
		daysInMonth[1] = 31;
		daysInMonth[2] = 29;   
		daysInMonth[3] = 31;
		daysInMonth[4] = 30;
		daysInMonth[5] = 31;
		daysInMonth[6] = 30;
		daysInMonth[7] = 31;
		daysInMonth[8] = 31;
		daysInMonth[9] = 30;
		daysInMonth[10] = 31;
		daysInMonth[11] = 30;
		daysInMonth[12] = 31;		
	
	
		
	//check for correct date input	
	if( date_exp.test(inputValue) ) value = inputValue.replace( date_exp, date_out ); else return ERRORS+=("Invalid "+fieldName+"\n");
	var intSplit=inputValue.split("/");//split date to check individual day, month and year
	
	

	//Only for Netscape 4.03 version does not read array well
	if (NetscapeVerArray==true){
		var intMonth=intSplit[1]
		var intDay=intSplit[2]
		var arrayYear=intSplit[3]
		if(intSplit.length!=6) return ERRORS+=("Invalid " +fieldName+". \nEnter date:(mm/dd/yyyy)\n");
	}
	else{
		var intMonth=intSplit[0]
		var intDay=intSplit[1]		
		var arrayYear=intSplit[2]
		if(intSplit.length!=3) return ERRORS+=("Invalid " +fieldName+". \nEnter date:(mm/dd/yyyy)\n");
	}
	

	//checks if date was entered in the correct format: mm/dd/yyyy		
	
	
	//check for the year digits 2 vs.4 and convert it to 4 digit format if necessary
	if (arrayYear.length==2) {
		intYear="20"+ arrayYear
		userDate=Date.parse(intMonth + "/" + intDay + "/" +intYear)		
		}
	else if ((arrayYear.length==4) && (arrayYear.charAt(0, 1)==0))//date was entered like mm/dd/0002
	   return ERRORS+=("Invalid " +fieldName+". \nEnter correct year:(mm/dd/yyyy)\n");
	
	else if ((arrayYear.length==3)||(arrayYear.length==1))//only 3 digits entered for the year
		return ERRORS+=("Invalid " +fieldName+". \nEnter correct year:(mm/dd/yyyy)\n");
	
	else var intYear = parseFloat(arrayYear);
	
	//check for the valid month    
    if(intMonth>12 || intMonth<0) return ERRORS+=("Invalid " + fieldName+". \nThis field must be a month number between 1 and 12 \n");
	
	// check for invalid days, except for February
    if (intDay > daysInMonth[intMonth])return ERRORS+=("Invalid " +fieldName+". \nThis field must be a day number between 1 and " + daysInMonth[intMonth]+ "\n");
    
    //check for February
    if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) return ERRORS+=("Invalid " +fieldName+ ". \nThis field must be a day number between 1 and " + daysInFebruary(intYear)+ "\n");

	//check if entered date is less then today's date
	//if(nowDate>userDate)return ERRORS+=("Invalid " +fieldName+" \n Date can't be less than today's date.\n");	 
	//if((intYear<2000)||(intYear>2010)) return ERRORS+=("Invalid " +fieldName+" \n Invalid year.\n");	 	
	//list of days by month
   
    return true;
}