﻿// JScript File

var FormType = "Find";

function handleHttpResponse(xml, listName) {
    var list = $("#" + listName);
    list.get(0).options.length = 0;
    
    $("VALUE", xml).each(
        function() {
            elemText = $("TEXT", this).text();
            elemID = $("ID", this).text();
            $("<option></option>")
            .attr("value", elemID)
            .html(elemText)
            .appendTo(list);
        }
    );
}

function GetFieldValue(xml){
        //alert(xml.xml);
        $("VALUE", xml).each(
        function() {
        $("#" + $("POLE", this).text()).attr("value",$("TEXT", this).text());
        $("#" + $("POLE", this).text()).html($("TEXT", this).text());
        }
        );
}

function onChangeList(obj) {
    //alert(obj);
    //alert(obj.getAttribute("fmn"));
    onChangeLoadList(obj.getAttribute("fmn"), obj.getAttribute("main"), obj.value);
}

function onChangeLoadList(objform, objmain, objvalue) {
  var list = document.getElementsByTagName("select");
  for (i = 0; i < list.length; i++)
  {     if (list[i].getAttribute("parent")==objmain & list[i].getAttribute("fmn")==objform)
        {
            url = "http://" + document.location.hostname + "/listvalue_" + list[i].id + "-" + objvalue+ ".axd";
            parentListName = list[i].id;
            LoadList(url, parentListName);
        }
  }
}


function LoadList(url, parentListName)
    {
                $.post(url, {}, function(xml) {
                handleHttpResponse(xml, parentListName);
                });
    }

function TabsForm(tabsTag, OperTag)
    {
        var $formtb = document.getElementById("aspnetForm");
        var elems = $formtb.getElementsByTagName("*");
         for (i = 0; i < elems.length; i++)
         {        
         if (elems[i].getAttribute("fm")=="1"){
                if (elems[i].getAttribute(tabsTag)=="1")
                    {elems[i].style.display = 'inline';}
                else if(elems[i].getAttribute(OperTag)=="1") {elems[i].style.display = 'none';}
            }
         }
         
    }
	
	/*
		opTabId
	*/
 function LoadTabContent(opTabId, visibleGroupIDArr,allGroupIDforOp)
    {
       
        //var $opForm = document.getElementById(opTabId);
        TabsForm(visibleGroupIDArr,allGroupIDforOp);
		/*
		for(var i=0; i<allGroupIDforOp.length; i++){
			if (allGroupIDforOp[i]!='-1'){
				group=document.getElementById(allGroupIDforOp[i]);
				group.style.display = 'none';
			}
		}
		
		for(var i=0; i<visibleGroupIDArr.length; i++){
			group=document.getElementById(visibleGroupIDArr[i]);
			group.style.display = 'block';
		}
*/
    }
	
	

function openHTMLedit(fldN)
{
    w=window.open("fckeditor\\htmleditor.aspx?pole="+fldN, "w" + fldN, "height=570,width=770,menubar=no,toolbar=no,location=no,status=no,resizable=yes,scrollbars=yes");
}

function OpenWindowLov(fldN, mID)
{
    var $fldN = document.getElementById(fldN);
    var urllimit="";
    if ($fldN.getAttribute("mainlimit")){
          var list = document.getElementsByTagName("input");
            for (i = 0; i < list.length; i++)
            { if (list[i].getAttribute("main")==$fldN.getAttribute("parent") & list[i].getAttribute("fmn")==$fldN.getAttribute("fmn"))
                {urllimit="&" + $fldN.getAttribute("mainlimit") +"=" + list[i].value; }
            }

          var list = document.getElementsByTagName("select");
            for (i = 0; i < list.length; i++)
            { if (list[i].getAttribute("main")==$fldN.getAttribute("parent") & list[i].getAttribute("fmn")==$fldN.getAttribute("fmn"))
                {urllimit="&" + $fldN.getAttribute("mainlimit") +"=" + list[i].value; }
            }
    }
    w=window.open("lov.aspx?mID="+mID+"&lovf=" + fldN + urllimit, "w" + fldN, "height=570,width=770,menubar=no,toolbar=no,location=no,status=no,resizable=yes,scrollbars=yes");
}

function onChangeText(t,intR,codeLoad) {
  if (intR) {if(/\D/.test(t.value)){t.value=t.defaultValue;}else{t.defaultValue=t.value;}}
  if(self.ww) {clearTimeout(ww);};
  if(self.ww1) {clearTimeout(ww1);};
  //alert("http://" + document.location.hostname + "/FieldsValue_"+ t.id +"-"+t.value+".axd");
  ww=setTimeout("Load('http://" + document.location.hostname + "/FieldsValue_"+ t.id +"-"+t.value+".axd');",600);
  
  ww1=setTimeout("onChangeLoadList('" + t.getAttribute("fmn") + "','" + t.getAttribute("main") + "','" + t.value + "')",1000);
}

function CodeLoad(obj) {
  var list = document.getElementsByTagName("select");
  for (i = 0; i < list.length; i++)
  {     if (list[i].getAttribute("parent")==obj.getAttribute("main"))
        {
            url = "http://" + document.location.hostname + "/listvalue_" + list[i].id + "-" + obj.value+ ".axd";
            parentListName = list[i].id;
            LoadList(url, parentListName);
        }
  }
}

function Load(url) { 
                $.post(url, {}, function(xml) {
                GetFieldValue(xml);
                });
                
}


function ShowHide(pDivName)
{
      var imgd = document.all[pDivName + "i"];

      if ($("#" + pDivName).is(":hidden")) {
        $("#" + pDivName).show();
        imgd.src="/themes/Standart/img/hidedv.gif";
      } else {
        $("#" + pDivName).hide();
        imgd.src="/themes/Standart/img/showdv.gif";
      }
}

function onSubmitForm()
{
    if (FormType!="Find")
    {
        if (FormType=="Update") {fn="editForm"};
        if (FormType=="Add") {fn="addForm"};
        
        var $formtb = document.getElementById(fn);
        var elems = $formtb.getElementsByTagName("*");
        
        for (i = 0; i < elems.length; i++)
        { 
            if (elems[i].getAttribute("nnull")!=null)
            {
                if (elems[i].value=="" || (elems[i].tagName=="SELECT" & elems[i].value=="-1"))
                {
                    alert(elems[i].getAttribute("nnull"));
                    return false;
                }
            }
        }
    } 
    return true;
}



function doModal(url, context_flex){
	//alert(context_flex);
	var cnFLD=eval('document.form1.'+context_flex)
	if (cnFLD.value) 
	{
		showModelessDialog(url+'&cnFLD='+cnFLD.value,window,"dialogWidth:400px;dialogHeight:500px;scroll:yes");
	}
	else{
		alert("Заполните контекстное поле!!!")
	}

}


function PrintForm (pIDF,mID)
{
	newwindow=window.open("drequests.aspx?mID=" + mID + "&idf=" + pIDF,'','height=500,width=600,scrollbars=1');
	if (window.focus) {newwindow.focus()}
	return false;
}

function CopyForm(mID)
{
     $("[name*=fm" + mID + "epl]").each( 
          function() { 
               var sourceID = this.name; 
               var sourceText = this.value; 
               var targetID = sourceID.replace(/epl/gi, "apl"); 
               //alert(sourceID + '-' + targetID + document.getElementsByName(targetID));
               if (document.getElementsByName(targetID)[0])
                    {
                    var targetObj=document.getElementsByName(targetID)[0];
                    targetObj.value = sourceText;
               //if ($("[name=" +  targetID + "]").get(0))
               //{$("[name=" +  targetID + "]").get(0).value=sourceText;
               //onChangeLoadList(targetObj.getAttribute("fmn"),targetObj.getAttribute("main"),targetObj.value)
               //onChangeText(document.getElementsByName(targetID)[0],false,true);
               Load("http://" + document.location.hostname + "/FieldsValue_"+ targetID +"-"+targetObj.value+".axd");
               if (targetObj.getAttribute("main")){
                onChangeLoadList(targetObj.getAttribute("fmn"),targetObj.getAttribute("main"),targetObj.value);
               }
               }
               
          }      ); 
}

