var bPageModified=false;

function replaceToNewLine(toReplaceStr){
    return toReplaceStr.replace(/(\(\*\#\*\))/g,"\n");
}

function document.onkeydown()
{
    if(bPageModified==true){
        return;
    } 

    //ignore 'Enter' and 'Tab' key
    if(event.keyCode!=13 && event.keyCode!=9){
    	bPageModified=true;
    }
}

function closeSelf(){
    window.close();
}

function canCloseSelf(confirmMsg,bNotRefreshOpener){
    if(bPageModified==true){
        if(!confirm(confirmMsg)){
            return;
        }
	}

    var iNotRefresh=0;

    try{
        iNotRefresh=parseInt(bNotRefreshOpener);
    }catch(e){}

    if(iNotRefresh>0){
        closeSelf();
    }else{
        closeAndReloadOpener();
    }
}

function removeLastSharp(source){
    var dest=source;

    if(source.substr(source.length-1,1)=='#'){
        dest=source.substring(0,source.length-1);
    }

    return dest;
}

function closeAndReloadOpener(){
    try{
        window.close();

	    window.opener.top.focus();
	    window.opener.top.location=window.opener.top.location;
    }catch(e){}
}

function openNewWindow(pageUrl)
{
    //remove last sharp
    pageUrl=removeLastSharp(pageUrl);

	var style='menubar=no,location=no,directories=no,toolbar=no,statusbar=no,'
    		+'scrollbars=yes,resizable=yes';

	var popwin=window.open(pageUrl,'_blank',style);

	popwin.focus();
}

function openNewSizeWindow(pageUrl,windowName,width,height,scrollbars)
{
    //remove last sharp
    pageUrl=removeLastSharp(pageUrl);

	var style='menubar=no,location=no,directories=no,toolbar=no,statusbar=no,resizable=yes,'
		+getCenterWindowPos(width,height)+",scrollbars="+scrollbars;

    var popwin=window.open(pageUrl,windowName,style);

	popwin.focus();
}

function getCenterWindowPos(pWidth,pHeight){
    var width=parseInt(''+pWidth);
    var height=parseInt(''+pHeight);

    var top=(screen.height*0.88-height)*0.5;
    var left=(screen.width*0.985-width)*0.5;

    if(top<0){
        top=0;
    }

    if(left<0){
        left=0;
	}

    var posStatement="top="+top+",left="+left+",width="+width+",height="+height;
    return posStatement;
}

function openModalWindow(pageUrl,windowName,width,height)
{
	var style="dialogHeight: "+height+"; dialogWidth: "+width+";  center: Yes; help: No; resizable: Yes; status: No;";
    window.showModalDialog(pageUrl,windowName,style);
}

function parseUrlParameter(parameter) {
 	if (parameter.substring(0,1)=="?") {
    	parameter=parameter.substring(1);
 	}
	if (parameter.substring(parameter.length-1)=="#") {
    	parameter=parameter.substring(0,parameter.length-1);
 	}
	return parameter;
}

function ReplaceAllSubString(str,strOld,strNew){
    while (str.indexOf(strOld)>-1){
        str = str.replace(strOld,strNew);
    }
    return str;
}

function confirmDelete(message) {
	if (confirm(message)) return true;
    else return false;
}

function callDelete(message,aForm) {
	if (confirm(message)) {
        try {
        	aForm.nsAction.value="Delete";
        }
        catch (e) {}
		aForm.submit();
	}
}

function callIFrameDelete(dataGridName,listViewFormName,noSelectMessage,confirmMessage){
    var aForm=getIFrameListViewForm(dataGridName,listViewFormName);
    if (!checkSomeoneSelected(aForm)) {
        alert(noSelectMessage);
        return;
    }
    callDelete(confirmMessage,aForm);
}

function getListRecordNo(aForm){
    if (typeof aForm.id != 'object'){
       return 0;
    }

    var bMoreThanOne=false;
    var elementNum=aForm.id.length;
    if (elementNum>=2){
       bMoreThanOne=true;
    }

    if(bMoreThanOne){
        return elementNum;
    }
    else{
        return 1;
    }
}

function getCheckedIds(aForm){
    var ids='';

    if (typeof aForm.id != 'object'){
       return ids;
    }

    var bMoreThanOne=false;
    var elementNum=aForm.id.length;
    if (elementNum>=2){
       bMoreThanOne=true;
    }

    if(bMoreThanOne){
        for (var i=0;i<elementNum;i++){
            if (aForm.id[i].checked){
                ids+=","+aForm.id[i].value;
            }
        }

        if(ids!=''){
        	ids=ids.substr(1);
    	}
    }
    else{
        if(aForm.id.checked){
	        ids=aForm.id.value;
        }
    }

    return ids;
}

function checkSomeoneSelected(aForm){
    if (typeof aForm.id != 'object'){
       return;
    }

    var bMoreThanOne=false;
    var elementNum=aForm.id.length;
    if (elementNum>=2){
       bMoreThanOne=true;
    }

    var bSomeoneChecked=false;

    if(bMoreThanOne){
        for (var i=0;i<elementNum;i++){
            if (aForm.id[i].checked){
                bSomeoneChecked=true;
                break;
            }
        }
    }
    else{
        bSomeoneChecked=aForm.id.checked;
    }

    return bSomeoneChecked;
}

function setSelectedSortKeyIdForEdit(keyId,listIndex,sName){
     document.all.parentId.value=keyId;
     document.all.slistIndex.value=listIndex;
     document.all.sName.value=sName;
}

function setSelectedSort(keyId,sortName){
    document.all.keyId.value=keyId;
    document.all.sortName.value=sortName;
}
function openSelectTypeModelWindow(poName,edit,field1,field2){
    var url;
    if (edit=="yes"){
        url="../treesort/TreeSortAction.do?nsAction=SelectEdit&poName="+poName;
    }
    else{
         url="../treesort/TreeSortAction.do?nsAction=Select&poName="+poName;
    }
    var returnStr=window.showModalDialog(url,"selectType","dialogHeight:600px;dialogWidth:500px;center:Yes;help:No;;resizable:Yes;status:No;");
    if (returnStr!=undefined) {
        if (returnStr!="***"){
            var arrStr =returnStr.split(";");
            field1.value=arrStr[0];
            field2.value=arrStr[1];
        }
    }
}
function openEditFromSelectWindow(poName){
 var url="TreeSortAction.do?nsAction=Edit&linkForm=selectForm&poName="+poName;
  openNewSizeWindow(url,'SelectTypeWindow','400','600','yes');
}
function openSelectModelWindow(url,field1,field2){
    var returnStr=window.showModalDialog(url,"selectType","dialogHeight:600px;dialogWidth:500px;center:Yes;help:No;;resizable:Yes;status:No;");
    if (returnStr!=undefined) {
        if (returnStr!="***"){
            var arrStr =returnStr.split(";");
            field1.value=arrStr[0];
            field2.value=arrStr[1];
        }
    }
}
function refreshViewMain(poName,keyId){
   var url;
   if (poName=="ExpendableSortPO"){
      url="../expendable/ExpendableListLoadAction.do?typeId="+keyId;
   }
   else if(poName=="CapitalAssetTypePO"){
       url="../capitalasset/CapitalAssetViewMainAction.do?typeId="+keyId;
   }
   else if(poName=="ExpendableRegisterPO"){
       url="../expendable/ExpendableHistoryLoadAction.do?historyType=register&date="+keyId;
   }
   else if(poName=="ExpendableGrantPO"){
       var key;
       if (keyId==""){
           key="root";
       }
       else{
           key=keyId;
       }
       url="../expendable/ExpendableHistoryLoadAction.do?historyType=grant&dept=&date="+key;
   }
   else if(poName=="ExpendableGrantPOForDept"){
       var key;
       if (keyId==""){
           key="root";
       }
       else{
           key=keyId;
       }
       url="../expendable/ExpendableHistoryLoadAction.do?historyType=grant&date=&dept="+encodeURI(escape(key));
   }
   else if(poName=="CapitalAssetApplyForUser"){
       url="../capitalasset/CapitalAssetForApplyViewMainAction.do?group=&user="+encodeURI(escape(keyId));
   }
   else if(poName=="CapitalAssetApplyForGroup"){
       url="../capitalasset/CapitalAssetForApplyViewMainAction.do?user=&group="+encodeURI(escape(keyId));
   }

   parent.viewmain.location=url;
}
function refreshComplaintView(pathName){
   parent.viewmain.location = pathName;
}

//-------------------------Validation Methods----------------------------//
//?????????????????????????????????,?????????????????????True,????????????????false
function isMoney(s){
	//????????????????false
	if (s.Trim()=="") return false;
	//???????????????false
	if (s.Trim()!=s) return false;
	//??????????????????????????????????false
	if (isNaN(s))	return false;
	//????????????????????????false
	if (parseFloat(s)<0)	return false;
	else	return true;
}

//????????????????????????????????????,????????????????True,????????????????false
function isAmount(s){
	//????????????????false
	if (s.Trim()=="") return false;
	//???????????????false
	if (s.Trim()!=s) return false;
	//??????????????0??????????????
    while (s.length > 1 && s.substring(0,1) == "0"){
	    s = s.substr(1);
    }
	//??????????????????????????????????false
	if (isNaN(s))	return false;
	//???????????????????????????false
	if (parseInt(s).toString()!=s) return false;
	//????????????????????????false
	if (parseInt(s)<0)	return false;
	else	return true;
}
//??????????????????????????????????????,????????????????True,????????????????false
function isInteger(s){
	//????????????????false
	if (s.Trim()=="") return false;
	//???????????????false
	if (s.Trim()!=s) return false;
	//??????????????0??????????????
    while (s.length > 1 && s.substring(0,1) == "0"){
	    s = s.substr(1);
    }
	//??????????????????????????????????false
	if (isNaN(s))	return false;
	//???????????????????????????false
	if (parseInt(s).toString()!=s) return false;
	else	return true;
}

//??????????????????????????,????????????????????????????true,????????????false
function isDate(str){
  var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/;
  var r = str.match(reg);

  if(r==null)return false;
  var d= new Date(r[1], r[3]-1,r[4]);
  var newStr=d.getFullYear()+r[2]+(d.getMonth()+1)+r[2]+d.getDate()
  //return newStr==str
  return true;
}

function CheckTimeNS(thestring){
	var ntimedate;
	var temtime;
	var tempstr;
	var temptime2;
	thetime=new Array(3);

	temptime=thestring;
	thetime=temptime.split('-')	;
	ntimedate=new Date(thetime[0],thetime[1]-1,thetime[2]);

	if (isNaN(ntimedate)) {
		return -1;
	}

	if (thetime[1].charAt(0)=="0"){
		tempstr=thetime[1].charAt(1);
	}else{
		tempstr=thetime[1];
	}

	if (parseInt(tempstr)!=parseInt(ntimedate.getMonth())+1){
		return -1;
	}

	if (thetime[2].charAt(0)=="0"){
		tempstr=thetime[2].charAt(1);
	}else{
		tempstr=thetime[2];
	}

	if (parseInt(tempstr)!=parseInt(ntimedate.getDate())){
		return -1;
	}

	return 1;
}

String.prototype.Trim=function(){
	//????????????Trim????????????
	return this.replace(/(^\s*)|(\s*$)/g,"");
}

function fieldMustBeChecked(objField) {
	if (objField.readOnly==true || objField.type=="hidden")
	{
		return false;
	}
	else {
		return true;
	}
}

function assertFieldNotNullWithoutFocus(objField , alertMsg){
    if (typeof(objField[0])=="object"){
        objField = objField[0];
    }

    if (typeof(objField)=="object"){
        if (objField.value.Trim()==""){
            alert(alertMsg);
        }else{
            return true;
        }
    }

    return false;
}
function assertFieldNotNull(objField , alertMsg){
    if (typeof(objField[0])=="object"){
        objField = objField[0];
    }

    if (typeof(objField)=="object"){
        if (objField.value.Trim()==""){
            alert(alertMsg);
            objField.focus();
        }else{
            return true;
        }
    }

    return false;
}
function assertFieldIsInteger(objField , alertMsg){
    if (!isAmount(objField.value)){
        alert(alertMsg);
        setFieldFocus(objField);
        return false;
    }else{
        return true;
    }
}

function assertFieldIsAnyInteger(objField , alertMsg){
    if (!isInteger(objField.value)){
        alert(alertMsg);
        setFieldFocus(objField);
        return false;
    }else{
        return true;
    }
}

function assertFieldIsDate(objField , alertMsg){
    if (CheckTimeNS(objField.value)==-1){
        alert(alertMsg);
        objField.focus();
        return false;
    }else{
        return true;
    }
}

function assertFieldIsMoney(objField , alertMsg){
    if (!isMoney(objField.value)){
        alert(alertMsg);
        objField.focus();
        return false;
    }else{
        return true;
    }
}

function assertFieldNotIncludeSpace(objField , alertMsg){
    if (objField.value.indexOf(" ")!=-1){
        alert(alertMsg);
        objField.focus();
        return false;
    }else{
        return true;
    }
}

function setFieldFocus(objField) {
	try
	{
		objField.focus();
	}catch (e){}
}

function registerExpendable(){
    openNewSizeWindow('../expendable/ExpendableRegisterLoadAction.do' ,'RegisterExpendable','780','500','no');
}

function grantExpendable(){
    openNewSizeWindow('../expendable/ExpendableGrantLoadAction.do' ,'GrantExpendable','780','500','no');
}

function openBaseData(poName){
    openNewSizeWindow('../basedata/baseDataMainAction.do?poname='+poName ,'openBaseData','400','300','no');
}

function openTreeSort(poName){
    openNewSizeWindow('../treesort/TreeSortAction.do?nsAction=Edit&poName='+poName ,'openTreeSort','400','500','yes');
}

function openExpendable(pageUrl){
    openNewSizeWindow(pageUrl,'editInfo','780','220','no');
}

function openExpendableHistoryInfo(pageUrl){
    openNewSizeWindow(pageUrl ,'HistoryInfo','780','400','no');
}
function openExpendableStockUpdateHistory(){
    openNewSizeWindow('../expendable/ExpendableStockUpdateListLoadAction.do' ,'stockUpdateList','780','500','no');
}
function openExpendableStat(type){
    if (type=="Register"){
        openNewSizeWindow('../expendable/ExpendableStatLoadAction.do?poName=ExpendableRegisterPO','registerStat','780','240','no');
    }
    else if(type=="Grant"){
        openNewSizeWindow('../expendable/ExpendableStatLoadAction.do?poName=ExpendableGrantPO','grantStat','780','240','no');
    }
    else if(type=="Expendable"){
        openNewSizeWindow('../expendable/ExpendableStatLoadAction.do?poName=ExpendablePO','expendableStat','780','240','no');
    }
}

function refreshRight(halfUrl) {
    var rightTopUrl='RoleGrantedOwnerViewAction.do?roleId='+halfUrl;
    var rightBottomUrl='RoleDenyedOwnerViewAction.do?roleId='+halfUrl;

    parent.rightbottom.location=rightBottomUrl;
    parent.righttop.location=rightTopUrl;
}

function openFullSizeWindow(pageUrl){
    var width=screen.width-6;
    var height=screen.height-60;

    var top=0;
    var left=0;

    var posStatement="top="+top+",left="+left+",width="+width+",height="+height;

   	var style='menubar=no,location=no,directories=no,toolbar=no,statusbar=no,resizable=yes,'
		+posStatement+",scrollbars=no";

    var popwin=window.open(pageUrl,'',style);

	popwin.focus();
}
function openFullSizeWindowWithScrollbar(pageUrl){
    var width=screen.width-6;
    var height=screen.height-60;

    var top=0;
    var left=0;

    var posStatement="top="+top+",left="+left+",width="+width+",height="+height;

   	var style='menubar=no,location=no,directories=no,toolbar=no,statusbar=no,resizable=yes,'
		+posStatement+",scrollbars=yes";

    var popwin=window.open(pageUrl,'',style);

	popwin.focus();
}

function contentSearchSubmit(queryKey,queryKeyNullMsg) {
    if(queryKey==''){
        alert(queryKeyNullMsg);
        return false;
    }
	//alert(queryKey);
    location.href='/search/index.do?queryKey='+encodeURI(escape(queryKey));
    return false;
 }
 
 function openFullSizeWindowWithToolbar(pageUrl){
    var width=screen.width-6;
    var height=screen.height-60;

    var top=0;
    var left=0;

    var posStatement="top="+top+",left="+left+",width="+width+",height="+height;

   	var style='menubar=yes,location=yes,directories=yes,toolbar=yes,statusbar=yes,resizable=yes,'
		+posStatement+",scrollbars=yes";

    var popwin=window.open(pageUrl,'',style);

	popwin.focus();
}

