// JScript File ItemPopup.js
// $Revision: 14 $
// [log at end of file]
/*function ReloadItems(btn, showconsprice, hideconsprice)
{
    if (showed == "false")
        showed = "true";
    else
        showed = "false";
    
    window.open('../ui/Tehava_ItemList.aspx?consprice=' + showed, 'itemlistframe');
    window.open('../ui/Tehava_Cart.aspx?consprice=' + showed, 'basketframe');
    if (showed == "true")
        document.getElementById(btn).value = hideconsprice;
    else
        document.getElementById(btn).value = showconsprice;
}*/

function focusSearchField(object)
{
    object.style.backgroundImage = '';
}

function blurSearchField(object, image)
{
    if (object.value == "")
    {
        object.style.backgroundImage = 'url(' + image + ')';
    }
}

function ClearSearchFields()
{
    document.getElementById(btnSubmit).focus();
    document.getElementById(txtGroup).selectedIndex = 0;    
    
    document.getElementById(txtKey).value = '';
    document.getElementById(txtKey).style.backgroundImage = 'url(' + keyimage + ')';
    
    document.getElementById(txtDescription).value = '';
    document.getElementById(txtDescription).style.backgroundImage = 'url(' + descimage + ')';
    
    document.getElementById(txtVendor).value = '';
    document.getElementById(txtVendor).style.backgroundImage = 'url(' + vendorimage + ')';
    
    document.getElementById(txtEAN).value = '';
    document.getElementById(txtEAN).style.backgroundImage = 'url(' + eanimage + ')';
}

function SearchItems()
{
    var query = "";

    var key = document.getElementById(txtKey).value;
    var desc = document.getElementById(txtDescription).value;
    var vendor = document.getElementById(txtVendor).value;
    var ean = document.getElementById(txtEAN).value;
    var groupddl = document.getElementById(txtGroup);
    
    if (groupddl.selectedIndex != 0)
    {
        query += "&Group=" + groupddl.options[groupddl.selectedIndex].value;
    }
    query += "&Description=" + desc;
    query += "&Key=" + key;
    query += "&vendor=" + vendor;
    query += "&EAN=" + ean;
    
    window.open("../ui/Tehava_ItemList.aspx?GetProductsBy=Search" + query, "itemlistframe"); 
}

function AddItemIfPossibleKeyPress(code, key, defq, nameaddition)
{    
	var quantity = document.getElementById("txtQuantity" + nameaddition + key).value;
    if (code == 13)
    {
        if (IsNumeric(quantity))
        {            
            window.open("Tehava_Cart.aspx?key=" + key + "&quantity=" + quantity, 'basketframe');
            document.getElementById("txtQuantity" + nameaddition + key).value = defq;
	        return false;
        }
	    else
	    {
    		return false;
	    }
    }
    else
    {
	    return true;
    }
}

function AddItemIfPossible(key, defq, nameaddition)
{
    var quantity = document.getElementById("txtQuantity" + nameaddition + key).value;
    if (IsNumeric(quantity))
    {
        window.open("Tehava_Cart.aspx?key=" + key + "&quantity=" + quantity, 'basketframe');
        document.getElementById("txtQuantity" + nameaddition + key).value = defq;
        return false;
    }
    else
    {
        return false;
    }
}
function AddItemIfPossibleNotUsed(key)
{
    if (!IsNumeric(document.getElementById('txtQuantity' + key).value))
    {
        return false;
    }
    else
    {
        document.getElementById('hidden_item_key').value = key;
        document.getElementById('hidden_item_quantity').value = document.getElementById('txtQuantity' + key).value;
        document.forms[0].submit();
    }
}

function AddItemIfPossibleFromBoxNotUsed(key)
{
    if (!IsNumeric(document.getElementById('txtQuantityBox' + key).value))
    {
        return false;
    }
    else
    {
        document.getElementById('hidden_item_key').value = key;
        document.getElementById('hidden_item_quantity').value = document.getElementById('txtQuantityBox' + key).value;
        document.forms[0].submit();
    }
}

function AddItemFromPopupIfPossibleNotUsed(key)
{
    if (!IsNumeric(document.getElementById('txtQuantityPopup' + key).value))
    {
        return false;
    }
    else
    {
        document.getElementById('hidden_item_key').value = key;
        document.getElementById('hidden_item_quantity').value = document.getElementById('txtQuantityPopup' + key).value;
        document.forms[0].submit();
    }
}

function GetItem(key)
{    
		//Call script proxy passing the input element data    
		document.getElementById("bufferATLAS").innerHTML = "<div>Loading...</div>";
		//var inputKeyCtrl= document.getElementById('inputKey');
		requestService = ScriptServer.TradePoint.WebServices.WebAdapter.GetItemOverview(
			 key, 
			 true,                       //params
			 OnCompleteItemOverview,                 //Complete event
			 OnTimeoutItemOverview                   //Timeout event
		);
		return false;
 }
function OnCompleteItemOverview(item) 
{              
		document.getElementById("bufferATLAS").innerHTML = CreateItemDiv(item);
		serviceOnComplete();
}
function OnTimeoutItemOverview(result)
{
    serviceOnTimeout();        
}
//--
 function OnCompleteShoppingBasket(item)
 {
		document.getElementById("bufferATLAS").innerHTML = CreateItemDiv(item);
		serviceOnComplete();
 }
 
 function CreateItemDiv(item)
 {
		var htmlString = "";
    
		htmlString += "<table style=\"width:100%;\">";

		htmlString += "<tr>";
		htmlString += "<td valign=\"top\">";
		if(item.ItemInfo)
			 if(item.ItemInfo.SmallImageUrl != "")
				  htmlString += "<img src='"+item.ItemInfo.SmallImageUrl+"' />"; 
		htmlString += "</td>";
    
		htmlString += "<td valign=\"top\">";
		htmlString += "<table>";
		htmlString += "<tr>"; 
		htmlString += "<td valign=\"top\">";
		htmlString += "<b>" + item.ItemInfo.FormattedName +": </b>";
		htmlString += "</td>";

		htmlString += "<td valign=\"top\">";
		htmlString += CreateDiv(item.Name, true)
		htmlString += "</td>";
		htmlString += "</tr>";
    
		htmlString += "<tr>";
		htmlString += "<td valign=\"top\">";
		htmlString += "<b>" + item.ItemInfo.FormattedKey +": </b>";
		htmlString += "</td>";
		htmlString += "<td valign=\"top\">";
		htmlString += CreateDiv(item.Key, true)
		htmlString += "</td>";
		htmlString += "</tr>";
    
	   /* 
      htmlString += "<tr>";
		htmlString += "<td>";
		htmlString += "<b>" + item.ItemInfo.FormattedAmount + ": </b>";
		htmlString += "</td>";
		htmlString += "<td>";
		htmlString += CreateDiv(item.Amount, true)
		htmlString += "</td>";
		htmlString += "</tr>";
		*/
    
		htmlString += "</table>";
		htmlString += "</tr>";
		htmlString += "</table>";
		htmlString += "</td>";
		htmlString += "</tr>";
		htmlString += "</table>";
		htmlString += "<hr/>";

		htmlString += "<table style=\"width:100%;\">";
		for(var i=0; i<item.FieldGroups.length; i++)
		{
			 var fieldGroup = item.FieldGroups[i];
			 //htmlString += "<h3>" +CreateDiv(fieldGroup.Name, true) + "</h3>";
        
			 for(var j=0; j<fieldGroup.Fields.length; j++)
			 {
				  var field = fieldGroup.Fields[j];
				  if (field != null)
				  {
						// This statement tests if the user wants to see Consumer prices, in which case some values must not be shown
						if (!((ShowCustomerPrice == "True") && ((field.Name == "UnitPrice Popup") || (field.Name == "Amount Popup"))))
						{
							 // This if statement only shows certain values if not null or empty.
							 if (field.FormattedValue != "")
							 {
								  htmlString += "<tr>";
								  htmlString += "<td valign=\"top\" style=\"width: 50%;\">";
								  htmlString += CreateDiv(field.FormattedName, true);
								  htmlString += "</td>";
								  htmlString += "<td valign=\"top\" style=\"width: 50%;\">";
								  htmlString += CreateDiv(field.FormattedValue, false);
								  htmlString += "</td>";
								  htmlString += "</tr>";
							 }
						}
				  }
			 }
		}
		htmlString += "</table>";
		
      htmlString += "<" 
      + "input type=\"button\" id=\"btn" + item.Key + "\" " 
		+ "onclick=\"javascript:window.open('" + itemviewpage + "?key=" + item.Key + "', '" + item.Key + "', 'toolbar=no,location=no,scrollbars=yes,resizable=yes,width=600, height=500')\" "
		+ "value=\"" + ExtendedInfo + "\" "
		+ "/>";
    
		return htmlString;
 }
 function CreateDiv(input, caption)
 {
		if(!caption)
			 return "<div>"+input+"</div>";
		else
			 return "<div><b>"+input+"</b></div>";
 }
 
//-- also in TradePoint/B2C/B2C.master 
//function OnTimeoutShoppingBasket(result) 
//{        
//    serviceOnTimeout();        
//}
//    
function Buy(key, num)
{        
    
   //get the id
   if (!IsNumeric(num))
        return;
  
   quantity = num;
             
   currentKeyItemView = key;
   currentQuantityItemView = quantity; 
  
   requestService = ScriptServer.TradePoint.WebServices.WebApplication.AddToShoppingBasket(
         key, 
         quantity, 
         OnCompleteShoppingBasket, 
         OnTimeoutShoppingBasket
   );                                                    
   return false;
} 
           
function IsNumeric(sText)
{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;

   for (i = 0; i < sText.length && IsNumber == true; i++) 
   { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
      {
          IsNumber = false;
     }
   }
   if (IsNumber) 
   {
        if (sText > 0)
        {  
            return true;
        }
   }
   
   alert(invalid_quantity_alert);
   return false;
} 

// end ItemPopup.js
/*
$Log: /TehavaDev/TPPlus/Scripts/ItemPopup.js $
 * 
 * 14    7-09-10 13:57 tl0609
 * 
 * 13    7-09-10 10:46 tl0609
 * 
 * 12    1-09-10 17:06 Ps0207
 * CALL1003400 20100902 PS:
 * - toon meer prijzen in pop-up
 * 
 * 11    18-06-10 10:00 tl0609
*/
