//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 checkDate(mm,dd,yyyy){
	leapyear =false;
	validdate=true;
	//alert(mm+dd+yyyy);
	
	if( yyyy%4 == 0 ){
		leapyear=true;
		//alert("Leap Year");
	}
	
	if( mm==1 || mm==3 || mm==5 || mm==7 || mm==8 || mm==10 || mm==12 ){
		if(dd > 31){ 
			validdate=false; 
		}
	}
	else if(mm==4 || mm==6 || mm==9 || mm==11){
		if(dd > 30){ 
			validdate=false; 
		}
	}
	else if(mm == 2){
		if(leapyear==true){
			if(dd > 29){ 
				validdate=false; 
			}
		}
		else{
			if(dd > 28){ 
				validdate=false; 
			}
		}
	}
	if(validdate == false){
		alert("Date is not valid.");
		return 0;
		
	}
	else{
		return 1;
	}
	
	
}

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 doit(){
		
var status1=true

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.emailAdd.value == "" || !check_email(document.form1.emailAdd.value)){
	alert("Please key in a valid email address.");
	document.form1.emailAdd.focus();
	return;
}




	



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(){
	document.form1.submit();
}

function textCounter(field,cntfield,maxlimit) {
if (field.value.length > maxlimit) // if too long...trim it!
field.value = field.value.substring(0, maxlimit);
// otherwise, update 'characters left' counter
else
cntfield.value = maxlimit - field.value.length;
}

//clear textbox
function cleartts(form) { 
document.form1.Location.value="";} 




function radio_button_checker()
{
// set var radio_choice to false
var radio_choice = false;

// Loop from zero to the one minus the number of radio button selections
for (counter = 0; counter < recUpdate.length; counter++)
{
// If a radio button has been selected it will return true
// (If not it will return false)
if (recUpdate[counter].checked)
radio_choice = true; 
}

if (!radio_choice)
{
// If there were no selections made display an alert box 
alert("Please select a letter.")
return (false);
}
return (true);
}

