﻿function isDate( s )
{
var sDay, sMonth, sYear, nMonth, nDay, nYear, nSep1, nSep2;
nSep1 = s.indexOf( "/" );	if ( nSep1 < 0 ) return false;
nSep2 = s.lastIndexOf( "/" );	if ( nSep2 < 0 ) return false;
if ( nSep1 == nSep2 ) return false;

sDay = s.substring( 0, nSep1  );
 sMonth = s.substring( nSep1 + 1, nSep2 );
sYear = s.substring( nSep2+1 );
if ( !sMonth.length || !sDay.length || !sYear.length ) return false;
// isNaN(empty) is false
if ( isNaN(sMonth) || isNaN(sDay) || isNaN(sYear) ) return false;
nMonth = parseInt(sMonth,10); nDay = parseInt(sDay,10); nYear = parseInt(sYear,10);
if ( nMonth<=0 || nDay<=0 || nYear<=0 || sYear.length != 4) return false;
if ( nMonth > 12 ) return false;
if ((nYear<1753)||(nYear>9999))	return false;
			

if (nMonth==1 || nMonth==3 || nMonth==5 || nMonth==7 || nMonth==8 || nMonth==10 || nMonth==12 )
	if ( nDay > 31 ) return false; 
if (nMonth==4 || nMonth==6 || nMonth==9 || nMonth==11 )
	if ( nDay > 30 ) return false; 
if (nMonth==2) {
	if ( (nYear % 4 == 0) && (nYear % 100 != 0)) { // leap year
		if ( nDay > 29 ) return false;
	} else if ( nDay > 28 ) return false;
}
return true;
} // isDate function

//============================================================================================================
// Form Validation
//============================================================================================================
function validate_ex(theForm, _ErrMsg, arrNotRequired) {
	validForm = true;
	firstError = null;
	errorstring = '';
	var x = document.forms[theForm].elements;
	var arr = ','+arrNotRequired.toString()+ ',';
	for (var i=0;i<x.length;i++) {
		if (arr.indexOf(','+x[i].name+',')==-1) {
			if (!x[i].value)
			{
				var str="";
				str = "".concat(x[i].type);
				if ((str.toString() != "undefined") && (str.toString() != "button") && (str.toString() != "hidden"))
				{
					writeError(x[i], _ErrMsg);
					if (firstError ==null)
					firstError = x[i];
				}
			}
			if (x[i].name == 'txtEmail')
			{			
				var strEmail = /^[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]$/;
				if(!strEmail.test(x['txtEmail'].value))
					writeError(x['txtEmail'],'e-mail is not valid !');
			}
			if (x[i].name == 'txtConfirm')
			{
				if(x['txtConfirm'].value != x['txtPassword'].value)
					writeError(x['txtConfirm'],'confirm password is not valid !');
			}
			if(x[i].name == 'txtPassword')
			{
				if(x['txtPassword'].value.length < 6)
					writeError(x['txtPassword'],'password must be longer than 6 characters !')
			}
			if(x[i].name == 'txtoldpass')
			{
				if(x['txtoldpass'].value.length < 6)
					writeError(x['txtoldpass'],'password must be longer than 6 characters !')
			}
			if(x[i].name == 'txtpassword')
			{
				if(x['txtpassword'].value.length < 6)
					writeError(x['txtpassword'],'password must be longer than 6 characters !')
			}
			if (x[i].name == 'txtconfirm')
			{
				if(x['txtconfirm'].value != x['txtpassword'].value)
					writeError(x['txtconfirm'],'confirm password is not valid !');
			}
			if (x[i].name == 'txtnewpass')
			{
				if(x['txtnewpass'].value.length < 6)
					writeError(x['txtnewpass'],'password must be longer than 6 characters !')
			}
			if (x[i].name == 'txtemail')
			{			
				var strEmail = /^[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]$/;
				if(!strEmail.test(x['txtemail'].value))
					writeError(x['txtemail'],'e-mail is not valid !');
			}			
			if (x[i].name == 'year'){
				if (x['year'].value == 0){
					writeError(x['year'],'(*)');
					writeError(x['month'],'');
					writeError(x['day'],'');
				}
				else{
					if (x['month'].value == 0){
						writeError(x['year'],'(*)');
						writeError(x['month'],'');
						writeError(x['day'],'');
					}
					else{
						if (x['day'].value == 0){
							writeError(x['year'],'(*)');
							writeError(x['month'],'');
							writeError(x['day'],'');
						}
						else{
							removeNodeError('bd_year');
							removeNodeError('bd_month');
							removeNodeError('bd_day');
						}
						
					}
				}
			}
			if (x[i].name == 'year'){
				var strDate = x['day'].value + '/' + x['month'].value + '/' + x['year'].value;				
				if(!isDate(strDate)) {
					writeError(x['year'], 'Invalid date');
				}
			}
			if (x[i].name == 'sltcountry'){
				if (x['sltcountry'].value == 0){
					writeError(x['sltcountry'],'(*)');
					
				}
			}
		}
	}
	if (!W3CDOM)
		alert(errorstring);
	if (firstError)
		firstError.focus();
	if (validForm)
		return true;
		
	return false; // I return false anyway to prevent actual form submission. Don't do this at home!
}
function validate(theForm, _ErrMsg ) {
	validForm = true;
	firstError = null;
	errorstring = '';
	var x = document.forms[theForm].elements;
	for (var i=0;i<x.length;i++) {
		
		if (!x[i].value)
		{
			var str="";
			str = "".concat(x[i].type);
			if ((str.toString() != "undefined") || (str.toString() != "button") || (str.toString() != "hidden"))
			{
				writeError(x[i], _ErrMsg);
				if (firstError ==null)
				firstError = x[i];				
			}
		}
		if (x[i].name == 'txtemail')
		{			
			var strEmail = /^[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]$/;
			if(!strEmail.test(x['txtemail'].value))
				writeError(x['txtemail'],'  *');
		}
		if (x[i].name == 'txtEmail2')
		{			
			var strEmail = /^[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]$/;
			if(!strEmail.test(x['txtEmail2'].value))
				writeError(x['txtEmail2'],'Hay nhap email hop le !');
		}
	}
	
	if (!W3CDOM)
		alert(errorstring);
	if (firstError)
		firstError.focus();
	if (validForm)
		return true;
		
	return false; // I return false anyway to prevent actual form submission. Don't do this at home!
}

function writeError(obj,message) {
	validForm = false;
	if (obj.hasError) return;
	if (W3CDOM) {
		obj.className += ' error';
		obj.onchange = removeError;
		var sp = document.createElement('span');
		sp.className = 'error';
		sp.appendChild(document.createTextNode(message));
		obj.parentNode.appendChild(sp);
		obj.hasError = sp;
	}
	else {
		errorstring += obj.name + ': ' + message + '\n';
		obj.hasError = true;
	}
	if (!firstError)
		firstError = obj;
}

function removeError() {
	this.className = this.className.substring(0,this.className.lastIndexOf(' '));
	this.parentNode.removeChild(this.hasError);
	this.hasError = null;
	this.onchange = null;
}
function removeNodeError(_nodeId){
	var obj = document.getElementById(_nodeId);
	try{
		obj.className = obj.className.substring(0,obj.className.lastIndexOf(' '));
		obj.parentNode.removeChild(obj.hasError);
		obj.hasError = null;
		obj.onchange = null;
	}
	catch(ex){
		
	}
}


//=======================================================================================================================
