﻿var FORM_SUBMITTED=false;
function canSubmit(){

	if(!FORM_SUBMITTED){
		FORM_SUBMITTED=true;
		hiddenHTMLObjects("A");
		hiddenHTMLObjects("SELECT");
		return true;
	}else{
		return false;
	}
}

function hiddenHTMLObjects(elmID){
	for (i = 0; i < document.all.tags(elmID).length; i++){
		obj = document.all.tags(elmID)[i];
		if(!obj){
			continue;
		}	
		obj.style.visibility = "hidden";
	}
}


function focusFirstField(){
    if (document.forms){
        if (document.forms.length==0) return;
    }
    //for(var i=0;i<document.forms(0).elements.length;i++){
    for(var i=0;i<100;i++){
        if (document.forms(0).elements(i)){
            if (document.forms(0).elements(i).type=="text"){
                document.forms(0).elements(i).focus();
                return;
            }
        }
    }
}

function setTextBoxDisabled(f,b){
    if(f.length==0) return;
    for(var i=0;i<f.elements.length;i++){
        if (f.elements(i)){
            if (f.elements(i).type=="text"){
                f.elements(i).disabled=b;
            }
        }
    }
}

function disableMouseRightClick(evt){
	if (event.button==2){
		alert("Mouse right click is disabled");
		return false;
	}
	return true;
}

function fixPoint(v,i){
	//round 'v' to 'i' decimal points
	var a=Math.pow(10,i);
	return Math.round(v*a)/a;
}

function round_up(v,i){
	//round 'v' to 'i' decimal points
	var d=fixPoint(v,i);
	var diff=v-d;
	var theshold=0.5*Math.pow(10,i);
	if(diff>=theshold){
		return d+1*Math.pow(10,i);
	}else{
		return d;
	}
}


function toUpperChar(thisfield)
{
thisfield.value = thisfield.value.toUpperCase();
}

function addDate(d,days){
var t=d.getTime();
t+=days*24*60*60*1000;

var d1=new Date();
d1.setTime(t);

return d1;
}

function addMonth(d,months){
var m=d.getMonth();

var k=(m+months)%12;
var y=Math.floor((m+months)/12);
if(k==0) k=12;

var d1=addYear(d,y);
d1.setMonth(k);

return d1;
}

function addYear(d,years){
var m=d.getYear();

var k=m+years;

var d1=new Date();
d1.setTime(d.getTime());
d1.setYear(k);

return d1;
}


function getDateByPaymentTerm(d,term){
var unit=term.substr(0,1);
var num=parseInt(term.substr(1,term.length-1));
if(unit=="M")	return addMonth(d,num);
if(unit=="D")	return addDate(d,num);
if(unit=="Y")	return addYear(d,num);
return d;
}


function printPage(){
	window.print();
}

function confirmSubmit(f,confirm_msg){
var agree=confirm(confirm_msg);
if(!agree) return;
f.submit();
}

function processReload(f){
	processSubmitForm(f,false,"");
}

function processSubmit(f){
	processSubmitForm(f,true,"");
}

function processSubmitMsg(f,confirm_msg){
	processSubmitForm(f,true,confirm_msg);
}


function processSubmitForm(f,needToConfirm,confirm_msg){
	if(needToConfirm){
		var msg_text="現在提交資料?";
		if(confirm_msg!=""){
			msg_text=confirm_msg;
		}
		var agree=confirm(msg_text);
		if (!agree){
			return;
		}
	}

	var message=document.all.processing_message.style;
	var cover=document.all.image_cover.style;

	//center the message box, hardcode to align the position
	message.top=(document.body.scrollTop)+(document.body.offsetHeight/2)-65;
	message.left=(document.body.scrollLeft)+(document.body.offsetWidth/2)-115;
	message.visibility='visible';

	//resize the image to cover the whole page
	//(preserve 3px to prevent page overflow)
	cover.height=document.body.scrollHeight-3;
	cover.width=document.body.scrollWidth;
	cover.visibility='visible';

	//override events to prevent further submit
	for(var i=0; i<f.length; i++){
		f.elements[i].onchange=function(){return false;};
		f.elements[i].onclick=function(){return false;};
	}

	//hidden all select menu to prevent overlap with message layer
	for(i=0;i<document.all.tags('SELECT').length;i++){
		obj=document.all.tags('SELECT')[i];
		if(!obj){
			continue;
		}	
		obj.style.visibility="hidden";
	}

	//disable all keyboard events(especially the tab key)
	document.body.onkeydown=function(){return false;};

	//show a wait cursor
	document.body.style.cursor ="wait";

	f.submit();
}

function cancelInput(page){
	var agree=confirm("取消輸入資料?");
	if (!agree){
		return;
	}
	window.location=page;
}


//since escape() is fail to handle encoding for unicode URI
//below 2 functions are going to encode unicode char(eg.chinese char) to %nn format

function utf8(wide) {
  var c, s;
  var enc = "";
  var i = 0;
  while(i<wide.length) {
    c= wide.charCodeAt(i++);
    // handle UTF-16 surrogates
    if (c>=0xDC00 && c<0xE000) continue;
    if (c>=0xD800 && c<0xDC00) {
      if (i>=wide.length) continue;
      s= wide.charCodeAt(i++);
      if (s<0xDC00 || c>=0xDE00) continue;
      c= ((c-0xD800)<<10)+(s-0xDC00)+0x10000;
    }
    // output value
    if (c<0x80) enc += String.fromCharCode(c);
    else if (c<0x800) enc += String.fromCharCode(0xC0+(c>>6),0x80+(c&0x3F));
    else if (c<0x10000) enc += String.fromCharCode(0xE0+(c>>12),0x80+(c>>6&0x3F),0x80+(c&0x3F));
    else enc += String.fromCharCode(0xF0+(c>>18),0x80+(c>>12&0x3F),0x80+(c>>6&0x3F),0x80+(c&0x3F));
  }
  return enc;
}

function encodeUTF8URI(s) {
  var s = utf8(s);
  var c;
  var enc = "";
  var okURIchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-";
  var hexchars = "0123456789ABCDEF";
  var tmp="";
  
  for (var i= 0; i<s.length; i++) {
    if (okURIchars.indexOf(s.charAt(i))==-1){
      tmp=s.charCodeAt(i)
	  enc += "%"+hexchars.charAt(tmp>>4)+hexchars.charAt(tmp & 0xF);
    }else{
      enc += s.charAt(i);
	}
  }
  return enc;
}


//convert date format from YYYYMMDD to DD/MM/YYYY
function convertDateFormat(obj){
	if(obj.value.length==8){
		var s=obj.value;
		obj.value=s.substr(6,2)+"/"+s.substr(4,2)+"/"+s.substr(0,4);
	}
	
}

function openModalDialog(url,h,w){
	return window.showModalDialog(
		url
		,""
		,"dialogHeight:"+h+"px;dialogWidth:"+w+"px;dialogTop:100px;dialogLeft:100px;status=yes;resizable=yes;help=no"
		);
}

function checkBeforeGotoPage(ele,ttl_pages){
	if(!isInteger(ele.value)) return false;

	var page_num=parseInt(ele.value);
	if(page_num<=0 || page_num>ttl_pages) return false;

	return true;
}
