//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 ismaxlength(obj){
	var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
	if (obj.getAttribute && obj.value.length>mlength)
	obj.value=obj.value.substring(0,mlength)
}

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() {

	//alert(event.keyCode);

	if ((event.keyCode < 48)||(event.keyCode > 57 )) {
	  if (event.keyCode != 43)
		{event.returnValue=false;}
	}
}
function isDigit(event) {
    if (event.keyCode) code = event.keyCode;
	else if (event.which) code = event.which;
	
	if ((code < 48)||(code > 57 )) {
	  if (code != 43        //+
	        && code != 8    //back
	        && code != 9   //tab
	        && code != 46)  //delete
		{return 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 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(){
	//mandatory for organization and department
	if (document.form1.organization.value == ""){
		alert("Please key in your Organization.");
		document.form1.organization.focus();
		return;
	}
	
	if (document.form1.department.value == ""){
		alert("Please key in your Department.");
		document.form1.department.focus();
		return;
	}
	
	//additional the message
	if (document.form1.message.value == ""){
		alert("Please key in your message.");
		document.form1.message.focus();
		return;
	}

/////////END of additional e-FORM

	//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].name != 'trialFirstConsult' && theForm[z].name != 'trialMdConsult' && theForm[z].name != 'chkAgreement')){
					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.country.value) == ""){
		alert("Please choose your country.");
		document.form1.country.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;
	}
	if (trim(document.form1.organization.value) == ""){
		alert("Please key in your organization.");
		document.form1.organization.focus();
		return;
	}
	if (trim(document.form1.department.value) == ""){
		alert("Please key in your department.");
		document.form1.department.focus();
		return;
	}


	//check the product trial
	if ( (document.form1.trialMdConsult(0).checked == false) && (document.form1.trialMdConsult(1).checked == false) ){
		alert("Please select your product to Trial");
		return 0;
	}


	//interested e-alert
	if(document.form1.e1.checked == false && document.form1.e2.checked == false && document.form1.e3.checked == false  &&  document.form1.e4.checked == false ){
			alert("Please tick at least one of your interested subscribtion");
			return 0;
	}

	document.form1.submit();
}

function direct(form1){
	if (trim(form1.first.value) == ""){
		alert("Please key in your first name.");
		form1.first.focus();
		return false;
	}
//	if (trim(form1.last.value) == ""){
//		alert("Please key in your last name.");
//		form1.last.focus();
//		return false;
//	}
	if (form1.email.value == "" || !check_email(form1.email.value)){
		alert("Please key in a valid email.");
		form1.email.focus();
		return false;
	}
	if (trim(form1.country.value) == ""){
		alert("Please choose your country.");
		form1.country.focus();
		return false;
	}
	if (form1.Category.selectedIndex < 1){
		alert("Please choose your profession.");
		form1.Category.focus();
		return false;
	}
	if (form1.SubCat.selectedIndex < 1){
		alert("Please choose your specialty.");
		form1.SubCat.focus();
		return false;
	}
	if (trim(form1.organization.value) == ""){
		alert("Please key in your organization.");
		form1.organization.focus();
		return false;
	}
	if (trim(form1.department.value) == ""){
		alert("Please key in your department.");
		form1.department.focus();
		return false;
	}


	//check the product trial
	if ( (form1.trialMdConsult1.checked == false) && (form1.trialMdConsult2.checked == false) ){
		alert("Please select your product to Trial");
		return false;
	}


	//interested e-alert
	if(form1.e1.checked == false && form1.e2.checked == false && form1.e3.checked == false  &&  form1.e4.checked == false ){
			alert("Please tick at least one of your interested subscribtion");
			return false;
	}
	
	//user agreement
	if(form1.chkAgreement.checked == false){
			alert("You need to accept the Terms and Condition before you register.");
			return false;
	}

	return true;
}

function temp_direct(){
	document.form1.submit();
}

function feedback_validation(){
	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 a valid email.");
		document.form1.youremail.focus();
	return;
	}
	if (trim(document.form1.comment.value) == ""){
		alert("Please key in your comment.");
		document.form1.comment.focus();
		return;
	}
	document.form1.submit();
}