$ERRORS = {};
		
		
$ERRORS["200"] = "网址有误";	
$ERRORS["201"] = "网址不能为空";	
$ERRORS["202"] = "网址格式错误";		
$ERRORS["203"] = "网址太长了";		
		
$ERRORS["300"] = "别名只能为数字和字母";
$ERRORS["301"] = "别名请不要超过10位";
$ERRORS["302"] = "别名至少需要3位";
$ERRORS["303"] = "此别名已经存在";

function showTab(index)
{
	$("tabHeader0").className = "tabHeader";
	$("tabHeader1").className = "tabHeader";
	$("tabHeader"+index).className = "tabHeader selectedHeader";
	
	$("tab0").className = "hidden tabContent";
	$("tab1").className = "hidden tabContent";
	$("tab"+index).className = "visible tabContent";
}

function generate(){
	var url = $("url").value;

	if((url.charAt(0)< 'a' || url.charAt(0)> 'z') && (url.charAt(0)< 'A' || url.charAt(0)> 'Z'))
	{
		DuanWZ.error("您输入的网址有错误");
		return;
	}

	if(url.indexOf("://") == -1)
	{
		DuanWZ.error("您输入的网址有错误");
		return;
	}
	
	url = url.replace("\r","")
	url = url.replace("\n","")
		
	if(url.length > 500)
	{
		DuanWZ.error("您输入的网址太长了");
		return;
	}
		
	var vars = "url="+encodeURIComponent(url);
	
		
		var custom = $("custom").value;
		if(custom.length > 0){
			if(custom.length < 3){
				DuanWZ.error ("别名不能少于三个字符");
				return;
			}
			else
				vars += "&custom="	+ custom;
		}
		
		
		if($("password").value != "")
		{
			vars += "&password="+ $("password").value;	
		}

	
	$("alias").value = "";
	$("loading").className = "visible";
	
	new Request.JSON({ url: 'api.json',onSuccess:generated}).send(vars);
}

function generated(result){

	$("loading").className = "hidden";
	$("result").className = "visible";
	$('result').highlight('#FF9900');
	
	if(result.status == "100" )
	{				
		var hostname = location.hostname;		
		/*if(hostname.indexOf("www.") > -1)
		{
			hostname = hostname.substr(4);
		}*/
		
		if(hostname.indexOf("preview.") > -1)
		{
			hostname = hostname.substr(8);
		}
		
		if(hostname.indexOf("stat.") > -1)
		{
			hostname = hostname.substr(5);
		}
		
		if(location.port != "" && location.port != 80 )
			host = hostname+":" + location.port;
		else
			host = hostname;
	
		aliasUrl = "http://"+host+"/"+result.value;
		
		$("alias").value= aliasUrl;
		$("open").href = aliasUrl;
		$("stat").href = "http://preview."+host+"/"+result.value;
		//$("preview").href = "http://preview."+hostname+"/"+value;
	}
	else
	{
		DuanWZ.error(result.value);
	}
}


function getRadioValue(radioName){
    var obj;    
    obj=document.getElementsByName(radioName);
    if(obj!=null){
        var i;
        for(i=0;i<obj.length;i++){
            if(obj[i].checked){
                return obj[i].value;            
            }
        }
    }
    return null;
}

function Ajax()
{
	var xmlhttp;
	try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
	catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
	catch (e) { try { xmlhttp = new XMLHttpRequest(); }
	catch (e) { xmlhttp = false; }}}
	if (!xmlhttp) return null;
	this.connect = function(url, method, vars, callback)
	{
		if (!xmlhttp) return false;
		
		method = method.toUpperCase();
		
		try {
			if (method == "GET")	{
				xmlhttp.open(method, url+"?"+vars, true);
				vars = null;
			}else{
				xmlhttp.open(method, url, true);
				xmlhttp.setRequestHeader("Method", "POST "+url+" HTTP/1.1");
				xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			}
	
			xmlhttp.onreadystatechange = function(){
				if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
					callback(xmlhttp.responseText);
			};
			
			xmlhttp.send(vars);
		}
	  	catch(e) { return false; }
		return true;
	};
	return this;
}


function copy() {	
	var alias = $("alias").value;
	
	if(window.clipboardData){
		window.clipboardData.setData("Text",alias); 
	}
	else{	
		var flashcopier = 'flashcopier';
		if(!document.getElementById(flashcopier)) {
			var divholder = document.createElement('div');
			divholder.id = flashcopier;
			document.body.appendChild(divholder);
		}
		document.getElementById(flashcopier).innerHTML = '';
		var divinfo = '<embed src="images/clipboard.swf" FlashVars="clipboard='+alias+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
		document.getElementById(flashcopier).innerHTML = divinfo;
	}
	DuanWZ.error('已将网址复制到剪切板')
}


function addAccelerator() {
    try{
         window.external.addService('../tools/accelerator.xml');
    }
    catch(e){     
        alert('请首先安装IE8');
    }    
}
