﻿function fnSetForm(strFormName){
    document.getElementById('hdnFormName').value = strFormName;
    document.forms[0].submit();
}

function fnSetRedirect(strRedirect){
    document.getElementById('hdnFormRedirect').value = strRedirect;
}

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;
}

function fnMTMSwitchStage(intStageID){
    for (i=1;i<=4;i++){
        document.getElementById('stage' + i).style.display = 'none';
        document.getElementById('stage' + i + 'link').className = '';
    }
    document.getElementById('stage' + intStageID).style.display = 'block';
    document.getElementById('stage' + intStageID + 'link').className = 'on';
}

function fnSetMTM(strValue){
    document.getElementById('basketoptions').style.display = (strValue=='curtains')?'none':'block';
    
    document.getElementById('hdnFormName').value = (strValue=='curtains')?'none':'romanblinds';
    
    document.getElementById('mtm_blinds').style.display = 'none';
    document.getElementById('mtm_curtains').style.display = 'none';
    document.getElementById('mtm_' + strValue).style.display = 'block';
}

function fnSetMTMButton(strValue){
    document.forms[0].elements['mtm_options_' + strValue].checked = true;
}

function fnRedirectSearch(strKeywordsElementID, strCollectionElementID, strStyleElementID, strColourElementID){
    var strKeywords = document.getElementById(strKeywordsElementID).value;
    var strCollectionID = (document.getElementById(strCollectionElementID)) ? document.getElementById(strCollectionElementID).value : '';
    var strStyleID = (document.getElementById(strStyleElementID)) ? document.getElementById(strStyleElementID).value : '';
    var strColourID = (document.getElementById(strColourElementID)) ? document.getElementById(strColourElementID).value : '';
    
    var strAppName = document.getElementById('hdnApplicationPath').value;
    
    if (strKeywords.length > 0){
        if (strAppName != '/'){
            document.location.href = strAppName + '/Search.aspx?search=' + URLEncode(strKeywords) + '&cid=' + strCollectionID + '&style=' + strStyleID + '&colour=' + strColourID;
        }else{
            document.location.href = '/Search.aspx?search=' + URLEncode(strKeywords) + '&cid=' + strCollectionID + '&style=' + strStyleID + '&colour=' + strColourID;
        }
    }
}

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 fnCheckCheckoutRedirect(){
    if (document.getElementById('offercode').value.length > 0){
        fnSetForm('updatebasket');
    }else{
        document.forms[0].submit();
    }
    return false;
}

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 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;
}
