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


// radio button validation
function validate_radio(inputValue, fieldName) {
    for(var i=0;i<inputValue.length;i++)
        if (inputValue[i].checked)
            return true;
    return ERRORS+=fieldName;
}

// check box checked?
function validate_checkbox(inputValue, fieldName) {
    if (inputValue.checked == true)
        return true;
    return ERRORS+=fieldName;
}

// gender
function validate_gender(inputValue, fieldName) {
    for(var i=0;i<inputValue.length;i++)
        if (inputValue[i].checked)
            return true;
    return ERRORS+=fieldName;
} // end function

// password confirmation 
function validate_confirmation(inputValue, compareValue, fieldName){
	if(((inputValue.value=="") || (isWhitespace (inputValue.value))) || ((compareValue.value=="") || (isWhitespace (compareValue.value))))
        return ERRORS+=fieldName;
    else { 
        if (!(inputValue.value==compareValue.value))
        	return ERRORS+=fieldName;
    }
}

// login
function validate_logins(inputValue, fieldName) {
    if( (!(inputValue.length<=4)) && ((inputValue.value=="") || (isWhitespace (inputValue.value))) )
            return ERRORS+=fieldName;
}

// phone      
function validate_phone(inputValue, fieldName){	
	if((inputValue.value=='')||(isWhitespace (inputValue.value)))
        return ERRORS+="No " + fieldName + "  entered \n";
    else {                        
        value=inputValue.value.replace(/\s/g, "")
     	var phoneNum= value.replace( /[\(\)-]/g, "" )
		if ((isNaN(phoneNum)==true ) || (!(phoneNum.length==10)))
    		return ERRORS+=("Invalid " + fieldName + "\nThis field must be a 10 digit phone number with area code, like (xx) xxxx-xxxx\n");
    }             
}                        

// email 
function validate_email(inputValue, fieldName) {
	if((inputValue.value=='') ||(isWhitespace(inputValue.value)))
        return ERRORS+=fieldName;
    else {
        if (!(isValid(email_exp, inputValue.value))) 
            return ERRORS+=fieldName;
    }
}
   		
// required fields
function validate_required(inputValue, fieldName) {
    if((inputValue.value=='') ||(isWhitespace(inputValue.value)))
        return ERRORS+=fieldName;
}      

// required fields for phone
function validate_required_phone(inputValue, fieldName) {
    if((inputValue.value=='') ||(isWhitespace(inputValue.value)) || (isNaN(inputValue.value)==true))
        return ERRORS+=fieldName;
}

// required fields for postcode
function validate_required_postcode(inputValue, fieldName) {
    if((inputValue.value=='') || (isWhitespace(inputValue.value)) || (isNaN(inputValue.value)==true) || (inputValue.value.length!=4))
        return ERRORS+=fieldName;
}

// pulldown box
function validate_pulldown(inputValue, fieldName) {
	if(inputValue.selectedIndex==0)
        return ERRORS+=fieldName;
}      
      
// date
function validate_date(inputValue, fieldName){
	if((inputValue.value=='') ||(isWhitespace(inputValue.value)))
        return ERRORS+=fieldName;
	else
        return validate_DateEntry(trimLeadSpace(inputValue.value), fieldName)
}

// date digits, isNumber and greater then 0
function isNumber(Num, fieldName){
	if(Num!="")
		if (Num!=0) {
			value=Num.replace(/\s/g, "")			                 
			result=value.replace(digits_exp, "")
			if (result.length>0)  	                
			return ERRORS+="Invalid " +fieldName +"\nThis field must be a digit input\n"; 
		}
		else
			return ERRORS+="Invalid " +fieldName +"\nThis field must be a digit input greater then 0 \n";
}

// trim function
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 );
}

//check two dates values. If date 1 is less then date 2/
function dateDiffLess(inputValue1, fieldName1, inputValue2, fieldName2){	

	var vDate1=Date.parse(inputValue1.value);
	var vDate2=Date.parse(inputValue2.value);	
	
	if(vDate1>vDate2)return ERRORS+=("Invalid " + fieldName2 +" \n Begin date can't be less than end date.\n");	 
}

// 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+=(fieldName+". Example: (dd/mm/yyyy)\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[2]
		var intDay=intSplit[1]
		var arrayYear=intSplit[3]
		if(intSplit.length!=6) return ERRORS+=(fieldName+". Enter date:(dd/mm/yyyy)\n");
	}
	else{
		var intMonth=intSplit[1]
		var intDay=intSplit[0]		
		var arrayYear=intSplit[2]
		if(intSplit.length!=3) return ERRORS+=(fieldName+". Enter date:(dd/mm/yyyy)\n");
	}

	//check if Month, Day and Year is a digit number
	isNumber(intMonth, "Month Entry in " + fieldName )
	isNumber(intDay, "Day Entry in " + fieldName)
		
	//checks if date was entered in the correct format: dd/mm/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(intDay + "/" + intMonth + "/" +intYear)		
		}
	else if ((arrayYear.length==4) && (arrayYear.charAt(0, 1)==0))//date was entered like dd/mm/0002
	   return ERRORS+=(fieldName+". Enter correct year:(dd/mm/yyyy)\n");
	
	else if ((arrayYear.length==3)||(arrayYear.length==1))//only 3 digits entered for the year
		return ERRORS+=(fieldName+". Enter correct year:(dd/mm/yyyy)\n");
	
	else var intYear = parseFloat(arrayYear);
	
	//check for the valid month    
    if(intMonth>12 || intMonth<0) return ERRORS+=(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+=(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+=(fieldName+ ". \nThis field must be a day number between 1 and " + daysInFebruary(intYear)+ "\n");
  
    return true;
}