var ns4 = document.layers;
var ie = document.getElementById && document.all;
var ns6 = document.getElementById && !document.all;
var dom = document.getElementById && !document.all;

// Show Image
function ImgOn(Nam) {
	if (document.images) {
		document.images[Nam].src = eval(Nam+ "_over.src");
    }
}

// Hide Image
function ImgOff(Nam) {
	if (document.images) {
		document.images[Nam].src = eval(Nam+ "_off.src");
    }
}

function PopUpWindow(targetUrl,name,width,height,resizable) {
	this.name = "main";
	OpenWin = window.open(targetUrl, name, "status=no,toolbar=no,directories=no,menubar=no,scrollbars=yes,resizable="+resizable+",width="+ (width+18) + ",height=" + height + ",left=30,top=30");
	OpenWin.focus();
}

function FullScreen(targetUrl, name, resizable) {
	this.name = "main";
	OpenWin = window.open(targetUrl, name, "status=no,toolbar=no,directories=no,menubar=no,scrollbars=yes,resizable="+resizable+",width="+ (screen.width-12) + ",height=" + (screen.height-60) + ",left=0,top=0");
	OpenWin.focus();
}

function isEmpty(s) {
	return ((s == null) || (s.length == 0));
}

function isWhitespace(s) {
	var i;
	var whitespace = " \t\n\r";

    if (isEmpty(s)) {
		return true;
	}

    for (i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if (whitespace.indexOf(c) == -1) {
			return false;
		}
    }

    return true;
}

function isEmail(s) {
	if (isEmpty(s)) {
       return false;
    }

    if (isWhitespace(s)) {
		return false;
	}

    var i = 1;
    var sLength = s.length;

    while ((i < sLength) && (s.charAt(i) != "@")) {
		i++;
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) {
		return false;
	}
    else {
		i+=2;
	}

    while ((i < sLength) && (s.charAt(i) != ".")) {
		i++;
    }

    if ((i >= sLength - 1) || (s.charAt(i) != ".")) {
		return false;
	}

    return true;
}

// Return relative path of a image
function RelativePath(s) {
	return s.substring(s.indexOf("/images/"));
}

function ShowDiv(control_id) {
	document.getElementById(control_id).style["display"] = "";
}

function HideDiv(control_id) {
	document.getElementById(control_id).style["display"] = "none";
}

function ShowHideDiv(control_id) {
	if (document.getElementById(control_id).style["display"] == "none") {
		document.getElementById(control_id).style["display"] = "";
	}
	else {
		document.getElementById(control_id).style["display"] = "none";
	}
}

function TabNextTextField(thisTextField, nextTextField, maxLimit) {
	if (document.getElementById(thisTextField).value.length == maxLimit) {
		document.getElementById(nextTextField).focus();
	}
}

function BookmarkSite(title, url) {
	if (window.sidebar) {	// firefox
		window.sidebar.addPanel(title, url, "");
	}
	else if (window.opera && window.print) {	// opera
		var elem = document.createElement('a');
		elem.setAttribute('href', url);
		elem.setAttribute('title', title);
		elem.setAttribute('rel', 'sidebar');
		elem.click();
	}
	else if (document.all) {	// ie
		window.external.AddFavorite(url, title);
	}
}

function SetDisplayImagePath(controlID, imagePath) {
	document.getElementById(controlID).src = imagePath;
}

// for product description and stone information
function SwitchDescTab(tab_id, tab_total) {
	for (i = 1; i <= tab_total; i++) {
		if (i == tab_id) {
			document.getElementById("TabDescLink" + i).className = "active_desc_tab";
			ShowDiv("TabDescContent" + i);
		}
		else {
			document.getElementById("TabDescLink" + i).className = "inactive_desc_tab";
			HideDiv("TabDescContent" + i);
		}
	}
}

/* ================================================================================================================================================ */
/* ajax - for search suggestion */
var xmlHttp;
var is_ie = (navigator.userAgent.indexOf('MSIE') >= 0) ? 1 : 0;
var is_ie5 = (navigator.appVersion.indexOf('MSIE 5.5') != -1) ? 1 : 0;
var is_netscape = (navigator.userAgent.indexOf('Netscape') >= 0) ? 1 : 0;

function show_data(val, url, txtbox) {	
	url = url + '?keyword=' + val + '&txtbox=' + txtbox;
	url = url + "&sid=" + Math.random();
	xmlHttp = GetXmlHttpObject(stateChangeHandler);
	xmlHttp_Get(xmlHttp, url);
} 

function stateChangeHandler() {   
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 'complete') {
		var str = xmlHttp.responseText;		
		document.getElementById('result').innerHTML = str;
	}
}

function xmlHttp_Get(xmlhttp, url) {
	xmlhttp.open("GET", url, true);
	xmlhttp.send(null);
}

function GetXmlHttpObject(handler) {
	var objXmlHttp = null;
	if (is_ie) 
	{
		var strObjName = (is_ie5) ? "Microsoft.XMLHTTP" : "Msxml2.XMLHTTP";
		try 
		{
			objXmlHttp = new ActiveXObject(strObjName);
			objXmlHttp.onreadystatechange = handler;
		}
		catch(e) 
		{
			return;
		}
	}
	else
	{
		objXmlHttp = new XMLHttpRequest();
		objXmlHttp.onload = handler;
		objXmlHttp.onerror = handler;
	}
	return objXmlHttp;
}