﻿// JScript File

var Rank = -1;
var MaxRank = -1;
var Custfailed = false;
var CustPrice = 0;
var PlotArray = new Array();

function RestrictShipPopup(link)
{
    window.open(link,null, 'height=300, width=500, status=yes, toolbar=no, menubar=no, location=no, scrollbars=yes'); void('');
}

function Toggle(node)
{
    var divArray = node.getElementsByTagName("img");

    switch (node.id)
    {
        case "proddesc":
        element = document.getElementById('divproddesc').style;
        break;

        case "specification":
        element = document.getElementById('divspecification').style;
        break;

        case "resources":
        element = document.getElementById('divresources').style;
        break;

        case "manufacturerinfo":
        element = document.getElementById('divmanufacturerinfo').style;
        break;

        case "similaritems":
        element = document.getElementById('divsimilaritems').style;
        break;

        case "customerdiscussion":
        element = document.getElementById('divcustomerdiscussion').style;
        break;
        
        case "engravingitems":
        element = document.getElementById('divengravingitems').style;
        break;
    }

    if (element.display == "block")
    {
        element.display = "none";
        divArray[0].src = "Images/expand.jpg";
        divArray[0].alt = "Expand";
    }
    else
    {
        element.display = "block";
        divArray[0].src = "Images/collapse.jpg";
        divArray[0].alt = "Collapse";
    }
}

function ToggleCheck(node)
{
   if(node.value == 0)
   {
      node.checked = true;
      node.value = 1;
   }
   else if(node.value == 1)
   {
      node.checked = false;
      node.value = 0;
   }
      
}

function ToggleCustDiscussionDesc(node)
{
    elementshort = document.getElementById('divdescshort' + node.id).style;
    elementfull = document.getElementById('divdescfull' + node.id).style;
    if (elementfull.display == "block")
    {
        elementfull.display = "none"
        elementshort.display = "block"
        node.alt = "Expand";
        
    }
    else
    {
        elementfull.display = "block"
        elementshort.display = "none"
        node.alt = "Collapse";
    }

}

function ValidateQty(node, position, cartlimit, minqty) 
{
    if (node.value == '') return;
    if (node.value < minqty) 
    {
        if (position == 0) {
            node.value = minqty;
            document.getElementById('divQtyMin').style.display = 'block';
        }
        else {
            node.value = minqty;
            document.getElementById('gcdivminqty' + position).style.display = 'block';
        }
    }
    else if (node.value > cartlimit && cartlimit != -1) 
    {
        if (position == 0)
            document.getElementById('divCartLimit').style.display = 'block';
        else
            document.getElementById('gcdivcartlimit' + position).style.display = 'block';
    }
    else
    {
        if (position == 0) {
            document.getElementById('divQtyMin').style.display = 'none';
            document.getElementById('divCartLimit').style.display = 'none';
        }
        else {
            document.getElementById('gcdivcartlimit' + position).style.display = 'none';
            document.getElementById('gcdivminqty' + position).style.display = 'none';
        }
    }
}

function Popup(node)
{
    if(node.id.substring(0,1) == 'T')
    {
        var id = 'DT' + node.id.substring(1);
        ToolTip_Show(node, document.getElementById(id).innerHTML, '');
    }
    else if(node.id.substring(0,1) == 'R')
    {
        id = 'DR' + node.id.substring(1);
        ToolTip_Show(node, document.getElementById(id).innerHTML, '');
    }
}


function ToolTip_Show(zxcobj,zxcdesc,zxcimg)
{
    intIndexOfMatch = 0;
    var zxctxt;
    
    if(zxcdesc != '' || zxcimg != '')
    {
        while (intIndexOfMatch != -1)
        {
            // Relace out the current instance.
            zxcdesc = zxcdesc.replace("¿", "'");
            // Get the index of any next matching substring.
            intIndexOfMatch = zxcdesc.indexOf( "¿" );
        }
        
       if(zxcimg == '')
         zxctxt = "<table><tr><td>" + zxcdesc + "</td></tr></table>";
       else
        zxctxt = "<table><tr valign='top'><td><img alt='' src='" + zxcimg + "'/>" + "</td><td>" + zxcdesc + "</td></tr></table>";
        
        zxcobj.tt=new zxcOOPToolTip(zxcobj,zxctxt);
        clearTimeout(zxcobj.tt.to);
        zxcobj.tt.setTimeOut('show()',0); 
    }
}

function zxcOOPToolTip(zxcobj,zxctxt)
{
    var tooltip = document.getElementById('divtooltip'); 
    tooltip.innerHTML = "<div style='text-align:right;'><img src='Images/close.gif' alt='' onclick='Tooltip_Hide()' /></div>" +   zxctxt;
    var leftPos = (Tooltip_getLeftPos(zxcobj) + zxcobj.offsetWidth);
    var topPos = Tooltip_getTopPos(zxcobj);
    tooltip.style.left = leftPos + 'px';
    tooltip.style.top = topPos + 'px';
    
    this.tt = tooltip;
    this.ref='zxcooptt';
    window[this.ref]=this;
    this.to=null;
}


zxcOOPToolTip.prototype.show=function()
{
    //zxcStyle(this.tt,{visibility:'visible'});
    this.tt.style.display = 'block';
}

zxcOOPToolTip.prototype.hide=function()
{
    //zxcStyle(this.tt,{visibility:'hidden'});
    this.tt.style.display = 'none';
}

zxcOOPToolTip.prototype.setTimeOut=function(zxcf,zxcd)
{
    this.to=setTimeout('window.'+this.ref+'.'+zxcf,zxcd);
}

function Tooltip_getTopPos(node)
{		
  var returnValue = node.offsetTop;
  while((node = node.offsetParent) != null)
  {
  	if(node.tagName!='HTML')returnValue += node.offsetTop;
  }
  return returnValue;
}

function Tooltip_getLeftPos(node)
{
  var returnValue = node.offsetLeft;
  while((node = node.offsetParent) != null)
  {
  	if(node.tagName!='HTML')returnValue += node.offsetLeft;
  }
  return returnValue;
}

function Tooltip_Hide()
{
    if(document.getElementById('divtooltip') != null)
    {
       document.getElementById('divtooltip').style.display = 'none';
    }
}


function CustDiscussionOnClick(itemno, pgindex)
{
    MC.ENET.ItemService.GetCustDiscussion(itemno, pgindex, OnCustDiscussionSucceeded, OnCustDiscussionFailed); 
    return false;  	
}

function OnCustDiscussionFailed()
{
}
function OnCustDiscussionSucceeded(result)
{
   ele = document.getElementById('divcustomerdiscussion');
   ele.innerHTML = result;
}

function DropDownListCompSelected(parentitemno, node, sourcecode)
{
   var pricedisplay = false;
   var pricematchid = 0;
   
   var value = node.options[node.selectedIndex].value;
   var title = node.options[node.selectedIndex].title; 
   var index = title.indexOf('¿', 0);

   if(index >= 0)
   {
       pricedisplay = title.substring(0,index);
       pricematchid = title.substring(index + 1);
   }
   
     
    for (row = document.getElementById("tblListComp").rows[0]; row != null; row = row.nextSibling)
    {
        if (row.nodeType == 1)
        {
             for (cell = row.firstChild; cell != null; cell = cell.nextSibling)
             {
                if (cell.nodeType == 1)
                {
                   for(ctl = cell.firstChild; ctl != null; ctl = ctl.nextSibling)
                   {
                        if(ctl.name == 'crdcomp')
                        {
                           if(ctl.value == value)
                           {
                              ctl.checked = true;
                              break;
                           }
                        }
                   }
                   
                }
             }
        }
    }
    ListCompSelectionChange(parentitemno, value, sourcecode, pricedisplay, pricematchid);
    
}

function RadioListCompSelected(parentitemno, node, sourcecode, pricedisplay, pricematchid)
{
    var value = node.value;
    
    var ddvalues = document.getElementById("ddlistcomp").options;
    for(i = 0; i < ddvalues.length; i++)
    {
      if(ddvalues[i].value == value)
      {
         ddvalues[i].selected = true;
      }
    }
    
    ListCompSelectionChange(parentitemno, value, sourcecode, pricedisplay, pricematchid);

}

function ListCompSelectionChange(parentitemno, selectedcompitemno, sourcecode, pricedisplay, pricematchid)
{
    MC.ENET.ItemService.GetSelectedListComp(parentitemno, selectedcompitemno, sourcecode, pricedisplay, pricematchid, OnListCompSelectionChangeSucceeded, OnListCompSelectionChangeFailed); 
    return false;  	
}

function OnListCompSelectionChangeFailed()
{
}
function OnListCompSelectionChangeSucceeded(result) {
    if (result != null) {
        for (row = document.getElementById("tblitem").rows[0]; row != null; row = row.nextSibling) {
            if (row.nodeType == 1) {
                for (cell = row.firstChild; cell != null; cell = cell.nextSibling) {
                    if (cell.nodeType == 1) {
                        for (ctl = cell.firstChild; ctl != null; ctl = ctl.nextSibling) {
                            if (ctl.id != null && ctl.id != '') {
                                if (ctl.id.indexOf("segshortdesc", 0) != -1)
                                    ctl.innerHTML = result[0];

                                else if (ctl.id.indexOf("segresourcepromo", 0) != -1) {
                                    if (result[1] != null && result[1] != '') {
                                        ctl.innerHTML = result[1];
                                        ctl.style.display = "block";
                                    }
                                    else
                                        ctl.style.display = "none";
                                }

                                else if (ctl.id.indexOf("segimage", 0) != -1)
                                    ctl.innerHTML = result[2];

                                else if (ctl.id.indexOf("segranking", 0) != -1)
                                    ctl.innerHTML = result[3];

                                else if (ctl.id.indexOf("segresources", 0) != -1) {
                                    if (result[4] != null && result[4] != '') {
                                        ctl.innerHTML = result[4];
                                        ctl.style.display = "block";
                                    }
                                    else
                                        ctl.style.display = "none";
                                }

                                else if (ctl.id.indexOf("segmanfinfo", 0) != -1) {
                                    if (result[5] != null && result[5] != '') {
                                        ctl.innerHTML = result[5];
                                        ctl.style.display = "block";
                                    }
                                    else
                                        ctl.style.display = "none";
                                }

                                else if (ctl.id.indexOf("segqty", 0) != -1)
                                    ctl.innerHTML = result[6];

                                else if (ctl.id.indexOf("segcustomization", 0) != -1) {
                                    if (result[7] != null && result[7] != '') {
                                        ctl.innerHTML = result[7];
                                        ctl.style.display = "block";
                                    }
                                    else {
                                        ctl.innerHTML = "";
                                        ctl.style.display = "none";
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}


function VKitSelectionChange(parentitemno, pricedisplay, pricematchid, sourcecode) 
{
    var vkitcompItemNos = '';

    for (row = document.getElementById("tbQty").rows[0]; row != null; row = row.nextSibling)
    {
        if (row.nodeType == 1)
        {
             for (cell = row.firstChild; cell != null; cell = cell.nextSibling)
             {
                if (cell.nodeType == 1)
                {
                   for(ctl = cell.firstChild; ctl != null; ctl = ctl.nextSibling)
                   {
                        if(ctl.name == 'ddvkitcomp')
                        {
                           vkitcompItemNos += '¿' + ctl.options[ctl.selectedIndex].value;
                        }
                   }
                   
                }
             }
        }
    }
    
    if(vkitcompItemNos != '')
    {
        MC.ENET.ItemService.GetVKitCompPriceandAvail(parentitemno, vkitcompItemNos, pricedisplay, pricematchid, sourcecode, OnVKitSelectionChangeSucceeded, OnVKitSelectionChangeFailed); 
        return false;  	

    }
}

function OnVKitSelectionChangeFailed()
{
}
function OnVKitSelectionChangeSucceeded(result)
{
    for (row = document.getElementById("tblitem").rows[0]; row != null; row = row.nextSibling)
    {
        if (row.nodeType == 1)
        {
             for (cell = row.firstChild; cell != null; cell = cell.nextSibling)
             {
                if (cell.nodeType == 1)
                {
                   for(ctl = cell.firstChild; ctl != null; ctl = ctl.nextSibling)
                   {
                       if(ctl.id != null && ctl.id != '')
                       {
                           if(ctl.id.indexOf("segprice", 0) != -1)
                           {
                              ctl.innerHTML = result;
                              setPostBackPrice();
                           }
                           
                       }
                   }
                   
                }
             }
        }
    }

}

function StyleSelectionChange(parentitemno, pricedisplayandpricematchid, sourcecode, errmsg) 
{
    var selectedpropvals = '';
    var selectedpropText = '';
    
    for (row = document.getElementById("tbQty").rows[0]; row != null; row = row.nextSibling)
    {
        if (row.nodeType == 1)
        {
             for (cell = row.firstChild; cell != null; cell = cell.nextSibling)
             {
                if (cell.nodeType == 1)
                {
                   for(ctl = cell.firstChild; ctl != null; ctl = ctl.nextSibling)
                   {
                        if(ctl.name == 'ddstyles')
                        {
                           selectedpropvals += '¿' + ctl.options[ctl.selectedIndex].value;
                           selectedpropText += ' ' + ctl.options[ctl.selectedIndex].title;
                        }
                   }
                   
                }
             }
        }
    }
    
    if(selectedpropvals != '')
    {
        MC.ENET.ItemService.GetSelectedStyleComp(parentitemno, selectedpropvals, selectedpropText, pricedisplayandpricematchid, sourcecode, errmsg, 0, 0, OnStyleSelectionChangeSucceeded, OnStyleSelectionChangeFailed); 
        return false;  	

    }
}

function IntStyleSelectionChange(parentitemno, pricedisplayandpricematchid, sourcecode, errmsg, key) {
    var selectedpropvals = '';
    var selectedpropText = '';

    for (row = document.getElementById("tbintstyle").rows[0]; row != null; row = row.nextSibling) {
        if (row.nodeType == 1) {
            for (cell = row.firstChild; cell != null; cell = cell.nextSibling) {
                if (cell.nodeType == 1) {
                    for (ctl = cell.firstChild; ctl != null; ctl = ctl.nextSibling) {
                        if (ctl.name == 'ddstyles') {
                            selectedpropvals += '¿' + ctl.options[ctl.selectedIndex].value;
                            selectedpropText += ' ' + ctl.options[ctl.selectedIndex].title;
                        }
                        else if (ctl.nodeName == "UL") {

                            if (key > 0) {
                                for (i = 0; i < ctl.childNodes.length; i++) {
                                    child = ctl.childNodes.item(i);
                                    if (child.id == key) {
                                        selectedpropvals += '¿' + child.id;
                                        child.childNodes[0].style.borderWidth = '3px';
                                    }
                                    else {
                                        child.childNodes[0].style.borderWidth = '1px';
                                    }
                                }
                            }
                            else {
                                for (i = 0; i < ctl.childNodes.length; i++) {
                                    child = ctl.childNodes.item(i);
                                    if (child.childNodes[0].style.borderWidth.indexOf('3px', 0) != -1) {
                                        selectedpropvals += '¿' + child.id;
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    var topid = 0;
    var leftid = 0;

    list = document.getElementById("topfilter")
    for (i = 0; i < list.childNodes.length; i++) {
        child = list.childNodes.item(i);
        if (child.innerHTML.indexOf("_on", 0) > 0) {
            topid = child.id.substring(1);
            break;
        }
    }
    list = document.getElementById("leftfilter")
    for (i = 0; i < list.childNodes.length; i++) {
        child = list.childNodes.item(i);
        if (child.innerHTML.indexOf("_on", 0) > 0) {
            leftid = child.id.substring(1);
            break;
        }
    }


    if (selectedpropvals != '') {
        MC.ENET.ItemService.GetSelectedStyleComp(parentitemno, selectedpropvals, selectedpropText, pricedisplayandpricematchid, sourcecode, errmsg, topid, leftid, OnStyleSelectionChangeSucceeded, OnStyleSelectionChangeFailed);
        return false;

    }
}

function OnStyleSelectionChangeFailed()
{
}
function OnStyleSelectionChangeSucceeded(result) {
    if (result.length == 1) {
        if (document.getElementById("ctl00_ContentPlaceHolder1_segintstyles").innerHTML != '') {
            err = document.getElementById("segstyleerr2");
        }
        else {
            err = document.getElementById("segstyleerr1");
        }
        err.innerHTML = result[0];
        err.style.display = "block";
        err.style.background = 'ivory';
        err.style.color = 'red';

    }
    else {
        document.getElementById("segstyleerr1").style.display = 'none';
        document.getElementById("segstyleerr2").style.display = 'none';
        
        for (row = document.getElementById("tblitem").rows[0]; row != null; row = row.nextSibling) {
            if (row.nodeType == 1) {
                for (cell = row.firstChild; cell != null; cell = cell.nextSibling) {
                    if (cell.nodeType == 1) {
                        for (ctl = cell.firstChild; ctl != null; ctl = ctl.nextSibling) {
                            if (ctl.id != null && ctl.id != '') {
                                if (ctl.id.indexOf("segshortdesc", 0) != -1)
                                    ctl.innerHTML = result[0];

                                else if (ctl.id.indexOf("segimage", 0) != -1) {
                                    ctl.innerHTML = result[1];
                                }

                                else if (ctl.id.indexOf("segranking", 0) != -1)
                                    ctl.innerHTML = result[2];

                                else if (ctl.id.indexOf("segprice", 0) != -1) {
                                    ctl.innerHTML = result[3];
                                    setPostBackPrice();
                                }

                                else if (ctl.id.indexOf("segqty", 0) != -1)
                                    ctl.innerHTML = result[4];

                                else if (ctl.id.indexOf("segcustomization", 0) != -1) {
                                    if (result[5] != null && result[5] != '') {
                                        ctl.innerHTML = result[5];
                                        ctl.style.display = "block";
                                        if (currencydisplay) {
                                            var label = document.getElementById("custpriceperchar");
                                            if (label != null)
                                                label.innerHTML = getInitPriceSaving(label.innerHTML);
                                            label = document.getElementById("custprice");
                                            if (label != null)
                                                label.innerHTML = getInitPriceSaving(label.innerHTML);
                                        }
                                    }
                                    else {
                                        ctl.innerHTML = "";
                                        ctl.style.display = "none";
                                    }
                                }
                                else if (ctl.id.indexOf("segintstyles", 0) != -1) {
                                    if (result[6] != '' && result[6] != null) {
                                        ctl.innerHTML = result[6];
                                        ctl.style.display = "block";
                                    }
                                    else
                                        ctl.style.display = "none";
                                }
                                else if (ctl.id.indexOf("seggrpitems", 0) != -1) {
                                    if (result[7] != '' && result[7] != null) {
                                        ctl.innerHTML = result[7];
                                        ctl.style.display = "block";
                                        SetDraggables();
                                    }
                                    else
                                        ctl.style.display = "none";
                                }
                            }
                        }
                    }
                }
            }
        }
        if (document.getElementById("ctl00_ContentPlaceHolder1_seggrpitems").innerHTML != '') {
            SetDroppables();
            SetPreloadedIcons();
            //calculate price including attached icons.
            CalIntItemPrice();

        }
    }
}

function EngSelectionChange(retailprice) 
{
    var engWebPrice = 0.0;

    for (row = document.getElementById("tbEngraving").rows[0]; row != null; row = row.nextSibling)
    {
        if (row.nodeType == 1)
        {
             for (cell = row.firstChild; cell != null; cell = cell.nextSibling)
             {
                if (cell.nodeType == 1)
                {
                   for(ctl = cell.firstChild; ctl != null; ctl = ctl.nextSibling)
                   {
                        if(ctl.name == 'ddengvkitcomp')
                        {
                           engWebPrice += ctl.options[ctl.selectedIndex].title;
                        }
                   }
                   
                }
             }
        }
    }
    
    MC.ENET.ItemService.EngItemPriceFormat(engWebPrice, OnEngPriceFormatSucceeded, OnEngPriceFormatFailed); 
    return false;  	
    
}

function OnEngPriceFormatFailed()
{
}
function OnEngPriceFormatSucceeded(result)
{
    var element = document.getElementById("divengprice");
    element.innerHTML = "<label class='ourprice4'>" + result + "</label>"
}

function OnGetNoAttachmentErrFailed() {
}
function OnGetNoAttachmentErrSucceeded(result) {
    var element = document.getElementById("segstyleerr1");
    element.innerHTML = result;
    element.style.display = 'block';
}

function AddToButtonClick(itemtype, id, itmno, minqty)
{
    var row;
    var cell;
    var ctl; 
    var itemno = '';
    var qty = '';
    var addtoorderitems = '';
    var webprice = 0;
    var surchrg = 0;
    var stock = false;
    var priceOvrdFlag = 0;
    var priceOvrdAmt = 0;
    var priceMatchId = 0;
    var customization = '';
    var vkitcompItemNos = '';
    var engitemno = '';
    var engqty = 1;
    var engitemtype = 1;
    var engcustomization = '';
    var engvkits = '';
    var checked = false;
    var MultiArray = new Array();
    var selected = false;
    var reg = new RegExp("[1-9]");
    var CustText = '';

    // check if interactive items has atleast one icon attachment
    if (document.getElementById("divimg") != null) {

        for (i = 0; i < PlotArray.length; i++) {
            if (PlotArray[i][2] != '') {
                selected = true;
                break;
            }
        }
        if (!selected) {
            MC.ENET.ItemService.GetNoAttachmentErr(OnGetNoAttachmentErrSucceeded, OnGetNoAttachmentErrFailed);
            return false;
        }
    }

    selected = false;
   // final check for required customization and calculate cust price.
    if (document.getElementById("tblCust") != null) {
        CalculateCustPrice();

        if (Custfailed)
            return;
    }
    
    if(id == "CART" || id == "WISHLIST")
    {
        itemno = document.getElementById("itemno").title;
        qty = document.getElementById("txtQuantity").value;
    }
    if(id == "EKITCART") // used in the case of EKITS 'Buy together and save'
    {
       itemno = itmno
       qty = minqty;
    }
    
 //get Recommended ItemNos if checked to add to cart
  
  if(id == "CART" || id == "GRIDCART" || id == "EKITCART")  
  {

      if(document.getElementById("tblRecItems") != null)
      {
            for (row = document.getElementById("tblRecItems").rows[0]; row != null; row = row.nextSibling)
            {
                if (row.nodeType == 1)
                {
                     for (cell = row.firstChild; cell != null; cell = cell.nextSibling)
                     {
                        if (cell.nodeType == 1)
                        {
                           for(ctl = cell.firstChild; ctl != null; ctl = ctl.nextSibling)
                           {
                                if(ctl.name == 'ckaddrectoorder')
                                {
                                    if(ctl.value == 1)
                                    {
                                        addtoorderitems += '¿' + ctl.id;
                                    }
                                }
                           }
                           
                        }
                     }
                }
            }
      }
       
        
       //get TopSelling ItemNos if checked to add to cart
        
      if(document.getElementById("tblTopSelling") != null)
      {
            for (row = document.getElementById("tblTopSelling").rows[0]; row != null; row = row.nextSibling)
            {
                if (row.nodeType == 1)
                {
                     for (cell = row.firstChild; cell != null; cell = cell.nextSibling)
                     {
                        if (cell.nodeType == 1)
                        {
                           for(ctl = cell.firstChild; ctl != null; ctl = ctl.nextSibling)
                           {
                                if(ctl.name == 'ckaddtoptoorder')
                                {
                                    if(ctl.value == 1)
                                    {
                                        addtoorderitems += '¿' + ctl.id;
                                    }
                                }
                           }
                           
                        }
                     }
                }
            }
        }
  }
  
  
  if(id == "CART" || id == "WISHLIST")
  {
      //get price for list items with multiple comps

      if(document.getElementById("tblListComp") != null)
      {
          
          for (row = document.getElementById("tblListComp").rows[0]; row != null; row = row.nextSibling)
          {
                
                if (row.nodeType == 1)
                {
                     for (cell = row.firstChild; cell != null; cell = cell.nextSibling)
                     {
                        if (cell.nodeType == 1)
                        {
                           for(ctl = cell.firstChild; ctl != null; ctl = ctl.nextSibling)
                           {
                                if(ctl.name == 'crdcomp')
                                {
                                   if(ctl.checked == true)
                                      selected = true;
                                }
                                else if(ctl.id != null && ctl.id != '')
                                {
                                    if(ctl.id.indexOf("cwebprice", 0) != -1)
                                    {
                                       if(selected == true)
                                         webprice = (ctl.innerText || ctl.textContent).trim();
                                    }
                                    else if (ctl.id.indexOf("cexclvatprice", 0) != -1) 
                                    {
                                        if (selected == true)
                                            webprice = (ctl.innerText || ctl.textContent).trim();
                                    }
                                    else if (ctl.id.indexOf("csurchrg", 0) != -1) 
                                    {
                                        if (selected == true)
                                            surchrg = (ctl.innerText || ctl.textContent).trim();
                                    }
                                    else if (ctl.id.indexOf("cexclvatsurchrg", 0) != -1) 
                                    {
                                        if (selected == true)
                                            surchrg = (ctl.innerText || ctl.textContent).trim();
                                    }
                                    else if (ctl.id.indexOf("cpriceoverride", 0) != -1)
                                    {
                                       if(selected == true)
                                       {
                                          priceOvrdFlag = 1;
                                          priceOvrdAmt = ctl.value.trim();
                                          if (!reg.test(priceOvrdAmt)) 
                                          {
                                              ctl.style.color = "red";
                                              ctl.focus();
                                              return;
                                          }
                                       }                            
                                    }
                                    else if (ctl.id.indexOf("cpricematch", 0) != -1)
                                    {
                                       if(selected == true)
                                       {
                                           priceOvrdFlag = 2;
                                           priceOvrdAmt = (ctl.innerText || ctl.textContent).trim();
                                       }
                                    }
                                }

                                else if (ctl.name == 'cpricematchid')
                                {
                                   if(selected == true)
                                   {
                                       priceMatchId = ctl.value;
                                   } 
                                }

                                else if (ctl.name == 'cavail')
                                {
                                   if(selected == true)
                                   {
                                      stock = ctl.value;
                                      selected = false;
                                   }                            
                                }
                           }
                           
                        }
                     }
                }
          }
      }
     
      // get price for non list items and list item with 1 comp
      else
      {
            for (row = document.getElementById("tbprice").rows[0]; row != null; row = row.nextSibling)
            {
                if (row.nodeType == 1)
                {
                     for (cell = row.firstChild; cell != null; cell = cell.nextSibling)
                     {
                        if (cell.nodeType == 1)
                        {
                           for(ctl = cell.firstChild; ctl != null; ctl = ctl.nextSibling)
                           {
                                if(ctl.id == 'webprice')
                                {
                                   webprice = (ctl.innerText || ctl.textContent).trim();
                                }
                                else if (ctl.id == 'exclvatprice') 
                                {
                                    webprice = (ctl.innerText || ctl.textContent).trim();
                                }
                                else if (ctl.id == 'surchrg') 
                                {
                                    surchrg = (ctl.innerText || ctl.textContent).trim();
                                }
                                else if (ctl.id == 'exclvatsurchrg') 
                                {
                                    surchrg = (ctl.innerText || ctl.textContent).trim();
                                }
                                else if (ctl.id == 'avail')
                                {
                                   stock = ctl.value;
                                }
                                else if (ctl.id == 'priceoverride')
                                {
                                   priceOvrdFlag = 1;
                                   priceOvrdAmt = ctl.value.trim();
                                   if (!reg.test(priceOvrdAmt))
                                   {
                                        ctl.style.color = "red";
                                        ctl.focus();
                                        return;
                                   }
                                   
                                }
                                else if (ctl.id == 'pricematch')
                                {
                                   priceOvrdFlag = 2;
                                   priceOvrdAmt = (ctl.innerText || ctl.textContent).trim();
                                }
                                else if (ctl.id == 'pricematchid')
                                {
                                   priceMatchId = ctl.value;
                                }
                                
                           }
                           
                        }
                     }
                }
            }
     }
     
 }

 if (id == "CART") 
 {
      // capture customization
      if (document.getElementById("tblCust") != null) {
          for (row = document.getElementById("tblCust").rows[0]; row != null; row = row.nextSibling) {
              if (row.nodeType == 1) {
                  for (cell = row.firstChild; cell != null; cell = cell.nextSibling) {
                      if (cell.nodeType == 1) {
                          for (ctl = cell.firstChild; ctl != null; ctl = ctl.nextSibling) {
                              if (ctl.name == 'txtCustReq') {
                                  customization += '¿' + ctl.value;
                              }
                              else if (ctl.name == 'txtCustOpt') {
                                  customization += '¿' + ctl.value;
                              }
                              else if (ctl.id == 'custdisptxt') {
                                  CustText = ctl.value;
                              }
                          }
                      }
                  }
              }
          }
      }

      //capture variablekit  

      if (document.getElementById("tbQty") != null) {
          for (row = document.getElementById("tbQty").rows[0]; row != null; row = row.nextSibling) {
              if (row.nodeType == 1) {
                  for (cell = row.firstChild; cell != null; cell = cell.nextSibling) {
                      if (cell.nodeType == 1) {
                          for (ctl = cell.firstChild; ctl != null; ctl = ctl.nextSibling) {
                              if (ctl.name == 'ddvkitcomp') {
                                  if (ctl.options[ctl.selectedIndex].value != 0)
                                      vkitcompItemNos += '¿' + ctl.options[ctl.selectedIndex].value;
                              }
                          }

                      }
                  }
              }
          }
      }

      //capture interactive icons if exists
      if (document.getElementById("divimg") != null) {

          for (i = 0; i < PlotArray.length; i++) {
              if (PlotArray[i][2] != '') {  //img id
                  vkitcompItemNos += '¿' + PlotArray[i][2] + '|' + PlotArray[i][1];
              }
          }
      }

      //capture Engraving
      if(document.getElementById("tbEngraving") != null)
      {
            for (row = document.getElementById("tbEngraving").rows[0]; row != null; row = row.nextSibling)
            {
                if (row.nodeType == 1)
                {
                     for (cell = row.firstChild; cell != null; cell = cell.nextSibling)
                     {
                        if (cell.nodeType == 1)
                        {
                           for(ctl = cell.firstChild; ctl != null; ctl = ctl.nextSibling)
                           {
                                if(ctl.id == 'ckengraving')
                                {
                                   if(ctl.value == 1)
                                      checked = true;
                                }
                                else if (ctl.id == 'eitemno')
                                {
                                   if(checked == true)
                                    engitemno = ctl.value;
                                }
                                else if (ctl.id == 'eitemtype')
                                {
                                   if(checked == true)
                                    engitemtype = ctl.value;
                                }
                                else if (ctl.name == 'txtCustParam')
                                {
                                   if(checked == true)
                                     engcustomization += '¿' + ctl.value;
                                }
                                else if (ctl.name == 'ddengvkitcomp')
                                {
                                   if(checked == true)
                                      engvkits += '¿' + ctl.options[ctl.selectedIndex].value;
                                }
                           }
                           
                        }
                     }
                }
            }
     } 
}

if (id == "GRIDCART" || id == "GRIDWISHLIST") {
    var i = 0; 

    $("#tbgridcomp:td *").each(function() {

        var ctlid = $(this).attr('id');
        var ctlname = $(this).attr('name');

        if (ctlid != null && ctlid != '') {
            if (ctlid.indexOf("gcitemno", 0) != -1) {
                itemno = $(this).attr('title');
            }
            else if (ctlid.indexOf("gcpricematch", 0) != -1) {
                priceOvrdFlag = 2;
                priceOvrdAmt = $(this).html().trim();
            }
            else if (ctlid.indexOf("gcpriceoverride", 0) != -1) {
                priceOvrdFlag = 1;
                priceOvrdAmt = $(this).val().trim();
                if (!reg.test(priceOvrdAmt)) {
                    $(this).css("color", "red");
                    $(this).focus();
                    return;
                }

            }
            else if (ctlid.indexOf("gcwebprice", 0) != -1) {
                webprice = $(this).html().trim();
            }
            else if (ctlid.indexOf("gcexclvatprice", 0) != -1) {
                webprice = $(this).html().trim();
            }
            else if (ctlid.indexOf("gcsurchrg", 0) != -1) {
                surchrg = $(this).html().trim();
            }
            else if (ctlid.indexOf("gcexclvatsurchrg", 0) != -1) {
                surchrg = $(this).html().trim();
            }
            else if (ctlid.indexOf("gctxtQuantity", 0) != -1) {
                qty = $(this).val();
                if (qty > 0) {
                    MultiArray[i] = new Array(10);
                    MultiArray[i][0] = itemno;
                    MultiArray[i][1] = itemtype;
                    MultiArray[i][2] = qty;
                    MultiArray[i][3] = priceOvrdFlag;
                    MultiArray[i][4] = priceOvrdAmt;
                    MultiArray[i][5] = priceMatchId;
                    MultiArray[i][6] = webprice;
                    MultiArray[i][7] = stock;
                    MultiArray[i][8] = surchrg;
                    MultiArray[i][9] = vkitcompItemNos;
                    i++;
                }
                vkitcompItemNos = '';
            }

        }
        else if (ctlname == 'gcitemtype') {
            itemtype = $(this).val();
        }
        else if (ctlname == 'gcpricematchid') {
            priceMatchId = $(this).val();
        }
        else if (ctlname == 'gcavail') {
            stock = $(this).val();
        }
        else if (ctlname == 'ddvkitcomp') {
            if ($(this).val() != '0')
                vkitcompItemNos += '¿' + $(this).val();
        }
    });
}
  
 if(id == "CART" || id == "EKITCART")
 {
     MC.ENET.ItemService.AddItemToCart(itemno, itemtype, qty, addtoorderitems, priceOvrdFlag, priceMatchId, priceOvrdAmt, customization, vkitcompItemNos, engitemno, engqty, engitemtype, engcustomization, engvkits, CustPrice, CustText, OnAddToCartSucceeded, OnAddToCartFailed); 
 }
 else if(id == "GRIDCART")
 {
     MC.ENET.ItemService.AddItemsToCart(MultiArray, addtoorderitems, OnAddToCartSucceeded, OnAddToCartFailed); 
 }
 else if(id == "WISHLIST")
 {
     MC.ENET.ItemService.AddItemToWishList(itemno, itemtype, qty, webprice, surchrg, stock, priceOvrdAmt, CustPrice, OnAddToWishListSucceeded, OnAddToWishListFailed); 
 }
 else if(id == "GRIDWISHLIST")
 {
    MC.ENET.ItemService.AddItemsToWishList(MultiArray, OnAddToWishListSucceeded, OnAddToWishListFailed); 
 }
 return false;  	

}

function OnAddToCartFailed()
{
}
function OnAddToCartSucceeded(result)
{
    
    if (result!=null || result.length>0)
    {
         window.location.href = "CartPage" + result;
    }
    else
         window.location.href = "CartPage.aspx";
}

function OnAddToWishListFailed()
{
}
function OnAddToWishListSucceeded(result)
{
   window.location.href = "Members/WishListUpdate.aspx" ;
}


function ToggleRateIt(node)
{
   var val = node.options[node.selectedIndex].value;
   Rank = val;
   MaxRank = node.length - 1;
   var div = document.getElementById('divcustrating');
   var i ;
   
   if(div != null)
   {
       for (i = 0; i < div.childNodes.length; i++) 
       {
           var child = div.childNodes.item(i);
           if (child.nodeName == "IMG")
           {
                if (val == -1) 
                    child.style.visibility = "hidden";
                else  
                    child.style.visibility = "visible";
                    
                break;
           }
       }
    }
       
}

function RateItOnClick(itemno)
{
    MC.ENET.ItemService.InsertItemOverallRanking(itemno, Rank, MaxRank, OnRateItSucceeded, OnRateItFailed); 
    return false;  	
}
function OnRateItFailed()
{
}
function OnRateItSucceeded(result)
{
   window.location.href = result ;
}

function CalculateCustPrice() {

    PricePerChar = 0;
    PricePerCharThld = 0;
    FlatFee = 0;
    defCurSymbol = '';
    customization = '';
    Custfailed = false;
    ctlreq = null;

    if (document.getElementById("tblCust") != null) {
        for (row = document.getElementById("tblCust").rows[0]; row != null; row = row.nextSibling) {
            if (row.nodeType == 1) {
                for (cell = row.firstChild; cell != null; cell = cell.nextSibling) {
                    if (cell.nodeType == 1) {
                        for (ctl = cell.firstChild; ctl != null; ctl = ctl.nextSibling) {
                            if (ctl.name == 'txtCustReq') {
                                if (ctl.value != null) {
                                    if (trim(ctl.value).length == 0) {
                                        ctl.value = '';
                                        ctl.focus();
                                        Custfailed = true;
                                        ctlreq.style.color = "Red";
                                        ctlreq = null;
                                        return -1;
                                    }
                                    else {
                                        ctlreq.style.color = "Black";
                                        ctlreq = null;
                                        customization += trim(ctl.value);
                                    }
                                }
                                else
                                    return;
                            }
                            else if (ctl.name == 'txtCustOpt') {
                                if (ctl.value != null) {
                                    customization += trim(ctl.value);
                                }
                            }
                            else if (ctl.nodeName == 'LABEL' && ctl.className == 'custreq') {
                                ctlreq = ctl;
                            }
                            else if (ctl.id == 'PricePerChar') {

                                PricePerChar = new Number(ctl.value);
                            }
                            else if (ctl.id == 'PricePerCharThld') {
                                PricePerCharThld = new Number(ctl.value);
                            }
                            else if (ctl.id == 'FlatFee') {
                                FlatFee = new Number(ctl.value);
                            }
                            else if (ctl.id == 'defSymbol') {
                                defCurSymbol = ctl.value;
                            }
                        }
                    }
                }
            }
        }
    }

    CustPrice = FlatFee;
    if (customization.length > 0 && (PricePerChar > 0 || FlatFee > 0)) {
        //calculate price to chars only not to space.
        j = 0;
        temp = new Array();
        for (i = 0; i < customization.length; i++) {
            if (customization[i] != ' ') {
                temp[j] = customization[i];
                j++;
            }
        }

        if (temp.length > PricePerCharThld)
            CustPrice = ((temp.length - PricePerCharThld) * PricePerChar) + FlatFee;

        CustPrice = removeHTMLEntities(setCustCalPrice(defCurSymbol + CustPrice.toFixed(2)));

        var label = document.getElementById('custprice');
        if (label != null) {
            label.innerHTML = CustPrice;
        }
    }

}

function GridVKitSelectionChange(GridHdr, parentitemno, pricedisplay, pricematchid, sourcecode, rowIndex) {
    var vkitcompItemNos = '';

    for (row = document.getElementById("tbgridcomp").rows[0]; row != null; row = row.nextSibling) {
        if (row.nodeType == 1 && row.rowIndex == rowIndex) {
            for (cell = row.firstChild; cell != null; cell = cell.nextSibling) {
                if (cell.nodeType == 1) {
                    for (ctl = cell.firstChild; ctl != null; ctl = ctl.nextSibling) {
                        if (ctl.name == 'ddvkitcomp') {
                            vkitcompItemNos += '¿' + ctl.options[ctl.selectedIndex].value;
                        }
                    }

                }
            }
        }
    }

    if (vkitcompItemNos != '') {
        MC.ENET.ItemService.GridVKitSelectionChange(GridHdr, parentitemno, vkitcompItemNos, pricedisplay, pricematchid, sourcecode, rowIndex, OnGridVKitSelectionChangeSucceeded, OnGridVKitSelectionChangeFailed);
        return false;

    }
}

function OnGridVKitSelectionChangeFailed() {
}
function OnGridVKitSelectionChangeSucceeded(result) {

    for (row = document.getElementById("tbgridcomp").rows[0]; row != null; row = row.nextSibling) {
        if (row.nodeType == 1 && row.rowIndex == result[0]) {
            for (cell = row.firstChild; cell != null; cell = cell.nextSibling) {
                if (cell.nodeType == 1) {
                    if (cell.innerHTML.indexOf('gcavail', 0) > 0) {
                        cell.innerHTML = result[1];
                    }
                    else if (cell.innerHTML.indexOf('tdprice', 0) > 0) {
                        cell.innerHTML = result[2];
                    }
                    else if (cell.innerHTML.indexOf('gctxtQuantity', 0) > 0) {
                        cell.innerHTML = result[3];
                    }
              
                }
            }
        }
    }
}

function TopFilterChange(node, itemno) {

    InteractiveImgID = 0;
    if (document.getElementById("intimgid") != null)
        InteractiveImgID = document.getElementById("intimgid").value;
        
    var list = document.getElementById("topfilter")
    var topid = 0;
    var leftid = 0;

    for (i = 0; i < list.childNodes.length; i++) {
        var child = list.childNodes.item(i);
        if (child == node) {
            child.innerHTML = child.innerHTML.replace("_off", "_on") || child.innerHTML.replace("_Off", "_on");
            topid = child.id.substring(1);
        }
        else {
            child.innerHTML = child.innerHTML.replace("_on", "_off") || child.innerHTML.replace("_On", "_off");
        }
    }
    
   list = document.getElementById("leftfilter")
   for (i = 0; i < list.childNodes.length; i++) {
       child = list.childNodes.item(i);
       if (child.innerHTML.indexOf("_on", 0) > 0 || child.innerHTML.indexOf("_On", 0) > 0) {
           leftid = child.id.substring(1);
           break;
       }
   }

   MC.ENET.ItemService.GetGroupItems(itemno, topid, leftid, InteractiveImgID, OnGetGroupItemsSucceeded, OnGetGroupItemsFailed);
   return false;

}

function LeftFilterChange(node, itemno) {

    InteractiveImgID = 0;
    if (document.getElementById("intimgid") != null)
        InteractiveImgID = document.getElementById("intimgid").value;

    var list = document.getElementById("leftfilter")
    var topid = 0;
    var leftid = 0;

    for (i = 0; i < list.childNodes.length; i++) {
        var child = list.childNodes.item(i);
        if (child == node) {
            child.innerHTML = child.innerHTML.replace("_off", "_on") || child.innerHTML.replace("_Off", "_on");
            leftid = child.id.substring(1);
        }
        else {
            child.innerHTML = child.innerHTML.replace("_on", "_off") || child.innerHTML.replace("_On", "_off");
        }
    }
    
   list = document.getElementById("topfilter")
   for (i = 0; i < list.childNodes.length; i++) {
       child = list.childNodes.item(i);
       if (child.innerHTML.indexOf("_on", 0) > 0 || child.innerHTML.indexOf("_On", 0) > 0) {
           topid = child.id.substring(1);
           break;
       }
   }

   MC.ENET.ItemService.GetGroupItems(itemno, topid, leftid, InteractiveImgID, OnGetGroupItemsSucceeded, OnGetGroupItemsFailed);
   return false;

}

function OnGetGroupItemsFailed() {
}
function OnGetGroupItemsSucceeded(result) {
    if (result != null) {
        var div = document.getElementById("ctl00_ContentPlaceHolder1_seggrpitems");
        if (div != null) {
            div.innerHTML = '';
            div.innerHTML = result;
            SetDraggables();
        }
    }
}

function GetAlternativeImgView(parentitemno, parentitemtype, selecteditemno, source, selectedimgid) {

    var topid = 0;
    var leftid = 0;

    list = document.getElementById("topfilter")
    for (i = 0; i < list.childNodes.length; i++) {
        child = list.childNodes.item(i);
        if (child.innerHTML.indexOf("_on", 0) > 0) {
            topid = child.id.substring(1);
            break;
        }
    }
    list = document.getElementById("leftfilter")
    for (i = 0; i < list.childNodes.length; i++) {
        child = list.childNodes.item(i);
        if (child.innerHTML.indexOf("_on", 0) > 0) {
            leftid = child.id.substring(1);
            break;
        }
    }

    MC.ENET.ItemService.GetAlternativeImgView(parentitemno, parentitemtype, selecteditemno, source, selectedimgid, topid, leftid, OnGetAlternativeImgViewSucceeded, OnGetAlternativeImgViewFailed);
    return false;

}

function OnGetAlternativeImgViewFailed() {
}
function OnGetAlternativeImgViewSucceeded(result) {
    if (result != null) {

        if (result[0] != null || result[0] != '') {
            div = document.getElementById("ctl00_ContentPlaceHolder1_segimage");
            if (div != null) {
                div.innerHTML = '';
                div.innerHTML = result[0];
                SetDroppables();
                SetPreloadedIcons();
                //calculate price including attached icons.
                CalIntItemPrice();
            }
        }

        if (result[1] != null || result[1] != '') {
            div = document.getElementById("ctl00_ContentPlaceHolder1_seggrpitems");
            if (div != null) {
                div.innerHTML = '';
                div.innerHTML = result[1];
                SetDraggables();
            }
        }
    }
}

function SetDroppables() {

    $(document).ready(function() {

        $("#divimg > div").droppable({

            accept: '#draggables:td img',
            drop: function(event, ui) {
                var Isexist = false;

                $(this).html('');
                $(this).css('backgroundColor', 'transparent');

                ui.draggable.attr('title', 'Double click to remove the icons');
                ui.draggable.attr('ondblclick', 'javascript:RemoveImage(this);');
                ui.draggable.ondblclick = function() { RemoveImage(this); };

                $(this).append(ui.draggable.clone().css('width', $(this).css('width')).css('height', $(this).css('height'))).fadeIn();

                //delete the noattachmenterr if exists
                var element = document.getElementById("segstyleerr1");
                element.innerHTML = '';
                element.style.display = 'none';

                for (j = 0; j < PlotArray.length; j++) {
                    if (PlotArray[j] != null) {
                        if (PlotArray[j][0] == $(this).attr('id')) {
                            PlotArray[j][1] = $(this).attr('title');
                            PlotArray[j][2] = ui.draggable.attr('id')
                            PlotArray[j][3] = ui.draggable.attr('src')
                            PlotArray[j][4] = ui.draggable.attr('alt')
                            Isexist = true;
                            break;
                        }
                    }
                }

                if (!Isexist) {
                    k = PlotArray.length;
                    PlotArray[k] = new Array(5);
                    PlotArray[k][0] = $(this).attr('id');
                    PlotArray[k][1] = $(this).attr('title');
                    PlotArray[k][2] = ui.draggable.attr('id')
                    PlotArray[k][3] = ui.draggable.attr('src')
                    PlotArray[k][4] = ui.draggable.attr('alt')
                }

                //calculate price including attached icons.
                CalIntItemPrice();
            }
        });
    });
}
function CalIntItemPrice() {

    attachItemIDs = '';
    for (i = 0; i < PlotArray.length; i++) {
        if (PlotArray[i][2] != '') {  //img id
            attachItemIDs += '¿' + PlotArray[i][2];
        }
    }
    if (attachItemIDs != '') {
        MC.ENET.ItemService.CalIntItemPrice(document.getElementById("itemno").title, attachItemIDs, document.getElementById("source").value, CalIntItemPriceSucceeded, CalIntItemPriceFailed);
        return false;
    }

}
function CalIntItemPriceFailed() {
}
function CalIntItemPriceSucceeded(result) {
    if (result != null) {
        var div = document.getElementById("ctl00_ContentPlaceHolder1_segprice");
        if (div != null) {
            div.innerHTML = '';
            div.innerHTML = result;
        }
    }
}
function SetDraggables() {
    $(document).ready(function() {
        $("#draggables:td img").draggable({
            revert: 'valid',
            cursor: 'move',
            helper: 'clone'
        });
    });
}


function SetPreloadedIcons() {
    var div = document.getElementById("divimg");
    if (div != null) {
        for (i = 0; i < div.childNodes.length; i++) {
            child = div.childNodes.item(i);
            if (child.id.indexOf('plot', 0) != -1) {
                for (j = 0; j < PlotArray.length; j++) {
                    if (PlotArray[j] != null) {
                        if (PlotArray[j][0] == child.id) {
                            if (PlotArray[j][2] != '') {
                                var image = document.createElement('IMG');
                                image.setAttribute('id', PlotArray[j][2]);
                                image.setAttribute('src', PlotArray[j][3]);
                                image.setAttribute('alt', PlotArray[j][4]);
                                image.setAttribute('width', child.style.width);
                                image.setAttribute('height', child.style.height);
                                image.setAttribute('title', 'Double click to remove the icons');
                                image.setAttribute('ondblclick', 'javascript:RemoveImage(this);');
                                image.ondblclick = function() { RemoveImage(this); };
                                child.style.backgroundColor = 'transparent';
                                child.appendChild(image);
                                image.style.width = child.style.width;
                                image.style.height = child.style.height;
                            }
                        }
                    }
                }
            }
        }
    }
}


function RemoveImage(node) {
    id = node.parentNode.id;
    node.parentNode.style.backgroundColor = 'transparent';
    node.parentNode.innerHTML = '';

    for (i = 0; i < PlotArray.length; i++) {
        if (id == PlotArray[i][0]) {
            PlotArray[i][2] = '';
            PlotArray[i][3] = '';
            PlotArray[i][4] = '';
        }
    }

    //calculate price including attached icons.
    attachItemIDs = '';
    for (i = 0; i < PlotArray.length; i++) {
        if (PlotArray[i][2] != '') {  //img id
            attachItemIDs += '¿' + PlotArray[i][2];
        }
    }
    MC.ENET.ItemService.CalIntItemPrice(document.getElementById("itemno").title, attachItemIDs, document.getElementById("source").value, CalIntItemPriceSucceeded, CalIntItemPriceFailed);
    return false;
}

// Removes leading whitespaces
function LTrim(value) {

    var re = /\s*((\S+\s*)*)/;
    return value.replace(re, "$1");

}

// Removes ending whitespaces
function RTrim(value) {

    var re = /((\s*\S+)*)\s*/;
    return value.replace(re, "$1");

}

// Removes leading and ending whitespaces
function trim(value) {

    return LTrim(RTrim(value));

}
