/*
 [Vicworl] (C)2006-2008 Xi'An Dreamsoft Inc.

 file: javascript/global.js

 */


function KeyDown(evt)
{
    if (evt.keyCode == 13)
    {
        evt.returnValue=false;
        evt.cancel = true;
        checkSubmit();
    }
}
function ctlent(event,btn_name) {
	if((event.ctrlKey && event.keyCode == 13) || (event.altKey && event.keyCode == 83)) {
		var btn=$(btn_name);
		if(btn){
			btn.click();
		}
	}
}
function get_extname(s)
{
	var s = new String(s)
	s = s.toLowerCase();
	if ((s.lastIndexOf(".")>-1) &&((s.length-1)>s.lastIndexOf(".")))
	{
		return s.substring(s.lastIndexOf(".")+1);
	}
	else
	{
		return false;
	}
}
_gObj = function(id){return document.getElementById(id);}

$ = function(id){return document.getElementById(id);}

//检查正则表达式
function chksafe(strString,myReg)
{ 
  if(myReg.test(strString)){
  return false; 
 }
 return true;
}

//测试字符串长度
function strlen(str){
 if(str.length > 0){
  return false;
 }
 return true;
}

//测试字符串长度(中文适用)
function checkVal(s){
	var k = 0
	for(var i=0;i<s.length;i++){
		if(s.charCodeAt(i) > 255){
		k += 2
		}
		else{
		k += 1
		}
	}
	return k;
}

function showadvinfo(obj)
{
	$('tips').style.display="none";
	$('_tips').style.display="none";
	
	if(obj.checked==true)
		$('advbox').style.display="";
	else
		$('advbox').style.display="none";
}

function showline(obj,str)
{
	xy=fGetXY(obj);
	$('tips').style.top=xy.y;
	$('tips').style.left=xy.x+170;
	$('_tips').style.top=xy.y+5;
	$('_tips').style.left=xy.x+164;
	$('tips').innerHTML=str;
	if(str==""||str==undefined)
	{
		$('tips').style.display="none";
		$('_tips').style.display="none";
	}
	else
	{
		$('tips').style.display="block";
		$('_tips').style.display="block";
	}
}

function fGetXY(aTag)
{
    var oTmp = aTag;
    var pt = new Point(0,0);
    do 
    {
        pt.x += oTmp.offsetLeft;
        pt.y += oTmp.offsetTop;
        oTmp = oTmp.offsetParent;
    } while(oTmp.tagName!="BODY");
    return pt;
}

function Point(iX, iY)
{
    this.x = iX;
    this.y = iY;
}

/*
* Interfaces:
* b64 = base64encode(data);
* data = base64decode(b64);
*/


var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var base64DecodeChars = new Array(
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
    52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
    -1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
    15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
    -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
    41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1);

function base64encode(str) {
    var out, i, len;
    var c1, c2, c3;

    len = str.length;
    i = 0;
    out = "";
    while(i < len) {
        c1 = str.charCodeAt(i++) & 0xff;
        if(i == len)
        {
            out += base64EncodeChars.charAt(c1 >> 2);
            out += base64EncodeChars.charAt((c1 & 0x3) << 4);
            out += "==";
            break;
        }
        c2 = str.charCodeAt(i++);
        if(i == len)
        {
            out += base64EncodeChars.charAt(c1 >> 2);
            out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
            out += base64EncodeChars.charAt((c2 & 0xF) << 2);
            out += "=";
            break;
        }
        c3 = str.charCodeAt(i++);
        out += base64EncodeChars.charAt(c1 >> 2);
        out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
        out += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6));
        out += base64EncodeChars.charAt(c3 & 0x3F);
    }
    return out;
}

function base64decode(str) {
    var c1, c2, c3, c4;
    var i, len, out;

    len = str.length;
    i = 0;
    out = "";
    while(i < len) {
        /* c1 */
        do {
            c1 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
        } while(i < len && c1 == -1);
        if(c1 == -1)
            break;

        /* c2 */
        do {
            c2 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
        } while(i < len && c2 == -1);
        if(c2 == -1)
            break;

        out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4));

        /* c3 */
        do {
            c3 = str.charCodeAt(i++) & 0xff;
            if(c3 == 61)
                return out;
            c3 = base64DecodeChars[c3];
        } while(i < len && c3 == -1);
        if(c3 == -1)
            break;

        out += String.fromCharCode(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2));

        /* c4 */
        do {
            c4 = str.charCodeAt(i++) & 0xff;
            if(c4 == 61)
                return out;
            c4 = base64DecodeChars[c4];
        } while(i < len && c4 == -1);
        if(c4 == -1)
            break;
        out += String.fromCharCode(((c3 & 0x03) << 6) | c4);
    }
    return out;
}
var Strusername;
function checkform(obj,Susername)
{
	Strusername=Susername;
	$('formsubmit').disabled=true;
	
	checku=check_username('username');
	
	valid=Validator.Validate(obj,3);
	
	checktu=check_emptylong('truename',4,20);
	
	checkidcard=check_emptylong('idcard',4,18);
	
	checkcf=check_emptylong('comefrom',4,20);
	
	checkpweb=check_emptylong('pweb',4,60);
	
	checkqq=check_emptyqq('qq',5,9);
	
	checkmsn=check_emptylong('msn',6,60);
	
	if(valid&&checku&&checktu&&checkidcard&&checkcf&&checkpweb&&checkqq&&checkmsn)
	{
		check_reg();
	}
	else
	{
		$('formsubmit').disabled=false;
	}
	return false;
}

function check_reg()
{
	if($('authcode')!=null)
	{
		var authcode = $('authcode').value;
	}
	else
	{
		var authcode = "0000";
	}
	if($('answer')!=null)
	{
		var answer = encodeURI($('answer').value);
	}
	else
	{
		var answer = "0000";
	}
	
	var username = encodeURI($('username').value);
	
	var url = 'ajax.php';
	var pars = 'action=check_reg&u='+username+'&s=' + authcode+'&a=' + answer;

	var myAjax = new Ajax.Request(
	url,
	{
		method: 'get',
		parameters: pars,
		onComplete: ajaxobject
	});
}

function ajaxobject(originalRequest){
	datastr=originalRequest.responseText;       
	var str= new Array();
	str=datastr.split(",");
	username=str[0];
	checkcode=str[1];
	answer=str[2];
	
	if(username=="0")
	{
		$('username_msg').style.display="";
		$('username_msg').innerHTML+=Strusername;
		checkuser=false;
	}
	else
	{
		$('username_msg').style.display="none";
		checkuser=true;
	}
	
	if(checkcode=="0")
	{
		$('num_msg').style.display="";
		checkc=false;
	}
	else
	{
		if($('num_msg'))
			$('num_msg').style.display="none";
		checkc=true;
	}

	if(answer=="0")
	{
		$('answer_msg').style.display="";
		checkanswer=false;
	}
	else
	{
		if($('answer_msg'))
			$('answer_msg').style.display="none";
		checkanswer=true;
	}
	if(checkuser&&checkc&&checkanswer)
	{
		document.reg.submit();
	}
	else
	{
		$('formsubmit').disabled=false;
	}
}

function check_username(evtstr)
{
	if (strlen($(evtstr).value))
	{
		$(evtstr+'_msg').style.display="";
		return false;
	}
	else
	{
	
		if (chksafe($(evtstr).value,/^[^ \'\"\.;`]{2,20}$/)||(checkVal($(evtstr).value)>20||checkVal($(evtstr).value)<4))
		{
			$(evtstr+'_msg').style.display="";
			return false;
		}
		else
		{
			$(evtstr+'_msg').style.display="none";
			return true;
		}
	}
	
}

function check_emptylong(evtstr,min,max)
{
	if($(evtstr).value!="")
	{
		if(checkVal($(evtstr).value)<min||checkVal($(evtstr).value)>max||chksafe($(evtstr).value,/^[^ \'\";`]+$/))
		{
			$(evtstr+'_msg').style.display="";
			return false;
		}
		else
		{
			$(evtstr+'_msg').style.display="none";
			return true;
		}
	}
	else
	{
		$(evtstr+'_msg').style.display="none";
	 	return true;
	}
}

function check_emptyqq(evtstr,min,max)
{
	if($(evtstr).value!="")
	{
		if(isNaN($(evtstr).value)||checkVal($(evtstr).value)<min||checkVal($(evtstr).value)>max||chksafe($(evtstr).value,/^[^ ]+$/))
		{
			$(evtstr+'_msg').style.display="";
			return false;
		}
		else
		{
			$(evtstr+'_msg').style.display="none";
			return true;
		}
	}
	else
	{
		$(evtstr+'_msg').style.display="none";
	 	return true;
	}
}

 function CopyText(id)
        {
                //copyToClipboard(document.getElementById(id).value);
                copy(document.getElementById(id).value);
        }
         function copy(text2copy) { 
        if (window.clipboardData) {   
          window.clipboardData.setData("Text",text2copy);   
        } 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="_clipboard.swf" FlashVars="clipboard='+escape(text2copy)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';//这里是关键
   document.getElementById(flashcopier).innerHTML = divinfo;
   alert('Text copied');
   }
   }
        function copyToClipboard(meintext)
    {
     if (window.clipboardData) 
       {
       alert("ie");
       // the IE-manier
       window.clipboardData.setData("Text", meintext);
       
       // waarschijnlijk niet de beste manier om Moz/NS te detecteren;
       // het is mij echter onbekend vanaf welke versie dit precies werkt:
       }
       else if (window.netscape) 
       { 
       
       // dit is belangrijk maar staat nergens duidelijk vermeld:
       // you have to sign the code to enable this, or see notes below 
       netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
       
       // maak een interface naar het clipboard
       var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
       if (!clip) return;
       alert("mozilla");
       // maak een transferable
       var trans = Components.classes['@mozilla.org/widget/transferable;1']
                      .createInstance(Components.interfaces.nsITransferable);
       if (!trans) return;
       
       // specificeer wat voor soort data we op willen halen; text in dit geval
       trans.addDataFlavor('text/unicode');
       
       // om de data uit de transferable te halen hebben we 2 nieuwe objecten 
       // nodig om het in op te slaan
       var str = new Object();
       var len = new Object();
       
       var str = Components.classes["@mozilla.org/supports-string;1"]
                    .createInstance(Components.interfaces.nsISupportsString);
       
       var copytext=meintext;
       
       str.data=copytext;
       
       trans.setTransferData("text/unicode",str,copytext.length*2);
       
       var clipid=Components.interfaces.nsIClipboard;
       
       if (!clip) return false;
       
       clip.setData(trans,null,clipid.kGlobalClipboard);
       
       }
       alert(meintext);
       return false;
}

function sendmsg(tit)
{
	if($('sendmsgdialog')==null)
	{
		smsg = new Window('sendmsgdialog', {className: "alphacube", title: tit, 
								  width:300, height:222,
								  resizable: false,maximizable:false,minimizable:false,closable: true, showEffectOptions: {duration:1}})
		smsg.getContent().innerHTML=$("sendmsg").innerHTML;
		smsg.showCenter();
	}
	else
	{
		smsg.showCenter();
	}
}

function rp()
{
	if($('rpdialog')==null)
	{
		rpd = new Window('rpdialog', {className: "alphacube", title: "", 
								  width:300, height:240,
								  resizable: false,maximizable:false,minimizable:false,closable: true, showEffectOptions: {duration:1}})
		rpd.getContent().innerHTML=$("rp").innerHTML;
		rpd.showCenter();
	}
	else
	{
		rpd.showCenter();
	}
}

function getdat()
{
    return data=thisdomain+"/attachment/"+userid+"/"+mediatype+"/"+id+".flv";
}

function makefriend(id)
{
	ajaxupdate("ajax_script","ajax.php?action=makefriend&friendid="+id);
}
function ajaxupdate(strObjName, strURL)
{
		new Ajax.Updater(strObjName,strURL,{asynchronous:true,evalScripts:true});
}
function sendmsgdata(to)
{
	ajaxupdate("ajax_script","ajax.php?action=sendmsg&to="+to+"&msg_title="+encodeURI($("msgtitle").value)+"&msg_content="+encodeURI($("msg_content").value));

}
function sendrpdata(from)
{
	ajaxupdate("ajax_script","ajax.php?action=sendrp&from="+from+"&rp_content="+encodeURI($("rp_content").value));

}
function pushup(id,isindex)
{
	if(isindex==1)		
	ajaxupdate("ajax_script","ajax.php?action=pushup&inindex=1&id="+id);
	else
	ajaxupdate("ajax_script","ajax.php?action=pushup&id="+id);
}
function showbox(str)
{
win = new Window({className: "alphacube", title: "Sample", width:200, height:150, destroyOnClose: true, recenterAuto:true});

win.getContent().update(str);
win.showCenter();
}

function DsAlert(str) {
   return Dialog.alert(str, {className: "alphacube",okLabel: "确定"});
}
function DsInfo(str,options) {
if(options==true)
		str="<br /><img src='image/default/ajax-loader.gif' /><br /><br />"+str;
   Dialog.info(str,
               {className: "alphacube",width:250, height:100});
}
  
function DsConfirm(str) {
    return Dialog.confirm(str,{width:250, className: "alphacube", okLabel: "确定", cancelLabel:"取消"})
}
  
function DsAjaxConfirm() {
      Dialog.confirm({url: "dialog_ajax.html", options: {method: 'get'}}, 
                     {width:250, className: "alphacube", okLabel: "确定", cancelLabel:"取消"})    
}

function readComment(strObjName, strURL)
{
		new Ajax.Updater(strObjName,strURL,{asynchronous:true,evalScripts:true});
}

function starin(index)
{
	obj=document.getElementsByName("starimg");
	for(i=4;i>=0;i--)
	{
		obj[i].src="image/default/btn_star0.gif";
	}
	for(i=index;i>=0;i--)
	{
		obj[i].src="image/default/btn_star1.gif";
	}
}
function starout(index)
{
	starin(rank_star_num);	
}
function rankup(rank,id)
{
	ajaxupdate("ajax_script","ajax.php?action=rankup&id="+id+"&rank="+rank);
}

function changecolor(idstr,size,thisnum,oldcolor,newcolor)
{
    for(i=0;i<size;i++)
    {
        $(idstr+"_"+i).style.color=oldcolor;
        if(i==thisnum)
            $(idstr+"_"+i).style.color=newcolor;
    }
}
function ajaxloading(str)
{
    $(str).innerHTML="<div align='center'><img src='image/default/ajax_loading.gif' /></div>"
}

function sethome(obj,vrl)
    {
        try
        {
                obj.style.behavior='url(#default#homepage)';obj.setHomePage(vrl);
        }
        catch(e){
                if(window.netscape) {
                        try {
                                netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); 
                        } 
                        catch (e) { 
                                alert("此操作被浏览器拒绝！\n请在浏览器地址栏输入“about:config”并回车\n然后将[signed.applets.codebase_principal_support]设置为'true'"); 
                        }
                        var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
                        prefs.setCharPref('browser.startup.homepage',vrl);
                 }
        }
    }

