﻿function isUndefined(variable) {
	return typeof variable == 'undefined' ? true : false;
}

function findtags(parentobj, tag) {
	if(!isUndefined(parentobj.getElementsByTagName)) {
		return parentobj.getElementsByTagName(tag);
	} else if(parentobj.all && parentobj.all.tags) {
		return parentobj.all.tags(tag);
	} else {
		return null;
	}
}
function doane(event) {
	e = event ? event : window.event ;
	if(is_ie) {
		e.returnValue = false;
		e.cancelBubble = true;
	} else {
		e.stopPropagation();
		e.preventDefault();
	}
}

function GetXMLHttp()
{
    var xmlhttp=false;
    
    try 
    {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } 
    catch (e) 
    {
        try 
        {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (E)
        {
            xmlhttp = false;
        }
    }

    // Mozilla then?
    if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
       xmlhttp = new XMLHttpRequest();
    }
    
    return xmlhttp;
}

function changeTab(id, count, prefix)
{
    for (var i = 1; i <= count; i++)
    {
        var tag = document.getElementById(prefix + "_tab_" + i);
        var ul = document.getElementById(prefix + "_body_" + i);
        
        if (i == id)
        {
            tag.className = "sel";
            ul.style.display = "block";
        }
        else
        {
            tag.className = "unsel";
            ul.style.display = "none";
        }
    }
}

function showHint(id)
{
	document.getElementById("download_"+id).style.display = "block";
}

function hiddenHint(id)
{
	document.getElementById("download_"+id).style.display = "none";
}

function showTip(url, name, width, height, id)
{
	html = "<div class='tooltip' style='width:"+(width+10)+"px; height:"+(height +30)+"px; '>";
	html += "<div class='bg' ></div>";
	html += '<h4>Full size of "' + name + '"</h4>';
	html += "<img id='img1_"+id+"' src='/images/spin_32.gif' style='margin-top:" + ((height - 32)/2) + "px;margin-left:" + ((height - 32)/2) + "px;'>";
	html += "<img id='img2_"+id+"' src='" + url +"' onload=\"document.getElementById('img1_"+id+"').style.display='none'; this.style.display='block';\" style='display: none;' /></div";
	//setTimeout("if (!isUndefined(document.getElementById('img1_"+id+"'))) {document.getElementById('img1_"+id+"').style.display='none'; document.getElementById('img2_"+id+"').style.display='block';}",1500);
	//alert(html);
	Tip(html, BGCOLOR, '' , BORDERWIDTH, 0, PADDING, 0, FADEIN, 200);
}

function addTag(id)
{
	debugger;
	var tag = $.trim($('#tag').val());
	if (tag.length < 2)
	{
		alert('The tag is too short.');
		return;
	}
	if (tag.length > 30)
	{
		alert('The tag is too long.');
		return;
	}
	//var regex = /^[\w\s]+$/;
	var regex = /^[\u4E00-\u9FA5\w\s]+$/;
	var regex = /^[^`~!@#$%^&*()+=|\\\][\]\{\}:;'\,.<>\/?]{1}[^`~!@$%^&()+=|\\\]  [\]\{\}:;'\,.<>?]{0,19}$/;
	if (!regex.test(tag))
	{
		alert('The tag you input is not valid.');
		return;
	}
	for(var i=0; i<tags.length; i++)
	{
		if (tag.toLowerCase() == tags[i].toLowerCase())
		{
			alert('The tag already exist!');
			return;
		}
	}

	$('#tags ul').append('<li><a href="/search/'+tag+'/">'+tag+'</a></li>');
	tags[tags.length] = tag;
	$('#tag').val('');
	alert(encodeURI(tag));
	$.get('/tag.php', 
		{ action: "add", id: id, tag: encodeURI(tag) }, 
		function(data){
			alert(data);
			});
}

function showPng(token, packToken, name, width, height)
{
	var dialog = document.getElementById("dialog");
	dialog.style.height =  (140 + height) + "px";
	
	var ww = 10;
	if (width > 256) 
		ww = width + 10 + 256 + 10 + 128 + 10 + 64 + 10 + 48 + 10 + 32 + 10 + 16 + 10;
	else if (width > 128)
		ww = width + 10 + 128 + 10 + 64 + 10 + 48 + 10 + 32 + 10 + 16 + 10;
	else if (width > 64)
		ww = width + 10 + 64 + 10 + 48 + 10 + 32 + 10 + 16 + 10;
	else if (width > 48)
		ww = width + 10 + 48 + 10 + 32 + 10 + 16 + 10;
	else if (width > 32)
		ww = width + 10 + 32 + 10 + 16 + 10;
	else if (width > 16)
		ww = width +10 + 16 + 10;

	dialog.style.width = ((parseInt(ww)<400 ? 400 : ww) + 80) + "px";

	var h = document.documentElement.clientHeight;
	dialog.style.top =  (parseInt(h) - parseInt(dialog.style.height))/2 + "px";
	var w = document.documentElement.clientWidth;
	dialog.style.left =  (parseInt(w) - parseInt(dialog.style.width))/2+'px';
	
	document.getElementById("ipng").style.height = (height + 100) + "px";
	document.getElementById('ititle').innerHTML = 'Png files of Icon "' + name + '"';
	$('#dialog').jqmShow();
	document.getElementById("ipng").src = "/png/"+packToken+'/'+token+".html";
}
function hidePng()
{
	$('#dialog').jqmHide();
	document.getElementById("ipng").src = "/loading.html";
}


