//this file is a javascript validation collection.
//please feel free to use this script.
//this is my own & my collegue javascript collection.
//can be free to use for doing project under Vintedge.Pte. Ltd.

function openWindow(url) {
	parameter = "width=610,height=440,toolbar=no,scrollbars=no,resizable=no";
	var v = window.open(url,'_blank',parameter);
}

function reset() {
	document.form1orms[0].reset();
}

function check_email(address) {
	//var emailPat = /^((\w|\.|\_)+)@(\[\d{1,3}(\.\d{1,3}){3}]|[A-Za-z]\w*(\.[A-Za-z]\w*)+)$/;
	var emailPat = /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/;
	var matchArray = address.match(emailPat);
	if (matchArray == null)
		return false;
	else
		return true;
}

function isDigit() {
if ((event.keyCode < 48)||(event.keyCode > 57 )) {
  if (event.keyCode != 46)
	{event.returnValue=false;}
}
}

function trim(inputString)
{
	var retValue = inputString;
	var ch = retValue.substring(0, 1);

	while (ch == " ")
	{ // Check for space at the start of the string
		retValue = retValue.substring(1, retValue.length);
		ch = retValue.substring(0, 1);
	}

	ch = retValue.substring(retValue.length-1, retValue.length);

	while (ch == " ")
	{ // Check for spaces at the end of the string
		retValue = retValue.substring(0, retValue.length-1);
		ch = retValue.substring(retValue.length-1, retValue.length);
	}
	
	return retValue;
}

function ebuzz(){
		if (trim(document.form1.firstname.value) == ""){
			alert("Please key in your first name.");
			document.form1.firstname.focus();
			return;
		}	
		if (trim(document.form1.lastname.value) == ""){
			alert("Please key in your last name.");
			document.form1.lastname.focus();
			return;
		}	
		if (document.form1.email.value == "" || !check_email(document.form1.email.value)){
			alert("Please key in a valid email.");
			document.form1.email.focus();
			return;
		}
		if (document.form1.contact.value == ""){
			alert("Please key in your mobile number.");
			document.form1.contact.focus();
			return;
		}
		
		if (document.form1.contact.value.length > 0)
		{
			if (((document.form1.contact.value.indexOf ('6') != 0) && (document.form1.contact.value.indexOf ('9') != 0) && (document.form1.contact.value.indexOf ('8') != 0)) || (document.form1.contact.value.length < 8)){
				alert("Your contact number format does not seem to be correct.");
				document.form1.contact.focus();
				return;
				}
		}	
		document.form1.submit();
}

function contactus(){
		if (trim(document.form1.name.value) == ""){
			alert("Please key in your name.");
			document.form1.name.focus();
			return;
		}	
		if (document.form1.email.value == "" || !check_email(document.form1.email.value)){
			alert("Please key in a valid email.");
			document.form1.email.focus();
			return;
		}
		if (trim(document.form1.emailtype.value) == ""){
			alert("Please select your nature of email.");
			document.form1.emailtype.focus();
			return;
		}		
		if (document.form1.contact.value == ""){
			alert("Please key in your mobile number.");
			document.form1.contact.focus();
			return;
		}
		
		if (document.form1.contact.value.length > 0)
		{
			if (((document.form1.contact.value.indexOf ('6') != 0) && (document.form1.contact.value.indexOf ('9') != 0) && (document.form1.contact.value.indexOf ('8') != 0)) || (document.form1.contact.value.length < 8)){
				alert("Your contact number format does not seem to be correct.");
				document.form1.contact.focus();
				return;
				}
		}	
		if (trim(document.form1.comments.value) == ""){
			alert("Please key in your comments.");
			document.form1.comments.focus();
			return;
		}
		if (trim(document.form1.comments.value).length  >= 1024){
			alert("Your comments is too long.");
			document.form1.comments.focus();
			return;
		}
		document.form1.submit();
}

function doit(){
		
	var status1=true

	
if (trim(document.form1.name.value) == ""){
	alert("Please key in your name.");
	document.form1.name.focus();
	return;
	}
if (trim(document.form1.nric.value) == ""){
	alert("Please key in your NRIC.");
	document.form1.nric.focus();
	return;
	}
	
if (document.form1.nric.value.length != 9){ //9
	alert("Your NRIC format does not seem to be correct. E.g.: SXXXXXXXA");
	document.form1.nric.focus();
	return;
	}
//new NRIC validation by AD
	if (document.form1.nric.value.charAt(0).toUpperCase() != "S"){ //9 first character must be S
		alert("Your NRIC format does not seem to be correct.Must be prefix with 'S'. E.g.: S1234567A");
		document.form1.nric.focus();
		return;
	}
	
	if (isNaN(document.form1.nric.value.charAt(8).toUpperCase()) == false){ //9 last character must be aplhabeth
		alert("Your NRIC format does not seem to be correct.Must be suffix with Alphabeth. E.g.: S1234567A");
		document.form1.nric.focus();
		return;
	}
	

	for(i=1;i<8;i++){
		if (isNaN(document.form1.nric.value.charAt(i).toUpperCase()) == true){ 
			status1=false;
		}
	}
	
	if (status1==false){ 
		alert("Your NRIC format does not seem to be correct. Digit 2-8 must be numeric. E.g.: S1234567A");
		document.form1.nric.focus();
		return;
	}
//end by AD

if (document.form1.email.value == "" || !check_email(document.form1.email.value)){
	alert("Please key in a valid email.");
	document.form1.email.focus();
	return;
}

if (document.form1.contact.value == ""){
	alert("Please key in your mobile number.");
	document.form1.contact.focus();
	return;
}

if (document.form1.contact.value.length > 0)
{
	if (((document.form1.contact.value.indexOf ('6') != 0) && (document.form1.contact.value.indexOf ('9') != 0) && (document.form1.contact.value.indexOf ('8') != 0)) || (document.form1.contact.value.length < 8)){
		alert("Your contact number format does not seem to be correct.");
		document.form1.contact.focus();
		return;
		}
}

if (document.form1.quizQn.value == ""){
	alert("Please key in your Quiz Question.");
	document.form1.contact.focus();
	return;
}

document.form1.submit();
}



function doit1(){

	//email cannot be the same as sender
	if(document.form1.e1.value != ""){
		if(document.form1.e1.value == document.form1.youremail.value){
			alert("Refer to friend email cannot be duplicated as sender email. Please check your friend email address"); 
			return;
		}
	}
	if(document.form1.e2.value != ""){
		if(document.form1.e2.value == document.form1.youremail.value){
			alert("Refer to friend email cannot be duplicated as sender email. Please check your friend email address"); 
			return;
		}
	}
	if(document.form1.e3.value != ""){
		if(document.form1.e3.value == document.form1.youremail.value){
			alert("Refer to friend email cannot be duplicated as sender email. Please check your friend email address"); 
			return;
		}
	}
	if(document.form1.e4.value != ""){
		if(document.form1.e4.value == document.form1.youremail.value){
			alert("Refer to friend email cannot be duplicated as sender email. Please check your friend email address"); 
			return;
		}
	}
	if(document.form1.e5.value != ""){
		if(document.form1.e5.value == document.form1.youremail.value){
			alert("Refer to friend email cannot be duplicated as sender email. Please check your friend email address"); 
			return;
		}
	}
	//----end the same with sender

	if (trim(document.form1.yourname.value) == ""){
		alert("Please key in your name.");
		document.form1.yourname.focus();
		return;
	}
	if (document.form1.youremail.value == "" || !check_email(document.form1.youremail.value)){
		alert("Please key in your valid email.");
		document.form1.youremail.focus();
	return;
	}
	
	if (trim(document.form1.f1.value) == ""){
		alert("Please key in your friend #1 name.");
		document.form1.f1.focus();
		return;
	}
	if (document.form1.e1.value == "" || !check_email(document.form1.e1.value)){
		alert("Please key in your friend #1 valid email.");
		document.form1.e1.focus();
	return;
	}

	//check for email validation friend num 2-5
	if(document.form1.e2.value != ""){
		if (!check_email(document.form1.e2.value)){
			alert("Please key in your friend #2 valid email.");
			document.form1.e2.focus();
		return;
		}
	}
	if(document.form1.e3.value != ""){
		if (!check_email(document.form1.e3.value)){
			alert("Please key in your friend #3 valid email.");
			document.form1.e3.focus();
		return;
		}
	}
	if(document.form1.e4.value != ""){
		if (!check_email(document.form1.e4.value)){
			alert("Please key in your friend #4 valid email.");
			document.form1.e4.focus();
		return;
		}
	}

	if(document.form1.e5.value != ""){
		if (!check_email(document.form1.e5.value)){
			alert("Please key in your friend #5 valid email.");
			document.form1.e5.focus();
		return;
		}
	}
	
	//check for name and email must be fill in together 2-5. eg. you must fill the name if you keyin the email. and also otherwise
	if( (document.form1.f2.value == "" && document.form1.e2.value != "") || (document.form1.f2.value != "" && document.form1.e2.value == "") ){ 
		alert("Name and Email friend #2 is not complete"); 
		document.form1.f2.focus();
		return;
	}
	if( (document.form1.f3.value == "" && document.form1.e3.value != "") || (document.form1.f3.value != "" && document.form1.e3.value == "") ){ 
		alert("Name and Email friend #3 is not complete"); 
		document.form1.f3.focus();
		return;
	}
	if( (document.form1.f4.value == "" && document.form1.e4.value != "") || (document.form1.f4.value != "" && document.form1.e4.value == "") ){ 
		alert("Name and Email friend #4 is not complete"); 
		document.form1.f4.focus();
		return;
	}
	if( (document.form1.f5.value == "" && document.form1.e5.value != "") || (document.form1.f5.value != "" && document.form1.e5.value == "") ){ 
		alert("Name and Email friend #5 is not complete"); 
		document.form1.f5.focus();
		return;
	}
	
	//check friend email cannot be the same. eg. if email not blank... email1 != email2 email2 != email3 etc......
	if( (document.form1.e1.value == document.form1.e2.value) || (document.form1.e1.value == document.form1.e3.value) || (document.form1.e1.value == document.form1.e4.value) || (document.form1.e1.value == document.form1.e5.value)){
			alert("Refer to friend email cannot be duplicated. Please check your friend email address"); 
			return;
		}
	
	if(document.form1.e2.value != ""){
		
		if((document.form1.e2.value == document.form1.e3.value) || (document.form1.e2.value == document.form1.e4.value) || (document.form1.e2.value == document.form1.e5.value) ){
			alert("Refer to friend email cannot be duplicated. Please check your friend email address"); 
			return;
		}
	}
	
	if(document.form1.e3.value != ""){
		if( (document.form1.e3.value == document.form1.e4.value) || (document.form1.e3.value == document.form1.e5.value) ){
			alert("Refer to friend email cannot be duplicated. Please check your friend email address"); 
			return;
		}
	}
	if(document.form1.e4.value != ""){
		if( (document.form1.e4.value == document.form1.e5.value) ){
			alert("Refer to friend email cannot be duplicated. Please check your friend email address"); 
			return;
		}
	}
	
	//check if there is a age restriction.
	
//send the form	
document.form1.submit();
}

	//check uncheck all CHECKBOX
	function checkUncheckAll(theElement) {
     var theForm = theElement.form, z = 0;
	 for(z=0; z<theForm.length;z++){
      if(theForm[z].type == 'checkbox' && theForm[z].name != 'checkall'){
	  theForm[z].checked = theElement.checked;
	  }
     }
    }

function direct(){
	if (trim(document.form1.first.value) == ""){
		alert("Please key in your first name.");
		document.form1.first.focus();
		return;
	}
	if (trim(document.form1.last.value) == ""){
		alert("Please key in your last name.");
		document.form1.last.focus();
		return;
	}
	if (document.form1.email.value == "" || !check_email(document.form1.email.value)){
		alert("Please key in a valid email.");
		document.form1.email.focus();
	return;
	}
	if (trim(document.form1.Category.value) == ""){
		alert("Please choose your profession.");
		document.form1.Category.focus();
		return;
	}
	if (trim(document.form1.SubCat.value) == ""){
		alert("Please choose your speciality.");
		document.form1.SubCat.focus();
		return;
	}
	
	//interested e-alert
	if(document.form1.e1.checked == false && document.form1.e2.checked == false && document.form1.e3.checked == false  && document.form1.e4.checked == false && document.form1.e5.checked == false ){
			alert("Please tick at least one of your interested e-Alert");
			return 0;
	}

	document.form1.submit();
}

function temp_direct(){
	//alert("lasdjkf laskjdf lasdfjk l");
	document.form1.submit();
}
