﻿
var gv_sopItemAC=new Array();
var gv_sopItemName=new Array();
var gv_sopItemIndex=0;
var gv_sopItemFocusIndex=0; 
var gv_sopItemNumPerPage=0;
var gv_sopItemCurPage=0; 
var gv_sopItemTotPage=0;
var gv_sopItemEditingData=0;
var gv_sopItemDataCategory=null;
var gv_sopItemEditedName=null;  
var gv_sopItemEditedValue=null; 
var gv_tempTable;
var gv_sopItemRowH=20;
var gv_scrollCurPage=0;
var gv_scrollTotPage=0;
var curPageName=0;
var gv_orderNo=0;
var gv_chooseDetail = null;

function sopItemPageLeft()
{
	if(gv_scrollCurPage<=0) return;

	var selitemcat=document.getElementById('selsopitemcat'); 
	var divtab=document.getElementById('tempTable');
	var tleft=divtab.style.pixelLeft;
	if(tleft>=gv_winw-30) {	
		gv_scrollCurPage--;
		divtab.style.pixelLeft=0;
		if(selitemcat) { 
			if(gv_sopItemEditingData==1) checkSopItemPageEdited(gv_scrollCurPage+1); 
			selitemcat.selectedIndex=gv_scrollCurPage;
			showSopItemDataPage();
		}
		else { 
			gv_sopItemCurPage=gv_scrollCurPage;		
			gv_sopItemIndex=0;
			showSopList();
		}
	}
	else {
		divtab.style.pixelLeft=tleft+30;
		setTimeout("sopItemPageLeft();",75);
	}
}

function sopItemPageRight()
{
	
	var selitemcat =document.getElementById('selsopitemcat');
	var divtab=document.getElementById('tempTable');
	
	var tleft=divtab.style.pixelLeft;
	if(tleft<30-gv_winw) {	
		gv_scrollCurPage++;
		divtab.style.pixelLeft=0;
		if(selitemcat) { //Customer data
			selitemcat.selectedIndex=gv_scrollCurPage;
			if(gv_sopItemEditingData==1) checkSopItemPageEdited(gv_scrollCurPage-1); //Save any edited data to memory
			showSopItemDataPage();			
		}
		else { //customer list
			gv_sopItemCurPage=gv_scrollCurPage;		
			gv_sopItemIndex=0;
			showSopList();
		}
	}
	else {
		divtab.style.pixelLeft=tleft-30;
		setTimeout("sopItemPageRight();",75);
	}
}

  function showSopItemDetail(cmdid)
{
	gv_sopItemEditingData=0;
	getSopItemDetail();
}

function getSopItemDetail()
{
	var ACIndex=gv_sopItemCurPage*gv_sopItemNumPerPage+gv_sopItemIndex;
	var url = gv_rooturl+"?cmd=SopItemDetail&OrderNo="+gv_orderNo+"&index="+ACIndex;
//	if(gv_chooseDetail == "chooseInvItem") {var url = gv_rooturl+"?cmd=InvItemDetail&OrderNo="+gv_orderNo+"&index="+ACIndex;}
	var xmlhttp = newHttpRequest(url,true);
	if( xmlhttp==null ) return;
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4 ) {
			if( xmlhttp.status!=200 ) alert("Connection error");
			else {
				gv_sopItemDataCategory=xmlhttp.responseXML.getElementsByTagName("DataCategory");
				if(gv_sopItemDataCategory.length==0) alert("Cannot load details!");
				else {
					gv_scrollCurPage=0;
					gv_scrollTotPage=gv_sopItemDataCategory.length;
					showSopItemDataPage();
				}
			}
			document.getElementById('idloading').style.visibility='hidden';
		}
	}
	xmlhttp.send(null);
	document.getElementById('idloading').style.visibility='visible';
	gv_sopItemEditedName=null;
	gv_sopItemEditedValue=null;
}

function editSopItemDetail(cmdid)
{
	gv_sopItemEditingData=1;
	getSopItemDetail();
}

function showSopItemDataPage()
{ 
 var widstyle="style='width:"+Math.round(gv_winw/2).toString()+"px' ";
 var tabstr="<table cellpadding='1' cellspacing='0' style='position:absolute;background-color:#98F898;width:"+(gv_winw-4)+"px;left:2px;'>"; 
 var ycur=gv_tempTable.style.pixelTop=gv_device==Nokia ? 28:26;
 var tdh="height='"+gv_sopItemRowH+"px'";  //height='24px'
 var rowJump="<tr><td align=right "+tdh+">Jump to Page</td><td><select class='edtxt' "+widstyle+" id='selsopitemcat' onchange='jumpToSopItemCat();'>";
    for(var ic=0; ic<gv_sopItemDataCategory.length; ic++) {
  var attName0=gv_sopItemDataCategory[ic].attributes.getNamedItem("title");
  if(attName0) {
   var tname=attName0.value.replace('_',' ');
   rowJump+="<option label='"+tname+"'";
   if(ic==gv_scrollCurPage) rowJump+=" selected=true";
   rowJump+=">"+tname+"</option>";
  }
 } 
 rowJump+="</select></td></tr>";
 

 for(var ic=gv_scrollCurPage; ic<gv_sopItemDataCategory.length; ic++) {
  var attName=gv_sopItemDataCategory[ic].attributes.getNamedItem("title"); 
  if(attName==null) continue;
  var rowtxt=ic==gv_scrollCurPage ? rowJump:'';
  rowtxt += "<tr><td colspan=2 align=center "+tdh+" style='color:blue;font-weight:bold;text-decoration:underline;'>"
   +attName.value+"</td></tr>";
   
   ycur+=gv_sopItemRowH;
 var curNode=gv_sopItemDataCategory[ic].firstChild;
  var end_tdr = gv_sopItemEditingData==1 ? "></td></tr>":" ReadOnly></td></tr>"; 

   while( curNode ) 
  {
   var itemName=curNode.nodeName;
   var itemId=ic.toString()+"_"+itemName;
   var ndv=curNode.firstChild;
   var itemVal=getEditedSopItemData(itemId);
   if(itemVal==null) itemVal=ndv?ndv.nodeValue:'';
      if(itemName == "Comment1")
      {
        rowtxt+="<tr><td align=right "+tdh+">Comment 1 &nbsp</td>";
        rowtxt+="<td "+tdh+"><input type='text' class='edtxt' onchange='showSaveSopItemDataButton();' "+widstyle+
        "value='"+itemVal+"' id='"+itemId+"'"+end_tdr;
      }
       else if(itemName == "Comment2")
      {
        rowtxt+="<tr><td align=right "+tdh+">Comment 2 &nbsp</td>";
        rowtxt+="<td "+tdh+"><input type='text' class='edtxt' onchange='showSaveSopItemDataButton();' "+widstyle+
        "value='"+itemVal+"' id='"+itemId+"'"+end_tdr;
      }
       else if(itemName == "Order_Line_Reference")
      {
        rowtxt+="<tr><td align=right "+tdh+">Order Line &nbsp<br>Reference &nbsp</td>";
        rowtxt+="<td "+tdh+"><input type='text' class='edtxt' onchange='showSaveSopItemDataButton();' "+widstyle+
        "value='"+itemVal+"' id='"+itemId+"'"+end_tdr;
      }
       else if(itemName == "Unit_Price_GBP")
      {
        rowtxt+="<tr><td align=right "+tdh+">Unit Price GBP &nbsp</td>";
        rowtxt+="<td "+tdh+"><input type='text' class='edtxt' onchange='showSaveSopItemDataButton();' "+widstyle+
        "value='"+itemVal+"' id='"+itemId+"'"+end_tdr;
      }
       else if(itemName == "Discount")
      {
        rowtxt+="<tr><td align=right "+tdh+">Discount % &nbsp</td>";
        rowtxt+="<td "+tdh+"><input type='text' class='edtxt' onchange='showSaveSopItemDataButton();' "+widstyle+
        "value='"+itemVal+"' id='"+itemId+"'"+end_tdr;
      }

      else 
      {
        rowtxt+="<tr><td align=right "+tdh+">"+itemName.replace('_',' ')+"&nbsp&nbsp"+"</td>";
        rowtxt+="<td "+tdh+"><input type='text' class='edtxt' onchange='showSaveSopItemDataButton();' "+widstyle+
        "value='"+itemVal+"' id='"+itemId+"'"+end_tdr;
      }
   curNode=curNode.nextSibling;
   ycur+=gv_sopItemRowH;
   }
  
  if(ic==gv_scrollCurPage) tabstr+=rowtxt; else break;
  //if(ic==gv_scrollCurPage || ycur+45<gv_winh) tabstr+=rowtxt; else break;
   }
    tabstr += "</table>";
    gv_tempTable.innerHTML = tabstr;
    if(document.getElementById('savesopitemdata')==null) 
    //setSopItemPageScrollCommands('','');
    setSopItemPageScroll();
    setTopTitle('Sop -> SopItem Data');
    doNokiaMenuSopItemPage();
}

function doNokiaMenuSopItemPage()
{
	if(window.widget) {
		window.menu.clear();
		var mBack = new MenuItem('Back',2102);
		mBack.onSelect=backSops;
		window.menu.append(mBack);
	}
}
function setSopItemPageScroll()
{
    var pncur1=document.getElementById('pn_cur3');
    pncur1.innerHTML='Page '+(gv_scrollCurPage+1)+' of '+gv_scrollTotPage;
    var pnleft1=document.getElementById('pn_left3');
    pnleft1.innerHTML=gv_scrollCurPage>0 ? gv_scrollCurPage.toString():'';
    var pnright1=document.getElementById('pn_right3');
    pnright1.innerHTML=gv_scrollCurPage<gv_scrollTotPage-1 ? (gv_scrollCurPage+2).toString():'';    
}

function setSopItemPageScrollCommands(cmd1,cmd2)
{
	document.getElementById('sopitembtn1').innerHTML=cmd1;
	document.getElementById('sopitembtn2').innerHTML=cmd2;
	var sopitemcmd=document.getElementById('sopItemCmds').style;
	var ncmds=cmd1.length<5 ? 0:1;
	if(cmd2.length>5) ncmds++;
	sopitemcmd.pixelWidth=150+50*ncmds;
	sopitemcmd.pixelLeft=gv_winw-sopitemcmd.pixelWidth-1;
}

function jumpToSopItemCat()
{
	var selitemcat = document.getElementById('selsopitemcat');
	if(gv_sopItemEditingData==1) checkSopItemPageEdited(gv_scrollCurPage); //Save any edited data to memory
	gv_scrollCurPage=selitemcat.selectedIndex;	
	curPageName="SopItemDetails";
	showSopItemDataPage();
}

function checkSopItemPageEdited(pagenum)
{
	var itemEle=null;
    for(var ic=pagenum; ic<gv_sopItemDataCategory.length; ic++) 
    {
		var attName=gv_sopItemDataCategory[ic].attributes.getNamedItem("title");	
		if(attName==null) continue;
		var curNode=gv_sopItemDataCategory[ic].firstChild;
		while( curNode ) 
		{
			var ndv=curNode.firstChild;
			var oldv=ndv?ndv.nodeValue:''
			var itemId=ic.toString()+"_"+curNode.nodeName;
			itemEle=document.getElementById(itemId);
			if(itemEle==null) break;
			var newv=itemEle.value;
			if(oldv!=newv) setEditSopItemData(itemId,newv);
			curNode=curNode.nextSibling;
		}
		if(itemEle==null) break;
    }
}

function getEditedSopItemData(itemName)
{
	if(!gv_sopItemEditedName) return null;	
	for(var i=0; i<gv_sopItemEditedName.length; i++) {
		if(gv_sopItemEditedName[i]==itemName) return gv_sopItemEditedValue[i];
	}
}

function setEditSopData(itemName,edValue)
{
	if(!gv_sopEditedName) {
		gv_sopEditedName=new Array();
		gv_sopEditedValue=new Array();
	}
	for(var i=0; i<gv_sopEditedName.length; i++){
		if(gv_sopEditedName[i]==itemName) {	gv_sopEditedValue[i]=edValue; return; }
	}
	gv_sopEditedValue[gv_sopEditedName.length]=edValue;	
	gv_sopEditedName[gv_sopEditedName.length]=itemName;
}

function getEditedSopData(itemName)
{
	if(!gv_sopEditedName) return null;	
	for(var i=0; i<gv_sopEditedName.length; i++) {
		if(gv_sopEditedName[i]==itemName) return gv_sopEditedValue[i];
	}
}

function showSaveSopItemDataButton()
{
	if(document.getElementById('saveitemsopdata')) return;
	setSopItemPageScrollCommands("<input type='button' id='saveitemsopdata' value='Save' onclick='saveSopItemData(0);' />","");
}

function checkSopItemPageEdited(pagenum)
{
	var itemEle=null;
    for(var ic=pagenum; ic<gv_sopItemDataCategory.length; ic++) {
		var attName=gv_sopItemDataCategory[ic].attributes.getNamedItem("title");	
		if(attName==null) continue;
		var curNode=gv_sopItemDataCategory[ic].firstChild;
		while( curNode ) 
		{
			var ndv=curNode.firstChild;
			var oldv=ndv?ndv.nodeValue:''
			var itemId=ic.toString()+"_"+curNode.nodeName;
			itemEle=document.getElementById(itemId);
			if(itemEle==null) break;
			var newv=itemEle.value;
			if(oldv!=newv) setEditSopItemData(itemId,newv);
			curNode=curNode.nextSibling;
		}
		if(itemEle==null) break;
    }
}
function saveSopItemData(cmdid)
{
	checkSopItemPageEdited(gv_scrollCurPage);
	if(gv_sopItemEditedName==null) return;
	var ACIndex=gv_sopItemCurPage*gv_sopItemNumPerPage+gv_sopItemIndex;
	var url=gv_rooturl+"?cmd=SaveSopItemData&OrderNo="+gv_sopItemAC[ACIndex]+"&index="+ACIndex;
	for(var i=0; i<gv_sopItemEditedName.length; i++) 
		url += "&"+gv_sopItemEditedName[i]+"="+gv_sopItemEditedValue[i];	
	var xmlhttp = newHttpRequest(url,true);
	if( xmlhttp==null ) return;
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4 ) {
			if( xmlhttp.status!=200 ) alert("Connection error");
			else if(xmlhttp.responseTEXT=='1') {
				alert("Data saved OK.");
				gv_sopItemEditedName=null;
				gv_sopItemEditedValue=null;
				backSops();
			}
			else alert("Server error!");
			document.getElementById('idloading').style.visibility='hidden';
			document.getElementById('idSaving').style.visibility='hidden';
		}
	}
	xmlhttp.send(null);
	document.getElementById('idloading').style.visibility='visible';
	document.getElementById('idSaving').style.visibility='visible';
}
