//GLOBAL

	function ControlPopupClose(){
	document.body.style.overflowY = "scroll";
	document.getElementById("ControlPopup").style.display = 'none';
	document.getElementById("ControlPopup").rows[0].cells[0].innerHTML = "";
	}
	

	function Panel(PanelLink){

	 PanelArray = document.getElementById("Panels").getElementsByTagName("div")
	 PanelLinks = PanelLink.parentElement.rows

	 for(i=0;i<PanelLinks.length;i++){
	  if(i==0){PanelLinks[i].childNodes[0].className = "panelofffirst"}
	  else{
	   PanelLinks[i].childNodes[0].className = "paneloff"
	  }
	 }

     for(i=0;i<PanelArray.length;i++){
      PanelArray[i].style.display = (PanelLink.rowIndex != PanelArray.length) ? "none" : "block"
     }

     if(PanelLink.rowIndex != PanelArray.length){
	    PanelArray[PanelLink.rowIndex].style.display = "block"
    }
    	
	 PanelLink.childNodes[0].className = "panelon"
	}


	function ResetCheck(Form, Element){
	 for(i=0;i<Form[Element.name].length;i++){
	 Form[Element.name][i].checked = false;
	 }
	 Element.checked = true;
	}

	

//LOGIN

	UserLoginFocus = function(){ 
	 document.forms['Login'].UserLogin.focus()
	}
	
	UserLoginGet = function(){ 
	 if(event.keyCode==13){document.forms["Login"].submit()}
	}
	
	UserLoginFailed = function(){ //FAILED LOGIN MESSAGE
	 document.getElementById("UserLoginFailed").style.display = "block"
	}
	

//CONFIGURATION

	function ConfigurationAddEdit(){

		var Form = document.forms["Configuration"]; var Error = new Array; var j = 1;

		if(Form.ConfigurationXml.value==""){Error.push("Configuration Xml")}

		if(Error!=""){document.getElementById("Error").innerHTML="ERROR - The following fields are required: " + Error.join(", ")}
		else{
         Form.submit()
		}

	}

    
//CATALOG

	function CatalogView(CatalogGuid){
	document.getElementById("ControlPopup").style.display = "block";
	
	  var Xml = new ActiveXObject("Microsoft.XMLHTTP");
	  Xml.open("GET", "/Catalog/View.asp?CatalogGuid="+CatalogGuid, false);
	  Xml.send(null);

	var Content = document.getElementById("ControlPopup").rows[0].cells[0]
	Content.innerHTML = Xml.responseText;
	}


	function CatalogAddEdit(){

		var Form = document.forms["Catalog"]; var Error = new Array; var j = 1;

	    if(Form.CatalogCommon.value==""){Error.push("Common Name")}
		if(Form.CatalogCategoryId.selectedIndex<1){Error.push("Category")}
	    if(Form.CatalogScientific.value==""){Error.push("Scientific Name")}
	    if(Form.CatalogCode.value==""){Error.push("Catalog Code")}
	
		if(Error!=""){document.getElementById("Error").innerHTML="ERROR: The following fields are required. " + Error.join(", ")+"<br/><br/>"}
		else{
         Form.submit()
		}

	}


	function CatalogDelete(CatalogGuid){
	document.getElementById("ControlPopup").style.display = "block";
	
	  var Xml = new ActiveXObject("Microsoft.XMLHTTP");
	  Xml.open("GET", "/Catalog/Delete.asp?CatalogGuid="+CatalogGuid, false);
	  Xml.send(null);

	var Content = document.getElementById("ControlPopup").rows[0].cells[0]
	Content.innerHTML = Xml.responseText;
	}

	
	function CatalogStockGet(CatalogGuid){
	document.getElementById("ControlPopup").style.display = "block";
	
	  var Xml = new ActiveXObject("Microsoft.XMLHTTP");
	  Xml.open("GET", "/Catalog/Stock/Add.asp?CatalogGuid="+CatalogGuid, false);
	  Xml.send(null);

	var Content = document.getElementById("ControlPopup").rows[0].cells[0]
	Content.innerHTML = Xml.responseText;
	}


	function CatalogStockAdd(CatalogGuid){

		var Form = document.forms["CatalogStock"]; var Error = new Array; var j = 1;

	    if(Form.CatalogStockSourceName.value==""){Error.push("Source")}
	    if(Form.CatalogStockUnit.value==""){Error.push("Unit")}
		if(Form.CatalogStockPack.selectedIndex<1){Error.push("Pack")}
	
		if(Error!=""){document.getElementById("ControlPopupError").innerHTML="<br/><br/>ERROR: The following fields are required. " + Error.join(", ")+"<br/>"}
		else{

            if(Form.CatalogStockDelete.checked){Form.CatalogStockUnit.value = Math.abs(Form.CatalogStockUnit.value) * -1}

	          var XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	          XmlHttp.open("POST", "/Catalog/Stock/Add.asp?CatalogGuid="+CatalogGuid, false);
              XmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
              XmlHttp.setRequestHeader("Content-length", ("CatalogStockSourceName="+escape(Form.CatalogStockSourceName.value)+"&CatalogStockUnit="+escape(Form.CatalogStockUnit.value)+"&CatalogStockPack="+escape(Form.CatalogStockPack.value)+"&CatalogStockComment="+escape(Form.CatalogStockComment.value)).length);
	          XmlHttp.send("CatalogStockSourceName="+escape(Form.CatalogStockSourceName.value)+"&CatalogStockUnit="+escape(Form.CatalogStockUnit.value)+"&CatalogStockPack="+escape(Form.CatalogStockPack.value)+"&CatalogStockComment="+escape(Form.CatalogStockComment.value));
              document.getElementById("Panels").getElementsByTagName("div")[1].innerHTML = XmlHttp.responseText

            ControlPopupClose()
		}

	}

    
   function CatalogStockSourceNameGet(CatalogStockSourceName){

  	var Form = document.forms["CatalogStock"]; 
	var Select = Form.CatalogStockSourceList
	  	  Select.options.length = 0

    var Xml = new ActiveXObject("Microsoft.XMLHTTP");
    	Xml.open("GET", "/Catalog/Stock/Source/Get.asp?CatalogStockSourceName="+CatalogStockSourceName, false);
		if(CatalogStockSourceName!=""){Xml.send(null)}
	
    var Options = Xml.responseXml.selectNodes("//option")
    Select.style.display = (CatalogStockSourceName=="" || Options.length==0) ? "none" : "block";

    if(Options.length!=0){
       for(i=0;i<Options.length;i++){
	        Select.options[i] = new Option(Options[i].childNodes[0].text,Options[i].attributes[0].value,false,false)
       }
	}

  }  


//LOCATION

	function LocationAddEdit(){

		var Form = document.forms["Location"]; var Error = new Array; var j = 1;

		if(Form.LocationCode.value==""){Error.push("Code")}
	    if(Form.LocationName.value==""){Error.push("Name")}
	    for(i=0;i<document.getElementById("LocationParent").getElementsByTagName("input").length;i++){
	        if(document.getElementById("LocationParent").getElementsByTagName("input")[i].checked){break;}
	    }
	    if(i==document.getElementById("LocationParent").getElementsByTagName("input").length){Error.push("Parent Location")}
	
		if(Error!=""){document.getElementById("Error").innerHTML="ERROR: The following fields are required. " + Error.join(", ")+"<br/><br/>"}
		else{
         Form.submit()
		}

	}


	function ResetLocationParent(Form, Element){

        ResetCheck(Form, Element)

	    for(i=0;i<document.getElementById("LocationParent").getElementsByTagName("TD").length;i++){
	    document.getElementById("LocationParent").getElementsByTagName("TD")[i].style.background = "none"
	    }

	    for(i=0;i<Element.parentElement.parentElement.childNodes.length;i++){
	    Element.parentElement.parentElement.childNodes[i].style.background = "url(/Image/BackgroundScreenGray.gif)"
	    }

	}


	function LocationDelete(LocationGuid){
	document.getElementById("ControlPopup").style.display = "block";
	
	  var Xml = new ActiveXObject("Microsoft.XMLHTTP");
	  Xml.open("GET", "/Location/Delete.asp?LocationGuid="+LocationGuid, false);
	  Xml.send(null);

	var Content = document.getElementById("ControlPopup").rows[0].cells[0]
	Content.innerHTML = Xml.responseText;
	}

	
//LOT

	function LotAddEdit(){

		var Form = document.forms["Lot"]; var Error = new Array; var j = 1;
        var LocationArray = document.getElementById('Location').getElementsByTagName('input')

		if(Form.LotNumber.value==""){Error.push("Lot Number")}
		if(Form.CatalogId.selectedIndex<1){Error.push("Plant Code")}

        for(i=0;i<LocationArray.length;i++){if(LocationArray[i].checked){break;}}
        if(i==LocationArray.length){Error.push("Parent Location")} 
	
		if(Error!=""){document.getElementById("Error").innerHTML="ERROR: The following fields are required. " + Error.join(", ")+"<br/><br/>"}
		else{
         Form.submit()
		}

	}


	function LotCatalogStockGet(LotGuid){
	document.getElementById("ControlPopup").style.display = "block";
	
	  var Xml = new ActiveXObject("Microsoft.XMLHTTP");
	  Xml.open("GET", "/Lot/CatalogStock/AddEdit.asp?LotGuid="+LotGuid, false);
	  Xml.send(null);

	var Content = document.getElementById("ControlPopup").rows[0].cells[0]
	Content.innerHTML = Xml.responseText;
	}


	function LotCatalogStockAddEdit(LotGuid){

		var Form = document.forms["LotCatalogStock"]; var Error = new Array; var j = 1;

	    if(Form.CatalogStockSourceId.selectedIndex<1){Error.push("Source")}
	    if(Form.CatalogStockUnit.value==""){Error.push("Unit")}
		if(Form.CatalogStockPack.selectedIndex<1){Error.push("Pack")}
	
		if(Error!=""){document.getElementById("ControlPopupError").innerHTML="<br/><br/>ERROR: The following fields are required. " + Error.join(", ")+"<br/>"}
		else{

            Form.CatalogStockUnit.value = Math.abs(Form.CatalogStockUnit.value) * -1;

	          var XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	          XmlHttp.open("POST", "/Lot/CatalogStock/AddEdit.asp?LotGuid="+LotGuid, false);
              XmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
              XmlHttp.setRequestHeader("Content-length", ("CatalogStockSourceId="+escape(Form.CatalogStockSourceId.options[Form.CatalogStockSourceId.selectedIndex].value)+"&CatalogStockUnit="+escape(Form.CatalogStockUnit.value)+"&CatalogStockPack="+escape(Form.CatalogStockPack.value)+"&CatalogStockComment="+escape(Form.CatalogStockComment.value)).length);
	          XmlHttp.send("CatalogStockSourceId="+escape(Form.CatalogStockSourceId.options[Form.CatalogStockSourceId.selectedIndex].value)+"&CatalogStockUnit="+escape(Form.CatalogStockUnit.value)+"&CatalogStockPack="+escape(Form.CatalogStockPack.value)+"&CatalogStockComment="+escape(Form.CatalogStockComment.value));
              document.getElementById("Panels").getElementsByTagName("div")[3].innerHTML = XmlHttp.responseText

            ControlPopupClose()
		}

	}


	function LotStockGet(LotGuid, LotStockActionId){
	document.getElementById("ControlPopup").style.display = "block";
	
	  var Xml = new ActiveXObject("Microsoft.XMLHTTP");
	  Xml.open("GET", "/Lot/Stock/AddEdit.asp?LotGuid="+LotGuid+"&LotStockActionId="+LotStockActionId, false);
	  Xml.send(null);

	var Content = document.getElementById("ControlPopup").rows[0].cells[0]
	Content.innerHTML = Xml.responseText;
	}


	function LotStockAddEdit(LotGuid, LotStockActionId){

		var Form = document.forms["LotStock"]; var Error = new Array; var j = 1; 
		var LotStockUnit = ""; var LotStockPack = ""; var LotNumber = ""; var LotGuidSource = ""; var CatalogStockPercentage = "";

        switch(LotStockActionId){
        
        case 1: //GROW
            LotStockUnit = Math.abs(Form.LotStockUnit.value) 
            LotStockPack = escape(Form.LotStockPack.value)
            if(Form.LotStockUnit.value==""){Error.push("Quantity")}
            if(Form.LotStockPack.selectedIndex<1){Error.push("Size")}
          break;    
          
        case 2: //DUMP
            LotStockUnit = Math.abs(Form.LotStockUnit.value) * -1
            if(Form.LotStockUnit.value==""){Error.push("Quantity")}
          break;

        case 3: //JOIN
            LotGuidSource = escape(Form.LotGuidSource.value)
            if(Form.LotGuidSource.selectedIndex<1){Error.push("Lot Code")}
          break;

        case 4: //SPLIT
            LotGuidSource = escape(LotGuid)
            LotNumber = escape(Form.LotNumber.value)
            LotStockUnit = Math.abs(Form.LotStockUnit.value)
            CatalogStockPercentage = Math.abs(Form.CatalogStockPercentage.value)
            if(Form.LotNumber.value==""){Error.push("Lot Code")}
            if(Form.LotStockUnit.value==""){Error.push("Quantity")}
            if(Form.CatalogStockPercentage.value==""){Error.push("Seed Percentage")}

        }


		if(Error!=""){document.getElementById("ControlPopupError").innerHTML="<br/><br/>ERROR: The following fields are required. " + Error.join(", ")+"<br/>"}
		else{

	          var XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	          XmlHttp.open("POST", "/Lot/Stock/AddEdit.asp?LotGuid="+LotGuid+"&LotStockActionId="+LotStockActionId, false);
              XmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
              XmlHttp.setRequestHeader("Content-length", ("LotStockUnit="+LotStockUnit+"&LotStockPack="+LotStockPack+"&LotNumber="+LotNumber+"&LotGuidSource="+LotGuidSource+"&CatalogStockPercentage="+CatalogStockPercentage+"&LotStockComment="+escape(Form.LotStockComment.value)).length);
	          XmlHttp.send("LotStockUnit="+LotStockUnit+"&LotStockPack="+LotStockPack+"&LotNumber="+LotNumber+"&LotGuidSource="+LotGuidSource+"&CatalogStockPercentage="+CatalogStockPercentage+"&LotStockComment="+escape(Form.LotStockComment.value));
              document.getElementById("Panels").getElementsByTagName("div")[2].innerHTML = XmlHttp.responseText

            ControlPopupClose()
		}

	}
    
//USER

	function UserAddEdit(UserGuid){

		var Form = document.forms["User"]; var Error = new Array; var j = 1;

		if(Form.UserLogin.value==""){Error.push("UserName")}
		if(Form.UserPassword.value==""){Error.push("Password")}

		if(Form.UserName.value==""){Error.push("Name")}
		if(Form.UserEmail.value==""){Error.push("Email")}

		if(Form.UserDefaultPage.selectedIndex==0){Error.push("Default Page")}
    
		if(Error!=""){document.getElementById("Error").innerHTML="ERROR - The following fields are required: " + Error.join(", ")+"<br/>"+"<br/>"}
		else{

               var PostData = new Array();
               var elements = document.forms["User"].elements

               for(i=0;i<elements.length;i++){
                if(elements[i].name!=""){   
                    if(elements[i].type=="select-one"){PostData[PostData.length] = elements[i].name +"=" + encodeURI(elements[i].options[elements[i].selectedIndex].value)}
                    else if(elements[i].type=="checkbox" && elements[i].checked){PostData[PostData.length] = elements[i].name +"=" + encodeURI(elements[i].value)}
                    else if(elements[i].type=="text"){PostData[PostData.length] = elements[i].name +"=" + encodeURI(elements[i].value)}
                }
               }

                var NoCache = new Date()

                    Xml = new ActiveXObject("Microsoft.XMLHTTP") 
		            Xml.open("POST", "/User/AddEdit.asp?UserGuid="+UserGuid+"&NoCacheUrl="+NoCache.getDate()+"_"+NoCache.getMonth()+"_"+NoCache.getYear()+"_"+NoCache.getHours()+"_"+NoCache.getMinutes()+"_"+NoCache.getSeconds(), false);
                    Xml.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                    Xml.setRequestHeader("Content-length", PostData.join("&").length);
		            Xml.send(PostData.join("&"));

                    if(Xml.responseText == "True"){
	                document.getElementById("ControlPopup").rows[0].cells[0].innerHTML = document.getElementById("UserNameExist").outerHTML
                	document.getElementById("ControlPopup").style.display = "block";
                    }
                    else{
                    location.href = "/User/AddEdit.asp?UserGuid=" +  Xml.responseText
                    }   

		}

	}
	
	
	function UserDelete(UserGuid){
	document.getElementById("ControlPopup").style.display = "block";
	
	  var Xml = new ActiveXObject("Microsoft.XMLHTTP");
	  Xml.open("GET", "/User/Delete.asp?UserGuid="+UserGuid, false);
	  Xml.send(null);

	var Content = document.getElementById("ControlPopup").rows[0].cells[0]
	Content.innerHTML = Xml.responseText;
	}

