/*
START ************* PNG transparency in Win IE 5.5 & 6. *************
Correctly handle PNG transparency in Win IE 5.5 & 6.
http://homepage.ntlworld.com/bobosola. Updated 18-Jan-2006.

Use in <HEAD> with DEFER keyword wrapped in conditional comments:
<!--[if lt IE 7]>
<script defer type="text/javascript" src="pngfix.js"></script>
<![endif]-->

*/
function correctPNG(img_file, w, h, element_id) // correctly handle PNG transparency in Win IE 5.5 & 6.
{
//	alert(img_file+', '+w+', '+h)
	var arVersion = navigator.appVersion.split("MSIE")
	var version = parseFloat(arVersion[1])
	
	if ((version >= 5.5) && (document.body.filters)) 
	{
		 var strNewHTML = "<span style=\"width: "+ w +"px; height: "+ h +"px;display:inline-block;"
		 + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
		 + "(src='"+ img_file +"', sizingMethod='scale');\"></span>" 
	}else{
//		var strNewHTML = '<img src="'+ img_file +'" width='+ w +' height='+ h +'>';
		var strNewHTML = '<img src="'+ img_file +'" width='+ w +'>';
	}
//		 alert(strNewHTML)
	
	$(element_id).innerHTML = strNewHTML;

}
/* END  ************* PNG transparency in Win IE 5.5 & 6. **************/

/*reload() after the greybox close*/
function hide_gb(){
	window.location.reload();
//	GB_CURRENT.hide();
}

function switch_lang(lang){
	myAjax = new Ajax.Request( host_url +'rpc/switch_lang.php', 
	{
	method: 'GET',
	asynchronous: false,
	parameters: {dz_lang: lang}
	} );
	location.reload();
}
function switch_lang_index(lang){
	myAjax = new Ajax.Request( host_url +'rpc/switch_lang.php', 
	{
	method: 'GET',
	asynchronous: false,
	parameters: {dz_lang: lang}
	} );
	location.replace(host_url );
}

/********************************************* START scroll content*/
function move_left_tee(){
//alert('waktu='+waktu)
	scrolling_pane_tee.scrollBy(-10,0);waktu=setTimeout('move_left_tee()',60);
}
function move_right_tee(){
	scrolling_pane_tee.scrollBy(10,0);waktu=setTimeout('move_right_tee()',60);
}
/********************************************** END scroll content*/

/*when first dropdown changes, then second dropdown box will change accordingly*/
function selectChange(control, controlToPopulate, ItemArray, GroupArray, sel) {
  var myEle ;
  var x ;
  // Empty the second drop down box of any choices
  for (var q=controlToPopulate.options.length;q>=0;q--) controlToPopulate.options[q]=null;

/*   ADD Default Choice - in case there are no values
  myEle = document.createElement("option") ;
  myEle.value = 0 ;
  myEle.text = "[SELECT]" ;
   controlToPopulate.add(myEle) ;
  controlToPopulate.appendChild(myEle)
*/  // Now loop through the array of individual items
  // Any containing the same child id are added to
  // the second dropdown box
  for ( x = 0 ; x < ItemArray.length  ; x++ ) {
    if ( GroupArray[x] == control.value ) {
      myEle = document.createElement("option") ;
      //myEle.value = x ;
      myEle.setAttribute('value',x);
	  // joemak
	  if(sel == x)
	      myEle.setAttribute('selected', 'selected');
      // myEle.text = ItemArray[x] ;
      var txt = document.createTextNode(ItemArray[x]);
      myEle.appendChild(txt)
      // controlToPopulate.add(myEle) ;
      controlToPopulate.appendChild(myEle)
    }
  }
}

//SuckerTree Horizontal Menu (Sept 14th, 06)
//By Dynamic Drive: http://www.dynamicdrive.com/style/

var menuids=["treemenu1"] //Enter id(s) of SuckerTree UL menus, separated by commas

function buildsubmenus_horizontal(){
for (var i=0; i<menuids.length; i++){
  var ultags=document.getElementById(menuids[i]).getElementsByTagName("ul")
    for (var t=0; t<ultags.length; t++){
		if (ultags[t].parentNode.parentNode.id==menuids[i]){ //if this is a first level submenu
			ultags[t].style.top=ultags[t].parentNode.offsetHeight+"px" //dynamically position first level submenus to be height of main menu item
			ultags[t].parentNode.getElementsByTagName("a")[0].className="mainfoldericon"
		}
		else{ //else if this is a sub level menu (ul)
		  ultags[t].style.left=ultags[t-1].getElementsByTagName("a")[0].offsetWidth+"px" //position menu to the right of menu item that activated it
    	ultags[t].parentNode.getElementsByTagName("a")[0].className="subfoldericon"
		}
    ultags[t].parentNode.onmouseover=function(){
    this.getElementsByTagName("ul")[0].style.visibility="visible"
    }
    ultags[t].parentNode.onmouseout=function(){
    this.getElementsByTagName("ul")[0].style.visibility="hidden"
    }
    }
  }
}

function add2cart(product_id, size, qty, act, main_product_id){
//alert('design_id='+product_id+', size='+size+', qty='+qty+', act='+act+', main_product_id='+main_product_id)
	var url = host_url+'rpc/add2cart.php';
	var myAjax = new Ajax.Updater('cart', url,
	{
			method: 'GET',
			parameters: {main_product_id: main_product_id, product_id: product_id, size: size, qty: qty, act: act},
			onLoading: function(){
					$('cart').innerHTML = '<img src="'+host_url+'images/spinner.gif" /> Processing...';
			}
	}
	);
}
function findPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }

  function findPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }

/* START drop down tab*/
//var menuids=["treemenu_sucker"] //Enter id(s) of SuckerTree UL menus, separated by commas

function buildsubmenus_horizontal(){
	var footer_nav = document.getElementById('footer_nav')
for (var i=0; i<menuids.length; i++){
  var ultags=document.getElementById(menuids[i]).getElementsByTagName("ul")
    for (var t=0; t<ultags.length; t++){
		if (ultags[t].parentNode.parentNode.id==menuids[i]){ //if this is a first level submenu
		   m=ultags[t].childNodes;
	//	   tall = m.length * 30; // assume each menu is 30 px height
//		alert('element='+ ultags[t].innerHTML +' m=' + m.length)
/*			tall = ultags[t].offsetHeight + ultags[t].parentNode.offsetHeight + findPosY(ultags[t].parentNode)

			if((tall + findPosY(ultags[t].parentNode)) > findPosY(footer_nav)){
				var top_px = tall * -1
				ultags[t].style.top= top_px+"px"
		   }else{
				ultags[t].style.top=ultags[t].parentNode.offsetHeight+"px" 
		   }
*/

			ultags[t].style.top=ultags[t].parentNode.offsetHeight+"px" 
			ultags[t].parentNode.getElementsByTagName("a")[0].className="mainfoldericon"
		}
		else{ //else if this is a sub level menu (ul)
		  ultags[t].style.left=ultags[t-1].getElementsByTagName("a")[0].offsetWidth+"px" //position menu to the right of menu item that activated it
    	ultags[t].parentNode.getElementsByTagName("a")[0].className="subfoldericon"
		}
    ultags[t].parentNode.onmouseover=function(){
    this.getElementsByTagName("ul")[0].style.visibility="visible"
//	    this.getElementsByTagName("ul")[0].style.display="block"
    }
    ultags[t].parentNode.onmouseout=function(){
    this.getElementsByTagName("ul")[0].style.visibility="hidden"
//		this.getElementsByTagName("ul")[0].style.display="none"
    }
    }
  }
}

/*if (window.addEventListener)
window.addEventListener("load", buildsubmenus_horizontal, false)
else if (window.attachEvent)
window.attachEvent("onload", buildsubmenus_horizontal)*/
/*END drop down tab*/

/*for the flash engine */
function login2blog(user_name, passwd){
	login2wp(user_name, passwd);
	login2forum(user_name, passwd);	
}

/*for reset password*/
function login3(){
	var url = host_url+'user/login.php';
	var myAjax = new Ajax.Updater('reset_box_msg', url, 
		{
			method: 'POST', 
			parameters: {email: $("reset_email").value, question: $('reset_question').value, answer: $('reset_answer').value, act: 'reset_passwd'},
			onLoading: function(){
				$('reset_box_msg').innerHTML = '<img src="'+host_url+'images/spinner.gif" /> Processing...';
			}
		}
	);	
}

/*for the popup login box*/
function login2(){
	var url = host_url+'user/login.php';
	var myAjax = new Ajax.Updater('login_box', url, 
		{
			method: 'POST', 
			parameters: {user_name: $("user_name2").value, passwd: $('passwd2').value, act: 'login'},
			onLoading: function(){
				$('login_box').innerHTML = '<img src="'+host_url+'images/spinner.gif" /> Processing...';
			}
		}
	);	
}
function logout(){
/*        var myAjax = new Ajax.Updater('load', host_url+'logout.php',
        {
                method: 'GET',
                onLoading: function(){
                        $('load').innerHTML = '<img src="'+host_url+'images/spinner.gif" /> Processing...';
                }
        }
        );
*/
// logout blog
/*	myAjax = new Ajax.Request( host_url +'wp/wp-login.php', 
	{
	asynchronous: false,
	method: 'POST', 
	parameters: {action: 'logout'}
	} );
*/
// logout forum
	myAjax = new Ajax.Request( host_url +'forum/logging.php',
	{
	asynchronous: false,
	method: 'POST',
	parameters: {action: 'logout', formhash: '180c9347'}
	} );

	location.replace(host_url+'logout.php');
}
function login(){
	login2dookaz($("user_name").value, $('passwd').value);
/*	login2wp($("user_name").value, $('passwd').value);
*/
	login2forum($("user_name").value, $('passwd').value);
}

function login2dookaz(user_name, passwd){
	var url = host_url +'login.php';
	var myAjax = new Ajax.Updater('load', url, 
	{
		method: 'POST', 
		parameters: {user_name: user_name, passwd: passwd},
		onLoading: function(){
			$('load').innerHTML = '<img src="'+host_url+'images/spinner.gif" /> Processing...';
		}
	}
	);
}

// login to blog
function login2wp(user_name, passwd){
	myAjax = new Ajax.Request( host_url +'wp/wp-login.php',
	{
	method: 'POST', 
	parameters: {log: user_name, pwd: passwd}
	} );
}

// login to forum
function login2forum(user_name, passwd){
	myAjax = new Ajax.Request( host_url +'forum/logging.php', 
	{
	method: 'POST', 
	parameters: {username: user_name, password: passwd, action: 'login', loginfield: 'username', loginsubmit: '\xe6\x9c\x83\xe5\x93\xa1\xe7\x99\xbb\xe9\x8c\x84'}
//	onComplete: showResponse
	} );
}

function showResponse(originalRequest){
	//	alert( originalRequest.responseText)
	$('top_sub_nav').innerHTML += originalRequest.responseText
}

/***********************************************
* Drop Down/ Overlapping Content- © Dynamic Drive (www.dynamicdrive.com)
* This notice must stay intact for legal use.
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

function getposOffset(overlay, offsettype){
var totaloffset=(offsettype=="left")? overlay.offsetLeft : overlay.offsetTop;
var parentEl=overlay.offsetParent;
while (parentEl!=null){
totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
parentEl=parentEl.offsetParent;
}
return totaloffset;
}

function overlay(curobj, subobjstr, opt_position){
	if (document.getElementById){
		var subobj=document.getElementById(subobjstr)
		subobj.style.display=(subobj.style.display!="block")? "block" : "none"

		var xpos=0
		var ypos=0
// start joe
		if(opt_position.indexOf("center")!=-1){
			xpos = (screen.availWidth - 600) / 2;
			ypos = (screen.availHeight - 300) / 2;
		}else{
			if(opt_position.indexOf("left")!=-1){
				xpos=(getposOffset(curobj, "left")+curobj.offsetWidth) - subobj.offsetWidth
			}
			if(opt_position.indexOf("right")!=-1){
				xpos=getposOffset(curobj, "left");
			}
			if(opt_position.indexOf("top")!=-1){
				ypos=getposOffset(curobj, "top") - subobj.offsetHeight;
			}
			if(opt_position.indexOf("bottom")!=-1){
				ypos=getposOffset(curobj, "top") + 30;
			}			
			//xpos=getposOffset(curobj, "left")+((typeof opt_position!="undefined" && opt_position.indexOf("right")!=-1)? -(subobj.offsetWidth-curobj.offsetWidth) : 0) 
			//ypos=getposOffset(curobj, "top")+((typeof opt_position!="undefined" && opt_position.indexOf("bottom")!=-1)? curobj.offsetHeight : 0)
		}
		subobj.style.left=xpos+"px"
		subobj.style.top=ypos+"px"
		// start joe
		return false
	}
	else
		return true
}

function overlayclose(subobj){
document.getElementById(subobj).style.display="none"
}
/***********************************************
* End Drop Down/ Overlapping Content
**********************************************/

function MM_jumpMenu(targ,selObj,restore){ //v3.0
//      if(selObj.selectedIndex != 0)
          eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function CheckAllProducts(form_obj) {
  for (var i = 0; i < form_obj.elements.length; i++) {
  	var obj = form_obj.elements[i]
    if(obj.type == 'checkbox' && obj.name == 'group_refs[]'){
	      form_obj.elements[i].checked = !(form_obj.elements[i].checked);
    }
  }
  if(form_obj.btn_select.value == 'Select All')
  	form_obj.btn_select.value = 'Unselect All'
  else
  	form_obj.btn_select.value = 'Select All'
}

/********************************************* START scroll content*/
var waktu=0;
function move_left(){
//alert('waktu='+waktu)
	scrolling_pane.scrollBy(-10,0);waktu=setTimeout('move_left()',60);
}
function move_right(){
	scrolling_pane.scrollBy(10,0);waktu=setTimeout('move_right()',60);
}


function selesai() {
    clearTimeout(waktu);
}
/********************************************** END scroll content*/

function goto_page(pn){
		document.browse_user_form.pn.value = pn
		document.browse_user_form.submit()
}
function resize_it(div_name, img){
        h = 500; 
		//alert(document.getElementById(div_name).offsetHeight);
	if(document.getElementById(div_name).offsetHeight < h){

			document.getElementById(img).height = h;
        }
 }

function popup(url,width,height, menubar)
{
	if(width > 0 && height > 0)
	{
		var centered;
		x = (screen.availWidth - width) / 2;
		y = (screen.availHeight - height) / 2;
		centered =',width=' + width + ',height=' + height + ',left=' + x + ',top=' + y + ',scrollbars=yes,resizable=yes,status=yes';
	}else
	{
		centered = 'scrollbars=yes,resizable=yes,status=yes';
	}
	if(menubar =='yes')
		centered += ',menubar=yes';
	var popup = window.open(url, '_blank', centered);
    	if (!popup.opener) popup.opener = self;
	popup.focus();
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

/** simple date checking */
function valid_date(d){
	var my_array=d.split("-");
	var year = my_array[0];
	var month = my_array[1]-1;
	var day = my_array[2];
	 dteDate=new Date(year,month,day);
//	alert(year+' '+ dteDate.getFullYear()+'\n'+month+' '+dteDate.getMonth()+'\n'+day+' '+dteDate.getDate())
	  return ((day==dteDate.getDate()) && (month==dteDate.getMonth()) && (year==dteDate.getFullYear()));
}

function checkbox_checker(formId, cName)
{
  for (i=0,n=formId.elements.length;i<n;i++)
  {
   if (formId.elements[i].className.indexOf(cName) !=-1)
   {
    if( formId.elements[i].checked == true ){
		return (true);
	}
   }
  }

    return (false);
}

function radio_button_checker(form_obj, inputStr)
{
var checkbox_choices = 0;
myarray = new Array();
myarray[0] = inputStr;

// alert(myarray.length);
for(var x = 0; x < myarray.length; x ++){
        var c =0;
//c= form_obj[myarray[x]].length; alert('l='+c); return;
        for (counter = 0; counter < form_obj[myarray[x]].length; counter++)
        {
                if (form_obj[myarray[x]][counter].checked){
                        c ++;
                }
        }

        if (c > 0)
                checkbox_choices = checkbox_choices + 1;
}

        if (checkbox_choices == myarray.length ){
                return (true);
        }else{
                return (false);
        }
}

function chk_pw(){
        var errors='';
        if(document.MM_returnValue){
                  pw1 = document.getElementById("pw").value;
                  pw2 = document.getElementById("pw2").value;
				  if(pw1.length < 6)
				  	errors += 'password is at least 6 characters.\n';
                  else if(pw1 != pw2) 
                  	errors += 'password does not match.\n';
 //                       errors += '密碼1='+pw1+' 和密碼2='+pw2+'\n';

                        if (errors) alert(errors);
                  document.MM_returnValue = (errors == '');
        }

}

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  var form_obj = args[0];
//  必須填寫
  for (i=1; i<(args.length-2); i+=3) { 
  	test=args[i+2]; val=MM_findObj(args[i]); field_name=args[i+1];

if (val) { nm=val.name; if ((val=val.value)!="") {
	//	alert('v='+val.value+ ' n='+nm)
	  if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+field_name+' is not correct.\n';
      }else if (test.indexOf('isCheckbox')!=-1) { 
		  if(!checkbox_checker(form_obj, "isCheckbox"))
		  	errors += '- '+field_name+' * is required.\n';
      }else if (test.indexOf('isRadio')!=-1) { 
		  if(!radio_button_checker(form_obj, args[i]))
		  	errors += '- '+field_name+' * is required.\n';
      }else if (test.indexOf('isDate')!=-1) { 
		  if(!valid_date(val))
		  	errors += '- '+field_name+' is not correct.\n';
	  }else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+ field_name +' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+ field_name +' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+field_name+' * is required.\n'; }
  } 
    if (errors) alert(errors);
  document.MM_returnValue = (errors == '');
}

function insertValueQuery(div_id){
	var oEditor = FCKeditorAPI.GetInstance('msg_editor') ;
    var myListBox = document.getElementById(div_id);
	var sel = myListBox.options[myListBox.selectedIndex].value
	var field = " {" + sel +"} ";
	oEditor.InsertHtml(field);
}

/*** for mouse over effect **/
function OMOver(OMO){OMO.style.backgroundColor='#e9f7db';}
function OMOut(OMO){OMO.style.backgroundColor='';}

/** for DIV expand or hidden **/
function getStyle(oElm, strCssRule){
    var strValue = "";
    if(document.defaultView && document.defaultView.getComputedStyle){
        strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
    }
    else if(oElm.currentStyle){
        strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
            return p1.toUpperCase();
        });
        strValue = oElm.currentStyle[strCssRule];
    }
    return strValue;
}
function expand(div_id){ 
	var obj = document.getElementById(div_id);
	//if(obj.currentStyle.display == "block"){  // close it

	if(getStyle(obj, "display") == "block"){
		obj.style.display="none"
	}else{
		obj.style.display="block"
	}
//alert()
}

//出处:网上搜集
//javascript去空格函数
function LTrim(str){ //去掉字符串 的头空格
var i;
for(i=0;i < str.length;i++){
	if(str.charAt(i)!=" "&&str.charAt(i)!=" ") break;
}
str = str.substring(i,str.length);
return str;
}
function RTrim(str){
var i;
for(i=str.length-1;i>=0;i--){
if(str.charAt(i)!=" "&&str.charAt(i)!=" ") break;
}
str = str.substring(0,i+1);
return str;
}
function Trim(str){

return LTrim(RTrim(str));

}

function regu_trim(stringToTrim) {
    return stringToTrim.replace(/^\s+|\s+$|\n/g,"");
}



