function Navigate(PageType)
{
    var theForm = getForm();
	theForm.PageType.value = PageType;
	theForm.Navigate.value = true;
	theForm.SelectionType.value ='';
 	theForm.submit(); 	
}

function GoTo(SelectionType, ID,MillId, CustomerId)
{
    var theForm = getForm();
	theForm.SelectionType.value = SelectionType;
	theForm.ID.value = ID;
	theForm.MillId.value=MillId;
	theForm.CustomerId.value=CustomerId;
	theForm.submit();
}

function GoToMCSDetails(SelectionType,TagId,HeatNum,ProdId,BolNumber,MillId,CustomerId)
{
    var theForm = getForm();
	theForm.SelectionType.value = SelectionType;
	theForm.TagId.value = TagId;
	theForm.HeatNum.value = HeatNum;
	theForm.ProdId.value = ProdId;
	theForm.BolNumber.value=BolNumber;
	theForm.MillId.value=MillId;
	theForm.CustomerId.value=CustomerId;
	theForm.submit();
}

function SwitchCustomers(CustomerId, MillId)
{
    var theForm = getForm();
	theForm.CustomerId.value = CustomerId;
	theForm.MillId.value = MillId;
	theForm.submit();
}

function Process(e)
{
	var keycode;
	if (window.event) 
	{
		keycode = window.event.keyCode;
	}
	else if (e) 
	{
		keycode = e.which;
	}
	else 
	{
		return true;
	}
	
	if (keycode == 13)
	{
	    var theForm = getForm();
		theForm.WasSubmitted.value = 'true';
		theForm.submit();
		return false;
	}
	else
	{
		return true;
	}
}


function CheckAll(Field, ToggleField,Dopostback)
{
    var theForm = getForm();
    var ToCheck = false;

    for (i = 0; i < theForm.length; i++) {
        if (theForm[i].type == 'checkbox' && theForm[i].name.indexOf(Field) > 0) {
            if (theForm[i].checked == false) {

                ToCheck = 'true';
            }
        }
    }     

	
	for (i=0; i < theForm.length; i++)
	{		
		if (theForm[i].type == 'checkbox' && theForm[i].name.indexOf(Field) > 0)
		{		
			if (ToCheck == 'true')
			{
				theForm[i].checked = true;
			}
			else
			{
				theForm[i].checked = false;
			}			
		}		
	}
	
	if (ToCheck == 'true')
	{
		ToCheck = 'false';
	}
	else
	{
		ToCheck = 'true';
	}

	theForm[ToggleField].value = ToCheck;
	
	if (Dopostback == 'True') {
	    theForm.submit();
	}


}

function OpenWindow(newUrl, width, height) 
{ 
	window.open(newUrl, "nothing", "menubar=no,toolbars=no,scrollbars=yes,status=no,width="+width+",height="+height+",resizable=yes,directories=no,left=150,top=100") ;	
}

function OpenFullWindow(newUrl, width, height) 
{ 
	window.open(newUrl, "nothing", "menubar=yes,toolbars=no,scrollbars=yes,status=no,width="+width+",height="+height+",resizable=yes,directories=no,left=150,top=100") 
}

function Toggle(item)
{

   var itemElement = document.getElementById(item);
   var theForm = getForm();
   
   if(itemElement != null)
   {
		if (itemElement.style.display == 'none') 
		{
				itemElement.style.display = '';
		      
				if (theForm.DisplayStyle != null)
				{
					theForm.DisplayStyle.value = '';
				}
		}
		else  
		{
				itemElement.style.display = 'none';
		      
      			if (theForm.DisplayStyle != null)
				{
					theForm.DisplayStyle.value = 'display:none';
				}
		} 
   }
}

function ChangeCompany(FormName)
{
	document.getElementsByName("hdnCompany")[0].value = document.getElementById("Company").value;
	document.forms.CompanyForm.submit();
}

function AddProduct(InventoryId, MillId)
{
    var theForm = getForm();
	var CurField = theForm.product;

	if (CurField.length == undefined)
	{
		CurField.checked = true;
	}
	else
	{
		for (i = 0; i < CurField.length; i++)
		{
			if (CurField[i].value == InventoryId + '_' + MillId)
			{
				CurField[i].checked = true;
			}
		}
	}
    
	theForm.AreItems.value = 'true';
	theForm.submit();
}

function SetTons(ProductKey)	   
{
    var theForm = getForm();
	if (theForm['tons1_' + ProductKey].value == '' && theForm['tons2_' + ProductKey].value == '' && theForm['tons3_' + ProductKey].value == '')
	{
		theForm['tons1_' + ProductKey].value = theForm['totaltons_' + ProductKey].value 
	}
	return true;
}

function GetDetails(ProductTypeId, Millid, PageType)
{
    var theForm = getForm();
	theFormProductTypeId.value = ProductTypeId;
	theForm.Millid.value = Millid;	
	theForm.PageType.value = PageType;	
	theForm.submit();
}

function DetailLoaded()
{
    var theForm = getForm();
	theForm.IsLoaded.value = 'true';
	theForm.submit();	
}

function ClosedDetails(OrderId, MillId, PageType)
{
    var theForm = getForm();
	theForm.OrderId.value = OrderId;
	theForm.MillId.value = MillId;
	theForm.PageType.value = PageType;	
	theForm.submit();
}

function LoadPartNum()
{	
    var theForm = getForm();
	var PartNum = theForm.Length.value.split("|_|");	
	theForm.CustomerPartNumber.value = PartNum[4];
}

function PrintSelected()
{
    var theForm = getForm();
	var MillId = theForm.MillId.value;
	var BolId = theForm.BolId.value;
	var CustomerId = theForm.CustomerId.value;
	var Cert = '';
	var Tags = '';
	var Heats = '';
	var Products = '';
	var CurField = theForm.Tags;
	var Length = 0;
	
	if (CurField.length > 0) {	
	    for (i = 0; i < CurField.length; i++)
	    {
		    if (CurField[i].checked) {		        
		        CurField[i].checked = false; 
		        var Cert = CurField[i].value.split("|");

		        if (Length > 0) {
		            Tags += ',';
		            Heats += ',';
		            Products += ',';		    
		        }
    		    
		        Tags += Cert[0];
		        Heats += Cert[1];
		        Products += Cert[2];
		        Length++;
		    }
	    }
	
	    OpenFullWindow('MillCertificationDetails.aspx?pf=t&b=' + BolId + '&m=' + MillId + '&tag=' + Tags + '&h=' + Heats + '&pi=' + Products + '&c=' + CustomerId, 630, 600);
	}
}

function TreeViewToggle(Node, imagePath)
{
	// Unfold the branch if it isn't visible
	if (Node.nextSibling.style.display == 'none')
	{
		// Change the image (if there is an image)
		if (Node.children != null && Node.children.length > 0)
		{
			if (Node.children.item(0).tagName == "IMG")
			{
				Node.children.item(0).src = imagePath + "minus.gif";
			}
		}

		Node.nextSibling.style.display = '';
	}
	// Collapse the branch if it IS visible
	else
	{
		// Change the image (if there is an image)
		if (Node.children != null && Node.children.length > 0)
		{
			if (Node.children.item(0).tagName == "IMG")
			{
				Node.children.item(0).src = imagePath + "plus.gif";
			}
		}

		Node.nextSibling.style.display = 'none';
	}
}

function SelectParentCategory(parentCategoryId, parentCategory, parentCatNameId)
{    
     var theForm = getForm();
     var myItem = document.getElementById(parentCatNameId);
     theForm.ParentCatID.value = parentCategoryId;
     myItem.value = parentCategory;
}

function ShowHideItems(myItem, myButton,imagePath) {
   
	var myItem = document.getElementById(myItem);
	var myButton = document.getElementById(myButton);
	if (myItem.style.display != "none") {
	    
	   myItem.style.display = "none";
	   myButton.src = imagePath + "plus.gif";
	}
	else {
	    
       myItem.style.display = "";
       myButton.src = imagePath + "minus.gif";
       
	}
}


function AttachFiles(pageURL)
{
	var Files = '';
	var theForm = getForm();
	var CurField = theForm.Files;

	for (i = 0; i < CurField.length; i++)
	{
		if (CurField[i].checked)
		{
			
			if (Files.length > 0)
			{
				Files += ',';
			}
			
			Files += CurField[i].value;
		}
	}
	
	if (Files.length > 0)
	{
        OpenWindow(pageURL + '&files=' + Files,550,300);
	}
}


function GoShipAddress(AddressId,MillId,s,oi,m,c,od)
{
    var theForm = getForm();
    
    theForm.AddAddress.value = true;
   	theForm.AddressId.value = AddressId;
	theForm.MillId.value=MillId;  

	theForm.s.value=s;  
	theForm.s.value=oi;  
	theForm.s.value=m;  
	theForm.s.value=c;  
	theForm.s.value=od;  

 	theForm.submit(); 	
}

function SubmitShippedAddress()
{
 	getForm().submit(); 	
}

function getForm()
{
    return document.getElementById('aspnetForm');
}

function ToggleSelectionFilter(item,myButton,imagePath,myFilter)
{
  
   var myButtonElement;
   if(document.getElementsByName(myButton) != null)
   {
		myButtonElement = document.getElementsByName(myButton)[0];
   }
   
   if(myButtonElement != null)
   {
		var myFilter = document.getElementById(myFilter);
		var element = document.getElementById(item);
		var theForm = getForm();

		myFilter.innerHTML="Selection Filter";
		   

		myButtonElement.src = imagePath + "minus.gif";
		   
		   
		if (element.style.display == 'none') 
		{
				element.style.display = '';   
		               
				if (theForm.DisplayStyle != null)
				{
				    theForm.DisplayStyle.value = '';
				    myButtonElement.src = imagePath + "minus.gif";
				}
		}
		else  
		{
				element.style.display = 'none';
		      
      			if (theForm.DisplayStyle != null)
				{
				    theForm.DisplayStyle.value = 'display:none';
				    myButtonElement.src = imagePath + "plus.gif";
				}
		}
   }
}

function DisplayRolloverText(item,myFilter,actionFlag)
{
   
   var myFilter = document.getElementById(myFilter);
   var element = document.getElementById(item);
   var theForm = getForm();
   
   if (actionFlag == 1)
   {
		if (element.style.display == 'none') 
		{
			if (theForm.DisplayStyle != null)
			{
			   myFilter.innerHTML="Click here for more options";
			}
		}
   }
   else
   {
      myFilter.innerHTML="Selection Filter";
   }
}

function TabNavigate(TabType)
{
    var theForm = getForm();
	theForm.FilterTabType.value = TabType;
	theForm.TabChanged.value = true;
 	theForm.submit(); 	
}

function ShowRandomImage(myImage,imageList,imagePath)
{
  
	var randomImage = document.getElementById(myImage); 

    if (randomImage != null)
    {          
		imageSet = new Array();		
			
        // si: start index, ei: end index,  cc: current count  
		si=0; 
		cc=0;
		ei = imageList.length;
	        
		for (i=1;i<ei;i++)
		{
			if (imageList.charAt(i) == ',') 
			{
				imageSet[cc] = imageList.substring(si,i);
				cc++;
				si=i+1;
			}
		}
	    
		ind = Math.floor(Math.random() *cc);		              
		randomImage.src = imagePath + imageSet[ind];               	    
    }
}

function ExpandCollapseImages(imagePath)
{    

    var ExpandCollapse = document.getElementById('ExpandCollapseLink');  
    var ImpExpCollapse = document.getElementById('ImgExpandCollapse');  
	var imgButtons = document.getElementsByName('imgProduct');
    var Expand;

    
    // Change the Expand/Collapse hyperlink name 
    if (ExpandCollapse.innerHTML=='Expand All') 
    {
        Expand = false;
        ExpandCollapse.innerHTML="Collapse All";    
   		ImpExpCollapse.src = imagePath + "minus.gif";    
    }
    else
    {
       Expand = true;
       ExpandCollapse.innerHTML="Expand All";
	   ImpExpCollapse.src = imagePath + "plus.gif";    
    }
   
    //Change Image and ImageRow display style
    for(var i=0;i<imgButtons.length;i++)
    {
        var imgButton = imgButtons[i];

        var imgRow = document.getElementById('N' + imgButton.id.substring(6));
       
          if (imgRow != undefined) {
              if (Expand) {
                
                  imgRow.style.display = 'none';
                  imgButton.src = imagePath + "plus.gif";
              }
              else {
                  
                  imgRow.style.display = '';
                  imgButton.src = imagePath + "minus.gif";
              }
          }

          var imgRow = document.getElementById('M' + imgButton.id.substring(14));
         
          if (imgRow != undefined) {
              if (Expand) {
                  
                  imgRow.style.display = 'none';
                  imgButton.src = imagePath + "plus.gif";
              }
              else {
                  
                  imgRow.style.display = '';
                  imgButton.src = imagePath + "minus.gif";
              }
          }             
    }

}



function fnDisplay_SelectedCompanies(ListCount)
{
   var objDiv = document.getElementById('tblSelectedCompanies');
   if(objDiv)
   {
      if(ListCount>0)
         objDiv.style.display='';
      else
         objDiv.style.display='none';
   }
}

function fnAdd_List(strText, strValue)
{
   var objListBox = document.getElementById(objSelectedCompany);

   if(objListBox)
   {
      var myOption = document.createElement('option');

      myOption.text = strText;
      myOption.value = strValue;

      objListBox.options.add(myOption);
   }

}

function fnGet_ListInfo()
{
    var objListBox = document.getElementById(objSelectedCompany);
    var objHidden = document.getElementById(objHiddenId);

    objHidden.value='';

    if(objListBox && objListBox.options.length>0)
    {
       for(var i=0;i<objListBox.options.length;i++)
         objHidden.value = objHidden.value + "#" + objListBox.options[i].text + "$" + objListBox.options[i].value;
    }

}

function fnExist_List(strText, strValue)
{
   var objListBox = document.getElementById(objSelectedCompany);

   if(objListBox && objListBox.options.length>0)
   {
      for(var i=0;i<objListBox.options.length;i++)
        if(objListBox.options[i].text==strText && objListBox.options[i].value==strValue)
           return i;
   }
   return -1;
}

function fnProcess_check(chkId)
{
  var objListBox = document.getElementById(objSelectedCompany);

  if(objListBox)
  {
      if(chkId.checked)
      {
        if(fnExist_List(chkId.value,chkId.id)<0)
          fnAdd_List(chkId.value,chkId.id);
      }
      else
      {
         var listIndex= fnExist_List(chkId.value,chkId.id);

         if(objListBox && listIndex>=0)
            objListBox.remove(listIndex);
      }
      fnDisplay_SelectedCompanies(objListBox.options.length);
  }
}

function SelectCustomers()
{
    var chkCustomers = document.getElementsByName('chkCustomer');
    var objCheckAll= document.getElementById('AreCustomersChecked');
    var ToCheck = objCheckAll.value;

    for(var i=0;i<chkCustomers.length;i++)
    { 
          var listIndex = fnExist_List(chkCustomers[i].value,chkCustomers[i].id);

          if (ToCheck == 'true')
          {
                chkCustomers[i].checked = false;

                if(listIndex>=0)
                   document.getElementById(objSelectedCompany).remove(listIndex);
          }
         else
          {
                chkCustomers[i].checked = true;

                if(listIndex<0)
                    fnAdd_List(chkCustomers[i].value,chkCustomers[i].id);
          }
    }

      if (ToCheck == 'true')
      {
            ToCheck = 'false';
      }
      else
      {
           ToCheck = 'true';
      }

      objCheckAll.value = ToCheck;
      
      var objListBox = document.getElementById(objSelectedCompany);
      if(objListBox)
        fnDisplay_SelectedCompanies(objListBox.options.length);
}

function getClientHeight()
{
        var viewportheight;

        // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
        if (typeof window.innerHeight != 'undefined')
        {
          viewportheight = window.innerHeight;
        }

        // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
        else if (typeof document.documentElement != 'undefined'  && typeof document.documentElement.clientHeight !=  'undefined' && document.documentElement.clientHeight != 0)
        {
          viewportheight = document.documentElement.clientHeight
        }

        // older versions of IE
        else
        {       
           viewportheight = document.getElementsByTagName('body')[0].clientHeight
        }
        return viewportheight;
}
    
function getClientWidth()
{
        var viewportwidth;

        // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
        if (typeof window.innerWidth != 'undefined')
        {
          viewportwidth = window.innerWidth;
        }

        // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
        else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0)
        {
          viewportwidth = document.documentElement.clientWidth
        }

        // older versions of IE
        else
        {       
           viewportwidth = document.getElementsByTagName('body')[0].clientWidth
        }
        return viewportwidth;
}

function OpenPdfWindow(newUrl, width, height)   //to display PDF version page
{ 
    var pdfUrl = newUrl + '&pdf=y';
    window.location.href=pdfUrl;
}

function ChangeMessage(appMessageId) 
{
    var theForm = getForm();
    theForm.AppMessageId.value = appMessageId;
    theForm.MessageChanged.value = true;
    theForm.submit();
}
function ConfirmDelDisplayName(txtCurrDispNameBoxId,txtLastDispNameBoxId) 
{
    var txtCurrDispNameBox = document.getElementById(txtCurrDispNameBoxId);
    var txtLastDispNameBox = document.getElementById(txtLastDispNameBoxId);
    if (txtCurrDispNameBox != 'undefined' && txtLastDispNameBox != 'undefined') {
        if (txtLastDispNameBox.value != '' && txtLastDispNameBox.value != null) {
            if (txtCurrDispNameBox.value == '' || txtCurrDispNameBox.value == null) {
                return confirm('Are you sure you want to delete the display name');
            }
        }
   }
   return true;
}
