window.onload = init;

function init() {
	var Allerrmsg = new Array("errname","erremail","errq-content","errtitle");	
	for (var s = 0; s < Allerrmsg.length; s++){
		setVisible(Allerrmsg[s],false);
	}
}

function setVisible(myObjID, myflag) {
	var obj = document.getElementById(myObjID);
	if (myflag){
		obj.style.display = "inline";
	}else{
		obj.style.display = "none";
	}
}


function expend(id,myflag){
	var obj = document.getElementById(id);
	if (myflag){
		obj.style.display = "block";
	}else{
		obj.style.display = "none";
	}
	
	return false;
}

function checking() {
	var focusObj = null;
	var formlist = document.getElementById("contactus").elements;
	var Allerrmsg = new Array("errname","erremail","errq-content","errtitle");		
	
	for (var s = 0; s < Allerrmsg.length; s++){
		setVisible(Allerrmsg[s],false);
	}
	
	for (var i=0; i <formlist .length; i++ ) {
		var currentObj = formlist[i];
		switch (currentObj.id) {
			case "name":
				if (isEmpty(currentObj.value) ) {		
					setVisible("errname",true);
					if (!focusObj)	
						focusObj = currentObj;
				}
				break;
			case "email":
				if (!isEmail(currentObj.value)) {					
					setVisible("erremail",true);
					if (!focusObj)	
						focusObj = currentObj;
				}					
				break;
			case "title":
				if (isEmpty(currentObj.value) ) {			
					setVisible("errtitle",true);
					if (!focusObj)	
						focusObj = currentObj;
				}					
				break;
			case "q-content":
				if (isEmpty(currentObj.value)) {							
					setVisible("errq-content",true);
					if (!focusObj)	
						focusObj = currentObj;
				}				
				break;
			default:
				break;
		}	
	}

	if (focusObj) {
		focusObj.focus();
		focusObj.select();	
		return false;
	}
	return true;

}

function isEmpty(inStr) {
	return ( (inStr == null || inStr == "" || trimString(inStr) == "") ? true : false);
}

function trimString(inStr) {
	if (typeof inStr != "string") { return inStr; }
   		
	var retValue = inStr;
		
	// Check for spaces at the beginning of the string
 	var ch = retValue.substring(0, 1);
   	while (ch == " ") { 
   		retValue = retValue.substring(1, retValue.length);
   		ch = retValue.substring(0, 1);
	}
		
	// Check for spaces at the end of the string
	ch = retValue.substring(retValue.length-1, retValue.length);
	while (ch == " ") { 
   		retValue = retValue.substring(0, retValue.length-1);
   		ch = retValue.substring(retValue.length-1, retValue.length);
	}
		
	// Note that there are two spaces in the string - look for multiple spaces within the string
	while (retValue.indexOf("  ") != -1) { 
   		retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); 
	}
		
	// Return the trimmed string back to the user
	return retValue; 
}

function isEmail(inStr) {
	var i=0, atPos = 0, periodPos = 0;
	var badChar = null;
	var invalidChars = " /:,;";
		
	// Return false if the contents of email is empty
	if (isEmpty(inStr)) 
		return false;
		
	// Return false if one of the most likely invalid characters in an email address: black space, slash, colon, comma, and semicolon
	for (i=0; i < invalidChars.length; i++) {
		badChar = invalidChars.charAt(i);
		if (inStr.indexOf(badChar, 0) > -1)
			return false;
	}
		
	// Look for the first @ sign in the email address
	// Return false if (1) no @ sign is found, or (2) more than one @ sign is found
	atPos = inStr.indexOf("@", 1);
	if ((atPos == -1)  || (inStr.indexOf("@", atPos + 1) > -1))
		return false;
			
	// Look for a period "."  after the @ sign
	// Return false if (1) no period is found, or (2) less than 2 characters after the period
	periodPos = inStr.indexOf(".", atPos);
	if ((periodPos == -1) || (periodPos + 3 > inStr.length) )
		return false;
			
	return true;
}
