var loginoptions = {
	required: false,
	closingFn: function(){showSWF(); location='http://jcard.mymo.com.tw'; },
	successFn: function(){
		var str = "" + location;
		if(str.indexOf('logout_message.jsp') > -1)
		  location = '/index.jsp'; 
		else location.reload();
	},
	failedFn: function(msg){alert(msg);}

};

function setHeaderCss(tableid,list,directions){
  $headers = $('#' + tableid +' tr:first th'); 
  sortCss = ['headerSortDown','headerSortUp'];
  $headers.removeClass(sortCss[0]).removeClass(sortCss[1]);
  for(var i = 0; i <list.length;i++){
  	$headers.eq(list[i]).addClass(sortCss[directions[i]]);
  }

}
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;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function openForm(title,url,w,h,form){
	if(url.indexOf('?') > 0){
		url = url + '&';
	} else {
	    url = url + '?';	
	}  
	hideSWF();

	tb_closing_fn = showSWF;
	if(eval(form))
		tb_show(title, url+form.serialize()+'&KeepThis=true&TB_iframe=true&height='+ h + '&width=' + w, null);
	else
		tb_show(title, url+'KeepThis=true&TB_iframe=true&height='+ h + '&width=' + w, null);
}
function switchSortTab(src,target){
	$('#'+src).css('display','none');
	$('#'+target).css('display','block');
}
function login(id, pwd,vcode){

	$.get("/member/MemberAction.do", {mid: id, passwd:pwd,code:vcode, op:2}, loginDone);
	
}
function loginDone(data){
	switch($(data).find("rstdata").attr("msgid")){
		case "0":
			//login failed
			msg = $(data).find("rstdata").attr("msg");
			loginoptions.failedFn(msg);
			break;
		case "1":
		case "2":
		default:
	
	
			//login success
			setTimeout("loginoptions.successFn()",250);
			showSWF();
		
			break;
		
		
		
	}
}
function getNaturalHeight(img) {
	if( img.naturalHeight ) {
		return img.naturalHeight;
	} else {
		lgi = new Image();
		lgi.src = img.src;
		return lgi.height;
	}
}
function getNaturalWidth(img) {
	if( img.naturalWidth ) {
		return img.naturalWidth;
	} else {
		lgi = new Image();
		lgi.src = img.src;
		return lgi.width;
	}
}


function adhereimg(img, size){
	var oh = getNaturalHeight(img);
	var ow = getNaturalWidth(img);
	var oo = (oh>ow)?oh:ow;
	var ratio = size/oo;
	img.height=oh*ratio;
	img.width=ow*ratio;

}
function adhereimg(img, w,h){
	var oh = getNaturalHeight(img);
	var ow = getNaturalWidth(img);
//	var oo = (oh>ow)?oh:ow;
	var oo = (h>w)?ow:oh;
//	var size = (oh>ow)?h:w;
	var size = (h>w)?w:h;
	var ratio = size/oo;
	if(oh == ow){
		size = (h < w ? h : w);
		ratio = size/oo;
	}
	var _h = oh*ratio;
	var _w = ow*ratio;
	if(_h > h){
		oo = oh;
		size = h;
		ratio = size/oo;
	}
	if(_w > w){
		oo = ow;
		size = w;
		ratio = size/oo;
	}
	
	img.height=oh*ratio;
	img.width=ow*ratio;

}
function isemail(email){
	var reg = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
	return reg.test(email);
}
function isphone(mphone){
		var reg = /^[0-9]{10}$/;
		return reg.test(mphone);
}
//搜尋
function searchformsubmit(fm){
	if(fm.keyword.value == ''){
		alert('請輸入關鍵字');
		return;
	}
	switch(fm.qtype.value){
		case '1':
			fm.action='/goods/GoodsItemAction.do?op=9&len=20';
			fm.pname.value = fm.keyword.value;
			
			break;
		case '2':
			fm.action='/news/NewsAction.do?op=11';
			break;
		case '3':
			fm.action='/bulletin/BulletinAction.do?op=11';
			break;
			
	}
	fm.submit();
}


// hide all flash
function hideSWF(){
	$('object').hide();

}

// show all flash
function showSWF(){
	$('object').show();

}
// trim !!!!!!!
function trim(stringToTrim)
{
 return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function switchRSideSaleListTab(src,target){
	$(src).children().eq(0).attr('src','/images/' + src.substring(1) + 'a.gif');
	$(target).children().eq(0).attr('src','/images/' + target.substring(1) + 'b.gif');
	$(src).css('cursor','hand');
	$(target).css('cursor','default');
	$($(src).attr('region')).hide();
	$($(target).attr('region')).show('slow');
	
}