/*
JavaScript Use for Fuman House
Author:			BN.Cho
Date Created:	2008/3/23	
Last Update:	2008/3/23 by BN
Contact:		programmer@ms95.url.com.tw
Copyright 2008 (c) programmer@ms95.url.com.tw All Rights Reserved.
*/

	function startsWith(xValue, xStr){
		if(xValue.length >= xStr.length && xStr.length > 0){
			if(xValue.substring(0, xStr.length) == xStr){
				return true;
			}
		}
		return false;
	}

	function parsePhone(xField){
		var parea = document.getElementById(xField + "_area");
		var pnum = document.getElementById(xField + "_number");
		if(startsWith(parea.value, "09")){
			if(parea.value.length == 4){
				pnum.focus();
			}
		}
		else if(startsWith(parea.value, "02") || startsWith(parea.value, "05") || startsWith(parea.value, "06") || startsWith(parea.value, "07")){
			pnum.focus();
		}
		else if(startsWith(parea.value, "03")){
			if(startsWith(parea.value, "037")){
				pnum.focus();
			}
			else if(parea.value.length > 2){
				pnum.focus();
				pnum.value = parea.value.substring(2, 3);
				parea.value = "03";
			}
		}
		else if(startsWith(parea.value, "04")){
			if(startsWith(parea.value, "049")){
				pnum.focus();
			}
			else if(parea.value.length > 2){
				pnum.focus();
				pnum.value = parea.value.substring(2, 3);
				parea.value = "04";
			}
		}
		else if(startsWith(parea.value, "08")){
			if(startsWith(parea.value, "089")){
				pnum.focus();
			}
			else if(startsWith(parea.value, "082")){
				if(startsWith(parea.value, "0826")){
					pnum.focus();
				}
				else if(parea.value.length > 3){
					pnum.focus();
					pnum.value = parea.value.substring(3, parea.value.length);
					parea.value = "082";
				}
			}
			else if(startsWith(parea.value, "083")){
				if(startsWith(parea.value, "0836")){
					pnum.focus();
				}
				else if(parea.value.length > 3){
					pnum.focus();
					pnum.value = parea.value.substring(3, parea.value.length);
					parea.value = "083";
				}
			}
			else if(parea.value.length > 2){
				pnum.focus();
				pnum.value = parea.value.substring(2, parea.value.length);
				parea.value = "08";
			}
		/*
			startsWith(parea.value, "0826") || startsWith(parea.value, "0836")
			startsWith(parea.value, "082") || startsWith(parea.value, "089")
			pnum.focus();
			*/
		}
		document.getElementById(xField).value = parea.value + pnum.value;
	}

function setCheck(xField, xList){
	if(xField.length){
		for(var i=0; i<xField.length; i++){
			if(xList.indexOf("," + xField[i].value + ",") > -1){
				xField[i].checked = true;
			}
		}
	}
	else{
		if(xList.indexOf("," + xField.value + ",") > -1){
			xField.checked = true;
		}
	}
}

function checkAll(xField, xBox){
	if(xField.length){
		for(var i=0; i<xField.length; i++){
			xField[i].checked = xBox.checked;
		}
	}
}

function getList(xField){
	var tstr = "";
	if(xField.length){
		for(var i=0; i<xField.length; i++){
			if(xField[i].checked && xField[i].value){
				tstr += xField[i].value + ",";
			}
		}
		if(tstr.length > 0){
			tstr = tstr.substring(0, tstr.length - 1);
		}
	}
	else{
		if(xField.checked && xField.value){
			tstr = xField.value;
		}
	}
	return tstr;
}


function Upload(xField){
	var file = window.showModalDialog("../.jsp/upload.jsp", "", "");
	if(file){
		xField.value = file;
	}
}

function uploadImage2(xField, xImage){
	var file = window.showModalDialog("../.jsp/upload.jsp", "", "");
	if(file){
		document[xImage].src = file;
		xField.value = file;
	}
}

function removeCombo(x){
	if (x.selectedIndex > -1){
		for (var i=x.selectedIndex; i<x.length -1 ; i++ ){
			x.options[i].text =x.options[i + 1].text;
			x.options[i].value = x.options[i + 1].value;
		}//for
		x.options.length --;
	}//if
	else{
		alert("尚未選取!!");
	}//else
}//removfeCombo

function selectUser(x){
	var User = window.showModalDialog("/.jsp/selectuser.jsp?maxuser=1", "", "status=no");
	if ((User) && (x)){
		x.value = User[0].UID;
	}//if

}//selectUser

function selectDate(xField){
    var date = window.showModalDialog("/.jsp/selectdate.jsp?mode=0", "", "status=no");
	if (date){
		xField.value = ((date.Year) ? date.Year : "2005") + ((date.Month) ? date.Month : "01") + ((date.Day) ? date.Day : "01");
	}//if
}//selectDate

function selectWeek(xField){
	var date = window.showModalDialog("/.jsp/selectdate.jsp?mode=1", "", "status=no");
	if (date){
		xField.value = date.Week;
	}//if
}//selectWeek

function selectCustomer(xField){
	var Cust = window.showModalDialog("/.jsp/selectcustomer.jsp?max=1", "", "status=no");
	if (Cust){
		xField.value = Cust[0].ID;
	}//if
}//addCustomer

function selectMCustomer(xField){
	var Cust = window.showModalDialog("/.jsp/selectcustomer.jsp", "", "status=no");
	if (Cust){
		xField.value = "";
		for (var i=0;i<Cust.length ;i++ ){
			xField.value += Cust[0].ID;
			if (i < Cust.length -1){
				xField.value += ",";
			}
		}//for
	}//if
}//addCustomer

function jumpPage(){
	iForm.page.value = pForm.page.value;
	iForm.submit();
}//jumpPage

function prevPage(){
	if (pForm.page.options.selectedIndex > 0){
		pForm.page.options.selectedIndex --;
		jumpPage();
	}//if
	else{
		alert("Already in First Page!!");
	}//else
}//prevPage

function nextPage(){
	if (pForm.page.options.selectedIndex < pForm.page.options.length - 1){
		pForm.page.options.selectedIndex ++;
		jumpPage();
	}//if
	else{
		alert("Already in Last Page!!");
	}//else
}//nextPage

function firstPage(){
	if (pForm.page.options.selectedIndex > 0){
		pForm.page.options.selectedIndex = 0;
		jumpPage();
	}//if
	else{
		alert("Already in First Page!!");
	}//else
}//nextPage

function lastPage(){
	if (pForm.page.options.selectedIndex < pForm.page.options.length - 1){
		pForm.page.options.selectedIndex = pForm.page.options.length - 1;
		jumpPage();
	}//if
	else{
		alert("Already in Last Page!!");
	}//else
}//nextPage
