
function SaveForm(pstrFormAction, plngPartID, pintQuantity, pstrItemType, plngConfigurationParentID)
{
	if (plngConfigurationParentID == null) plngConfigurationParentID = 0;
	
	document.frmShoppingBasket.inpAction.value = pstrFormAction;
	document.frmShoppingBasket.inpItemID.value = plngPartID;
	document.frmShoppingBasket.inpQuantity.value = pintQuantity;
	document.frmShoppingBasket.inpItemType.value = pstrItemType;
	document.frmShoppingBasket.inpItemConfigurationParentID.value = plngConfigurationParentID;
	document.frmShoppingBasket.submit();
}

function FlagConfigurationUpdate(plngConfigurationID, pstrUpdateFlagFieldName, poConfigurationQuantity)
{

	poConfigurationQuantity.value = poConfigurationQuantity.value.replace(/^\s*|\s*$/g,'');
	if (isNaN(parseInt(poConfigurationQuantity.value))) {
		poConfigurationQuantity.value = poConfigurationQuantity.defaultValue;
		alert('Please enter only numeric quantities.');
	} else {
		//Force integer
		poConfigurationQuantity.value = parseInt(poConfigurationQuantity.value);
		bFlagUpdated = true;
	}

	if (poConfigurationQuantity.value != poConfigurationQuantity.defaultValue) {
		// Set update flag for the row
		document.getElementsByName(pstrUpdateFlagFieldName)[0].value = '1';
	}
	
	// Ensure all configuration quantities are up to date
	// (handles actions not captured by when called by the
	// onKeyUp event - e.g. copy/paste with a mouse)
	UpdateConfigurationQuantities(plngConfigurationID, poConfigurationQuantity)
}

function UpdateConfigurationQuantities(plngConfigurationID, poConfigurationQuantity)
{
	// If updating a base unit, synchronize the configuration's part quantities, else exit
	if (plngConfigurationID != 0) {
		
		var strUpdateFlagField = 'inpConfigUpdateFlag';
		var strQuantityField = 'inpConfigQuantity';
		var strQuantityDiv = 'ConfigurationQuantity';
		var frmShoppingBasket = document.frmShoppingBasket;
		var regParentID = /^inpConfigParentID/i;
		
		for (var numCounter=0; numCounter < frmShoppingBasket.elements.length; numCounter++) {
			
			var oElement = frmShoppingBasket.elements[numCounter];

			if (oElement.type != 'hidden') continue;	
			if (oElement.name.search(regParentID) == -1 ) continue;
			if (oElement.value != plngConfigurationID) continue;
			
			var strElementName = oElement.name;
			var strRowID = strElementName.replace(regParentID, '');
			
			document.getElementsByName(strQuantityField + strRowID)[0].value = poConfigurationQuantity.value;
			document.getElementsByName(strUpdateFlagField + strRowID)[0].value = '1';
		}
	
		// Update the base unit's read only quantity
		document.getElementById(strQuantityDiv + plngConfigurationID).innerHTML = poConfigurationQuantity.value;
	}
}
	
function ApplySector(pstrSector)
{
	// Show the shopping basket in one of the sector skins
	document.frmShoppingBasket.inpAction.value = '';
	document.frmShoppingBasket.action = '/' + pstrSector + '/Products/ShoppingBasket.asp';
	document.frmShoppingBasket.submit();
}
	
function CreateQuote(pstrAuthenticationLevel, pbCanCreateQuote)
{
	var strPortalAlias = getPortalAliasFromWindow();
	var strAction = 'CreateQuote.asp?step=2&mode=basket';

	// Add portal alias to querystring (if applicable)
	if (strPortalAlias.length > 0) {
		strAction = strAction + '&portalalias=' + strPortalAlias;
	}
		
	// Quote requires action and returnlocation
	document.frmShoppingBasket.inpAction.value='';
	document.frmShoppingBasket.action = strAction;
	document.frmShoppingBasket.submit();
}
	
function CreateOrder(pstrSector)
{
    // Initialise local variables
    var strAction = '';
    var blnLegacyAspBasket = false;
    
    // Check if using legacy basket from eStore
    if (IsValidObject(document.getElementById('LegacyASPBasket'))) {
		blnLegacyAspBasket = true;
    }
    
    if (blnLegacyAspBasket == true) {
		
		var posDoubleSlash;
		var posQustionMark;
		var posLastSlash;
		
		// Get current location
		strAction = '' + window.location.href + '';
		
		// Get marker points in the url
		posDoubleSlash = strAction.indexOf('//');
		posQustionMark = strAction.indexOf('.asp') + 4;

		// Remove everything in querystring and make url https
		strAction = strAction.substring(posDoubleSlash,posQustionMark);
		strAction = 'https:' + strAction;
		
		// Remove everything after the last slash
		posLastSlash = strAction.lastIndexOf('/');
		strAction = strAction.substring(0,(posLastSlash+1));
		
		// Finally build url to create order
		strAction = strAction + 'CreateOrder.asp?Step=Initial';
		
		// Order requires action and return location
		document.frmShoppingBasket.inpAction.value='INITIAL';
		document.frmShoppingBasket.action = strAction;
		document.frmShoppingBasket.submit();
    
    
    } else {
    
		strAction = 'https://' + window.location.host + '/';
    
		if (pstrSector!='')
		    strAction = strAction + pstrSector + '/'

		// Order requires action and return location
		document.frmShoppingBasket.inpAction.value='INITIAL';
		document.frmShoppingBasket.action= strAction + 'Products/CreateOrder.asp?Step=Initial';
		document.frmShoppingBasket.submit();
	}
}

function FlagUpdated(poPartQuantity, poPartUpdateFlag) {

	var bFlagUpdated;

	poPartQuantity.value = poPartQuantity.value.replace(/^\s*|\s*$/g,'');
	if (isNaN(parseInt(poPartQuantity.value))) {
		poPartQuantity.value = poPartQuantity.defaultValue;
		alert('Please enter only numeric quantities.');
		bFlagUpdated = false;
	} else {
		//Force integer
		poPartQuantity.value = parseInt(poPartQuantity.value);
		bFlagUpdated = true;
	}
			
	if (poPartQuantity.value != poPartQuantity.defaultValue) {
		// Mark current part as updated
		poPartUpdateFlag.value = '1';
		// If not already done so for another part, flag that a part is updated
		if (document.frmShoppingBasket.inpPartUpdateFlagAny.value != '1') {
			document.frmShoppingBasket.inpPartUpdateFlagAny.value = '1';
		}
	}
	
	return bFlagUpdated;
}

function RemoveQuote() {
	SaveForm('REMOVEQUOTE', 0, 0, '');
}

function ClearBasket() {
	SaveForm('CLEAR', 0, 0, '');
}

function AddQuoteLineOption(plngPartID, plngPosition, plngDebug) {
	var lngQuantity = document.frmShoppingBasket['inpQIOptionQuantity' + plngPosition].value;
	SaveForm('ADDQUOTEOPTION', plngPartID, lngQuantity, 'Quote/Quote_Header/Quote_Lines');
}

function ChooseUpsellOption(plngQuoteLineID) {
	return '';
	SaveForm('ADDUPSELLOPTION', 0, 0, 'Quote/Quote_Header/Quote_Lines');
}

function AlterQuoteLineOption(plngCurrentQuoteLineID, plngPartID, plngPosition) {
	document.frmShoppingBasket.inpQuoteLineID.value = plngCurrentQuoteLineID;
	var oPartQuantity = document.frmShoppingBasket['inpQISelectedQuantity'+plngPosition];
	var oPartUpdateFlag = document.frmShoppingBasket['inpQISelectedUpdateFlag'+plngPosition];
	FlagUpdated(oPartQuantity, oPartUpdateFlag);
	SaveForm('SWITCHQUOTEOPTION', plngPartID, oPartQuantity.value, 'Quote/Quote_Header/Quote_Lines');
}

function QuantityLimitsPermitting(obj, min, max, inpObj, pbInternalQuote) {
	if (!FlagUpdated(obj, inpObj)) {
		//not ok
	} else if (min=='' && max=='') { 
		//ok
	} else if ((obj.value >= min || min == '') && (obj.value <= max || max == '')) {
		//ok
	} else {
		var strMessage;
		if (pbInternalQuote) {
			strMessage = 'Your quoted price is based on the assumption that you are purchasing ';
		} else {
			strMessage = 'Your quote features an Online Bulk Discount price for ';
		}
			
		if (min !='' && max!= '') {
			strMessage = strMessage + 'between ' + min + ' and ' + max;
		} else if (min!='') {
			strMessage = strMessage + 'at least ' + min;
		} else {
			strMessage = strMessage + 'no more than ' + max;
		}
		strMessage = strMessage + ' of this item.\n\n';
		// set the input value to either min or max depending which way user was heading
		if (obj.value < min) {
			obj.value = min;
			if (min !='' && max!= '') {
				strMessage = strMessage + 'If you need to change quantities beyond this range';
			} else {
				strMessage = strMessage + 'If you\'d like to order fewer than ' + min + ',';
			}
		} else if (obj.value > max) {
			obj.value = max;
			if (min !='' && max!= '') {
				strMessage = strMessage + 'If you need to change quantities beyond this range';
			} else {
				strMessage = strMessage + 'If you\'d like to order more than ' + max + ',';
			}
		}
		strMessage = strMessage + ' please contact your Sales Adviser.';
		FlagUpdated(obj, inpObj);
		alert(strMessage);
	}
}

function PreventQuoteSave(reasonCode, parentQuoteNo) {
	if(reasonCode == 'NON_EDITABLE_MARKED_QUOTE') {
		alert('This quote has been marked as non-editable.\nPlease contact our sales team for advice');
	}
	else if(reasonCode == 'EDITED_INTERNAL_QUOTE') {
		alert('This quote is an edited version of Quote No: ' + parentQuoteNo + ' , and cannot be edited.\nPlease edit the original quote to make any desired changes.');
	}
}

function ViewQuote(plngQuoteHeaderID) {
	// Submit the form to view the required quote
	document.frmShoppingBasket.inpQuoteHeaderID.value = plngQuoteHeaderID;
	document.frmShoppingBasket.action = '/Account/Member/Quote.asp';
	document.frmShoppingBasket.submit();
}

function ViewQuoteInBasket(plngQuoteHeaderID) {
	// Submit the form to view the required quote
	document.frmShoppingBasket.inpQuoteHeaderID.value = plngQuoteHeaderID;
	document.frmShoppingBasket.action = '/Products/ShoppingBasket.asp';
	document.frmShoppingBasket.submit();
}

function IsValidObject(objToTest) {
	if (objToTest == null) {
		return false;
	}
	if (typeof(objToTest) == 'undefined') {
		return false;
	}
	return true;
}
