﻿/*-------------------------------------------------------------------------------
	*	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 (parseInt(nMonth)==2 && parseInt(nDay) > 29) return false;
	if (parseInt(nMonth)==2 && parseInt(nDay) ==29 && parseInt(nYear) % 4 !=0) return false;
	if (parseInt(nDay)==31 && (parseInt(nMonth) == 4 || parseInt(nMonth) == 6 || parseInt(nMonth) == 9 || parseInt(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_\-]{8,51}$/.test(strUserName);
	//return /^[A-Za-z0-9!@#$%_\^\&\*\.\?]{8,51}$/.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 CheckAll_bottom(f)
{
	if(f.allbox_up.checked) {
		for(var i=0;i<f.elements.length;i++)
		{
			var e=f.elements[i];
			if((e.name!='allbox_up')&&(e.type=='checkbox')&&(e.disabled==false)) {
				if(!e.checked) {
					e.click();
				}
			}
		}
	}else {
		for(var i=0;i<f.elements.length;i++)
		{
			var e=f.elements[i];
			if((e.name!='allbox_up')&&(e.type=='checkbox')&&(e.disabled==false)) {
				e.click();
			}
		}
	}
}
function CheckAll_bottom_v2(f)
{
	if(f.allbox_up.checked) {
		for(var i=0;i<f.elements.length;i++)
		{
			var e=f.elements[i];
			if((e.name!='allbox_up')&&(e.type=='checkbox')&&(e.disabled==false)) {
				if(!e.checked) {
					e.click();
				}
			}
		}
	}else {
		for(var i=0;i<f.elements.length;i++)
		{
			var e=f.elements[i];
			if((e.name!='allbox_up')&&(e.type=='checkbox')&&(e.disabled==false)) {
				e.click();
			}
		}
	}
}
 function pasteClass(e,classSelect,classNormal)
    {
	var r = null;
	if (e.parentNode && e.parentNode.parentNode) {
	    r = e.parentNode.parentNode;
	}
	else if (e.parentElement && e.parentElement.parentElement) {
	    r = e.parentElement.parentElement;
	}
	if (r) {
		r.className = e.checked ? classSelect : classNormal;
	}
    }

 function nothing(e,bg)
    {
	var r = null;
	if (e.parentNode && e.parentNode.parentNode) {
	    r = e.parentNode.parentNode;
	}
	else if (e.parentElement && e.parentElement.parentElement) {
	    r = e.parentElement.parentElement;
	}
	if (r) {
		r.style.backgroundColor=bg;
	}
    }

											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('Please input your full name !');
													f.sender.focus();
													return false;
												}
												if(!isEmail(f.email.value)){
													alert('Please input valid email, we\'ll reply to you via this email !');
													f.email.focus();
													return false;
												}
												if(f.title.value==''){
													alert('Please input the subject !');
													f.title.focus();
													return false;
												}
												if(f.content.value==''){
													alert('Please input the content !');
													f.content.focus();
													return false;
												}
												return true;
											}
											function checkRegister(f){
												f.username.value = Trim(f.username.value);
												f.first_name.value = Trim(f.first_name.value);
												f.last_name.value = Trim(f.last_name.value);
												f.address1.value = Trim(f.address1.value);
												f.address2.value = Trim(f.address2.value);
												f.country.value = Trim(f.country.value);
												f.phone1.value = Trim(f.phone1.value);
												f.phone2.value = Trim(f.phone2.value);
												f.phone3.value = Trim(f.phone3.value);
												var phone = f.phone1.value + f.phone2.value + f.phone3.value ;
												f.email.value = Trim(f.email.value);
												//f.note.value = Trim(f.note.value);
												if(f.username.value==''){
													alert('Please select username !');
													f.username.focus();
													return false;
												}
												if(!isUserName(f.username.value)){
													alert('Your username is not valid.\nUsername include [a-z0-9_-] , more than 3 characters and less than 16 characters !\nPlease check again !');
													f.username.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.address1.value==''){
													alert('Please input your address !');
													f.address1.focus();
													return false;
												}
												*/
												if(f.country.value==''){
													alert('Please input your country !');
													f.country.focus();
													return false;
												}
												if(!isEmail(f.email.value)){
													alert('Please input your valid email !');
													f.email.focus();
													return false;
												}
												if((phone!=='')&&(!isPhone(phone))){
													alert('Your phone number is not valid !')	
													f.phone1.focus();
													return false;
												}
												return true;
											}

function checkLogin(f){
		if(Trim(f.username.value)=='') {
				alert('Vui lòng nhập username để login !');
				f.username.focus();
				return false;
		}
		if(f.password.value=='') {
				alert('Vui lòng nhập password để login !');
				f.password.focus();
				return false;
		}
		return true;
}

											function checkProfile(f){
												f.first_name.value = Trim(f.first_name.value);
												f.last_name.value = Trim(f.last_name.value);
												f.address1.value = Trim(f.address1.value);
												f.address2.value = Trim(f.address2.value);
												f.phone1.value = Trim(f.phone1.value);
												f.phone2.value = Trim(f.phone2.value);
												f.phone3.value = Trim(f.phone3.value);
												var phone = f.phone1.value + f.phone2.value + f.phone3.value ;
												f.email.value = Trim(f.email.value);
												//f.address.value = Trim(f.address.value);
												f.country.value = Trim(f.country.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.address1.value==''){
													alert('Please input your address !');
													f.address1.focus();
													return false;
												}
												*/
												if(f.country.value==''){
													alert('Please input your country !');
													f.country.focus();
													return false;
												}
												if(!isEmail(f.email.value)){
													alert('Please input your valid email !');
													f.email.focus();
													return false;
												}
												if((phone!=='')&&(!isPhone(phone))){
													alert('Your phone number is not valid !')	
													f.phone1.focus();
													return false;
												}
												if(f.change_pass.checked){
													if(f.password_old.value==''){
														alert('Please input your old password !');
														f.password_old.focus();
														return false;
													}
													if(f.password_new.value==''){
														alert('Please input your new password !');
														f.password_new.focus();
														return false;
													}
													if(f.password_new.value.length<4){
														alert('Password should be more than 5 characters !');	
														f.password_new.focus();
														return false;
													}
													if(f.retype.value==''){
														alert('Please retype password !');
														f.retype.focus();
														return false;
													}
													if(f.retype.value!=f.password_new.value){
														alert('Password is not match ! Please check again !');	
														f.retype.focus();
														return false;
													}
												}
												return true;
											}
function checkForgot(f){
	f.username.value = Trim(f.username.value);
	f.email.value = Trim(f.email.value);
	if((f.username.value=='')&&(f.email.value=='')){
		alert('Please input your email or username !');
		f.username.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=300,height=100');
		return false;
	}

function checkSendMail2Friend(f){
	f.email.value = Trim(f.email.value)
	window.open('send_mail.php?email_to_1='+f.email.value,'','width=400,height=300');
	return false;
}
function checkComment(f){
	var nameObj = document.getElementById('guest_name');
	if(nameObj){
		nameObj.value = Trim(nameObj.value);	
		if(nameObj.value==''){
			alert('Please input your full name !');
			nameObj.focus();
			return false;
		}
	}
	var emailObj = document.getElementById('email');
	if(emailObj){
		emailObj.value = Trim(emailObj.value);
		if(!isEmail(emailObj.value)){
			alert('Please input a valid email, thank you !');
			emailObj.focus();
			return false;
		}
	}
	f.title.value = Trim(f.title.value);
	f.content.value = Trim(f.content.value);
	if(f.title.value==''){
		alert('Please input your subject, thank you !');
		f.title.focus();
		return false;
	}
	if(f.content.value.length < 30){
		alert('Comment more than 30 characters, please check again !');	
		f.content.focus();
		return false;
	}
	//f.content.value = TypingCheck(f.content.value);
	return true;
}
function checkLyric(f){
	f.lyric.value = Trim(f.lyric.value);
	if(f.lyric.value.length < 30){
		alert('Lyric more than 30 characters, please check again !');	
		f.lyric.focus();
		return false;
	}
	//f.lyric.value = TypingCheck(f.lyric.value);
	return true;
}
function checkPost(f){
	f.content.value = Trim(f.content.value);
	if(f.content.value==''){
		alert('Please input your informations, thank you !');	
		f.content.focus();
		return false;
	}
	//f.lyric.value = TypingCheck(f.lyric.value);
	return true;
}
function checkTopSearch(f){
	if((f.withby.value=='album')||(f.withby.value=='production')) {
		f.action = 'albums.php';
		if(f.withby.value=='production') f.manu_id.value='';
	}
	else {f.action = 'musics.php';}
}
function checkVote(f){
		return true;
}
											function checkShipping(f){
												if(f.ship_to.value=='1'){
													if(f.name!=null){
														f.name.value = Trim(f.name.value);
														if(f.name.value==''){
															alert('Vui lòng nhập họ tên của bạn !');
															f.name.focus();
															return false;
														}
													}
													if(f.address!=null){
														f.address.value = Trim(f.address.value);	
														if(f.address.value==''){
															alert('Vui lòng nhập địa chỉ của bạn !');
															f.address.focus();
															return false;
														}
													}
													if(f.city!=null){
														f.city.value = Trim(f.city.value);	
														if(f.city.value==''){
															alert('Vui lòng nhập tỉnh/thành của bạn !');
															f.city.focus();
															return false;
														}
													}
													if(f.country!=null){
														f.country.value = Trim(f.country.value);
														if(f.country.value==''){
															alert('Vui lòng chọn quốc gia của bạn !');
															f.country.focus();
															return false;
														}
													}
												}
												f.submit();
												return true;
											}

											function checkStep1(f){
													if(f.billing_first_name!=null){
														f.billing_first_name.value = Trim(f.billing_first_name.value);
														if(f.billing_first_name.value==''){
															alert('Please input billing first name !');
															f.billing_first_name.focus();
															return false;
														}
													}
													if(f.billing_last_name!=null){
														f.billing_last_name.value = Trim(f.billing_last_name.value);
														if(f.billing_last_name.value==''){
															alert('Please input billing last name !');
															f.billing_last_name.focus();
															return false;
														}
													}
													if(f.billing_email!=null){
														f.billing_email.value = Trim(f.billing_email.value);
														if(!isEmail(f.billing_email.value)){
															alert('Please input correct billing email !');
															f.billing_email.focus();
															return false;
														}
													}
													if(f.billing_address!=null){
														f.billing_address.value = Trim(f.billing_address.value);	
														if(f.billing_address.value==''){
															alert('Please input billing email !');
															f.billing_address.focus();
															return false;
														}
													}
													if(f.billing_city!=null){
														f.billing_city.value = Trim(f.billing_city.value);	
														if(f.billing_city.value==''){
															alert('Please input billing city !');
															f.billing_city.focus();
															return false;
														}
													}
													if(f.billing_country!=null){
														f.billing_country.value = Trim(f.billing_country.value);
														if(f.billing_country.value==''){
															alert('Please input billing country !');
															f.billing_country.focus();
															return false;
														}
													}
													if(f.billing_phone!=null){
														f.billing_phone.value = Trim(f.billing_phone.value);
														if(f.billing_phone.value==''){
															alert('Please input billing phone !');
															f.billing_phone.focus();
															return false;
														}
													}
													if(f.delivery_first_name!=null){
														f.delivery_first_name.value = Trim(f.delivery_first_name.value);
														if(f.delivery_first_name.value==''){
															alert('Please input shipping first name !');
															f.delivery_first_name.focus();
															return false;
														}
													}
													if(f.delivery_last_name!=null){
														f.delivery_last_name.value = Trim(f.delivery_last_name.value);
														if(f.delivery_last_name.value==''){
															alert('Please input shipping last name !');
															f.delivery_last_name.focus();
															return false;
														}
													}
													if(f.delivery_email!=null){
														f.delivery_email.value = Trim(f.delivery_email.value);
														if(!isEmail(f.delivery_email.value)){
															alert('Please input correct shipping email !');
															f.delivery_email.focus();
															return false;
														}
													}
													if(f.delivery_address!=null){
														f.delivery_address.value = Trim(f.delivery_address.value);	
														if(f.delivery_address.value==''){
															alert('Please input shipping email !');
															f.delivery_address.focus();
															return false;
														}
													}
													if(f.delivery_city!=null){
														f.delivery_city.value = Trim(f.delivery_city.value);	
														if(f.delivery_city.value==''){
															alert('Please input shipping city !');
															f.delivery_city.focus();
															return false;
														}
													}
													if(f.delivery_country!=null){
														f.delivery_country.value = Trim(f.delivery_country.value);
														if(f.delivery_country.value==''){
															alert('Please input shipping country !');
															f.delivery_country.focus();
															return false;
														}
													}
													if(f.delivery_phone!=null){
														f.delivery_phone.value = Trim(f.delivery_phone.value);
														if(f.delivery_phone.value==''){
															alert('Please input shipping phone !');
															f.delivery_phone.focus();
															return false;
														}
													}
												//f.submit_img.disabled = true;	
												return true;
											}
function exportBilling(f,field_src,field_dest){
	field_src.value = upper_first_char(field_src.value);
	if(f.same_billing.checked){
		field_dest.value = field_src.value;
	}
	return ;
}
function exportBillingAll(f){
	f.delivery_first_name.value = f.billing_first_name.value
	f.delivery_last_name.value = f.billing_last_name.value
	f.delivery_email.value = f.billing_email.value
	f.delivery_address1.value = f.billing_address1.value
	f.delivery_address2.value = f.billing_address2.value
	f.delivery_city.value = f.billing_city.value
	f.delivery_postcode.value = f.billing_postcode.value
	f.delivery_state.value = f.billing_state.value
	f.delivery_country.value = f.billing_country.value
	f.delivery_phone.value = f.billing_phone.value
	return ;
}
function cardImage(value){
	var cardImgObj = document.getElementById('card_img');
	switch(value){
		case 'MasterCard':
			cardImgObj.src = 'images/mastercard_ccid.jpg';
			break;
		case 'Discover':
			cardImgObj.src = 'images/discover_ccid.jpg';
			break;
		case 'Amex':
			cardImgObj.src = 'images/amex_ccid.jpg';
			break;
		default :
			cardImgObj.src = 'images/visa_ccid.jpg';
			break;
	}
	
}
function checkStep2(f){
	if(f.payment_method[1].checked){
		f.creditCardNumber.value = Trim(f.creditCardNumber.value);
		f.cvv2Number.value = Trim(f.cvv2Number.value);
		if(f.creditCardNumber.value==''){
			alert('Please input credit card number !');
			f.creditCardNumber.focus();
			return false;
		}
		if(f.cvv2Number.value==''){
			alert('Please input CCID !');
			f.cvv2Number.focus();
			return false;
		}
	}
	return true;
}
