// JavaScript Document to Process forms and send to PHP files for mailing
/*
Following are the list of the global fields of the form

var cText = array();
var cTextName = array();

var cSelect = array();
var cSelectName = array();

var cEmail = array();
var cEmailName = array();

var cNumber = array();
var cNumberName = array();

var cOther = array();
var cOtherName = array();

var redirect_article = 0;
*/
/*
function getDrop(fieldName){
  var currIndex = document.getElementById(fieldName).selectedIndex;
  var value = document.getElementById(fieldName).options[currIndex].value;
  return value;
}
function setDrop(id,value){
  var length = document.getElementById(id).options.length;
  for(var x=0;x<length;x++){
       var val = document.getElementById(id).options[x].value;
       if(val==value){
           document.getElementById(id).options[x].selected = true
           break;
       }
  }
}
function getText(id){
  return document.getElementById(id).value;
}
*/
var errorCount = 0;
var stri="";

//Trims whitespaces
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

//Checks if form value is an Int
function isInt(s)
{
var n = trim(s);
return n.length > 0 && !(/[^0-9]/).test(n);
}

//Checks Email
function checkEmail(id) {
	var str = getText(id);
	return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}

//Check Text
function checkText(id){
	if(trim(getText(id))!=""){
		return true;	
	}
	return false;
}

//Check Select
function checkSelect(id){
	if(getDrop(id)!="" && getDrop(id)!=-1){
		return true;	
	}
	return false;
}

function checkForm(){
	// Check the Compulsory Text First
	for(var x=0;x<cText.length;x++){
		if(checkText(cText[x])){
			highLightNoError(cText[x]);
			stri += "::"+cTextName[x]+"___"+getText(cText[x]);
		}else{
			highLightError(cText[x]);
			errorCount++;
		}
	}

	// Check the Compulsory Text First
	for(var x=0;x<cNumber.length;x++){

		if(checkText(cNumber[x])){
			highLightNoError(cNumber[x]);
			stri += "::"+cNumberName[x]+"___"+getText(cNumber[x]);
		}else{
			highLightError(cNumber[x]);
			errorCount++;
		}
	}
	
	// Check the Compulsory Select First
	for(var x=0;x<cSelect.length;x++){

		if(checkSelect(cSelect[x])){
			highLightNoError(cSelect[x]);
			stri += "::"+cSelectName[x]+"___"+getDrop(cSelect[x]);
		}else{
			highLightError(cSelect[x]);
			errorCount++;
		}
	}
	
	// Check the Compulsory Email
	for(var x=0;x<cEmail.length;x++){

		if(checkEmail(cEmail[x])){
			highLightNoError(cEmail[x]);
			stri += "::"+cEmailName[x]+"___"+getText(cEmail[x]);
		}else{
			highLightError(cEmail[x]);
			errorCount++;
		}
	}
	
	if(errorCount==0){
		// Add the others
		for(var x=0;x<cOther.length;x++){		

			highLightNoError(cOther[x]);
			stri += "::"+cOtherName[x]+"___"+getText(cOther[x]);
		}
		stri +="::article___"+redirect_article; //TO BE REPLACED BY EMAIL SCRIPT
		stri.replace("&","and");
		//alert(stri);
		window.location = "custom/sendMail.php?args="+stri;
	}else{
		alert("There are errors in the form and it could not be processed. \n Please makes amends in the red-highlighted fields.");
	}
	errorCount=0;
	stri = "";
}

function highLightError(id){
	document.getElementById(id).style.backgroundColor = "#ffc4c4";
}

function highLightNoError(id){
	document.getElementById(id).style.backgroundColor = "#c4ffe0";
}