﻿// JScript File

// constant variables


// get element object
//get object in Nescape Navigator 4.x
function getObjNN4(obj,name)
{
	var x = obj.layers;
	var foundLayer;
	for (var i=0;i<x.length;i++)
	{
		if (x[i].id == name)
		 	foundLayer = x[i];
		else if (x[i].layers.length)
			var tmp = getObjNN4(x[i],name);
		if (tmp) foundLayer = tmp;
	}
	return foundLayer;
}

// get Browser name
function browser()
{
    var name='';
    if (navigator.appName == 'Microsoft Internet Explorer')
        name = 'ie';
    
    if (navigator.appName == 'Netscape')
        name = 'ns';
        
    if (navigator.appName == 'Opera')
        name = 'op';
        
    return name;
}

//get element position
function getElemTop(Elem)
{
    if(document.getElementById) {
		var elem = document.getElementById(Elem);
	} else if (document.all){
		var elem = document.all[Elem];
	}
	
    var iReturn=0;
    while (elem != null) 
    {
        iReturn+=elem.offsetTop;
        elem=elem.offsetParent;
    }
    return iReturn;
}
function getElemLeft(Elem)
{
    if(document.getElementById) {
		var elem = document.getElementById(Elem);
	} else if (document.all){
		var elem = document.all[Elem];
	}

    var iReturn=0;
    while (elem != null) 
    {
        iReturn+=elem.offsetLeft;
        elem=elem.offsetParent;
    }
    return iReturn;
}

function getElementHeight(Elem) {
	if(document.getElementById) {
		var elem = document.getElementById(Elem);
	} else if (document.all){
		var elem = document.all[Elem];
	}
	if (browser == 'op') { 
		xPos = elem.clientHeight;
	} else {
		xPos = elem.offsetHeight;
	}
	return xPos;
}

function getElementWidth(Elem) {
	if(document.getElementById) {
		var elem = document.getElementById(Elem);
	} else if (document.all){
		var elem = document.all[Elem];
	}
	if (browser() == 'op') {
		xPos = elem.clientWidth;
	} else {
		xPos = elem.offsetWidth;
		//alert(browser());
	}
	return xPos;
}

function getWindowWidth() {
    if (parseInt(navigator.appVersion)>3) {
        if (navigator.appName=="Netscape") {
            return window.innerWidth-16;
        }
        if (navigator.appName.indexOf("Microsoft")!=-1) {
            return document.body.offsetWidth-20;
        }
    }
}

function getWindowHeight() {
    if (parseInt(navigator.appVersion)>3) {
        if (navigator.appName=="Netscape") {
            return window.innerHeight-16;
        }
        if (navigator.appName.indexOf("Microsoft")!=-1) {
            return document.body.offsetHeight-20;
        }
    }
}

// Get passed query string of the url
function queryString(ji) 
{
    hu = window.location.search.substring(1);
    gy = hu.split("&");
    for (i=0;i<gy.length;i++) {
        ft = gy[i].split("=");
        if (ft[0] == ji) {
            return ft[1];
        }
    }
}

// Clear text from a controls list
function clearText(ctrArray, ctrCount)
{
    var oCtr = null;
    for (var i=0;i<ctrCount;i++)
    {
        oCtr = document.getElementById(ctrArray[i]);
        if (oCtr) {
            oCtr.value = "";
        }
    }
    return 0;
}

// Change background images
function changeBgImage(id, imgName)
{
    var obj = document.getElementById(id);
    if (obj)
    {
        obj.style.backgroundImage = 'url(' + rootFolder + imgName + ')';
        obj.style.backgroundRepeat = 'no-repeat';
    }
}

// Change Image source of IMG tag
function changeImageSrc(imgId, imgAbsPath, imgRelPath, imgName)
{
    var oImg = document.getElementById(imgId);
    if (oImg) {
        if (imgAbsPath != '')
            oImg.src = imgAbsPath + imgName;               
        else {
            oImg.src = rootFolder + imgRelPath + imgName;
            //alert(oImg.src);
        }
    }
    return 0;
}

// Open a pop up windows
function popupWindow(name, url, posx, posy, width, height)
{
    window.open(url, name,
            'resizable=no,toolbar=0,status=0,scrollbars=yes,location=0,menubar=0,directories=0,'+
            'titlebar=0,width=' + width + ',height=' + height + ',top=' + posy + ',left=' + posx);
    
    return true;
}

// Change page background (inluding top-zone, right-col, left-col, bottom-zone)
// id is index number of the image
function changePageBackground(prefix,id)
{
    // top-zone
    changeBgImage('main_content',prefix + id);
    
    // left-col
    changeBgImage('left_content',prefix + '_left_' + id);
    
    // right-col
    changeBgImage('left_content',prefix + '_right_' + id);
    
    // bottom-zone
    changeBgImage('left_content',prefix + '_bottom_' + id);
}

// Convert chu co' da'u tha`nh ko da'u
function cv2urltitle($text) {

    $text = str_replace(
    array(' ','%',"/","\\",'"','?','<','>',"#","^","`","'","=","!",":" ,",,","..","*","&","__","▄"),
    array('_','' ,'' ,'' ,'' ,'' ,'' ,'' ,'' ,'' ,'' ,'' ,'-','' ,'-','' ,'' ,'' , "_" ,"" ,""),
    $text); 

    $chars = array("a","A","e","E","o","O","u","U","i","I","d", "D","y","Y");

    $uni[0] = array("á","à","ạ","ả","ã","â","ấ","ầ", "ậ","ẩ","ẫ","ă","ắ","ằ","ặ","ẳ"," ");
    $uni[1] = array("Á","À","Ạ","Ả","Ã","Â","Ấ","Ầ", "Ậ","Ẩ","Ẫ","Ă","Ắ","Ằ","Ặ","Ẳ"," ");
    $uni[2] = array("é","è","ẹ","ẻ","ẽ","ê","ế","ề" ,"ệ","ể","ễ");
    $uni[3] = array("É","È","Ẹ","Ẻ","Ẽ","Ê","Ế","Ề" ,"Ệ","Ể","Ễ");
    $uni[4] = array("ó","ò","ọ","ỏ","õ","ô","ố","ồ", "ộ","ổ","ỗ","ơ","ớ","ờ","ợ","ở"," ");
    $uni[5] = array("Ó","Ò","Ọ","Ỏ","Õ","Ô","Ố","Ồ", "Ộ","Ổ","Ỗ","Ơ","Ớ","Ờ","Ợ","Ở"," ");
    $uni[6] = array("ú","ù","ụ","ủ","ũ","ư","ứ","ừ", "ự","ử","ữ");
    $uni[7] = array("Ú","Ù","Ụ","Ủ","Ũ","Ư","Ứ","Ừ", "Ự","Ử","Ữ");
    $uni[8] = array("í","ì","ị","ỉ","ĩ");
    $uni[9] = array("Í","Ì","Ị","Ỉ","Ĩ");
    $uni[10] = array("đ");
    $uni[11] = array("Đ");
    $uni[12] = array("ý","ỳ","ỵ","ỷ","ỹ");
    $uni[13] = array("Ý","Ỳ","Ỵ","Ỷ","Ỹ");

    for($i=0; $i<=13; $i++) {
        $text = str_replace($uni[$i],$chars[$i],$text);
    }

    return $text;
}



// ---------------------------
// Home page
var bannerImages = new Array();
bannerImages[0] = rootFolder + "images/home_banner1.jpg";
bannerImages[1] = rootFolder + "images/home_banner2.jpg";

function loadBannerImage()
{
    var num = Math.floor(Math.random()*bannerImages.length);
    var banner = document.getElementById('home_banner');
    if (banner)
        banner.style.backgroundImage='url(' + bannerImages[num] + ')';
}

// actions accur when clicking on body
function bodyClick() {
    
}

// ---------------------------
// Products-Thumbnails pages scripts //
var productTimer=null;
var activeSpecs=null;

function showSpecs(no)
{
    // hide current specs
    hideSpecs(activeSpecs);    
        
    var obj = document.getElementById('specs' + no);
    if(obj)
    {
        var tbl_obj = document.getElementById('itm_img' + no);
        var obj_pos = getElementPos(tbl_obj);
        var img_width = getElementWidth('itm_img' + no);  
    
        obj.style.display = 'block';
        obj.style.visibility = 'visible';
        obj.style.top=obj_pos.y - 80 + 'px';
        obj.style.left=obj_pos.x + img_width + 'px';   
        
        activeSpecs = obj;         
    }
}

function hideSpecs()
{
    if(activeSpecs)
    {
        activeSpecs.style.visibility = 'hidden';
        activeSpecs = null;
    }
}

function viewImage(imgElem, imgPath)
{
    var img = document.getElementById(imgElem);
    if (img)
    {
        var path = document.getElementById('path' + imgPath);        
        //alert(path.innerHTML);
        img.src = path.innerHTML;
    }
}



function productOut()
{
    if (activeSpecs)
        productTimer = setTimeout("hideSpecs(activeSpecs)", 100);
    else
        hideSpecs(activeSpecs);
}
function productOver(no)
{
    clearTimeout(productTimer);
    productTimer = setTimeout("showSpecs('" + no + "')",500);
}

// pop up a window to send email to friend
function sendToFriend(productUrl)
{
    popupWindow('','send-to-friends.aspx?=' + productUrl,0,0,450,450);
}

// pop up a window to print product's info
function printProduct(productid)
{
    popupWindow('','print.aspx?productid=' + productid,0,0,600,768);
}

// Set a link to active style
function setActive(iconId, activeIconImage, linkId, activeStyle)
{
    var oIcon = document.getElementById(iconId);
    var oLink = document.getElementById(linkId);
    
    if (oIcon)
        oIcon.src = rootFolder + 'images/' + activeIconImage;
    
    if (oLink)
        oLink.className = activeStyle;
        
    return 0;
}

//-----
// Products-Details page scripts //
//-----

var timeOnProdList = null;
    
function prodListOver() {         
    clearTimeout(timeOnProdList);
}

function prodListOut() 
{        
    timeOnProdList = setTimeout("hideProdList()", 500);    
}

// Hide the product list
function hideProdList()
{
    var obj = document.getElementById("productList");
    if (obj) {            
        obj.style.display = 'none';
        obj.style.visibility = 'hidden';
    }
}

// Show product list in the same category when user mouse over the anchor
function showProdList()
{
    var obj = document.getElementById("productList");
    if (obj) {      
        obj.style.display = 'block';      
        obj.style.visibility = 'visible';
        
        var listWidth = getElementWidth('productList');            
        
        var lTop = getElemTop("listTop");
        var lLeft = getElemLeft("listTop");
        obj.style.top = lTop + 'px';
        obj.style.left = lLeft + getElementWidth("listTop") - listWidth + 'px';
    }
}

// Open a window to view large image of this product
function viewLargeImage(productID, url)
{
    if (url=='')
        window.open('largeview.aspx?ProductID='+productID,'Product',
            'resizable=no,toolbar=0,status=0,scrollbars=yes,location=0,menubar=0,directories=0,'+
            'titlebar=0,width=600,height=' + screen.height + ',top=0,left=100');
    else
        window.open(url + '?ProductID='+productID,'Product',
            'resizable=no,toolbar=0,status=0,scrollbars=yes,location=0,menubar=0,directories=0,'+
            'titlebar=0,width=600,height=' + screen.height + ',top=0,left=100');
}

function popupWindow1(productID, url)
{
    if (url=='')
        window.open('largeview.aspx?ProductID='+productID,'Product',
            'resizable=no,toolbar=0,status=0,scrollbars=yes,location=0,menubar=0,directories=0,'+
            'titlebar=0,width=600,height=' + screen.height + ',top=0,left=100');
    else
        window.open(url + '?ProductID='+productID,'Product',
            'resizable=no,toolbar=0,status=0,scrollbars=yes,location=0,menubar=0,directories=0,'+
            'titlebar=0,width=600,height=' + screen.height + ',top=0,left=0');
}
