//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 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 IsEmpty(v){
	varValue = Trim(v);
	if (varValue.length == 0){ return true; }else{ return false; }
}

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 IsDigit(o,e) {
	if (!e) var e = window.event
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	if (((code < 48)||(code > 57 ))) 
	{
		//if (code != 8 && code != 46 && code != 9 && code != 45 && code != 40 && code != 41)
		//{
			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 validate_form() 
{
if (trim(document.getElementById("name").value) == ""){
	alert("Please key in your name.");
	document.getElementById("name").focus();
	return;
}



if (document.getElementById("email").value == "" || !check_email(document.getElementById("email").value)){
	alert("Please key in a valid email.");
	document.getElementById("email").focus();
	return;
}

alert("Thank you. Your information has been submitted.")

 document.form1.submit();
  
}




