function OpenNotes(url)
{
    window.open(url);
}

function OpenPopUp(hyperlink, e)
{
    OpenDivPopup(hyperlink.href);
    return CancelEventBubble(e);
}

function ConfirmDelete()
{
    if(!confirm("Are you sure you want to delete?"))
    {
        return false;
    }
    //return true;
}

function CancelEventBubble(e)
{
    if(!e)
    {
        e = event;
    }
    e.cancelBubble = true;
    return false;
}

function AlertForLogin(msg, e)
{
    alert(msg);
    return CancelEventBubble(e);
}

function OpenDivPopup(url, width, height, windowName)
{
    width = SafeValue(width, 0); //default
    height = SafeValue(height, 0); //default
    //modal = showPopWin(url, width, height, windowName);
    
    if(width > 0 || height > 0)
    {
        win = window.open(url, windowName, 'width=' + width + 'px,height=' + height + 'px,scrollbars=yes');
    }
    else
    {
        win = window.open(url);
    }
    win.focus();
}

function SafeValue(value, defaultValue)
{
    var toReturn = value;
    if(toReturn == null || toReturn == 'undefined')
    {
        toReturn = defaultValue;
    }
    return toReturn;
}

function ReturnFloat(tb)
{
    if(isNaN(tb.value) || tb.value == null || tb.value.length == 0)
    {
        tb.value = 0;
    }
    return parseFloat(tb.value);
}

function ShowDiv(divname)
{
    var obj = document.getElementById(divname);
    obj.style.display = 'block';
}

function HideDiv(divname)
{
    var obj = document.getElementById(divname);
    obj.style.display = 'none';
}

function Round(MyVal, Length)
{
    return Math.round(MyVal * Math.pow(10, Length)) / Math.pow(10, Length);
}

function OpenInParent(url, closeSelf)
{
    //window.top.location.href = url; //For Virtual Popup using div
    window.opener.document.location.href = url;
    window.close();
}

function OpenClassicWindow(url, height, width)
{
	window.open(url, 'popupWindow', 'height=' + height + ',width=' + width + ',toolbar=no');
}


function CopyToParent(checkbox, parentControlId, value)
{
    parentControl = window.parent.document.getElementById(parentControlId);
    parentControl.value = value;
}

function CopyToParentMultiple(checkbox, parentControlId, value)
{
    parentControl = window.parent.document.getElementById(parentControlId);
    if(checkbox.checked)
        {
            if(parentControl.value != null && parentControl.value.length > 0)
                {
                    parentControl.value = parentControl.value + ',';
                }
            parentControl.value = parentControl.value + value;
        }
    else
        {
            if(parentControl.value != null && parentControl.value.length > 0)
                {
                    parentControl.value = parentControl.value.replace(',' + value, '') //First Try
                    parentControl.value = parentControl.value.replace(value + ',', '') //Second Try
                    parentControl.value = parentControl.value.replace(value, '') //Third Try
                }
        }
}

function PreviousPage()
{
    history.go(-1);
}

function PromptInput(description, errorMessage, fieldName, e)
{
    var value = prompt(description, '');
    
    if(value != null)
    {
        value = Trim(value);
    }
    
    if(value != null && value.length != 0)
    {
        //Setting value to the hidden field
        var obj = GetControlById(fieldName);
        if(obj != null)
        {
            obj.value = value;
        }
        return true;
    }
    else
    {
        alert(errorMessage);
        return CancelEventBubble(e);
    }
    return true;
}

function GetControlById(controlId)
{
    return document.getElementById(controlId);
}

function SetSubscriptionPlan(obj)
{
    var control = GetControlById('tbSubscriptionPlanId');
    
    var arr = obj.value.split(',');
    
    control.value = arr[0];
    
    shipmentsControl = GetControlById('ctl00_uxContentPlaceHolder_ContentPanel1_uxCustomAmount');
    if(control.value != '5')
    {
        shipmentsControl.value = arr[1];
        shipmentsControl.disabled = true;
    }
    else
    {
        shipmentsControl.value = '0';
        shipmentsControl.disabled = false;
    }
}


function ValidateSubscriptionPlanSelection(paymentModeControlId, customShipmentControlId, subscriptionSelectionControlId, e)
{
    var paymentModeControl = GetControlById(paymentModeControlId);
    var customShipmentControl = GetControlById(customShipmentControlId);
    var subscriptionSelectionControl = GetControlById(subscriptionSelectionControlId);
    
    var paymentModeId = paymentModeControl.value;
    var customShipment = customShipmentControl.value;
    var subscriptionId = subscriptionSelectionControl.value;
    
    if(subscriptionId == 0)
    {
        alert('Select subscription plan');
        return CancelEventBubble(e);
    }
    
    if(paymentModeId == 0)
    {
        alert('Select payment mode to proceed');
        return CancelEventBubble(e);
    }
    
    if(subscriptionId == 5) //Customized
    {
//        if(paymentModeId != 1) //Not Credit Card
//        {
//            alert('Only credit card can be used to buy customized shipments');
//            return CancelEventBubble(e);
//        }
//        else
//        {
            var invValue = 0;
            if(customShipment.length > 0)
                invValue = parseInt(customShipment);
            if(invValue < 20000)
            {
                alert('You must enter 20000 or higher for this plan');
                customShipmentControl.focus();
                return CancelEventBubble(e);
            }
//        }
    }
    return true;
}

function Redirect(redirectUrl)
{
    setTimeout("document.location.href = '" + redirectUrl + "'", 3000);
}

function ToggleControls(divName, show, inline)
{
 var x = document.getElementById( divName );
 if(x == null || x == undefined)
 {
    return;
 }
 
 var z = x.getElementsByTagName('SPAN');
 
 if(!inline)
 {
    inline = false;
 }
 if(show == null || show == undefined)
 {
    show = x.style.display == "none";
 }
 
 if(z)
 {
     for(i=0; i< z.length; i++)
     {
      var webControl = z[i];
      // check if it is really a validator control
      if(webControl.id != null && webControl.id.indexOf("Validator") > -1)
      {
       ValidatorEnable(webControl, show);
      }
     }
 }
 
 if(show)
 {
    if(inline)
        x.style.display = "inline";
    else
        x.style.display = "block";
 }
 else
 {
  x.style.display = "none";
 }
}


function ToggleContent(elementId)
{
    ToggleControls(elementId);
}

function attachEnterKey(controlId, buttonId)
{
}

function ConvertUnits(qtyControlId, unitFromControlId, unitToControlId, resultControlId)
{
    qtyFrom = document.getElementById(qtyControlId);
    unitFromDropdown = document.getElementById(unitFromControlId);
    unitToDropdown = document.getElementById(unitToControlId);
    resultControl = document.getElementById(resultControlId);

    qty = parseFloat(qtyFrom.value);
    unitFrom = unitFromDropdown.options[unitFromDropdown.selectedIndex].value;
    unitTo = unitToDropdown.options[unitToDropdown.selectedIndex].value;
    
    if(unitFrom.indexOf("||") > -1)
    {
        i = unitFrom.indexOf("||");

        formula = unitFrom.substring(0, i-1);
        formula = formula.replace("{0}", qty);
        
        qty = eval(formula);
        
        i = unitTo.indexOf("||");
        formula = unitTo.substring(i+2);
        
        formula = formula.replace("{0}", qty);
        qty = eval(formula);
        
        resultControl.value = formatFloat(qty);
        
        return;
    }
    
    if(unitFrom.indexOf("{0}") > -1)
    {
        alert('need to evaluate expression');
        return;
    }
    
    unitFrom = parseFloat(unitFrom);
    unitTo = parseFloat(unitTo);
    
    resultControl.value = formatFloat(qty * (unitFrom / unitTo));
}

function formatFloat(number, X) {
// rounds number to X decimal places, default is 6
    X = (!X ? 6 : X);
    return Math.round(number * Math.pow(10,X)) / Math.pow(10,X);
}

function resetForm(containerId)
{
    container = document.getElementById(containerId);
    inputs = container.getElementsByTagName('input');
    selects = container.getElementsByTagName('select');
    textAreas = container.getElementsByTagName('textarea');
    
    nLen = inputs.length;
    for(i=0; i < nLen; i++)
    {
        inputType = inputs[i].type;
        if(inputType == "text")
        {
            inputs[i].value = "";
        }
    }
}

function Processing(searchBtnControlId)
{
    searchBtn = GetControlById(searchBtnControlId);
    if(searchBtn != null)
    {
        searchBtn.value = 'Searching....';
        searchBtn.style.display = 'none';
    }
    ToggleControls('Processing', true, false);
    //ToggleControls('SearchDiv', false, false);
}

function AddToParent(selectedValue, parentControlId)
{
    /* TODO: Need to work for firefox */
    var toAdd = true;
    
    parentControl = window.opener.document.getElementById(parentControlId);
    if(parentControl == null || selectedValue.length == 0)
    {
        return false;
    }
    
    selectedValue = Trim(selectedValue);
    
    if(parentControl.value.length > 0)
    {
        var tempString = ',' + parentControl.value + ',';
        if(tempString.indexOf(',' + selectedValue + ',') > -1)
        {
            toAdd = false;
        }
    }
    
    if(toAdd)
    {
        if(parentControl.value.length > 0)
        {
            parentControl.value = parentControl.value + ',';
        }
        parentControl.value = parentControl.value + selectedValue;
    }
    
    ObjAddedMsgDiv = document.getElementById('addedMsgDiv');
    ObjAddedMsgDiv.style.display = 'block';
    setTimeout("HideDiv('addedMsgDiv')",1000);
}

function OpenShipmentLookup(lookupTypeId, controlId)
{
    OpenDivPopup('PickupList.aspx?LookupTypeId=' + lookupTypeId + '&ParentControlId=' + controlId, 500, 620, 'ShipmentPickup');
    return false;
}

function Trim(str)
{  while(str.charAt(0) == (" ") )
  {  str = str.substring(1);
  }
  while(str.charAt(str.length-1) == " " )
  {  str = str.substring(0,str.length-1);
  }
  return str;
}

function CheckHSCode(sender, args)
{
    var hsCode = args.Value;
    
    var control = GetControlFromSender(sender);
    
    hsCode = '' + hsCode;
    hsCode = hsCode.replace('.','');
    hsCode = hsCode.replace(' ','');
    if(control != null)
    {
        control.value = hsCode;
    }

    args.IsValid = true;
}

function GetControlFromSender(sender)
{
    var controlHtml = sender.outerHTML;
    startIndex = controlHtml.indexOf('controltovalidate');
    endIndex = controlHtml.indexOf('errormessage');
    htmlLength = endIndex - startIndex;
    
    controlId = controlHtml.substr(startIndex + 19, htmlLength - 21);
    return control = GetControlById(controlId);
}

function CheckShipmentSearch(tbControl, ddlControl)
{
    //tbControl = GetControlById(tbControlId);
    //ddlControl = GetControlById(ddlControlId);
    if(tbControl.value.length > 0)
    {
        var hsCode = parseFloat(tbControl.value);
        if(hsCode > 0)
        {
            hsCode = tbControl.value;
            if(ddlControl != null)
            {
                if(ddlControl.value == 0)
                {
                    return false;
                }
            }
            else
            {
                hsCode = '' + hsCode;
                hsCode = hsCode.replace('.','');
                hsCode = hsCode.replace(' ','');
                tbControl.value = hsCode;
                if(hsCode.length != 2 & hsCode.length != 4 & hsCode.length != 6 & hsCode.length != 8 & hsCode.length != 10 & hsCode.length != 12)
                {
                    return false;
                }
            }
        }
    }
    return true;
}

function CheckSearchValue(sender, args)
{
    //Sender is the textbox
    objDropDown = GetControlById(GetSearchDropDownClientId());
    objTextBox = GetControlById(GetSearchTextBoxClientId());
    
    if(CheckShipmentSearch(objTextBox, objDropDown))
    {
        args.IsValid = true;
    }
    else
    {
        args.IsValid = false;
    }
}

function CheckBlur(sender, defaultText)
{
    if(Trim(sender.value).length == 0)
    {
        sender.value = defaultText;
    }
}

function CheckFocus(sender, defaultText)
{
    if(sender.value == defaultText)
    {
        sender.value = '';
    }
}

function AllowTextOnly(e)
{
    var keynum;
    
    if(window.event)
    {
        keynum = e.keyCode;
    }
    else if(e.which)
    {
        keynum = e.which;
    }

    if(keynum >= 48 && keynum <= 57) //Allow all except numbers
    {
        alert('Number input not allowed');
    
        //Cancel Input
        if(window.event)
        {
            e.returnValue = false;
        }
        else
        {
            e.preventDefault();
        }
    }
}

function AllowNumbersOnly(e)
{
    var keynum;
    
    if(window.event)
    {
        keynum = e.keyCode;
    }
    else if(e.which)
    {
        keynum = e.which;
    }
    
    if((keynum >= 48 && keynum <= 57) || (keynum == 44)) //Allowing comma
    {
        return;
    }
    
    alert('Text input not allowed');

    //Cancel Input
    if(window.event)
    {
        e.returnValue = false;
    }
    else
    {
        e.preventDefault();
    }
}

function DisablePaste(e)
{
    alert('Paste not allowed');
    CancelEvent(e);
}

function CancelEvent(e)
{
    //Cancel Input
    if(window.event)
    {
        e.returnValue = false;
    }
    else
    {
        e.preventDefault();
    }
}