//
// Front end JavaScript include file - this should be included on the front end.
//


// Used to display the multi image viewer popup window...
function getMultiViewerPopup(linkID, itemRef) {
	destURL = document.getElementById(linkID).href;		// Fetch our destination URL...
	popup(destURL, itemRef, 542, 631, false);
}


// Used to display the product image viewer popup window...
function getProductPopup(linkID, productName) {
	destURL = document.getElementById(linkID).href;		// Fetch our destination URL...
	if(destURL.indexOf('|') != -1) destURL += '&hidenav=on'; else destURL += '?hidenav=on';		// Add 'hidenav=on' to the URL...
	popup(destURL, productName, 542, 724, false);
}


// Used to verify (via Ajax) the deliverability of the specified country id...
function verifyCountryDeliverability(countryISO) {
	
	debug('verifyCountryDeliverability: '+countryISO+'...');	
	
	if(countryISO && document.getElementById('countrydeliverycheckdiv')) {
		requestString = '/customer/addresses.php?mode=checkcountrydelivery&countryisonum='+countryISO;
		sendRequest('verifyCountryDeliverability', requestString);
	}
}


// Used to reload the delivery calendar for the specified itemID, reading in the required month and year...
function fetchDeliveryCalendar(itemID, baseStamp) {

	debug('fetchDeliveryCalendar called with - itemID: '+itemID+', baseStamp: '+baseStamp+'.');

	// If there's a popup we can use, use it...
	if(document.getElementById('item_'+itemID+'_month')) {
		month = document.getElementById('item_'+itemID+'_month')[document.getElementById('item_'+itemID+'_month').selectedIndex].value;
		year = document.getElementById('item_'+itemID+'_year')[document.getElementById('item_'+itemID+'_year').selectedIndex].value;
		requestString = '/checkout/calendar.php?itemid='+itemID+'&basestamp='+baseStamp+'&day=1&month='+month+'&year='+year;
	}
	else {
		// Read in the current value from the hidden field...
		ukDate = document.getElementById('item_'+itemID+'_datevalue').value;
		dateChunks = ukDate.split('/');
		requestString = '/checkout/calendar.php?itemid='+itemID+'&basestamp='+baseStamp+'&day='+dateChunks[0]+'&month='+dateChunks[1]+'&year='+dateChunks[2];
	}

	sendRequest('fetchDeliveryCalendar', requestString);		
}


// Used to update the specified item's requested delivery date...
function setDeliveryDate(itemID, dateString) {

	setSourceValue('item_'+itemID+'_datevalue', dateString);
	setSourceData('item_'+itemID+'_datestring', dateString);
	hideDiv('item_'+itemID+'_calendar');		// Hide the calendar...

	return(true);
}


// Used to update the gift tag section of the checkout process...
function updateGiftTag(itemID) {

	srcElementName = 'item_'+itemID+'_gifttagblank';
	destElementName = 'item_'+itemID+'_gifttagtext';

	if(document.getElementById(srcElementName) && document.getElementById(destElementName)) {
		if(document.getElementById(srcElementName).checked) document.getElementById(destElementName).disabled = true;
		else document.getElementById(destElementName).disabled = false;
	}

	return(true);
}


// Used to update the shipping method and resultant charge text, etc.
shippingURL = '';														// This will be updated in-page via JavaScript output by PHP...
function updateDeliveryShippingMethod(addressID) {
	
	// Has the user selected an option, or was there only one in the first place?
	shippingData = document.getElementById('addressid_'+addressID+'_option_shippingdata').value;
	shippingData = shippingData ? shippingData 
									: document.getElementById('addressid_'+addressID+'_option_shippingdata')[document.getElementById('addressid_'+addressID+'_option_shippingdata').selectedIndex].value;

	shippingChunks = shippingData.split('_');
	shippingMethodID = shippingChunks[0];
	shippingChargeID = shippingChunks[1];	

	//debug('updateDeliveryShippingMethod called with addressID: '+addressID+', shippingMethodID: '+shippingMethodID+', shippingChargeID: '+shippingChargeID);

	if(shippingURL && shippingMethodID && shippingChargeID) {

		// Fire our Ajax request...
		updateRequest  = shippingURL+'?mode=updatedelivery&addressid='+addressID+'&methodid='+shippingMethodID+'&chargeid='+shippingChargeID;
		sendRequest('updateDeliveryShippingMethod', updateRequest);
		return(true);
	}
	else {
		document.getElementById('addressid_'+addressID+'_charge').innerHTML = '<strong class="amber">Please select a delivery method.</strong>';
		updateEstimatedDeliveryDate(addressID, '<span class="grey">N/A</span>');		// Hide any delivery estimate...
	}

	return(false);
}


// Used to update the display of the relevant select's edit button...
function updateDeliveryAddressSelector(sourceSelectID, itemID) {

	if(document.getElementById(sourceSelectID)) {
		selectValue = document.getElementById(sourceSelectID).value;
		if(selectValue == '' || selectValue == 'usemultipleaddresses') hideDiv(sourceSelectID+'edit');
		else showDiv(sourceSelectID+'edit');
	}

	return(true);
}


// Used to update the estimated shipping date for the supplied address ID...
function updateEstimatedDeliveryDate(addressID, dateString) { if(document.getElementById('addressid_'+addressID+'_estimatedate')) document.getElementById('addressid_'+addressID+'_estimatedate').innerHTML = dateString; return(true); }


// Used to update the multi-address areas within the shipping editor...
function updateDeliveryMultiAddressSelector(sourceSelectID) {

	if(document.getElementById(sourceSelectID)) {
		// Toggle the multi-address shipping bits...
		selectionData = document.getElementById(sourceSelectID)[document.getElementById(sourceSelectID).selectedIndex].value;
		if(sourceSelectID == 'deliveryaddressid') {
			if(selectionData == 'usemultipleaddresses') showDiv('multipleitems');
			else hideDiv('multipleitems');
		}
	}

	return(true);
}


// Used to update the checkout form's mode settings and submit the form...
function submitCheckoutForm(mode, value, sourceselector) {
	document.getElementById('mode').value = mode;
	document.getElementById('value').value = value;
	document.getElementById('srcselector').value = sourceselector;
	document.getElementById('basketcheckoutaddressform').submit();
	return(true);
}


// Used to update the SKU options on the product details page...
productID = '';														// This will be updated in-page via JavaScript output by PHP...
destSKUOptionURL = '/view/detail.php?mode=optionupdate&prodid=';
function updateSKUOptions(productID, srcElement) {

	//debug('updateSKUOptions called with - productID: '+productID+', srcElement: '+srcElement+'.');
	showDiv('processing');		// Show the processing animation...
	requestString = '';
	
	// Grab the selector count...
	selectorCount = document.getElementById('product_'+productID+'_selectorcount').value
	
	// Read and scan the values of the available option selectors... 
	for(loop=0; loop<optionDivIDs.length; loop++) {
		tempName = optionDivIDs[loop];
		if(document.getElementById(tempName)) tempValue = document.getElementById(optionDivIDs[loop])[document.getElementById(optionDivIDs[loop]).selectedIndex].value
		else tempValue = 'null';
		requestString += tempValue ? '&'+tempName+'='+tempValue : '';
		
		//debug('updateSKUOptions - productID: \''+productID+'\' ('+selectorCount+'), option selector: \''+tempName+'\' - value: \''+(tempValue ? tempValue : '<strong class="red">No element!</strong>')+'\'')
	}
	
	// Fire our Ajax request...
	requestString = destSKUOptionURL+productID+'&selectors='+selectorCount+'&src='+srcElement+requestString;	
	sendRequest('updateSKUOptions', requestString);
}


// Used to update the SKU images on the product details page...
destImageViewerURL = '/view/detail.php?mode=imageupdate&prodid=';
function updateSKUImages(productID, skuID, optionData) {
	
	debug('updateSKUImages called with productID: '+productID+', skuID: '+(skuID ? skuID : 'N/A')+', optionData: '+(optionData ? optionData : 'N/A'));
	
	if(productID) {
		// Fire our Ajax request...
		requestString = destImageViewerURL+productID+(skuID ? '&skuid='+skuID : '')+(optionData ? '&colouroptiondata='+optionData : '');	
		sendRequest('updateSKUImages', requestString);
	}
}


// Used to ensure that the product detail form can be submitted...
function validateProductSubmission() {
	
	debug('Add basket called with skuID: '+document.getElementById('skuid').value);
	if(document.getElementById('skuid').value == '0') return(false);

	return(true);	
}


// Image viewer related items follow...
currentImageResourceID = 0;											// This will be updated in-page via JavaScript output by PHP...
articleID = 0;														// This will be updated in-page via JavaScript output by PHP...

// Used to set the source image for the specified element...
function setSourceImage(element, source, resourceid) {
	currentImageResourceID = resourceid;
	document.getElementById(element).src = source;
}

// Used to set the source image's link URL...
function setLinkURL(element, url, target) { document.getElementById(element).href = url; document.getElementById(element).target = target ? target : null; }

// Used to set the source image's alt text for the specified element...
function setAlt(element, altText) { document.getElementById(element).alt = altText; return(true); }

// Used to set the source image's title text for the specified element...
function setTitle(element, titleText) { document.getElementById(element).title = titleText; return(true); }

// Used to set the source image's longdesc for the specified element...
function setLongDesc(element, descURI) { document.getElementById(element).longDesc = descURI; return(true); }			

// Used to set the source innerHTML for the specified elements...
function setSourceData(element, data) { document.getElementById(element).innerHTML = data; return(true); }
