
function strlens(text){
	if(text == '') return 0;
    var len =0,i=0;
    text = text.replace(/^\s*/,"");
    text = text.replace(/\s*$/,"");
    for(var i=0;i< text.length;i++){
      len += (text.charCodeAt(i) > 255)?2:1;
    }
    return len;
}
 
function imageError(obj) {
   obj.src = "http://img.bid.pchome.com.tw/images/no-product.gif";
} 
  function keyDown(name)
{
        var keyCode=event.keyCode;
        if (keyCode == 13) { chk_form(name);}
}
function trim(untrimstr) {
  trimstr = untrimstr.replace(/^\s+|\s+$/g, "");
  return trimstr;
} 

function check_mail(obj){
  eMail = trim(obj.value);
  re = /^([\w\.%-]+)\@([\w%-]+\.[\w+\.%-]+)$/i;
  found=eMail.match(re);
  if(!found){
  	alert('聯絡email格式錯誤!');
  	return obj.focus();
  }
  return found[0];
}


function open_win(url, type) {
  var win_width, win_height, win_left, win_top;

  if (type == 0) {
    win_width = 480;
    win_height = 240;
    win_left = (screen.width - win_width) / 2;
    win_top = (screen.height - win_height - 75) / 2;
    if (navigator.appName == "Netscape")
      msg_window = window.open(url, 'msg_window', 'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,directories=0,resizable=yes,scrollbars=yes,width='+win_width+',height='+win_height+',screenX='+win_left+',screenY='+win_top);
    else
      msg_window = window.open(url, 'msg_window', 'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,directories=0,resizable=yes,scrollbars=yes,width='+win_width+',height='+win_height+',left='+win_left+',top='+win_top);
  }
  
  if(type==1){
    win_width = 500;
    win_height = 375;
    win_left = (screen.width - win_width) / 2;
    win_top = (screen.height - win_height - 75) / 2;
    if (navigator.appName == "Netscape")
      msg_window = window.open(url, 'msg_window', 'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,directories=0,resizable=yes,scrollbars=yes,width='+win_width+',height='+win_height+',screenX='+win_left+',screenY='+win_top);
    else
      msg_window = window.open(url, 'msg_window', 'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,directories=0,resizable=yes,scrollbars=yes,width='+win_width+',height='+win_height+',left='+win_left+',top='+win_top);
  
    //msg_window = window.open(url, 'msg_window');
  }
  window.msg_window.focus();
}


//秀出年月日的tag
function select_date(name,selected){
     today = new Date();
     end_year = today.getYear()+5;
     y=selected.substring(0,4);
     m=selected.substring(4,6);
     d=selected.substring(6,8);
     sub ="<select size='1' name='year_"+name+"'>";
      for(i=2004;i<=end_year;i++){
       is_select=(i==y)?"selected":""; 
       sub+="  <option value='"+ i +"' "+is_select+">"+i+"年</option>";
      }
     sub +="</select>";
     sub +="<select size='1' name='month_"+name+"'>";
      for(i=1;i<=12;i++){
       is_select=(i==m)?"selected":""; 
       j=new String(i);
       if(j.length==1){
         j="0"+j;	
       }
       sub+="  <option value='"+ j +"' "+is_select+">"+j+"月</option>";
      }
     sub +="</select>";
     sub +="<select size='1' name='day_"+name+"'>";
      for(i=1;i<=31;i++){
       is_select=(i==d)?"selected":""; 
       j=new String(i);
       if(j.length==1){
         j="0"+j;	
       }
       sub+="  <option value='"+j+"' "+is_select+">"+j+"日</option>";
      }
     sub+="</select>";
    return sub;
}


function resubmit(gourl,theform){
	if(typeof(theform)=='undefined'){
		theform=document.main;
	}
	if(gourl){
		theform.action = gourl;
		theform.submit();
	}
}

function submit_search(i,j,gourl,theform){
	var a,b,c,d,e,f,g,h,i1,j1,k,l,val,val2,len,url_str;
	if(typeof(theform)=='undefined'){
		theform=document.main;
	}
	if(i != 5){
		a = eval("theform.ser_word_"+i);
	}else if(i == 5){
		a = eval("theform.ser_word_2");
	}
	if(a.value.match(/&/)){
		re = /&/g;
		val2 = a.value.replace(re, "<#@#>");
	}else{
		val2 = a.value;
	}
	val = URLEncode(val2);
	theform.ser_word.value = val;
	
	if(j > 0) b = eval("theform.ser_class_"+i);
	
	len = strlens(a.value);
	if( len > 255){
		alert("關鍵字請勿輸入超過255個字 !!!");
		a.focus();
		return false;
	}
	
	if(j > 0) theform.ser_class.value = b.value;
	if(!theform.ser_class.value) theform.ser_class.value = '';
	c = theform.ser_type.value;
	d = theform.ser_class.value;
	
	
	if(i == 2){
		if(theform.ser_pic.checked) e = theform.ser_pic.value;
		else e = 0;
		if(theform.ser_day.checked) f = theform.ser_day.value;
		else f = 0;
		if(theform.ser_direct.checked) g = theform.ser_direct.value;
		else g = 0;
		if(theform.ser_base.checked) h = theform.ser_base.value;
		else h = 0;
		if(theform.ser_now.checked) i1 = theform.ser_now.value;
		else i1 = 0;
		if(theform.ser_now1.value == '(最低)') theform.ser_now1.value = '';
		if(theform.ser_now1.value) j1 = theform.ser_now1.value;
		else j1 = 0;
		if(theform.ser_now2.value == '(最高)') theform.ser_now2.value = '';
		if(theform.ser_now2.value) k = theform.ser_now2.value;
		else k = 0;
		//if(theform.ser_end.value) l = theform.ser_end.value;
		//else l = 0;
		l = 0;
	}else{
		e = f = g =h = k = i1 = j1 = k = l = 0;
	}
	if(i == 2 || i == 3 || i == 5){
		m = theform.m.value;
		o = theform.o.value;
		if( i == 5 && theform.ser_word.value == ""){ theform.ser_word.value = "all_alert"; }
	}else{
		m = o = '';
	}
	url_str = "http://class.ruten.com.tw/search/search_ok.htm?"+c+"&"+val+"&"+d+"&"+e+"&"+f+"&"+g+"&"+h+"&"+i1+"&"+j1+"&"+k+"&"+l+"&"+m+"&"+o;
	//Window.location.href = url_str;
	resubmit(gourl,theform);
}

function URLEncode(plaintext)
{
 var SAFECHARS = "0123456789" +     // Numeric
     "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + // Alphabetic
     "abcdefghijklmnopqrstuvwxyz" +
     "-_.!~*'()";     // RFC2396 Mark characters
 var HEX = "0123456789ABCDEF";
 var encoded = "";
 for (var i = 0; i < plaintext.length; i++ ) {
  var ch = plaintext.charAt(i);
  if (ch == " ") {
    encoded += "+";    // x-www-urlencoded, rather than %20
  } else if (SAFECHARS.indexOf(ch) != -1) {
    encoded += ch;
  } else {
    var charCode = ch.charCodeAt(0);
    //alert(charCode);
  	if (charCode > 255) {
			encoded += ch;
   	} else {
    encoded += "%";
    encoded += HEX.charAt((charCode >> 4) & 0xF);
    encoded += HEX.charAt(charCode & 0xF);
   }
  }
 }
 return encoded;
}

function check_uid(uid){
  uid = uid.toUpperCase();
  if(!uid.match(/^[A-Z]\d{9}$/)) return alert("身份證格式不正確！");
  if(uid == "A123456789") return alert("請輸入正確身份證字號！");
  var eng2num = new Array(1,10,19,28,37,46,55,64,39,73,82,2,11,20,48,29,38,47,56,65,74,83,21,3,12,30);
  var uidsum = eng2num[uid.charCodeAt(0) - 65];
  for(var i=1;i<10;i++) uidsum += parseInt(uid.charAt(i))*((9-i)?(9-i):1);
  if(uidsum%10) return alert("您的身份證字號有誤,請查看一下!!\n");
  return uid;
}

function check_row(){
	var m_len = main.elements.length;
	for(i=0;i<m_len;i++){
		err = 0;
	  if(main.elements[i].isContentEditable){
	    if(main.elements[i].title) m_title = main.elements[i].title;
	    tmp_value =  main.elements[i].value.replace(/^\s*/,"");
	    if(!tmp_value.replace(/\s*$/,"")){ alert(" " + ((m_title)?m_title:"") + "資料填寫內容不可空白！"); err=1;}
	    if(main.elements[i].value.match(/('|"|\$|\%|\&|\\|\|)/i)){ alert(" " + ((m_title)?m_title:"") + "資料填寫內容不可有\n『 \'、 \"、\$、\%、\&、\\ 或 \|』符號"); err=1;}
	    if(err) return main.elements[i];
	  }
	}
}



