﻿var userAgent = navigator.userAgent.toLowerCase();
var is_opera  = ((userAgent.indexOf('opera') != -1) || (typeof(window.opera) != 'undefined'));
var is_saf    = ((userAgent.indexOf('applewebkit') != -1) || (navigator.vendor == 'Apple Computer, Inc.'));
var is_webtv  = (userAgent.indexOf('webtv') != -1);
var is_ie     = ((userAgent.indexOf('msie') != -1) && (!is_opera) && (!is_saf) && (!is_webtv));
var is_ie4    = ((is_ie) && (userAgent.indexOf('msie 4.') != -1));
var is_moz    = ((navigator.product == 'Gecko') && (!is_saf));
var is_kon    = (userAgent.indexOf('konqueror') != -1);
var is_ns     = ((userAgent.indexOf('compatible') == -1) && (userAgent.indexOf('mozilla') != -1) && (!is_opera) && (!is_webtv) && (!is_saf));
var is_ns4    = ((is_ns) && (parseInt(navigator.appVersion) == 4));
var is_mac    = (userAgent.indexOf('mac') != -1);


function fetch_object(idname)
{
	if (document.getElementById)
	{
		return document.getElementById(idname);
	}
	else if (document.all)
	{
		return document.all[idname];
	}
	else if (document.layers)
	{
		return document.layers[idname];
	}
	else
	{
		return null;
	}
}

function fetch_tags(tag)
{
	if (typeof document.getElementsByTagName != 'undefined')
	{
		return document.getElementsByTagName(tag);
	}
	else if (document.all && document.all.tags)
	{
		return document.all.tags(tag);
	}
	else
	{
		return new Array();
	}
}

// Cac ham ve ajax

function createxmlHttp() {
		var xmlHttp;
		try	{
			xmlHttp = new XMLHttpRequest();
		}
		catch(e) {
				var XmlHttpVersions = new Array('MSXML2.XMLHTTP.6.0',
												'MSXML2.XMLHTTP.5.0',
												'MSXML2.XMLHTTP.4.0',
												'MSXML2.XMLHTTP.3.0',
												'MSXML2.XMLHTTP',
												'Microsoft.XMLHTTP');
				for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++){
					try	{
							xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
					}
					catch (e) {}
				}
		}
		if (!xmlHttp)
			alert('Sorry we can not creat an ActiveX on your browser, please update your browser version or change to another browser.');
		else
		return xmlHttp;
}

// Cac ham ve ajax

function CheckAllCheckbox(f,checkboxname){
	var len=f.elements.length;
	for(var i=0;i<len;i++){
		if(f.elements[i].name==checkboxname){
			f.elements[i].checked=true;
		}
	}
	return;
}

//-------------------------------------------------------------------------------------------
function UnCheckAllCheckbox(f,checkboxname){
	var len=f.elements.length;
	for(var i=0;i<len;i++){
		if(f.elements[i].name==checkboxname){
			f.elements[i].checked=false;
		}
	}
	return;
}

//------------------------------------------------
function LTrim(Str) {
	return Str.replace(/^\s+/, '');
}

//------------------------------------------------
function RTrim(Str) {
	return Str.replace(/\s+$/, '');
}
//------------------------------------------------
function Trim(Str) {
	return RTrim(LTrim(Str));
}
//-------------------------------------------------------------------------------------------
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 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 isPhone(strPhone) {
	return  /^(\d{6,15})$/.test(strPhone);
	//return  /^[\+\-\(]?(\d*[\.\-\(\)\s\+]*\d*)*$/.test(strPhone);
}

function isMobile(strMobile) {
	return  /^(\d{9,15})$/.test(strMobile);
	//return  /^[\+\-\(]?(\d*[\.\-\(\)\s\+]*\d*)*$/.test(strPhone);
}

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 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 openNewWindow(linkurl,imgh,imgw,s) {
	var w = screen.availWidth;
	var h = screen.availHeight;
	var leftPos = (w-imgw)/2, topPos = (h-imgh)/2;
	window.open(linkurl,'popup','location=0,status=0,scrollbars='+s+',width=' + imgw + ',height=' + imgh + ',top=' + topPos + ',left=' + leftPos);
}

function getExtension(fileName){
		return fileName.substr(fileName.lastIndexOf(".")+1);
}

function hiddeContentBlock(blid) {
	if(fetch_object('block_content_'+blid).style.display == 'none') {
		fetch_object('block_content_'+blid).style.display ='block';
	} else {
		fetch_object('block_content_'+blid).style.display ='none';
	}				
}	

function showAlert(e_name,msg){
	var eObj = document.getElementById(e_name + '_err');
	if(eObj){
		if(msg!=null){	
			eObj.innerHTML = msg + "<br>";
		}
		eObj.style.display = '';	
	}
}
function hideAlert(e_name){
	var eObj = document.getElementById(e_name + '_err');
	if(eObj){
		eObj.style.display = 'none';	
	}
}

function FormatNumber(str){
			var strTemp = GetNumber(str);
			if(strTemp.length <= 3)
				return strTemp;
			strResult = "";
			for(var i =0; i< strTemp.length; i++)
				strTemp = strTemp.replace(",", "");
			for(var i = strTemp.length; i>=0; i--)
			{
				if(strResult.length >0 && (strTemp.length - i -1) % 3 == 0)
					strResult = "," + strResult;
				strResult = strTemp.substring(i, i + 1) + strResult;
			}	
			return strResult;
		}
		
function GetNumber(str,ale)
			{
				for(var i = 0; i < str.length; i++)
				{	
					var temp = str.substring(i, i + 1);		
					if(!(temp >= 0 && temp <=9))
					{
						alert(ale);
						return str.substring(0, i);
					}
					
				}
				return str;
			}

function aj_fetch_string(str) {
	str = str.replace(/&/g,"**am**");
	str = str.replace(/=/g,"**eq**");
	str = str.replace(/\+/g,"**pl**");	
	return str;
}

function changeto(obj,strClass){
	if(strClass!=""){
		obj.className = strClass;
	}
	obj.style.cursor = 'hand';
}
function dropCategory(obj){
	if(obj.className == "on"){
		obj.className = "off";
		document.frmTemp.objdrop.value = "";	
	}
	else{
		obj.className = "on";
		if(document.frmTemp.objdrop.value != ""){
			identity=document.getElementById(document.frmTemp.objdrop.value);
			identity.className = "off";
		}
		document.frmTemp.objdrop.value = obj.id;
	}
}

var separator = ",";  // use comma as 000's separator
var decpoint = ".";  // use period as decimal point
var percent = "%";
var currency = "$";  // use dollar sign for currency

function formatNumber(number, format, print) {  // use: formatNumber(number, "format")
	if (print) document.write("formatNumber(" + number + ", \"" + format + "\")<br>");

	if (number - 0 != number) return null;  // if number is NaN return null
	var useSeparator = format.indexOf(separator) != -1;  // use separators in number
	var usePercent = format.indexOf(percent) != -1;  // convert output to percentage
	var useCurrency = format.indexOf(currency) != -1;  // use currency format
	var isNegative = (number < 0);
	number = Math.abs (number);
	if (usePercent) number *= 100;
	format = strip(format, separator + percent + currency);  // remove key characters
	number = "" + number;  // convert number input to string

	 // split input value into LHS and RHS using decpoint as divider
	var dec = number.indexOf(decpoint) != -1;
	var nleftEnd = (dec) ? number.substring(0, number.indexOf(".")) : number;
	var nrightEnd = (dec) ? number.substring(number.indexOf(".") + 1) : "";

	 // split format string into LHS and RHS using decpoint as divider
	dec = format.indexOf(decpoint) != -1;
	var sleftEnd = (dec) ? format.substring(0, format.indexOf(".")) : format;
	var srightEnd = (dec) ? format.substring(format.indexOf(".") + 1) : "";

	 // adjust decimal places by cropping or adding zeros to LHS of number
	if (srightEnd.length < nrightEnd.length) {
	  var nextChar = nrightEnd.charAt(srightEnd.length) - 0;
	  nrightEnd = nrightEnd.substring(0, srightEnd.length);
	  if (nextChar >= 5) nrightEnd = "" + ((nrightEnd - 0) + 1);  // round up

	// patch provided by Patti Marcoux 1999/08/06
	  while (srightEnd.length > nrightEnd.length) {
	    nrightEnd = "0" + nrightEnd;
	  }

	  if (srightEnd.length < nrightEnd.length) {
	    nrightEnd = nrightEnd.substring(1);
	    nleftEnd = (nleftEnd - 0) + 1;
	  }
	} else {
	  for (var i=nrightEnd.length; srightEnd.length > nrightEnd.length; i++) {
	    if (srightEnd.charAt(i) == "0") nrightEnd += "0";  // append zero to RHS of number
	    else break;
	  }
	}

	 // adjust leading zeros
	sleftEnd = strip(sleftEnd, "#");  // remove hashes from LHS of format
	while (sleftEnd.length > nleftEnd.length) {
	  nleftEnd = "0" + nleftEnd;  // prepend zero to LHS of number
	}

	if (useSeparator) nleftEnd = separate(nleftEnd, separator);  // add separator
	var output = nleftEnd + ((nrightEnd != "") ? "." + nrightEnd : "");  // combine parts
	output = ((useCurrency) ? currency : "") + output + ((usePercent) ? percent : "");
	if (isNegative) {
	  // patch suggested by Tom Denn 25/4/2001
	  output = (useCurrency) ? "(" + output + ")" : "-" + output;
	}
	return output;
}

function strip(input, chars) {  // strip all characters in 'chars' from input
	var output = "";  // initialise output string
	for (var i=0; i < input.length; i++)
	  if (chars.indexOf(input.charAt(i)) == -1)
	    output += input.charAt(i);
	return output;
}

function separate(input, separator) {  // format input using 'separator' to mark 000's
	input = "" + input;
	var output = "";  // initialise output string
	for (var i=0; i < input.length; i++) {
	  if (i != 0 && (input.length - i) % 3 == 0) output += separator;
	  output += input.charAt(i);
	}
	return output;
}