// JavaScript Document

//確認表單
function checkForm(f){
	
	var c = /[^\s^\n]/;
	var m = /^([a-zA-Z0-9])([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9])+$/;
	
		for(i=0;i<f.length;i++){
			var fg = f[i].getAttribute("chk");
			if((fg == "theText" || fg == "theMail" || fg == "theNum" || fg == "theUrl") && f[i].value.match(c) == null){
				alert(f[i].getAttribute("err"));
				f[i].focus();
				return false;			
			 }else if(fg == "theMail" && f[i].value.match(m) == null){
				 alert("Email格式錯誤");
				 f[i].focus();
				 return false;				 
			 }else if(fg == "theNum" && f[i].value != parseInt(Math.abs(f[i].value))){
				alert("數字格式錯誤");
				f[i].focus();
				return false;
			 }else if(fg == "theUrl" && f[i].value.substr(0,7) != "http://"){
				 alert("連結格式錯誤");
				 f[i].focus();
				 return false;
			 }
			
		}
		
		return confirm("填寫內容確定無誤嗎?");

}

//開啟置中視窗
function centerWindow(theURL,width,height) {
    var window_width = width;
    var window_height = height;
    var window_top = (screen.height-window_height)/2;
    var window_left = (screen.width-window_width)/2;
    newWindow=window.open(''+ theURL + '','','width=' + window_width + ',height=' + window_height + ',top=' + window_top + ',left=' + window_left + ',scrollbars=yes');
    newWindow.focus();
}


//切換產品排列方式
function chOrder(o,s){
	if(s != undefined){
		window.location="?theOrder="+o+"&p_sub="+s;
	}else{
		window.location="?theOrder="+o;
	}
}

//切換保險公司排列方式
function chOrder2(o,id){
		window.location="?theOrder="+o+"&sc_id="+id;
}

//切換強力搜尋排列方式
function chOrder3(o,ps){
		var l = ps.indexOf("theOrder=")+9;
		str = ps.substr(0,l)+o;
		window.location= "psearch2.php?"+str;
}

//切換一般搜尋排列方式
function chOrderSearch(o,keyword){
	window.location = "?theOrder=" + o + "&keyword=" + keyword;
}

//定義等級
function setLevel(str){
	var ls = document.getElementById("levelSelect");
	var levelItem = ["第一類內勤行政","第二類外勤業務","第三類技術操作","第四類製造建築","第五類","第六類"];

	arr = str.split(",");
	ls.length = arr.length + 1;
	
		if(str=="")	ls.options[0].text = "無"; else	ls.options[0].text = "請選擇類別";
	
	if(str!=""){
		for(i=0;i<arr.length;i++){
			ls.options[i+1].text = levelItem[arr[i]-1];
			ls.options[i+1].value = "career"+arr[i]+".php";
		}
	}
	
}

//檢驗搜尋
function checkSearch(f){
	var c = /[^\s^\n]/;
	if(f.keyword.value.match(c) == null){
		alert("忘記填寫內容");
		f.keyword.focus();
		return false;
	}
}

//檢察一元起標(產品型)
function checkBid(){
	if(!ck("bid_subject","主題")){
		return false;
	}
	if(!ck("bid_condition","紅利資格")){
		return false;
	}
	if(!ck("bid_startDate","開始日期")){
		return false;
	}
	if(!ck("bid_endDate","結束日期")){
		return false;
	}
	if(dg("p_id").value == ""){
		alert("忘記選擇商品");
		return false;
	}
	if(new Date(dg("bid_startDate").value) < new Date()){
		alert("起始日期不能晚於今天");
		return false;
	}
	if(new Date(dg("bid_endDate").value) < new Date(dg("bid_startDate").value)){
		alert("結束日期不能小於起始日期");
		return false;
	}
	return true;
}

//檢察一元起標(紅利型)
function checkBid2(){
	if(!ck("bid_subject","主題")){
		return false;
	}
	if(!ck("bid_bonus","競標紅利")){
		return false;
	}
	if(!ck("bid_condition","紅利資格")){
		return false;
	}
	if(!ck("bid_startDate","開始日期")){
		return false;
	}
	if(!ck("bid_endDate","結束日期")){
		return false;
	}
	if(new Date(dg("bid_startDate").value) < new Date()){
		alert("起始日期不能晚於今天");
		return false;
	}
	if(new Date(dg("bid_endDate").value) < new Date(dg("bid_startDate").value)){
		alert("結束日期不能小於起始日期");
		return false;
	}
	return true;
}


//檢查空白值
function ck(id,err){
	var c = /[^\s^\n]/;
	if(document.getElementById(id).value.match(c)==null){
		alert("忘記填寫"+err);
		document.getElementById(id).focus();
		return false;
	}
	return true;
}

//簡略document.getElement
function dg(id){
	return document.getElementById(id);
	}
	
//預設排序欄位
function orderDefault(){
	var cr = 0;
	var r = document.getElementsByName("radiobutton");
	for(i=0;i<r.length;i++){
		if(r[i].checked){
			cr = 1;	
		}
	}
	if(cr==0){
		r[2].checked = true;	
	}
}

//切換保險公司
function chCompany(c){
	top.window.location = "company_pd.php?sc_id="+c.options[c.selectedIndex].value;
}

//check bid submit
function bidForm(theBonus){
	
	if(theBonus == undefined){
		theBonus = 0;	
	}
	
	if(!ck("br_bonus","紅利")){ return false; }
	
	if(dg("br_bonus").value != parseInt(Math.abs(dg("br_bonus").value))){
		alert("紅利必須為正確數字");
		return false;
	}
	if(dg("br_bonus").value < 2){
		alert("出價點數至少必須為2元");
		return false;
	}	
	
	if(dg("br_bonus").value < theBonus){
		alert("出價點數必須大於目前出價點數");
		return false;
	}


	
	return confirm("確定出價數目嗎?");
}

//change order status
function chOrderStatus(obj,id){ 
	var myStatus = obj.parentNode.parentNode.cells[12].firstChild.value;
	centerWindow("order_function.php?f=updStatus&o_id="+id+"&status="+myStatus,200,50);
}
function NchOrderStatus(obj,id){
	var myStatus = obj.parentNode.parentNode.cells[10].firstChild.value;
	centerWindow("n_order_function.php?f=updStatus&idx="+id+"&status="+myStatus,200,50);
}
function SchOrderStatus(obj,id){
	var myStatus = obj.parentNode.parentNode.cells[9].firstChild.value;
	centerWindow("seller_function.php?f=updStatus&idx="+id+"&status="+myStatus,200,50);
}