﻿/*-------------------------------------------------------------------------------
	*	FileName ; $common.js
	* Function : validative functions
	* CreatedDate : 01/02/2003
	* LastUpdate : 25/10/2004
-------------------------------------------------------------------------------*/
/*The following functions are described by their names*/

//-------------------------------------------------------------------------------
function isSpaces (Str) {
	if (isEmpty (Str)) return true;
	var i = 0;	
	while (Str.charAt(i)==' ' && i<Str.length) {
		i++;
	}
	if (i== Str.length) return true;
	return false;
}

//-------------------------------------------------------------------------------
function isEmpty(Str) {
	empty = (Str === "") ? true :  false;
	return empty;
}

//-------------------------------------------------------------------------------
function isNumber(Digit) {
	return /^\d+[\.\d*]?$/.test(Digit);
}

//------------------------------------------------------------------------------
function isAlphabet(Digit) {
	return /^[a-zA-Z]$/.test(Digit);
}

//-------------------------------------------------------------------------------
function isInteger(Str) {
	return /^[+-]?\d+$/.test(Str);
}

//-------------------------------------------------------------------------------
function isFloat(Str) {
		return /^[+-]?\d+\.{1}\d*$/.test(Str);
}

//-------------------------------------------------------------------------------
function isCurrency(Str) {
		return /^\d+[.]{1}[0-9]{2,}$/.test(Str);
}

//-------------------------------------------------------------------------------
function isDate(Str) {
	var bool1=/^[0]?\d[\/|-][0-2]\d[\/|-]\d{4}$/.test(Str);		//0x month format 0X-2X date format
	var bool2=/^[1][0-2][\/|-][0-2]\d[\/|-]\d{4}$/.test(Str);	//1x month format 3X date format
	var bool3=/^[1][0-2][\/|-][3][0,1][\/|-]\d{4}$/.test(Str);	
	var bool4=/^[0]?\d[\/|-][3][0,1][\/|-]\d{4}$/.test(Str);
	return ((bool1)||(bool2)||(bool3)||(bool4));
}

//-------------------------------------------------------------------------------
function isValidDate(nDay,nMonth,nYear) {
	if (nMonth==2 && nDay > 29) return false;
	if (nMonth==2 && nDay ==29 && nYear % 4 !=0) return false;
	if (nDay==31 && (nMonth == 4 || nMonth == 6 || nMonth == 9 || nMonth == 11 )) return false;
	return true;
}

//-------------------------------------------------------------------------------
function isTime(Str) {
		var bool1 = /^[0-1]?\d:[0-5]\d(:[0-5]\d)?$/.test(Str);
		var bool2 = /^[2][0-3]:[0-5]\d(:[0-5]\d)?$/.test(Str);
		return ((bool1)||(bool2));
}

//-------------------------------------------------------------------------------
function isDateTime(Str) {
		var str = RemoveSpace(Str).split(' ');
		return isDate(str[0]) && isTime(str[1]);
}

//-------------------------------------------------------------------------------
function isDomain (Str) {
	// The pattern for matching all special characters. 
  	//These characters include ( ) < > [ ] " | \ / ~ ! @ # $ % ^ & ? ` ' : ; , 
	var specialChars="\\(\\)<>#\\$&\\*!`\\^\\?~|/@,;:\\\\\\\"\\.\\[\\]";
	// The range of characters allowed in a username or domainname. 
	// It really states which chars aren't allowed. 
	var validChars="\[^\\s" + specialChars + "\]";
	 // An atom (basically a series of  non-special characters.) 
	var atom=validChars + '+';
	// The structure of a normal domain 
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	
	// Check if IP
	var ipDomainPat=/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
	var IPArray=Str.match(ipDomainPat);
	if (IPArray!=null) {
  	// this is an IP address
	 	 for (var i=1;i<=4;i++) {
	    		if (IPArray[i]>255) {
	 			return false
	   		 }
   		 }
	}
	// Check Domain
	var domainArray=Str.match(domainPat)
	if (domainArray==null) {
    		return false;
	}

	/* domain name seems valid, but now make sure that it ends in a
	 three-letter word (like com, edu, gov ... ) or a two-letter word,
   	representing country (uk, vn) or a four-letter word (.info), and that there's a hostname preceding 
   	the domain or country. */

	/* Now we need to break up the domain to get a count of how many atoms
	it consists of. */
	var atomPat=new RegExp(atom,"g")
	var domArr=Str.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>4) {
	 // the address must end in a two letter or three letter word or four-letter word.
		return false;
	}

	// Make sure there's a host name preceding the domain.
	if (len<2) {
   		 return false;
	}

	return true;
}

//-------------------------------------------------------------------------------
function isOpenDomain (Str) { // E.g : lengvu.saigonnet.vn:81 or 203.162.6.65:8080
	var pos=Str.indexOf(':');
	if (pos==-1) {
		return (isDomain(Str))
	}
	else {
		domain=Str.substring(0,pos);
		openDomain = Str.substring(pos,Str.length);
	}
		return ((/^[\:]{1}\d+$/.test(openDomain))&&(isDomain(domain)));
}

//-------------------------------------------------------------------------------
function isUser (Str) {
	var specialChars="\\(\\)<>#\\$&\\*!`\\^\\?~|/@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	/* The pattern applies if the "user" is a quoted string (in
   	which case, there are no rules about which characters are allowed
   	and which aren't; anything goes).  E.g. "le nguyen vu"@webtome.com
   	is a valid (legal) e-mail address. */
	var quotedUser="(\"[^\"]*\")";
	var atom=validChars + '+'
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	// See if "user" is valid 
	if (Str.match(userPat)==null) {
    		return false ;
	}
	return true;
}

//-------------------------------------------------------------------------------
function isURL(Str) { //not include http://
	var pos=Str.indexOf('/');
	var domain = (pos==-1)?Str:Str.substring(0,pos);
	var subURL = (pos==-1)?'':Str.substring(pos,Str.length);
	if (!isOpenDomain(domain)) {
		return false;
	}
	if ((subURL=='')||(subURL.length==1)) {
		return true;
	}
	var subPat = /^\/[^\/\\]+\.?[^\/\\]+(\/[^\/\\]*\.{0,1}[^\/\\]*)*$/;
	var ArrayURL=subURL.match(subPat);
	if (ArrayURL==null) {
		return false;
	}
	return true;
}

//-------------------------------------------------------------------------------
function isEmail (emailStr) {
	/* The pattern for matching fits the user@domain format. */
	var emailPat=/^(.+)@(.+)$/ ;
	var matchArray=emailStr.match(emailPat);
	if (matchArray==null) {
 	 /* Too many/few @'s or something; basically, this address doesn't
    	 even fit the general mould of a valid e-mail address. */
		return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];

	// See if "user" is valid 
	if (!isUser(user)) {
    	// user is not valid
   		 return false ;
	}

	// Check Domain
	if (!isDomain(domain)) {
   		return false;
	}
	return true;
}
function isUserName(strUserName){
	return /^[a-z0-9_\-]{4,15}$/.test(strUserName);
}
//-------------------------------------------------------------------------------
function isPhone(strPhone) {
	return  /^(\d{6,15})$/.test(strPhone);
	//return  /^[\+\-\(]?(\d*[\.\-\(\)\s\+]*\d*)*$/.test(strPhone);
}

//-------------------------------------------------------------------------------
function checkNumRange (value, nMin,nMax){
	if (!isInteger(value)) return false;
	if (value <nMin || value > nMax ) return false;
	return true;
}

//-------------------------------------------------------------------------------
function isFlash(fileName) {
  if (fileName=='') {
   	return false;   	
  }
  var ext = getExtension(fileName).toLowerCase();
  var e;
for(e in arrFlashFiles){
	if(arrFlashFiles[e]==ext) return true;
}
return false;
}

//-------------------------------------------------------------------------------
function isPix(fileName) {
  if (fileName=='') {
   	return false;   	
  }
  var ext = getExtension(fileName).toLowerCase();
  var e;
for(e in arrPixFiles){
	if(arrPixFiles[e]==ext) return true;
}
return false;
}

//-------------------------------------------------------------------------------
function getExtension(fileName){
		return fileName.substr(fileName.lastIndexOf(".")+1);
}


function checkContact(f){
			f.sender.value = Trim(f.sender.value);
			f.address.value = Trim(f.address.value);
			f.phone.value = Trim(f.phone.value);
			f.email.value = Trim(f.email.value);
			f.content.value = Trim(f.content.value);
			f.title.value = Trim(f.title.value);
			
			if(f.sender.value==''){
				alert(msg_pls_yourname);
				f.sender.focus();
				return false;
			}
			if(f.address.value==''){
				alert(msg_pls_address);
				f.address.focus();
				return false;
			}
			if((f.email.value !='') && (!isEmail(f.email.value))){
				alert(msg_pls_valild_email);
				f.email.focus();
				return false;
			}
			if(f.phone.value==''){
				alert(msg_pls_phone);
				f.phone.focus();
				return false;
			}
			
			if(f.title.value==''){
				alert(msg_pls_title);
				f.title.focus();
				return false;
			}
			if(f.content.value==''){
				alert(msg_pls_content);
				f.content.focus();
				return false;
			}
			return true;
		}
	
function checkEmail(f){
		f.email.value = Trim(f.email.value)
		f.name.value = Trim(f.name.value)
		if(!isEmail(f.email.value)){
			alert(msg_pls_valild_email);
			f.email.focus();
			return false;
		}
		var vleft = (screen.width - 400)/2;
		var vtop = (screen.height - 400)/2;
		window.open('newsletter.php?name='+f.name.value+'&email='+f.email.value,'','left='+vleft+',top='+vtop+',width=400,height=200');
		return false;
	}

function checkSendMail2Friend(f){
	f.email.value = Trim(f.email.value); 
	if(!isEmail(f.email.value)){
			alert(msg_pls_valild_email);
			f.email.focus();
			return false;
		}
	window.open('send_mail.php?email_to_1='+f.email.value,'','width=400,height=300');
	return false;
}
function AcceptNumbersOnly() {
	switch (event.keyCode) {
		case 48:
		case 49:
		case 50:
		case 51:
		case 52:
		case 53:
		case 54:
		case 55:
		case 56:
		case 57:
		case 8:
		case 9:
		case 35:
		case 36:
		case 37:
		case 39:
		case 46:
		case 96:
		case 97:
		case 98:
		case 99:
		case 100:
		case 101:
		case 102:
		case 103:
		case 104:
		case 105:
			break;
		case 86:
			if (event.ctrlKey) {
				event.returnValue=true;
			}	else {
				event.returnValue=false;
			}
			break;
		case 45:
			event.returnValue=true;
			break;
		default:
			event.returnValue=false;
			break;
	}
}
function AcceptNumbersAndPlus() {
	var compareString = '0123456789+';
		var mainString=String.fromCharCode(window.event.keyCode);
		if(compareString.indexOf(mainString)!=-1)
			return;
		else
			window.event.returnValue=false;
}


function checkRegister(f){
	
	f.first_name.value = Trim(f.first_name.value);
	f.last_name.value = Trim(f.last_name.value);
	f.birthday.value = Trim(f.birthday.value);
	f.email.value = Trim(f.email.value);
	
	f.address.value = Trim(f.address.value);
	f.address1.value = Trim(f.address1.value);
	f.country.value = Trim(f.country.value);
	f.state.value = document.getElementById('state_input').value;
	f.postcode.value = Trim(f.postcode.value);
	
	f.phone.value = Trim(f.phone.value);
	
	f.retype_email.value = Trim(f.retype_email.value);
	f.password.value = Trim(f.password.value);
	f.retype.value = Trim(f.retype.value);
	
	if(f.first_name.value==''){
		alert('Please input your frst name !');
		f.first_name.focus();
		return false;
	}
	if(f.last_name.value==''){
		alert('Please input your last name !');
		f.last_name.focus();
		return false;
	}
	if(f.birthday.value==''){
		alert('Please input your date of birth!');
		f.birthday.focus();
		return false;
	}
	if(!isDate(f.birthday.value)){
		alert('Please input your valid date of birth!');
		f.birthday.focus();
		return false;
	}
	if(!isEmail(f.email.value)){
		alert('Please input your valid email !');
		f.email.focus();
		return false;
	}
	
	if(f.address.value==''){
		alert('Please input your street address !');
		f.address.focus();
		return false;
	}
	
	
	if(f.country.value==''){
		alert('Please choose your country !');
		f.country.focus();
		return false;
	}
	if(f.state_input.value==''){
		alert('Please choose state/province !');
		f.state_input.focus();
		return false;
	}
	if(f.postcode.value==''){
		alert('Please input your post code !');
		f.postcode.focus();
		return false;
	}
	if(f.phone.value==''){
		alert('Please input your telephone number !');
		f.phone.focus();
		return false;
	}
	if(f.retype_email.value==''){
		alert('Please input Confirm your email address !');
		f.retype_email.focus();
		return false;
	}
	if(f.retype_email.value!=f.email.value){
			alert('Confirm email is not match, please check again !');
			f.retype_email.focus();
			return false;
	}
	if(f.password.value==''){
		alert('Please input your password !');
		f.password.focus();
		return false;
	}
	if(f.retype.value==''){
		alert('Please input password confirmation !');
		f.retype.focus();
		return false;
	}
	if(f.retype.value!=f.password.value){
			alert('Confirm password is not match, please check again !');
			f.retype.focus();
			return false;
	}
	return true;
}

function checkRegister_b(f){
	
	f.company_name.value = Trim(f.company_name.value);
	f.position.value = Trim(f.position.value);
	f.first_name.value = Trim(f.first_name.value);
	f.last_name.value = Trim(f.last_name.value);
	f.birthday.value = Trim(f.birthday.value);
	f.email.value = Trim(f.email.value);
	
	f.address.value = Trim(f.address.value);
	f.address1.value = Trim(f.address1.value);
	f.country.value = Trim(f.country.value);
	f.state.value = document.getElementById('state_input').value;
	f.postcode.value = Trim(f.postcode.value);
	
	f.phone.value = Trim(f.phone.value);
	
	f.retype_email.value = Trim(f.retype_email.value);
	f.password.value = Trim(f.password.value);
	f.retype.value = Trim(f.retype.value);
	
	if(f.company_name.value==''){
		alert('Please input your company !');
		f.company_name.focus();
		return false;
	}
	if(f.address.value==''){
		alert('Please input your street address !');
		f.address.focus();
		return false;
	}
	
	if(f.country.value==''){
		alert('Please choose your country !');
		f.country.focus();
		return false;
	}
	if(f.state_input.value==''){
		alert('Please choose state/province !');
		f.state_input.focus();
		return false;
	}
	if(f.postcode.value==''){
		alert('Please input your post code !');
		f.postcode.focus();
		return false;
	}
	if(f.position.value==''){
		alert('Please input your position !');
		f.position.focus();
		return false;
	}
	if(f.first_name.value==''){
		alert('Please input your frst name !');
		f.first_name.focus();
		return false;
	}
	if(f.last_name.value==''){
		alert('Please input your last name !');
		f.last_name.focus();
		return false;
	}
	if(f.birthday.value==''){
		alert('Please input your date of birth!');
		f.birthday.focus();
		return false;
	}
	if(!isDate(f.birthday.value)){
		alert('Please input your valid date of birth!');
		f.birthday.focus();
		return false;
	}
	
	
	if(f.phone.value==''){
		alert('Please input your telephone number !');
		f.phone.focus();
		return false;
	}
	if(!isEmail(f.email.value)){
		alert('Please input your valid email !');
		f.email.focus();
		return false;
	}
	if(f.retype_email.value==''){
		alert('Please input Confirm your email address !');
		f.retype_email.focus();
		return false;
	}
	if(f.retype_email.value!=f.email.value){
			alert('Confirm email is not match, please check again !');
			f.retype_email.focus();
			return false;
	}
	if(f.password.value==''){
		alert('Please input your password !');
		f.password.focus();
		return false;
	}
	if(f.retype.value==''){
		alert('Please input password confirmation !');
		f.retype.focus();
		return false;
	}
	if(f.retype.value!=f.password.value){
			alert('Confirm password is not match, please check again !');
			f.retype.focus();
			return false;
	}
	return true;
}

function checkLogin(f){
		
		if(Trim(f.email.value)=='') {
				alert('Please input your email to login !');
				f.email.focus();
				return false;
		}
		if(f.password.value=='') {
				alert('Please input your password to login !');
				f.password.focus();
				return false;
		}
		f.action = '?pgid=login';
		return true;
		
}

function checkProfile(f){	
	f.first_name.value = Trim(f.first_name.value);
	f.last_name.value = Trim(f.last_name.value);
	f.birthday.value = Trim(f.birthday.value);
	f.email.value = Trim(f.email.value);
	f.phone.value = Trim(f.phone.value);
	f.password.value = Trim(f.password.value);
	
	if(f.first_name.value==''){
		alert('Please input your frst name !');
		f.first_name.focus();
		return false;
	}
	if(f.last_name.value==''){
		alert('Please input your last name !');
		f.last_name.focus();
		return false;
	}
	if(!isDate(f.birthday.value)){
		alert('Please input your valid date of birth!');
		f.birthday.focus();
		return false;
	}
	if(!isEmail(f.email.value)){
		alert('Please input your valid email !');
		f.email.focus();
		return false;
	}
	
	if(f.phone.value==''){
		alert('Please input your telephone number !');
		f.phone.focus();
		return false;
	}
	if(f.password.value==''){
		alert('Please input your password !');
		f.phone.focus();
		return false;
	}

	return true;
}

function checkForgot(f){
	f.email.value = Trim(f.email.value);
	if(f.email.value==''){
		alert('Please input your email !');
		f.email.focus();
		return false;
	}
}
	function checkEmail(f){
		f.email.value = Trim(f.email.value)
		if(!isEmail(f.email.value)){
			alert('Please input your valid email, thank you !');
			f.email.focus();
			return false;
		}
		window.open('newsletter.php?email='+f.email.value,'','width=400,height=150');
		return false;
	}

