﻿/**
 * @author cuong.tran
 * @email cuong.tran@enclaveit.com, cuongtranquoc@gmail.com
 * @function Common
 * @last update: 09/01/2009 
 */
var isIE6 = ((navigator.userAgent.indexOf("MSIE 6.") != -1) && (navigator.userAgent.indexOf("Opera") == -1));
var isIE8 = navigator.userAgent.indexOf('MSIE 8.') > -1;
var isIE7 = navigator.userAgent.indexOf('MSIE 7.') > -1;
var isIE = (navigator.appName.indexOf("Microsoft")!=-1)?1:0;
function XBrowserAddHandler(target, eventName, fnHandler){
    var originalHandler = target["on" + eventName];
    if (originalHandler) {
        target["on" + eventName] = function(e){
            XBrowserApplyHandler(this, originalHandler, e);
            XBrowserApplyHandler(this, fnHandler, e);
        };
    }
    else {
        target["on" + eventName] = function(e){
            XBrowserApplyHandler(this, fnHandler, e)
        };
    }
};
function XBrowserApplyHandler(target, fn, e){
    if (!e) 
        e = window.event;
    if (Function.prototype.call) {
        fn.call(target, e);
    }
    else {
        target.__XBrowserElementApply = fn;
        target.__XBrowserElementApply(e)
    }
};
function removeURLPara(para){
    para = para.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + para + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results != null) {
        return window.location.href.replace('?' + para + '=' + results[1], '').replace('&' + para + '=' + results[1], '');
    }
    else {
        return window.location.href;
    }
};
function replaceURLPara(thisUrl, para, value){
    para = para.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + para + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(thisUrl);
    if (results != null) {   
        if (results[1]==''){
            thisUrl = thisUrl.replace(results[0],results[0]+value);        
        }else{            
            thisUrl = thisUrl.replace(results[0],results[0].substring(0,1)+ para+ '='+value);
        }
    }
    else {    
        if (thisUrl.indexOf('?') == -1) {
            thisUrl += '?' + para + '=' + value;
        }
        else {
            thisUrl += '&' + para + '=' + value;
        }
    }
    return thisUrl;
};
function AddURLPara(thisUrl, para, value){
    para = para.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + para + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(thisUrl);        
    if (results != null) {  
        thisUrl=replaceURLPara(thisUrl, para, value);         
    }
    else {        
        if (thisUrl.indexOf('?') == -1) {
            thisUrl += '?' + para + '=' + value;
        }
        else {
            thisUrl += '&' + para + '=' + value;
        }
    }
    return thisUrl;
};
function getRequestValue(para){
    if (para !=NaN){
        para = para.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
        var regexS = "[\\?&]"+para+"=([^&#]*)";
        var regex = new RegExp( regexS );
        var results = regex.exec(window.location.href);
        if( results == null ){
            return '';
        }
        else{
            return results[1];
        }
    };
    return '';    
};
function replaceWindowLocationURLPara(para, value){
    var thisUrl = window.location.href;
    para = para.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + para + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(thisUrl);
    if (results != null) {
        if (results[1]==''){
            thisUrl = thisUrl.replace(results[0],results[0]+value);        
        }else{            
            thisUrl = thisUrl.replace(results[0],results[0].substring(0,1)+ para+ '='+value);
        }
    }
    else {
        if (thisUrl.indexOf('?') == -1) {
            thisUrl += '?' + para + '=' + value;
        }
        else {
            thisUrl += '&' + para + '=' + value;
        }
    }
    return thisUrl;
};
function trim(input){
    try{
        return input.replace(/^\s+|\s+$/, '');
    }catch(e){
        return '';
    }
};
function focusField(fieldname){
    var thisField = document.getElementById(fieldname);
    try {
        thisField.select();
        thisField.focus();
    } 
    catch (e) {
    }
};
function clearField(fieldname){
    var thisField = document.getElementById(fieldname);
    if (thisField) {
        thisField.value = '';
    }
};
function IncludeJavaScript(jsFile){
    document.write('<script type="text/javascript" src="' + jsFile + '"></scr' + 'ipt>');
};
function IncludeCss(cssFile){
    document.write('<link  rel="stylesheet" type="text/css" href="' + cssFile + '" />');
};
function addLoadEvent(func){
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    }
    else {
        window.onload = function(){
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
};
function getForm(name){
    var arrFrom = document.getElementsByTagName("form");
    for (var i = 0; i < arrFrom.length; i++) {
        if (arrFrom[i].name == name) {
            return arrFrom[i];
        }
    };
    return NaN
};
function getElementHeight(Elem){
    var elem;
    if (document.getElementById) {
        elem = document.getElementById(Elem);
    }
    else 
        if (document.all) {
            elem = document.all[Elem];
        }
    return elem.offsetHeight;
};
function getElementWidth(Elem){
    var elem;
    if (document.getElementById) {
        elem = document.getElementById(Elem);
    }
    else 
        if (document.all) {
            elem = document.all[Elem];
        }
    return elem.offsetWidth;
};
function getElementTop(elm){
    var x = 0;   
    while (elm != null) {
        x += elm.offsetTop;
        elm = elm.offsetParent;
    }
    return parseInt(x);
};
function getElementLeft(elm){
    var x = 0;    
    while (elm != null) {
        x += elm.offsetLeft;
        elm = elm.offsetParent;
    }
    return parseInt(x);
};
function setBroserTitle(titleSt){
    document.title = titleSt;
};        
function browserDetect(){
	var isIE,isFirefox,isChrome,isKonqueror,isSafari,isOpera,isOther;
	isIE=isFirefox=isChrome=isKonqueror=isSafari=isOpera=isOther=false;

	if(!(isIE=!-'\v1') && !(isFirefox=1*({toString:0,valueOf:function(x){return !!x;}}))){
		try{/./('');isOther=true;}catch(e){isKonqueror/*orIE*/=true;}
		if(!isKonqueror/*orOtherThatDoesntSupport/./()*/ && !(isSafari=/^($)?$/("")[1]=='') && !(isOpera='object'==(typeof /./))){
			isChrome/*orFForOpera*/=(function(){var z=function(y){var x=/\d/g;return x(y);};z(0);return !z(1);})();
			isOther/*thatSupports/./()*/=!isChrome;
		}else{isOther=false;}
	}
	var isWebkit=isSafari||isChrome||isKonqueror;
	return ['ie','ff','ch','kq','sa','op','??'][isIE?0:isFirefox?1:isChrome?2:isKonqueror?3:isSafari?4:isOpera?5:6];
};
function hideAllMask() {
    if (document.getElementById("product_category")) {
        var i, divTagCount;
        var allDivTag = document.getElementById("product_category").getElementsByTagName("div");
        divTagCount = allDivTag.length;
        for (i = 0; i < divTagCount; i++) {
            if (allDivTag[i].id.match(/\Mask_/ig)) {
                allDivTag[i].style.display = "none";
            }
        }
    }
};
function fnGetDomain(url){return url.match(/:\/\/(.[^/]+)/)[1];};