﻿function fnRedirectProduct(strValue){
    if (strValue!=''){ 
        document.location.href='ProductsSystems.aspx?cat=' + strValue;
    }else{
        document.location.href='ProductsSystems.aspx';
    }
}

function fnRedirectSelect(strValue, strPath){
    if (strValue!=''){ 
        document.location.href= strPath + '?c=' + strValue;
    }else{
        document.location.href=strPath;
    }
}

function fnRedirectToProduct(strValue){
    if (strValue!=''){
        var strAppName = document.getElementById('hdnApplicationPath').value;
        if (strAppName != '/'){
            document.location.href = strAppName + '/' + strValue;
        }else{
            document.location.href = strValue;
        }
    }
}

function fnUpdateBasket(strBasketID, strQuantity){
    document.getElementById('basketid').value = strBasketID;
    document.getElementById('quantity').value = strQuantity;
}

function fnSetForm(strFormName){
    document.getElementById('hdnFormName').value = strFormName;
    document.forms[0].submit();
}

function fnSetRedirect(strRedirect){
    document.getElementById('hdnFormRedirect').value = strRedirect;
}

function fnUpdateInvoiceFlag(){
    document.getElementById('invoice').value = '1';
}

function fnCheckDefault(obj, strDefault){
    if (obj.value==strDefault){ obj.value=''; }
}

function fnCopyCardHolder(){
    var strAdd1 = document.forms[0].elements['address1'].value;
    var strAdd2 = document.forms[0].elements['address2'].value;
    var strAdd3 = document.forms[0].elements['address3'].value;
    var strTown = document.forms[0].elements['town'].value;
    var strCounty = document.forms[0].elements['county'].value;
    var strPostcode = document.forms[0].elements['postcode'].value;
    
    document.forms[0].elements['deliveryaddress1'].value = strAdd1;
    document.forms[0].elements['deliveryaddress2'].value = strAdd2;
    document.forms[0].elements['deliveryaddress3'].value = strAdd3;
    document.forms[0].elements['deliverytown'].value = strTown;
    document.forms[0].elements['deliverycounty'].value = strCounty;
    document.forms[0].elements['deliverypostcode'].value = strPostcode;
}


/* old */

function fnRedirectSearch(strKeywordsElementID){
    var strKeywords = document.forms[0].elements['keywords'].value;
    
    if (strKeywords.length > 0){
        var strAppName = document.getElementById('hdnApplicationPath').value;
        if (strAppName != '/'){
            //var strRedir = strAppName + '/Search.aspx?search=' + URLEncode(strKeywords);
            //alert(strRedir);
            document.location.href = strAppName + '/Search.aspx?search=' + URLEncode(strKeywords);
            return false;
        }else{
            //var strRedir = '/Search.aspx?search=' + URLEncode(strKeywords);
            //alert(strRedir);
            document.location.href = '/Search.aspx?search=' + URLEncode(strKeywords);
            return false;
        }
    }
}

function fnResetKeyword(strKeyword){
    if (strKeyword == ' - Enter search text - '){ document.getElementById('searchkeywords').value=''; }
}

function fnShowPostageDetails(objID, strDate, strCourier, strURL, strTrackingNumber){
    document.getElementById('dispatcheddate').innerHTML = strDate;
    document.getElementById('dispatchedcourier').innerHTML = strCourier;
    document.getElementById('dispatchedurl').href = strURL;
    document.getElementById('dispatchedtrackingnumber').innerHTML = strTrackingNumber;

    var obj = document.getElementById('orderdetails');
    obj.style.left = (parseInt(getElementLeft(objID)) - 130) + 'px';
    obj.style.top = (parseInt(getElementTop(objID)) - 30) + 'px';
    obj.style.display = 'block';
}

function fnHidePostageDetails(){
    document.getElementById('orderdetails').style.display = 'none';
}

function fnTrapEnter(e){
    var keycode;
    if (window.event) keycode = window.event.keyCode;
    else if (e) keycode = e.which;
    else return true
    if (keycode == 13){
        e.returnValue = false;
        e.cancel = true;
        return false;
    }else{
        return true;
    }
}


function checkEnter(e){ //e is event object passed from function invocation
    var characterCode; //literal character code will be stored in this variable

    if(e && e.which){ //if which property of event object is supported (NN4)
        e = e
        characterCode = e.which; //character code is contained in NN4's which property
    }
    else{
        e = event
        characterCode = e.keyCode; //character code is contained in IE's keyCode property
    }
    if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
        
        
        fnRedirectSearch('keywords'); //submit the form
        return false;
    }
    else{
        return true;
    }
}

function disableEnterKey(e)
{
     var characterCode; //literal character code will be stored in this variable

    if(e && e.which){ //if which property of event object is supported (NN4)
        e = e
        characterCode = e.which; //character code is contained in NN4's which property
    }
    else{
        e = event
        characterCode = e.keyCode; //character code is contained in IE's keyCode property
    }

    if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
        return false;
    }
}

function URLEncode(clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') {
    	output += match[1];
      x += match[1].length;
    } else {
      if (clearString[x] == ' ')
        output += '+';
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}

function URLDecode(encodedString) {
  var output = encodedString;
  var binVal, thisString;
  var myregexp = /(%[^%]{2})/;
  while ((match = myregexp.exec(output)) != null
             && match.length > 1
             && match[1] != '') {
    binVal = parseInt(match[1].substr(1),16);
    thisString = String.fromCharCode(binVal);
    output = output.replace(match[1], thisString);
  }
  return output;
}

function getElementLeft(Elem) {
	var elem;
	if(document.getElementById) {
		var elem = document.getElementById(Elem);
	} else if (document.all){
		var elem = document.all[Elem];
	}
	xPos = elem.offsetLeft;
	tempEl = elem.offsetParent;
	while (tempEl != null) {
		xPos += tempEl.offsetLeft;
  		tempEl = tempEl.offsetParent;
	}
	return xPos;
}


function getElementTop(Elem) {
	if(document.getElementById) {	
		var elem = document.getElementById(Elem);
	} else if (document.all) {
		var elem = document.all[Elem];
	}
	yPos = elem.offsetTop;
	tempEl = elem.offsetParent;
	while (tempEl != null) {
		yPos += tempEl.offsetTop;
  		tempEl = tempEl.offsetParent;
	}
	return yPos;
}
