﻿function checkLength(from, to, len, submit_btn, e)
{
    if (!e)
        e = window.event;
    var c = e.keyCode ? e.keyCode : (e.which ? e.which : 0);
    if (c == 13)
        document.getElementById(submit_btn).click();
    else if (to && document.getElementById(from).value.length >= len && c != 8 && c != 9 && c != 16)
        document.getElementById(to).focus();
}

function DefaultKeyPress(btn, e)
{
    var button = document.getElementById(btn);

    if (e.keyCode == 13) 
    {
        e.returnValue = false;
        e.cancel = true;
        button.click();
    }
}

function GotoLogin()
{
    window.location.href="/Login.aspx";
}

function ConfirmDelete()
{
    return confirm("Are you sure you want to delete the selected item?");
}

function ConfirmUnlock()
{
    return confirm("Are you sure you want to unlock the selected item?");
}

function getValidForm(formName)
{
	var theform;
	if (window.navigator.appName.toLowerCase().indexOf("microsoft") > -1)
	{
		theform = eval("document." + formName);
	}
	else
	{
		theform = document.forms[formName];
	}
	return theform;
}

//Before postBack
function PostDelete(itemName, line2)
{
	var res;
	if(itemName && (itemName != ""))
	{
		res = 'Are you sure you want to delete "' + itemName + '"?';
	}
	else
	{
		res = 'Are you sure you want to delete this item?';
	}
	if(line2 && (line2 != ""))
	{
		res += "\n" + line2;
	}
	return confirm(res);
}

function PopUp(url, width, height, name)
{
	if(!width) width = 550;
	if(!height) height = 450;
	if(!name) name = null;
	var left = (screen.width/2) - (width/2);
	var top = (screen.height/2) - (height/2);
	var win;
	win = window.open(url, null, 'left=' + left + ',top=' + top + ',width=' + width + ',height=' + height + ',toolbar=0,resizable=0');
	win.focus();
	return(win);
}

function Swap(controlId, onImageUrl, offImageUrl)
{
	if(document.getElementById(controlId).src.indexOf(onImageUrl) > -1)
		document.getElementById(controlId).src = offImageUrl;
	else
		document.getElementById(controlId).src = onImageUrl;		
}

function PopUpFormula(id, customer)
{
	var width = 520;
	var height = 500;
	var name = 'wFormula';
	var url = 'AdminFormulaInput.aspx?id=' + id + "&customer=" + customer;

	var left = (screen.width/2) - (width/2);
	var top = (screen.height/2) - (height/2);
	var win;

	win = window.open(url, null, 'left=' + left + ',top=' + top + ',width=' + width + ',height=' + height + ',toolbar=0,resizable=1,scrollbars=1');
	win.focus();
	return(win);
}

function ConfirmArchive()
{
    return confirm("Are you sure you want to archive this item?");
}

function ViewCardImage(which, alt)
{
    var objImg=document.getElementById&&!document.all;
    crossobj=document.getElementById? document.getElementById("featuredCardDiv") : document.all.showimage
   // if (crossobj.style.visibility=="visible")
   // {
	    crossobj.innerHTML='<img src="'+which+'" alt="'+alt+'">'
	    crossobj.style.visibility="visible"
   // }
}

function SetImageDefault(which)
{
    crossobj=document.getElementById? document.getElementById("featuredCardDiv") : document.all.showimage
    crossobj.innerHTML='<img src="'+which+'">'
	crossobj.style.visibility="visible"
}

function closeDivClick(div){		
		document.getElementById(div).style.visibility = "hidden";
	}

function ShowCustomerPanel(id)
{
    var DivLog =  document.getElementById(id);
    if (DivLog)
    {
        if (DivLog.style.display != "")
        {
            DivLog.style.left = "50px";
            DivLog.style.top = "40px";
            DivLog.style.display = "";
        }
        else
        {
            DivLog.style.display = "none";
        }
    }
}

function WindowOpen(url, name, w, h)
{
    w += 32;
    h += 96;
    wleft = (screen.width - w) / 2;
    wtop = (screen.height - h) / 2;
    if (wleft < 0)
    {
        w = screen.width;
        wleft = 0;
    }
    if (wtop < 0)
    {
        h = screen.height;
        wtop = 0;
    }
    var win = window.open(url,
                        name,
                        'width=' + w + ', height=' + h + ', ' +
                        'left=' + wleft + ', top=' + wtop + ', ' +
                        'status=1, scrollbars=yes');
    win.resizeTo(w, h);
    win.moveTo(wleft, wtop);
    win.focus();
}

function ImageWindowOpen(ClientId, name,w,h)
{
   var image= document.getElementById(ClientId);
   WindowOpen(image.src, name, w, h);
}
/*Function hideDive: will close a Div, when we move the mouse Out of it */
function hideDiv(div){
		document.getElementById(div).onmouseout = closeDiv;
}


/*Function closeDiv: will check the Event object, to find which element has been touched by the mouse*/
function closeDiv(event) {

  var current, related;

  if (window.event) {
    current = this;
    related = window.event.toElement;
  }
  else {
    current = event.currentTarget;
    related = event.relatedTarget;
  }

  //If the 'related' element is an item inside the Div we want to close, the event is ignored. Otherwise, the Div will close.
  if (current != related && !contains(current, related)) 
    current.style.display = "none";
}

/*Function contains: will determine if an element is inside another*/
function contains(a, b) {
  // Return true if node a contains node b.
  while (b.parentNode)
    if ((b = b.parentNode) == a)
      return true;
  return false;
}

function ChangeMainMenuVisibility(ID, current, idCategory)
{	
	var div = document.getElementById(ID);
	if(div) { 
		if (div.style.display == 'block') 
		{				
			div.style.display = 'none';
			IntroducirCookie(idCategory, "none");
			//current.class = 'url(/graphics/new/Menu/Red-menu-bar2c.png) no-repeat';
			//current.style.color = "#DA9C0D";
		}
		else
		{
			div.style.display = 'block';
			IntroducirCookie(idCategory, "block");
			//current.style.background = 'url(/graphics/new/Menu/Red-menu-bar2b.png) no-repeat';
			//current.style.color = "#F4C659";
		}
	}
}

function ChangeVisibility(ID, current, idCategory)
{	
	var div = document.getElementById(ID);
	if(div) { 
		if (div.style.display == 'block') 
		{				
			div.style.display = 'none';
			IntroducirCookie(idCategory, "none");
			current.style.background = 'url(/graphics/global/leftColMenuItem.gif) repeat-y right 0';
		}
		else
		{
			div.style.display = 'block';
			IntroducirCookie(idCategory, "block");
			current.style.background = 'url(/graphics/global/leftColMenuItemOn.gif) repeat-y right 0';
		}
	}
}

function ChangeVisibilityDiv(ID,IDlnkButton)
{	
	var div = document.getElementById(ID);
	var lnkBtn = document.getElementById(IDlnkButton);
	if(div) { 
		if (div.style.display == 'block') 
		{				
			div.style.display = 'none'
			lnkBtn.innerText = "Show";
		}
		else
		{
			div.style.display = 'block'
			lnkBtn.innerText  = "Hide";
		}
	}
}


function ShowHidePriceControl(ClientId,ClientIdvld,ClientIdreg)
    {
        var vdivprice=document.getElementById('divPrice');
        var vchkRegular=document.getElementById(ClientId);
        var vRegularExp=document.getElementById(ClientIdreg);
        var vValidator=document.getElementById(ClientIdvld);

        if(vchkRegular.checked==true)
           { 
            vdivprice.style.display='';
            vRegularExp.enabled=true;
            vValidator.enabled=true;
            }
        else
           {
            vdivprice.style.display='none';
            vRegularExp.enabled=false;
            vValidator.enabled=false;
            }

    }

    function ShowHidePriceGoldPointControl(ClientId,ClientIdvld,ClientIdreg)
    {
        var vdivprice=document.getElementById('divGoldPointPrice');
        var vchkRegular=document.getElementById(ClientId);
        var vRegularExp=document.getElementById(ClientIdreg);
        var vValidator=document.getElementById(ClientIdvld);

        if(vchkRegular.checked==true)
           { 
            vdivprice.style.display='';
            vRegularExp.enabled=true;
            vValidator.enabled=true;
            }
        else
           {
            vdivprice.style.display='none';
            vRegularExp.enabled=false;
            vValidator.enabled=false;
            }

    }


    function ShowHidePurchaseAddress(IDtoshow,IDtohide)
    {	
	    var divToShow = document.getElementById(IDtoshow);
	    var divToHide = document.getElementById(IDtohide);

		    if (divToHide.style.display == 'block') 
		    {				
			    divToHide.style.display = 'none'
			    divToShow.style.display = 'block'
		    }
		    else
		    {
			    divToHide.style.display = 'block'
			    divToShow.style.display = 'none'
		    }
   }


   function ShowHideDivRange(ClientIDRange,ClientIdCV, ClientIDREQStartDate, ClientIDREQEndDate)
    {

        var vdivrange=document.getElementById('divRangeDate');
        var vCVRange=document.getElementById(ClientIdCV);
        var vReqStart=document.getElementById(ClientIDREQStartDate);
        var vReqEnd=document.getElementById(ClientIDREQEndDate);

        if(vdivrange.style.display=='none')
           { 
            ValidatorEnable(vCVRange, true);
            ValidatorEnable(vReqStart, true);
            ValidatorEnable(vReqEnd, true);

            vdivrange.style.display='';
            }
        else
           {

            ValidatorEnable(vCVRange, false);
            ValidatorEnable(vReqStart, false);
            ValidatorEnable(vReqEnd, false);

            vdivrange.style.display='none';
            }
    }



    function ShowRangeDate(ClientIDRange,ClientIdCV, ClientIDREQStartDate, ClientIDREQEndDate)
    {

        var vdivRangeDate = document.getElementById("divRangeDate");
        var ctrRange = document.getElementById(ClientIDRange);
        var cvRange = document.getElementById(ClientIdCV);
        var reqStart = document.getElementById(ClientIDREQStartDate);
        var reqEnd =  document.getElementById(ClientIDREQEndDate);

        if(ctrRange.selectedIndex==4)
        {
            ValidatorEnable(cvRange, true);
            ValidatorEnable(reqStart, true);
            ValidatorEnable(reqEnd, true);

            vdivRangeDate.style.display='';
        }
        else
        {
            ValidatorEnable(cvRange, false);
            ValidatorEnable(reqStart, false);
            ValidatorEnable(reqEnd, false);

            vdivRangeDate.style.display='none';
        }

    }
