/**
 * Converts the key event represented by [eventKeyCode] into a mouse click
 * action on the element with the id [targetId]
 */
function dispatchKeyToMouseClickEvent(sourceEvent, eventKeyCode, targetId) {

    var keynum;

    if(window.event) {	  
        keynum = sourceEvent.keyCode;

    }else if(sourceEvent.which){
        keynum = sourceEvent.which;

    }
    if ( keynum == eventKeyCode){	
        var target = document.getElementById(targetId);
        target.onclick();
    }
}

/**
 * Sets the focus to the element with the [id] passed in as a parameter
 */
function setFocus(targetId){
    var element = document.getElementById(targetId);
    element.focus();
    element.select();
}

/**
 * Restrict the maximum length of a text field to the [maxLength] parameter
 */
function restrictTextFieldCharacterLength(textfieldId, maxLength){
    var textfield = document.getElementById(textfieldId);
    if (textfield.value.length > maxLength) {
        textfield.value = textfield.value.substring(0, maxLength);
    }
}

/**
 * Checks the shopping cart to see if it has been saved or not before logging out.
 */
function checkCartBeforeLogout(cartElementId) {
    var navigate = true;
    var cart = document.getElementById(cartElementId);    
    if (cart.value > 0) {
        navigate = confirm("Are you sure you want to log out without saving your basket?");
    }
    return navigate;
}

/**
 * Allows user to specify that the would like to continue with an action that will 
 * eventually dump all the entries of the shopping cart.
 */
function confirmDumpCartEntries(cartElementId) {
    var navigate = true;
    var entries = document.getElementById(cartElementId);    
    if (entries.value > 0) {
        navigate = confirm("This will overwrite any items you may already have in your basket, and may change your discounts.");
    }
    return navigate;
}

/**
 * Disables a control based on the length of text in a related control
 */
function disableSubstituteElement(sourceElementId, substituteElementId, substituteIndex){

    var sourceElement = document.getElementById(sourceElementId);
    var substituteElement = document.getElementById(substituteElementId);

    if (sourceElement.value.length > 0){

        substituteElement.value="";
        substituteElement.title="This field is disabled. Please clear the " + sourceElementId.split(':')[substituteIndex] + " field in order to enable this field" ;
        substituteElement.disabled = true;

    } else if(substituteElement.disabled) {
		
        substituteElement.disabled=false;
        substituteElement.title="";
    }
}

function copyElementHeight( sourceElementId , targetElementId) {
  if (document.getElementById(sourceElementId) && document.getElementById(targetElementId)) {
          var discountHeight = document.getElementById(sourceElementId).offsetHeight; 
          var fc = document.getElementById(targetElementId);
          fc.style.height = discountHeight+"px";
  }          
}
