﻿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 GetAd(url, link, className, target, title, width)
{
	var retad;
    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))
    {
        retad = "\
        <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
    {
        retad = "\
        <a " + className + " " + link + " " + title + " " + target + ">\
            <img src='" + url + "' " + width + "/>\
        </a>\
        ";
    }
    return retad;
}

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 ShowGoldPricevn(){
	document.write('<table width=100% border=1 bordercolor=#eeeeee 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 ShowGoldPriceen(){
	document.write('<table width=100% border=1 bordercolor=#eeeeee cellpadding=2><tr><td width=100>Buy</td><td align=right>');
	document.write(vGoldSjcBuy);
	document.write('</td></tr><tr><td>Sell</td><td align=right>');
	document.write(vGoldSjcSell);
	document.write('</td></tr></table>');
}

function ShowForexRate(){
	document.write('<table width=100% border=1 bordercolor=#eeeeee 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 PlayClip(url, img)
{
	var s1 = new SWFObject('/player/player.swf','player','198','154','9');
	s1.addParam("allowfullscreen","true");
	s1.addParam("allownetworking","all");
	s1.addParam("allowscriptaccess","always");
	s1.addParam("flashvars","file="+url+"&image="+img+"&autostart=true&displayclick=fullscreen");
	s1.write("previewclip");
}

function SendToFriend(iframe)
{
    if(!iframe) return;
    iframe = document.getElementById(iframe);
    if(!iframe) return;
    if(iframe.style.display == "none")
    {
        iframe.src = "/SendToFriend/";
        iframe.style.display = "";
    }
    else
    {
        iframe.style.display = "none";
    }
}

function validEmail(obj) {
	var s = obj.value;
	for (var i=0; i<s.length; i++)
		if (s.charAt(i)==" "){
			return false;
		}
	var elem, elem1;
	elem=s.split("@");
	if (elem.length!=2)	return false;

	if (elem[0].length==0 || elem[1].length==0)return false;

	if (elem[1].indexOf(".")==-1)	return false;

	elem1=elem[1].split(".");
	for (var i=0; i<elem1.length; i++)
		if (elem1[i].length==0)return false;
	return true;
}

function testSpaceBar(obj){
	if(obj.value=="")return false;
	else{		
		var s = obj.value;
		var temp = s.split(" ");
		var str = "";
		for(var i=0; i<temp.length; i++)str=str + temp[i];
		if(str==""){
			obj.value = str.substring(0,str.length);
			return false;
		}
	}//else
	return true;
}

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 ShowWeatherBox(vId){
	var sLink = '';
	sLink = 'http://vnexpress.net/ListFile/Weather/';
	switch (parseInt(vId)){	    	
		case 1: sLink = sLink.concat('Sonla.xml');break;
		case 2: sLink = sLink.concat('Viettri.xml');break;
		case 3: sLink = sLink.concat('Haiphong.xml');break;
		case 4: sLink = sLink.concat('Hanoi.xml');break;
		case 5: sLink = sLink.concat('Vinh.xml');break;
		case 6: sLink = sLink.concat('Danang.xml');break;
		case 7: sLink = sLink.concat('Nhatrang.xml');break;
		case 8: sLink = sLink.concat('Pleicu.xml');break;		
		case 9: sLink = sLink.concat('HCM.xml');break;	
		default: sLink = sLink.concat('Hanoi.xml');break;
	}
	AjaxRequest.get(
		{
			'url':sLink
			,'onSuccess':function(req){
				var vAdImg, vAdImg1, vAdImg2, vAdImg3, vAdImg4, vAdImg5, vWeather;
				vAdImg = req.responseXML.getElementsByTagName('AdImg').item(0).firstChild.nodeValue;
				vAdImg1 = req.responseXML.getElementsByTagName('AdImg1').item(0).firstChild.nodeValue;
				if(req.responseXML.getElementsByTagName('AdImg2').item(0).firstChild != null)
					vAdImg2 = req.responseXML.getElementsByTagName('AdImg2').item(0).firstChild.nodeValue;
				if(req.responseXML.getElementsByTagName('AdImg3').item(0).firstChild != null)
					vAdImg3 = req.responseXML.getElementsByTagName('AdImg3').item(0).firstChild.nodeValue;
				if(req.responseXML.getElementsByTagName('AdImg4').item(0).firstChild != null)
					vAdImg4 = req.responseXML.getElementsByTagName('AdImg4').item(0).firstChild.nodeValue;
				if(req.responseXML.getElementsByTagName('AdImg5').item(0).firstChild != null)
					vAdImg5 = req.responseXML.getElementsByTagName('AdImg5').item(0).firstChild.nodeValue;
				vWeather = req.responseXML.getElementsByTagName('Weather').item(0).firstChild.nodeValue;
				GetWeatherBox(vAdImg, vAdImg1, vAdImg2, vAdImg3, vAdImg4, vAdImg5, vWeather);				
				}
			,'onError':function(req){}
		}
	)
}

function GetWeatherBox(vImg, vImg1, vImg2, vImg3, vImg4, vImg5, vWeather){
	var sHTML = '';
	sHTML = sHTML.concat('<img src="/Images/Weather/').concat(vImg).concat('" class="img-weather" alt="" />&nbsp;');
	sHTML = sHTML.concat('<img src="/Images/Weather/').concat(vImg1).concat('" class="img-weather" alt="" />');
	if(vImg2!=null) sHTML = sHTML.concat('<img src="/Images/Weather/').concat(vImg2).concat('" class="img-weather" alt="" />');
	if(vImg3!=null) sHTML = sHTML.concat('<img src="/Images/Weather/').concat(vImg3).concat('" class="img-weather" alt="" />');
	if(vImg4!=null) sHTML = sHTML.concat('<img src="/Images/Weather/').concat(vImg4).concat('" class="img-weather" alt="" />');
	if(vImg5!=null) sHTML = sHTML.concat('<img src="/Images/Weather/').concat(vImg5).concat('" class="img-weather" alt="" />');
	sHTML = sHTML.concat('<img src="/Images/Weather/c.gif" class="img-weather" alt="" />');
	
	gmobj('img-Do').innerHTML = sHTML;
	gmobj('txt-Weather').innerHTML = vWeather;
}

function show_current(){
	var id = activeMenuId;
	var ItemLi = document.getElementById('li_'+id);
	var ItemA = document.getElementById('a_'+id);
	ItemA.className = "aactive";
	ItemLi.className = "liactive";
}

function goLink(){	
	var url = document.weblink.link.options[document.weblink.link.selectedIndex].value;
	window.open(url);
}

