﻿function WriteAd(url, link, className, target, title, width)
{
    if(!url) return;
    width = width ? "width='" + width + "'": "";
    link = link ? "href='" + link + "'": "";
    title = title ? "title='" + title + "'": "";
    target = target ? "target='" + target + "'": "";
    className = className ? "class='" + className + "'": "";
    if(url.match(/.swf$/ig))
    {
        document.write("\
        <a " + className + " " + link + " " + title + " " + target + ">\
                <embed src='" + url + "' quality='high' wmode='transparent' " + width + " allowScriptAccess='sameDomain' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' />\
        </a>\
        ");
    }
    else
    {
        document.write("\
        <a " + className + " " + link + " " + title + " " + target + ">\
            <img src='" + url + "' " + width + "/>\
        </a>\
        ");
    }
}

function Search(form, url)
{
    if(!form["key"] || !form["key"].value) return false;
    location.href = url + encodeURI(form["key"].value);
    return false
}

function LoadImages(className, imageClassName)
{
    var links = document.links;
    for(var i in links)
    {
        var link = links[i];
        if(link && link.className)
        {
            if(link.className == className || link.className.indexOf(className + " ") != -1 || link.className.indexOf(" " + className) != -1)
            {
                var img = document.createElement("IMG");
                img.className = imageClassName;
                img.onload = function()
                {
                    this.style.position = "static";
                    this.style.visibility = "visible";
                }
                img.style.position = "absolute";
                img.style.visibility = "hidden";
                img.src = link.rel;
                link.innerHTML = "";
                link.appendChild(img);
                if(link.style.width)
                {
                    img.style.width = link.style.width;
                    link.style.width = "auto";
                    img.style.height = "auto";
                }
            }
        }
    }
}

function CreateButtonStyles(classNames, spacerSource)
{
    if(!classNames) return;
    classNames = classNames.split(",");
    classNames.contains = function(className)
    {
        if(!className) return false;
        for(var i in this)
        {
            if(this[i] == className) return true;
        }
        return false;
    }
    
    var links = document.links;
    
    if(!spacerSource) spacerSource = "spacer.gif";
    for(var i = 0; i < links.length; i++)
    {
        var link = links[i];
        if(classNames.contains(link.className))
        {
            link.oldClickHandler = link.onclick;
            link.url = link.href;
            link.href = "#none";
            link.onclick = function(e)
                {
                    e = e ? e : window.event;
                    var result;
                    if(this.oldClickHandler) result = this.oldClickHandler(e);
                    if(typeof(result) != "undefined" && !result) return false;
                    window.open(this.url, this.target ? this.target : "_self");
                }
            var settings = link.getAttribute("settings", "");
            if(!settings) settings = "";
            settings = settings.split(",");
            var color = settings[0] ? settings[0] : "Black";
            var width = settings[1] ? "width:" + settings[1] + ";" : "";
            var fontSize = settings[2] ? "font-size:" + settings[2] + ";" : "";
            var extendedClass = settings[3] ? settings[3] : "";
            var text = typeof(link.textContent) == "undefined" ? link.innerText : link.textContent;
            var html = "<button style='" + width + fontSize + "'><table border='0' cellpadding='0' cellspacing='0'><tr><td class='" + link.className + "Left" + color + " " + extendedClass + "'></td><td class='" + link.className + "Center" + color + " " + extendedClass + "'>" + text + "</td><td class='" + link.className + "Right" + color + " " + extendedClass + "'></td></tr></table></button>";
            //var html = "<img class='" + link.className + "Left " + color + " " + extendedClass + "' src='" + spacerSource + "' alt='' /><button class='" + link.className + "Center " + color + " " + extendedClass + "' style='" + width + fontSize + "'>" + text + "</button><img class='" + link.className + "Right " + color + " " + extendedClass + "' src='" + spacerSource + "' alt='' />";
            link.innerHTML = html;
        }
    }
}
            
function CreatePager(pageCount, currentPage, url)
{
    var pages = new Array();
    var startPage = 0;
    var endPage = 0;
    
    if(eval(pageCount) == 1)
    {
    	document.getElementById("showpager").display = "none";
    }
    startPage = currentPage - 2;
    if(startPage < 0) startPage = 0;
    if(startPage + 4 < pageCount)
    {
        endPage = startPage + 4;
    }
    else
    {
        endPage = pageCount - 1;
    }
    if(endPage - 4 < 0)
    {
        startPage = 0;
    }
    else
    {
        startPage = endPage - 4;
    }
    
    pages[pages.length] = "<a class='Page' " + (pageCount > 0 ? "href='" + url.replace("{page}", 0) + "'" : "") + ">|&lt;</a>";
    pages[pages.length] = "<a class='Page' " + (currentPage - 1 >= 0 ? "href='" + url.replace("{page}", currentPage - 1) + "'" : "") + ">&lt;</a>";
    for(var i = startPage; i <= endPage; i++)
    {
        pages[pages.length] = "<a class='" + (i == currentPage?"Current":"Page") + "' href='" + url.replace("{page}", i) + "'>" + (i + 1) + "</a>";
    }
    pages[pages.length] = "<a class='Page' " + (currentPage + 1 < pageCount ? "href='" + url.replace("{page}", currentPage + 1) + "'" : "") + ">&gt;</a>";
    pages[pages.length] = "<a class='Page' " + (pageCount > 0 ? "href='" + url.replace("{page}", pageCount - 1) + "'" : "") + ">&gt;|</a>";
    document.write(pages.join("&nbsp;&nbsp;"));
}

function RemoveFormat(text)
{
    //text = text.replace(/<[/]?(font|span|xml|del|ins|[ovwxp]:\w+)[^>]*?>/ig, "");
    text = text.replace(/<\?[^>]*\?>/ig, "");
    text = text.replace(/<([^>]*)(?:size|face)=(?:'[^']*'|""[^""]*""|[^>]+)([^>]*)>/ig,"<$1 style=\"font-size: 9pt;\" $2>"); 
    text = text.replace(/style="([^"]*)font-family([^"]*)"/ig, "style=\"$1;$2\"");
    text = text.replace(/style="([^"]*)font-size([^"]*)"/ig, "style=\"$1;$2\"");
    
    return text;
}

function ConvertToUnformatText(container)
{
    container = GetTag(container);
    if(container) container.innerHTML = RemoveFormat(container.innerHTML);
}

function GetTag(tag)
{
    if(!tag) return null;
    if(typeof(tag) == "string") tag = document.getElementById(tag);
    return tag;
}

function WriteUnformatText(container, text)
{
    container = GetTag(container);
    if(container)
        container.innerHTML = RemoveFormat(text);
    else
        document.write(RemoveFormat(text));
}

function InitDefaultValueTextbox(textbox)
{
    if(!textbox) return;
    if(textbox.initialized) return;
    textbox.initialized = true;
    textbox.onfocus = function()
        {
            if(!this.valueChanged)
            {
                this.defaultValue = textbox.value;
                this.value = "";
            }
        }
    textbox.onchange = function()
        {
            this.valueChanged = true;
        }
    
    textbox.onblur = function()
        {
            if(this.value)
            {
                if(!this.valueChanged && this.value != this.defaultValue)
                {
                    this.value = this.defaultValue;
                }
            }
            else
            {
                this.valueChanged = false;
                this.value = this.defaultValue;
            }
        }
        
    textbox.onfocus();
}

function ShowImage(image, style, largeImage, popupStyle)
{
    if(!image) return;
    image.style.display = "";
    if(style != null) DropShadow(image, style);
    if(largeImage)
    {
        image.style.cursor = "pointer";
        image.onclick = function()
        {
            PopupImage(largeImage, popupStyle);
        }
    }
}

function DropShadow(obj, style)
{
	if(window.showModalDialog)
	{
		switch(style)
		{
			case 0:
				obj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='Images/ImageShadow0.png', sizingMethod='scale')";
    			obj.style.paddingLeft = (obj.offsetWidth / 14) + "px";
    			obj.style.paddingRight = (obj.offsetWidth / 14) + "px";
    			obj.style.paddingTop = (obj.offsetHeight / 14) + "px";
    			obj.style.paddingBottom = (obj.offsetHeight / 14) + "px";
				break;
			case 1:
				obj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='Images/ImageShadow1.png', sizingMethod='scale')";
    			obj.style.paddingLeft = (obj.offsetWidth / 11.5) + "px";
    			obj.style.paddingRight = (obj.offsetWidth / 10.1) + "px";
    			obj.style.paddingTop = (obj.offsetHeight / 11) + "px";
    			obj.style.paddingBottom = (obj.offsetHeight / 10) + "px";
				break;
			case 2:
			    obj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='Images/ImageShadow2.png', sizingMethod='scale')";
    			obj.style.paddingLeft = (obj.offsetWidth / 12) + "px";
    			obj.style.paddingRight = (obj.offsetWidth / 14) + "px";
    			obj.style.paddingTop = (obj.offsetHeight / 15) + "px";
    			obj.style.paddingBottom = (obj.offsetHeight / 15) + "px";
		}
	}
	else
	{
		obj.style.border = "1px solid silver";
		obj.style.padding = "5px";
		obj.style.backgroundColor = "white";
	}
}

function PopupImage(url, style)
{
    if(!window.popupWindow)
    {
    	var div = document.createElement("DIV");
    	window.popupWindow = div;
    	var iframe = document.createElement("IFRAME");
        iframe.className = "PopupImage";
        iframe.scrolling = "no";
        iframe.frameBorder = 0;
        
        div.style.visibility = "hidden";
        div.style.position = "absolute";
        div.appendChild(iframe);
        div.style.cursor = "pointer";
        document.body.appendChild(div);
        
        var doc = iframe.contentWindow.document;
        doc.open();
        doc.write("<html><body style='margin: 0px; cursor: pointer; padding: 0px; padding-top: 14px; background: transparent url(Images/Close.gif) right top no-repeat;'></body></html>");
        doc.close();
        window.popupImage = doc.createElement("IMG");
        
        if(style == null) style = 2;
        window.popupImage.onload = function()
            {
                iframe.width = this.offsetWidth;
                iframe.height = this.offsetHeight;
                div.style.width = this.offsetWidth;
                div.style.height = this.offsetHeight;
                DropShadow(div, style);
                
                var top, left;
                top = 5 + ((typeof(window.pageYOffset) == "undefined") ? document.documentElement.scrollTop : window.pageYOffset);
                left = ((typeof(window.pageXOffset) == "undefined") ? document.documentElement.scrollLeft : window.pageXOffset) + document.documentElement.clientWidth - div.offsetWidth;
                div.style.left = left + "px";
                div.style.top = top + "px";
                div.style.visibility = "visible";
            }
        doc.body.appendChild(window.popupImage);
        div.onclick = function()
        {
            div.style.visibility = "hidden";
        }
        
        doc.body.onclick = div.onclick;
    }
    window.popupImage.src = url;
}

function ShowDate(container, date)
{
    if(!date) date = new Date();
    var dateNames = new Array("Chủ nhật", "Thứ hai", "Thứ ba", "Thứ tư", "Thứ năm", "Thứ sáu", "Thứ bảy");
    var dateTemplate = "{date}, {day}/{month}/{year}"
    dateTemplate = dateTemplate.replace("{date}", dateNames[date.getDay()]);
    dateTemplate = dateTemplate.replace("{day}", date.getDate());
    dateTemplate = dateTemplate.replace("{month}", date.getMonth() + 1);
    dateTemplate = dateTemplate.replace("{year}", date.getFullYear());
    if(!container)
        document.write(dateTemplate);
    else
    {
        container = document.getElementById(container);
        if(container) container.innerHTML = dateTemplate;
    }
}
function addCommas(nStr){
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function ShowHideOpt(divmenu){
    if(!divmenu) return;
    divmenu = document.getElementById(divmenu);
    if(divmenu.style.display == "none")
    {
        divmenu.style.display = "";
    }
    else
    {
        divmenu.style.display = "none";
    }
}

function ShowGoldPrice(){
	document.write('<table width=100% border=1 bordercolor=#f6f6f6 cellpadding=2><tr><td width=100>Mua vào</td><td align=right>');
	document.write(vGoldSjcBuy);
	document.write('</td></tr><tr><td>Bán ra</td><td align=right>');
	document.write(vGoldSjcSell);
	document.write('</td></tr></table>');
}

function ShowForexRate(){
	document.write('<table width=100% border=1 bordercolor=#f6f6f6 cellpadding=2><tr><td width=100>'+vForexs[0]+'</td><td align=right>');
	document.write(vCosts[0]);
	document.write('</td></tr><tr><td>'+vForexs[1]+'</td><td align=right>');
	document.write(vCosts[1]);
	document.write('</td></tr><tr><td>'+vForexs[2]+'</td><td align=right>');
	document.write(vCosts[2]);
	document.write('</td></tr><tr><td>'+vForexs[3]+'</td><td align=right>');
	document.write(vCosts[3]);
	document.write('</td></tr><tr><td>'+vForexs[4]+'</td><td align=right>');
	document.write(vCosts[4]);
	document.write('</td></tr><tr><td>'+vForexs[5]+'</td><td align=right>');
	document.write(vCosts[5]);
	document.write('</td></tr><tr><td>'+vForexs[6]+'</td><td align=right>');
	document.write(vCosts[6]);
	document.write('</td></tr><tr><td>'+vForexs[7]+'</td><td align=right>');
	document.write(vCosts[7]);
	document.write('</td></tr><tr><td>'+vForexs[8]+'</td><td align=right>');
	document.write(vCosts[8]);
	document.write('</td></tr><tr><td>'+vForexs[9]+'</td><td align=right>');
	document.write(vCosts[9]);
	document.write('</td></tr><tr><td>'+vForexs[10]+'</td><td align=right>');
	document.write(vCosts[10]);
	document.write('</td></tr></table>');
}

function removeCommasVNE(s){
	if (s.length == 0)
		return false;
	s = s.replace('.00', '');
	for (var n = 0; n < s.length; n++)
		s = s.replace(',', '');
	return s;
}

function CalcPrice(price, unitprice, toprice){
	if(unitprice=='vnd'){
		if(toprice=='vnd') return price;
		if(toprice=='sjc') return Math.round((price/removeCommasVNE(vGoldSell))*100)/100;
		if(toprice=='usd') return Math.round((price/removeCommasVNE(vCosts[0]))*100)/100;
	}
	if(unitprice=='sjc'){
		if(toprice=='vnd') return Math.round((price*removeCommasVNE(vGoldSell))*100)/100;
		if(toprice=='sjc') return price;
		if(toprice=='usd') return Math.round(((price*removeCommasVNE(vGoldSell))/removeCommasVNE(vCosts[0]))*100)/100;
	}
	if(unitprice=='usd'){
		if(toprice=='vnd') return Math.round((price*removeCommasVNE(vCosts[0]))*100)/100;
		if(toprice=='sjc') return Math.round(((price*removeCommasVNE(vCosts[0]))/removeCommasVNE(vGoldSell))*100)/100;
		if(toprice=='usd') return price;
	}
}

function ShowActivePrice(unitprice, index){
	document.getElementById('vnd'+index).className = "nm";
	document.getElementById('sjc'+index).className = "nm";
	document.getElementById('usd'+index).className = "nm";
	document.getElementById('vndp'+index).style.display = "none";
	document.getElementById('sjcp'+index).style.display = "none";
	document.getElementById('usdp'+index).style.display = "none";
	document.getElementById(unitprice+index).className = "act";
	document.getElementById(unitprice+'p'+index).style.display = "block";
}

var dayarray=new Array("Chủ nhật","Thứ hai","Thứ ba","Thứ tư","Thứ năm","Thứ sáu","Thứ bảy")
var montharray=new Array("/01","/02","/03","/04","/05","/06","/07","/08","/09","/10","/11","/12")
function getthedate(){
	var mydate=new Date()
	var year=mydate.getYear()
	if (year < 1000)
	year+=1900
	var day=mydate.getDay()
	var month=mydate.getMonth()
	var daym=mydate.getDate()
	if (daym<10)
	daym="0"+daym
	var hours=mydate.getHours()
	var minutes=mydate.getMinutes()
	var seconds=mydate.getSeconds()
	var dn="AM"
	if (hours>=12)
	dn="PM"
	if (hours>12){
	hours=hours-12
	}
	{
	 d = new Date();
	 Time24H = new Date();
	 Time24H.setTime(d.getTime() + (d.getTimezoneOffset()*60000) + 3600000);
	 InternetTime = Math.round((Time24H.getHours()*60+Time24H.getMinutes()) / 1.44);
	 if (InternetTime < 10) InternetTime = '00'+InternetTime;
	 else if (InternetTime < 100) InternetTime = '0'+InternetTime;
	}
	if (hours==0)
	hours=12
	if (minutes<=9)
	minutes="0"+minutes
	if (seconds<=9)
	seconds="0"+seconds
		
	var cdate="&nbsp;" + dayarray[day] + ",&nbsp;" + daym + montharray[month]+ "/" + year + "&nbsp;&nbsp;" + hours + ":"+minutes+":"+seconds+" "+dn
	if (document.all)
	document.all.clock.innerHTML=cdate
	else if (document.getElementById)
	document.getElementById("clock").innerHTML=cdate
	else
	document.write(cdate)
}

function goforit(){
	if (document.all||document.getElementById)
	setInterval("getthedate()",1000)
}

function ShowHideMenu(divmenu, divicon)
{
    if(!divmenu) return;
    divmenu = document.getElementById(divmenu);
    divicon = document.getElementById(divicon);
    if(!divmenu) return;
    if(divmenu.style.display == "none")
    {
        divmenu.style.display = "";
        divicon.src = "/images/bg_menuexpand.gif";
    }
    else
    {
        divmenu.style.display = "none";
        divicon.src = "/images/bg_menucollapse1.gif";
    }
}
