/* Common JS for web project */

function validateContactForm(){
	if(""==document.contact.name.value){
		alert("Please enter your name.");
		document.contact.name.focus();
		return false;
	}
	if(""==document.contact.email.value){
		alert("Please enter your email.");
		document.contact.email.focus();
		return false;
	}
	var emailID=document.contact.email;
	if (echeck(emailID.value)==false){
		emailID.value="";
		emailID.focus();
		return false;
	}
	if(""==document.contact.message.value){
		alert("Please enter a message.");
		document.contact.message.focus();
		return false;
	}
	return true
}

function echeck(str) {
		var at="@";
		var dot=".";
		var lat=str.indexOf(at);
		var lstr=str.length;
		var ldot=str.indexOf(dot);
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail");
		   return false;
		}
		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail");
		   return false;
		}
		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail");
		    return false;
		}
		if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail");
		    return false;
		}
		if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail");
		    return false;
		}
		if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail");
		    return false;
		}
		if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail");
		    return false;
		}
		return true;
}

function setfocus(){
	if (document.forms[0] != null || document.forms[0] != undefined) {
		document.forms[0].name.focus();
	}
}

function pauseJS(timeInMilliS) {
	var date = new Date();
	var curDate = null;
	do { curDate = new Date(); }
	while(curDate-date < timeInMilliS);
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}
/* ---- addLoadEvent Usage ------
addLoadEvent(func1);
addLoadEvent(func2);

function func1() {
  alert("This is the first.");
}

function func2() {
  alert("This is the second.");
}

addLoadEvent(function() {
    document.body.style.backgroundColor = '#EFDF95';
})
*/