//g_rootpath is generated by Peak to locate the application root path
var g_ProductCallOutAddressChange = false;

function Products_ProductDetail(mid, prdId)
{	
	var url =  g_ProductsPath + "ProductDetails.aspx?productid=" + prdId + "&prodtyp=product";
	Peak_PopupPage(null, url, 0, mid, "ProductDetail", 500, 700, 10, 10,true,false, true);
}
function Products_RelatedProduct(mid, prdId)
{
    var url = g_ProductsPath + "RelatedProduct.aspx?productID=" + prdId ;
    Peak_RedirectPage(url,0,mid);

}
function redirect(linkid) {
    window.opener.location.href = linkid;
    window.close();
}

function redirectpopup(linkid) {    
    if (window.opener.location.href.indexOf("ProductDetails") > -1) {
        window.opener.opener.location.href = linkid;
        window.opener.close();
        window.close();
    }
    else {
        window.opener.location.href = linkid;
        window.close();
    }
}

function Products_ResetAdvanceSearchFilters(div1ID)
{
    document.getElementById("txtAuthor").value = "";    
    document.getElementById("DropDownListCategory").selectedIndex = 0;
    document.getElementById("DropDownListMediaType").selectedIndex = 0;
    document.getElementById("txtTitle").value = "";
    document.getElementById("txtKeyword").value = "";
    document.getElementById(div1ID).style.display = "block";
}

function Products_Validate(form)
{
     if (form.txtQty.value == "")
     {
        alert("Quantity field can not be blank");
        return false;
     }
     return true;
}


function Validate_Textbox()
{
    var searchID = document.getElementById("txtSearch").value
     if (searchID.trim == "" || searchID.value== null)
          { 
           alert("Search text is required");
            return false;
          }
         else
           return true;


}
function Products_ValidateQuantity(id,qty, backOrderStatus,ItemQuantityInCart)
{
    
    var qid = document.getElementById(id).value ;
    
    var sid=0;
    
    if (qid == null || trim_prod(qid) == "" || qid == 0 || isNumeric(qid) == false) {    
        sid= 0;
    }
    else {
        sid=parseInt(qid);        
    }
    
    
    if (sid<=0)
    {
        alert("Please enter a numeric quantity for this item");
        return false;
    }
    else if((sid > 0 && sid < parseInt(qty)) && backOrderStatus != "N") 
    {    
    
       return true;
    }
    else if (sid > parseInt(qty) || parseInt(ItemQuantityInCart) >= parseInt(qty))
    {
       
       if (backOrderStatus == "N")
       {
           alert("Item discontinued, availability limited to quantity in stock.");
           return false;
       }
        else 
        {            
            return true;
        }
    }
    
}

function isNumeric(strString)
//  check for valid numeric value
 {
   var strValidChars = "0123456789";
   var strChar;
   var blnResult = true;

  // if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
   }

function Products_CollapseExpandAdvancedSearch(ObjImage,ObjDiv)
{	
	var objImageSource = document.images[ObjImage.id].src;
    var pos = objImageSource.indexOf("Plus");       
    if(pos > -1)
    {
        document.images[ObjImage.id].src = g_rootpath +'/WD/Products/images/Minus.gif';
        document.getElementById(ObjDiv).style.display ='';
    }
    else
    {
        document.images[ObjImage.id].src = g_rootpath +'/WD/Products/images/Plus.gif';
        document.getElementById(ObjDiv).style.display ='none';
    }

}
function Products_ValidSearchText(source, args)
{
    var searchTextBox = document.getElementById(source.txtKeywordId);
    var mediaType = document.getElementById(source.ddlMediaType);
    var catagoryType = document.getElementById(source.ddlCategoryType);
    var btnAdvSearch = document.getElementById(source.btnAdvSearch);
    
    if(((trim(searchTextBox.value) == "") || (trim(searchTextBox.value).length < 3)) && (mediaType.selectedIndex==0 && catagoryType.selectedIndex==0))
    {
       args.IsValid = false;
       Page_IsValid = false;
       return false;
    }
    else
    {
      args.IsValid = true;
      Page_IsValid = true;
      return true;
    }
}

//temporary.  following functions should be moved to central area
function ltrim_prod(str) { 
	                for(var k = 0; k < str.length && isWhitespace_prod(str.charAt(k)); k++);
	                return str.substring(k, str.length);
                }
                function rtrim_prod(str) {
	                for(var j=str.length-1; j>=0 && isWhitespace_prod(str.charAt(j)) ; j--) ;
	                return str.substring(0,j+1);
                }
                function trim_prod(str) {
	                return ltrim_prod(rtrim_prod(str));
                }
                function isWhitespace_prod(charToCheck) {
	                var whitespaceChars = " \t\n\r\f";
	                return (whitespaceChars.indexOf(charToCheck) != -1);
                }
                
                
function Products_TextSearchSubmit(ev,btnId)
{
    var charCode = (ev.charCode) ? ev.charCode : ((ev.which) ? ev.which : ev.keyCode);                       
    if(charCode==13)
    {               
        arg  = document.getElementById(btnId).href;
        window.location = arg;
        return true;
    }
}

function Products_HideAdvanceSearchButton(btnId)
{
    var searchButton = document.getElementById(btnId)
    searchButton.style.display = (Page_IsValid) ? "none" : "";
}

//**** CallOut Functions ****
function ProdCallOuts_Add(gridid, mid, mode) 
{
    var page = g_ProductsPath + "CallOutEdit.aspx";
    var img = g_ProductsImagesPath + "callouts.gif";
    //Peak_OpenPage(grid, page, id, mid, title/field, img, height, width, mode, top, left, showscroll, showmenu, resizeable)
    Peak_OpenPage(gridid, page, 0, mid, "New CallOut", img, 600, 700, mode, true, true, true);
}

function ProdCallOuts_Edit(gridid, mid, mode) 
{
    var page = g_ProductsPath + "CallOutEdit.aspx";
    var img = g_ProductsImagesPath + "callouts.gif";
    //Peak_OpenPage(grid, page, id, mid, title/field, img, height, width, mode, top, left, showscroll, showmenu, resizeable)
    Peak_OpenPage(gridid, page, -1, mid, "Title", img, 600, 700, mode, true, true, true);
}

function ProdCallOuts_Update(addrChange) 
{
    if (g_ProductCallOutAddressChange) 
    {
        addrChange.value = "true";
        return confirm('The address has changed, would you like to create a One-Time Address?');
    }
    return true;
}

function ProdCallOuts_PlaceOrder(addrChange) 
{
    if (!confirm('No changes will be allowed after placing an order. Continue?'))
        return false;
    if (g_ProductCallOutAddressChange) 
    {
        addrChange.value = "true";
        return confirm('The address has changed, would you like to create a One-Time Address?');
    }
    return true;
}

function ProdCallOuts_AddItem(gridid, comboid) 
{
    var wc = ISGetObject(comboid);
    if (wc.Value.length > 0) 
    {
        var grid = ISGetObject(gridid);
        var newRow = grid.RootTable.NewRow(); // create new row object
        var cells = newRow.GetCells(); // get WebGridCell collection
        cells.GetNamedItem("PRODUCTID").SetText(wc.Value, true);
        newRow.Update(); // insert new record
    }
    else
        alert("No product selected");
}

