
function doSubmit( ){
	//doSubmit uses DOM to get all the information from the form and perfom replaces.
	//first, deal with inputboxes
	clearDiv('errorDiv');
	var targets = document.getElementById("genearlSubForm").getElementsByTagName("input");

	for (var i=0; i < targets.length; i++) {
	while (targets[i].value.indexOf("\"") > -1) {
	        targets[i].value = targets[i].value.replace("\"","'");
	        
	    }
	}

	
	validateForm();
}

function CheckMaxLength(Object, MaxLen) {
  if(Object.value.length > MaxLen) {     
	Object.value = Object.value.substring(0, MaxLen);
  }
}


function is_a_number(arg) {
    if (((arg / arg) != 1) && (arg != 0)) {return false;} else {return true;}
}


function validEmail(Email) {
	invalidChars = "/:,;"
	if (Email == "") {
		return false
	}
	// Checks for invalid characters
	for(i=0; i<invalidChars.length; i++) {
		badChar = invalidChars.charAt(i)
		// if the result of the indexOf function is -1, the char isnt in the string, and you dont get a false result
		if (Email.indexOf(badChar,0) > -1) {
			return false
		}
	}
	// Checks for first @ sign starting at the second character
	atPos = Email.indexOf("@",1)
	if (atPos == -1) {
		return false
	}
	// Making sure theres only one @ sign
	if (Email.indexOf("@", atPos+1) > -1){
		return false
	}
	//Checks if theres a full stop somewhere after the @ sign
	periodPos = Email.indexOf(".", atPos)
	if (periodPos == -1) {
		return false
	}
	// Checks if theres at least 2 chars after the full stop in the address
	if (periodPos+3 > Email.length)	{
		return false
	}
	
	return true
	
}

// write to a div

function clearDiv(whichDiv) {
    if(document.getElementById) {
		document.getElementById(whichDiv).innerHTML = "";
	} else if (document.all){
		document.all(whichDiv).innerHTML = "";
	}
}

function checkSubmit(errorDiv){
	if(document.getElementById){
		if (document.getElementById(errorDiv).innerHTML == ""){
			return true;
		} else {
			return false
		}
	}

    if (document.all) {
        if (document.all(errorDiv).innerHTML == ""){
			return true;
		}
        else {
			return false
		}
    }
}

function writeToDiv(whichDiv,whatText) {
	if(document.getElementById) {
		document.getElementById(whichDiv).innerHTML = (document.getElementById(whichDiv).innerHTML + '<br />' + '<span style="color: #f00; font-weight:bold">&#187;</span> ' + whatText);
	} else if(document.all){
		document.all(whichDiv).innerHTML = document.all(whichDiv).innerHTML + '<br />' + '<span style="color: #000; font-weight: bold">&#187;</span> ' + whatText;
	} else if (document.layers){
		nsWriteToDivWidth = '100%';
		whatText = '<div id="nsRelativeFixDiv" style="position:absolute; left:0px; top:0px; width:'+nsWriteToDivWidth+'; height:1; z-index:1; visibility: visible;">'+whatText+'</div>';
		eval('document.'+whichDiv+'.document.open()');
		eval('document.'+whichDiv+'.document.write(\''+whatText+'\')');
		eval('document.'+whichDiv+'.document.close()');
	}
}

//* form function for validating entry into feedback form */
function validateForm(){
	// if(document.contactform.FirstName.value == ""){
	// writeToDiv('errorDiv','Please enter your First Name.')
	// }

    if(document.contactform.yourname.value == ""){
		writeToDiv('errorDiv','Please enter your First Name.')
	}

	if(!(validEmail(document.contactform.email.value))){
		writeToDiv('errorDiv','Please enter a valid Email address.')
	}
	
	if(document.contactform.query.value == ""){
		writeToDiv('errorDiv','Please enter your feedback.')
	}
	if (checkSubmit('errorDiv')) {
	    if (confirm("This form is being sent by email. Please continue only if you are satisfied with your form.")) {
	        document.contactform.submit();
	    }
	}
    // if(document.contactform.Declaration.value == ""){
	//	writeToDiv('errorDiv','Please accept the declartion.')
	// }	
	
}

