var lastSelectRow = null ;
var lastSelectRecordNo = null ;

function Search(buttonElement){
 var formElement ;
 var elements ;
 
	PrepareForm(buttonElement);
 	
 	formElement = buttonElement ;

 	while( formElement.tagName != "FORM" ){
 		formElement = formElement.parentElement ;
 	}
 	
  	for( i = 0 ; i < formElement.elements.length ; i++){
 		if( formElement.elements[i].name == "__pageno" ||
 			formElement.elements[i].name == "__pageNo" 
 		){
 			formElement.elements[i].value = 1 ;
 		}
 	}

 	formElement.submit();
}//end function Search(buttonElement){


function SaveRecord(buttonElement,actionType){

	if( actionType == "view" || actionType == "update")
		UpdateRecord(buttonElement);
	else		
		InsertRecord(buttonElement);
}//end function SaveRecord(buttonElement,actionType){


function InsertRecord(buttonElement){
 var formElement = null ;
 var formElementNames = new Array();
 var lastElementName ;
 var fileName ;
 var i ;

 	formElement= buttonElement ;
 	while( formElement.tagName != "FORM" ){
 		formElement = formElement.parentElement ;
 	}

	formElement.elements("__actionType").value = "insert" ;
	
 	fieldNamesElement = formElement.elements("__field_names");
 	if( fieldNamesElement == null ){
 		formElement.insertAdjacentHTML(
 		"afterBegin" ,
 		"<input type=HIDDEN style='width:300px' name=__field_names   >") ;
	 	fieldNamesElement = formElement.elements("__field_names");
 	}
 	
 	for( i = 0 ; i < formElement.elements.length ; i++){
 		if( formElement.elements(i).name.indexOf("__") != 0 &&
 			formElement.elements(i).name != ""
 		){
 			if( lastElementName != formElement.elements(i).name ){
			 	formElementNames = formElementNames.concat(formElement.elements(i).name);
			 	//formElementNames.push(formElement.elements(i).name);
 				if( formElement.elements(i).can_null == "false" && formElement.elements(i).value == ""){
 					formElement.elements(i).style.backgroundColor = "#ffcccc" ;
 					formElement.elements(i).focus();
 					//alert("欄位不能為 空值");
 					alert("請輸入資料!");
 					formElement.elements(i).style.backgroundColor = "" ;
 					return ;
 				}
 				if( formElement.elements(i).name == "email"){
 					if( !CheckEmail(formElement.elements(i).value)){
	 					formElement.elements(i).style.backgroundColor = "#ffcccc" ;
	 					formElement.elements(i).focus();
	 					alert("電子郵件格式不符!");
	 					formElement.elements(i).style.backgroundColor = "" ;
	 					return ;
 					}
				}
   				if( formElement.elements(i).data_type == "number" && 
 					(isNaN(formElement.elements(i).value) == true)){
 					formElement.elements(i).style.backgroundColor = "#ffcccc" ;
 					formElement.elements(i).focus();
 					//alert("欄位必須是 數字");
 					alert("此欄位必須是數字!");
 					formElement.elements(i).style.backgroundColor = "" ;
 					return ;
 				};
 			};//end if( lastElementName != formElement.elements(i).name ){
		 	lastElementName = formElement.elements(i).name ;
		}
 	}
	
 	fieldNamesElement = formElement.elements("__field_names");

 	fieldNamesElement = formElement.elements("__field_names");
	fieldNamesElement.value = formElementNames.join(","); 

	formElement.submit();
}//end function InsertRecord(buttonElement){


function ValidateRecord(buttonElement){
 var formElement ;
 
 	formElement= buttonElement ;
 	while( formElement.tagName != "FORM" ){
 		formElement = formElement.parentElement ;
 	}

}//end function ValidateRecord(buttonElement){

function UpdateRecord(buttonElement){
 var formElement = null ;
 var fileName ;
 var formElementNames = new Array();
 var lastElementName ;
 var i ;

 	formElement= buttonElement ;
 	while( formElement.tagName != "FORM" ){
 		formElement = formElement.parentElement ;
 	}

	
	formElement.elements("__actionType").value = "update" ;
	
 	fieldNamesElement = formElement.elements("__field_names");
 	if( fieldNamesElement == null ){
 		formElement.insertAdjacentHTML(
 		"afterBegin" ,
 		"<input type=Hidden style='width:300px' name=__field_names   >") ;
	 	fieldNamesElement = formElement.elements("__field_names");
 	}
 	
 	for( i = 0 ; i < formElement.elements.length ; i++){
 		if( formElement.elements(i).name.indexOf("__") != 0 &&
 			formElement.elements(i).name != ""
 		){
 			if( lastElementName != formElement.elements(i).name ){
 				if( formElement.elements(i).can_null == "false" && formElement.elements(i).value == ""){
 					formElement.elements(i).style.backgroundColor = "#ffcccc" ;
 					formElement.elements(i).focus();
 					//alert("欄位不能為 空值");
 					alert("請輸入資料!");
 					formElement.elements(i).style.backgroundColor = "" ;
 					return ;
 				};
 				if( formElement.elements(i).name == "email"){
 					if( !CheckEmail(formElement.elements(i).value)){
	 					formElement.elements(i).style.backgroundColor = "#ffcccc" ;
	 					formElement.elements(i).focus();
	 					alert("電子郵件格式不符!");
	 					formElement.elements(i).style.backgroundColor = "" ;
	 					return ;
 					}
				}
 				if( formElement.elements(i).data_type == "number" && 
 					(isNaN(formElement.elements(i).value) == true)){
 					formElement.elements(i).style.backgroundColor = "#ffcccc" ;
 					formElement.elements(i).focus();
 					//alert("欄位必須是 數字");
 					alert("此欄位必須是數字!");
 					formElement.elements(i).style.backgroundColor = "" ;
 					return ;
 				};
 				
			 	formElementNames = formElementNames.concat(formElement.elements(i).name);
			 	//formElementNames.push(formElement.elements(i).name);
			 }
		 	lastElementName = formElement.elements(i).name ;
		}
 	}

 	fieldNamesElement = formElement.elements("__field_names");

 	fieldNamesElement = formElement.elements("__field_names");
	fieldNamesElement.value = formElementNames.join(","); 

	formElement.submit();	
}//end UpdateRecord(buttonElement){


function SelectRecord(buttonElement){
 var trElement ;
 var tdElement ;
 
 	  	
	if( lastSelectRow != null ){
 		for( i = 0 ; i < lastSelectRow.cells.length ;i++ ){
 			lastSelectRow.cells[i].style.backgroundColor = ""
 		}
 	}

  	trElement= buttonElement ;
 	while( trElement.tagName != "TR" ){
 		trElement = trElement.parentElement ;
 	}
 
 	for( i = 0 ; i < trElement.cells.length ;i++ ){
 		trElement.cells[i].style.backgroundColor = "#A0A0FF"
 		
 		if( trElement.cells[i].name == "reocrdNo" ){
	 		lastSelectRecordNo = trElement.cells[i].innerText ;
	 	}
 	}
 	lastSelectRow = trElement ;
}//function SelectRecord(buttonElement){

function SortList(buttonElement,fieldName){
	PrepareForm(buttonElement);	
 	
 	formElement= buttonElement ;
 	while( formElement.tagName != "FORM" ){
 		formElement = formElement.parentElement ;
 	}
 	
 	
 	orderByElement = formElement.elements("__orderby");
 	sortOrderElement = formElement.elements("__sortorder");

	orderByElement.value = fieldName ;
	
	if( sortOrderElement.value == "" ){
		sortOrderElement.value = "asc" ;
	}else if( sortOrderElement.value == "asc" ){
		sortOrderElement.value = "desc" ;
	}else if( sortOrderElement.value == "desc" ){
		sortOrderElement.value = "asc" ;
	};
	
	formElement.submit();
}//end function SortList(buttonElement,fieldName){


function DeleteRecord(buttonElement,deleteRecordNo){
 var formElement = null ;
 var deleteRecordNoElement ;
 var i ;

	PrepareForm(buttonElement);

 	formElement= buttonElement ;
 	while( formElement.tagName != "FORM" ){
 		formElement = formElement.parentElement ;
 	}
	
	
 	fieldNamesElement = formElement.elements("__delete_recordno");
 	if( fieldNamesElement == null ){
 		formElement.insertAdjacentHTML(
 		"afterBegin" ,
 		"<input type=Hidden style='width:300px' name=__delete_recordno  >") ;
	 	fieldNamesElement = formElement.elements("__delete_recordno");
 	}
 
 	deleteRecordNoElement = formElement.elements("__delete_recordno");

	deleteRecordNoElement.value = deleteRecordNo 
	
	formElement.submit();
}//end function Delete(buttonElement){


function UpdateUpdateRecord(buttonElement){
}//end function Update(buttonElement){


function ViewRecord(element,url,recordNo){
	formElement= element ;
 	while( formElement.tagName != "FORM" ){
 		formElement = formElement.parentElement ;
 	}
	
	window.parent.lastRecordNo = recordNo ;

	
	open(url + "?__actionType=view&recordNo="+recordNo, 
		 "_blank","height=460,width=830,status=0,titlebar=0,toolbar=0,scrollbars=1,resizable=1");
}//end function EditPage(url,recordNo){


function ViewWebDetail(element,url,recordNo){
	formElement= element ;
 	while( formElement.tagName != "FORM" ){
 		if( formElement.tagName == "BODY" ){
 			alert("此畫面不包含 FORM 元件");
 			return ;
 		}
 		formElement = formElement.parentElement ;
 	}
	
	window.parent.lastRecordNo = recordNo ;

	open(url + "?__actionType=view&recordNo="+recordNo, 
		 "_self" );
}//end ViewWebDetail(element,url,recordNo){


function SelectLastRecord(tableElement){
 var lastRecordNo ;
 var i ;

 	lastRecordNo = window.parent.lastRecordNo ;
 	if( lastRecordNo != null ){
	 	for( i = 0 ; i < tableElement.rows.length ; i++ ){
	 		if( tableElement.rows[i].recordno == lastRecordNo ){
	 			SelectRecord(tableElement.rows[i]);
	 			break ;
	 		}
	 	}
	}

 	window.parent.lastRecordNo = null ;
}//end  function SelectLastRecord(tableElement){

function ViewNewRecord(url){
 var conditionQueryString ;
 var conditionFieldNames = new Array();
 var lastElementName ;
 
 	formElement= event.srcElement ;
 	while( formElement.tagName != "FORM" ){
 		formElement = formElement.parentElement ;
 	}

	conditionQueryString = "" ;

 	for( i = 0 ; i < formElement.elements.length ; i++){
 		if( formElement.elements(i).name.indexOf("__condition") == 0 &&
	 		formElement.elements(i).name != "__condition" &&
 			formElement.elements(i).name != ""
 		){
 			if( lastElementName != formElement.elements(i).name ){
			 	conditionFieldNames = conditionFieldNames.concat(formElement.elements(i).name);
	 			//conditionFieldNames.push(formElement.elements(i).name);
	 			if( formElement.elements(i).type != "radio" ||
	 				formElement.elements(i).checked == true 
	 			){
		 			fieldValue = formElement.elements(i).value ;
		 			conditionQueryString += 
			 				formElement.elements(i).name + "=" + fieldValue + "&"
			 	};
			};
			lastElementName = formElement.elements(i).name ;
		};
 	};

	open(url + "?__actionType=create&" + conditionQueryString ,
		 "_blank","height=460,width=830,status=0,titlebar=0,toolbar=0,scrollbars=1,resizable=1");
}



function OpenFirstPage(buttonElement){

	PrepareForm(buttonElement);

 	formElement = buttonElement ;

 	while( formElement.tagName != "FORM" ){
 		formElement = formElement.parentElement ;
 	}
 
 	pageNoElement = formElement.elements("__pageno");
 	
	pageNoElement.value = 1;
	
	formElement.submit();
}

function ChangePageNo(selectElement){
	PrepareForm(selectElement);
 	formElement = selectElement ;

 	while( formElement.tagName != "FORM" ){
 		formElement = formElement.parentElement ;
 	}
 
 	pageNoElement = formElement.elements("__pageno");
 	
	pageNoElement.value = selectElement.value ;
	
	formElement.submit();
}


function OpenPreviousPage(buttonElement){
	PrepareForm(buttonElement);

 	formElement = buttonElement ;

 	while( formElement.tagName != "FORM" ){
 		formElement = formElement.parentElement ;
 	}
 
 	pageNoElement = formElement.elements("__pageno");
 	
 	currPageNo = (new Number(pageNoElement.value) + 0 );
	pageNoElement.value = currPageNo-1 ;
	
	formElement.submit();
}

function OpenNextPage(buttonElement){
	PrepareForm(buttonElement);

 	formElement = buttonElement ;

 	while( formElement.tagName != "FORM" ){
 		formElement = formElement.parentElement ;
 	}
 
 	pageNoElement = formElement.elements("__pageno");
 	currPageNo = (new Number(pageNoElement.value) + 0 );
	pageNoElement.value = currPageNo+1 ;
	
	formElement.submit();
}


function OpenLastPage(buttonElement){
	PrepareForm(buttonElement);

 	formElement = buttonElement ;

 	while( formElement.tagName != "FORM" ){
 		formElement = formElement.parentElement ;
 	}
 
 	pageNoElement = formElement.elements("__pageno");
 	pageCountElement = formElement.elements("__pagecount");
 	pageCount = (new Number(pageCountElement.value) + 0 );

	pageNoElement.value = pageCount ;
	
	formElement.submit();
}



function SubmitForm(submitElement){
 var formElement = null ;
 var formElementNames = new Array();
 var lastElementName ;
 var i ;

 	formElement= submitElement ;
 	while( formElement.tagName != "FORM" ){
 		formElement = formElement.parentElement ;
 	}

 	fieldNamesElement = formElement.elements("__field_names");
 	if( fieldNamesElement == null ){
 		formElement.insertAdjacentHTML(
 		"afterBegin" ,
 		"<input type=hidden name=__field_names   >") ;
	 	fieldNamesElement = formElement.elements("__field_names");
 	}
 	
 	for( i = 0 ; i < formElement.elements.length ; i++){
 		if( formElement.elements(i).name.indexOf("__") != 0 &&
 			formElement.elements(i).name != ""
 		){
			formElementNames = formElementNames.concat(formElement.elements(i).name);
		 	//formElementNames.push(formElement.elements(i).name);
		}
 	}

	fieldNamesElement.value = formElementNames.join(","); 

 	for( i = 0 ; i < formElement.elements.length ; i++){
 		if( formElement.elements(i).name.indexOf("__") != 0 &&
 			formElement.elements(i).name != ""
 		){
 			if( lastElementName != formElement.elements(i).name ){
			 	formElementNames = formElementNames.concat(formElement.elements(i).name);
			 	//formElementNames.push(formElement.elements(i).name);
 				if( formElement.elements(i).can_null == "false" && formElement.elements(i).value == ""){
 					formElement.elements(i).style.backgroundColor = "#ffcccc" ;
 					formElement.elements(i).focus();
 					//alert("欄位不能為 空值");
 					alert("請輸入資料!");
 					formElement.elements(i).style.backgroundColor = "" ;
 					return ;
 				}
 				if( formElement.elements(i).name == "email"){
 					if( !CheckEmail(formElement.elements(i).value)){
	 					formElement.elements(i).style.backgroundColor = "#ffcccc" ;
	 					formElement.elements(i).focus();
	 					alert("電子郵件格式不符!");
	 					formElement.elements(i).style.backgroundColor = "" ;
	 					return ;
 					}
				}
   				if( formElement.elements(i).data_type == "number" && 
 					(isNaN(formElement.elements(i).value) == true)){
 					formElement.elements(i).style.backgroundColor = "#ffcccc" ;
 					formElement.elements(i).focus();
 					//alert("欄位必須是 數字");
 					alert("此欄位必須是數字!");
 					formElement.elements(i).style.backgroundColor = "" ;
 					return ;
 				};
 			};//end if( lastElementName != formElement.elements(i).name ){
		 	lastElementName = formElement.elements(i).name ;
		}
 	}	
	formElement.submit();	
}


function PrepareForm(submitElement){
 var formElement = null ;
 var formElementNames = new Array();
 var i ;

 	formElement= submitElement ;
 	while( formElement.tagName != "FORM" ){
 		if( formElement.tagName == "BODY" ){
 			alert("此畫面不包含 FORM 元件");
 			return ;
 		} 
 		formElement = formElement.parentElement ;
 	}

 	fieldNamesElement = formElement.elements("__field_names");
 	if( fieldNamesElement == null ){
 		formElement.insertAdjacentHTML(
 		"afterBegin" ,
 		"<input type=Hidden name=__field_names   >") ;
	 	fieldNamesElement = formElement.elements("__field_names");
 	}
 	for( i = 0 ; i < formElement.elements.length ; i++){
 		if( formElement.elements(i).name.indexOf("__") != 0 &&
 			formElement.elements(i).name != ""
 		){
			formElementNames = formElementNames.concat(formElement.elements(i).name);
		 	//formElementNames.push(formElement.elements(i).name);
		}
 	}
	fieldNamesElement.value = formElementNames.join(","); 

	conditionStr = "" ;
 	conditionElement = formElement.elements("__condition");
	
 	if( conditionElement == null ){
 		formElement.insertAdjacentHTML(
 		"afterBegin" ,
 		"<input type=Hidden name=__condition  >") ;
	 	conditionElement = formElement.elements("__condition");
 	}

 	for( i = 0 ; i < formElement.elements.length ; i++){
 		if( formElement.elements(i).name.indexOf("__condition") == 0 &&
	 		formElement.elements(i).name != "__condition" &&
 			formElement.elements(i).name != ""
 		){
 			
 			fieldName = formElement.elements(i).name.replace("__condition_","") ;
 			fieldValue = formElement.elements(i).value ;
 			if( fieldValue != "" ){
	 			if( formElement.elements(i).data_type=="number")
			 		conditionStr += fieldName + "=" + formElement.elements(i).value + " " ;
			 	else
			 		conditionStr += fieldName + " like '%" + formElement.elements(i).value + "%' " ;
				conditionStr += " and " ;
			}			
		}
 	}

	conditionStr = conditionStr.replace(/and $/,"");
	
	conditionElement.value = conditionStr ;
}



function ChangeDateValue(element){
  var dateValue ;
  
 	divElement = element ;

 	while( divElement.tagName != "SPAN" ){
 		divElement = divElement.parentElement ;
 	}
 	
 	dateValue = "" 
 	
 	for( i = 0 ; i < divElement.children.length ; i++ ){
 		selectElement = divElement.children[i] ;
 		if( selectElement.tagName == "SELECT" ){
	 		dateValue += selectElement.value + "/" ;
	 	}
 	}
 	
 	dateValue = dateValue.replace(/\/$/,"");
 	
 	if( dateValue.charAt(0)=="/"){
		divElement.children[0].value = "" ;
 	}else
		divElement.children[0].value = dateValue;
	
}


function fnEnableAttach(big_pic_text, objButton, big_pic) {
	
	big_pic_text.disabled = true;
	objButton.style.display = "none";
	big_pic_text.style.display = "none";
	big_pic.disabled = false;
	big_pic.style.display = "inline";
	
	form1.btnPic.style.display = "none";
}

function EnableAttach(buttonElement) {
 var fileWidgetElement ;
 var fileNameTextElement
 var fileNameTextElement
 	
 	fileWidgetElement = buttonElement.parentElement;

 	fileNameTextElement = fileWidgetElement.children[0] ;
 	fileElement = fileWidgetElement.children[1] ;
 	updateElement = fileWidgetElement.children[2] ;
 	previewElement = fileWidgetElement.children[3] ;
 	
 	fileNameTextElement.style.display = "none" ;
 	fileElement.disabled = false ;
	fileElement.style.display = "inline";
	updateElement.style.display = "none" ;
	previewElement.style.display = "none"
	fileElement.click();
}


function fnViewPic(file_name) {
	
	if ( file_name == '' ) {
		return;
	}
	
	open("../../../Upload_img/news/" +file_name, "_blank","height=240,width=310,status=0,titlebar=0,toolbar=0");
		
}

function fnPreviewPic(file_name) {
	alert()
	if ( file_name == '' ) {
		alert("沒有圖片");
		return;
	}
	
	open(file_name, "_blank","height=240,width=310,status=0,titlebar=0,toolbar=0");
		
}

function ShowDownloadPage(buttonElement){
 var url ;
 var parentElement ;
 var filePath ;
 var folderPath ;
 var filePathElement ;
 var fileElement ;
 
	parentElement = buttonElement.parentElement ;
	lastClicElement = buttonElement ;
	filePathElement = parentElement.children[0] ;
	fileNameElement = parentElement.children[1] ;

	filePath = filePathElement.value ;
	folderPath = "../../../Upload_img" ;

 	url = "../file_upload/file_upload.asp?folder_path=" + folderPath + "&file_path=" + filePath ;
 	
	//resultObj = showModalDialog(url,null,"dialogWidth=500px;dialogHeight=200px");

	resultObj = window.open(url,"DOWNLOAD_PAGE","width=500px,height=200px");
/*
	if( resultObj != null ){
		filePathElement.value = resultObj.filePath ;
		fileNameElement.value = resultObj.fileName ;
	}
*/
};

function ViewFile(buttonElement){
 var parentElement ;
 var filePath ;
 var folderPath ;

	parentElement = buttonElement.parentElement ;

	filePathElement = parentElement.children[0] ;

	window.open(filePathElement.value , 
		 		"_blank",
		 		"height=460,width=600,status=0,titlebar=0,toolbar=0,scrollbars=1,resizable=1");
}


function CheckEmail(myEmail) {
if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(myEmail)){
	return (true)
}

	return (false)
}


function Trim( str ){
 	str = str.replace(/^\s+/,"") ;
 	str = str.replace(/\s+$/,"") ;
 	return str ;
};//Trim( str ){ 

function fnResetSpace(obj) {
	if ( obj != null && obj.eEditorArea.document.body.innerText == "" && obj.eEditorArea.document.body.innerHTML.indexOf("<IMG") < 0)
		obj.eEditorArea.document.body.innerHTML = "";
}
