/*
DUTCH MAJESTIC: js/jsfunctions.js //-->
This file contains all the javascript which mainly the shop
needs in order to function properly.
*/

function showDetails(item_id) {

	// display the shirt details and reset the shirt images
	fetchById('image_details').src = 'layout/spacer.gif';
	fetchById('image_zoom1').src = 'layout/spacer.gif';
	fetchById('details').style.display = 'inline';
	
	// reset the size and quantity options
	fetchById('size').options.length = 0;
	fetchById('category').options.length = 0;
	fetchById('quantity').value = 1;
	
	// define some variables and fill the size and category options
	var size = '• size: ';
	var sizes = new Array('S', 'M', 'L', 'XL');
	var category = '';
	var categories = new Array('men', 'women', 'children');
	
	var shirt_id = eval('shirt_' + item_id);
	fetchById('size').options[fetchById('size').options.length] = new Option('(shirt size)', '0');
	fetchById('size').options[0].style.color = '#666633';
	fetchById('category').options[fetchById('category').options.length] = new Option('(category)', '0');
	fetchById('category').options[0].style.color = '#666633';
	
	// size options
	for (var i = 0; i < shirt_id[2].length; i++) {
		if (shirt_id[2][i]) {
			size += sizes[i];
			fetchById('size').options[fetchById('size').options.length] = new Option('' + shirt_id[2][i], shirt_id[2][i]);
		}
		else {
			size += '<span style="color:#CCCC99;">' + sizes[i] + '</span>';
		}
		
		if (i < shirt_id[2].length-1) {
			size += ', ';
		}
	}
	
	// category options
	for (var i = 0; i < shirt_id[3].length; i++) {
		if (shirt_id[3][i]) {
			category += shirt_id[3][i];
			fetchById('category').options[fetchById('category').options.length] = new Option('' + shirt_id[3][i], shirt_id[3][i]);
		}
		
		else {
			category += '<span style="color:#CCCC99;">' + categories[i] + '</span>';
		}
		
		if (i < shirt_id[3].length-1) {
			category += ' / ';
		}
	}
	
	// load the appropriate images and set the details
	setInnerHTML(fetchById('shirt_name'), '&nbsp;&nbsp;' + shirt_id[0][0] + '&nbsp;&nbsp;');
	setInnerHTML(fetchById('shirt_details'), '• &#8364; ' + shirt_id[1][0] + ' / $ ' + shirt_id[1][1] + '<br />' + size + '<br />• ' + category);
	setInnerHTML(fetchById('announcement'), shirt_id[4][0]);
	fetchById('image_details').src = 'image_shop.php?q=' + item_id + '/image_details';
	fetchById('image_zoom1').src = 'image_shop.php?q=' + item_id + '/image_zoom';

	fetchById('zoom').onmouseover = function() { 
		fetchById('image_details').src = 'image_shop.php?q=' + item_id + '/image_zoom';
		fetchById('image_zoom1').src = 'image_shop.php?q=' + item_id + '/image_details';
	}
	fetchById('zoom').onmouseout  = function() {
		fetchById('image_details').src = 'image_shop.php?q=' + item_id + '/image_details';
		fetchById('image_zoom1').src = 'image_shop.php?q=' + item_id + '/image_zoom';
	}
	
	fetchById('shirt').value = item_id;
	fetchById('timestamp').value = new Date().getTime();
}

function hideDetails() {
	var item_id = fetchById('shirt').value;
	fetchById('details').style.display = 'none';
	
	fetchById('size').options.length = 0;
	fetchById('category').options.length = 0;
}

function setAddQuery() {
	if ((fetchById('size').value != '0') && (fetchById('category').value != '0')) {
		if (confirm('Are you sure you want to add this item to your basket?')) {
			fetchById('add').href = 'basket.php?action=add;order=' + fetchById('timestamp').value + '/' + fetchById('shirt').value + '/' + fetchById('quantity').value + '/' + fetchById('category').value + '/' + fetchById('size').value;
		}
	}
	else {
		alert('Please fill out the \'shirt size\' and \'category\' fields first!');
	}
}

function setOrderQuery() {
	if ((fetchById('size').value != '0') && (fetchById('category').value != '0')) {
		if (confirm('Are you sure you want to order this item?')) {
			fetchById('order').href = 'basket.php?action=add;direct=1;order=' + fetchById('timestamp').value + '/' + fetchById('shirt').value + '/' + fetchById('quantity').value + '/' + fetchById('category').value + '/' + fetchById('size').value;
		}
	}
	else {
		alert('Please fill out the \'shirt size\' and \'category\' fields first!');
	}
}

function selectAllOptions() {
//	var selectArray = document.getElementsByTagName('select');
//	for (var x = 0; x < (selectArray.length)-3; x++) {
//		selectArray[x].multiple = true;
//		for (var y = 0; y < selectArray[x].options.length; y++) {
//			selectArray[x].options[y].selected = true;
//		}
//	}
	var soap_values = new Array();
	for (var i = 0; i < fetchById('soap_published').options.length; i++) {
		soap_values[i] = fetchById('soap_published').options[i].value;
	}
	var card_values = new Array();
	for (var i = 0; i < fetchById('cards_published').options.length; i++) {
		card_values[i] = fetchById('cards_published').options[i].value;
	}
	fetchById('soap_pub').value = soap_values.join('|');
	fetchById('card_pub').value = card_values.join('|');
	
	//alert (	fetchById('soap_pub').value + ' ' + fetchById('card_pub').value );
}

function addOption(section) {
	var unpublished = fetchById(section + 'unpublished');
	var unpublished_options = unpublished.options;
	var unpublished_index = unpublished.selectedIndex;
	
	var published = fetchById(section + 'published');
	var published_options = published.options;
	
	if (unpublished_index > -1 && unpublished.multiple == false && published.multiple == false) {
		published_options[published_options.length] = new Option(unpublished[unpublished_index].text, unpublished[unpublished_index].value);
		unpublished[unpublished_index] = null;
	}
}

function removeOption(section) {
	var unpublished = fetchById(section + 'unpublished');
	var unpublished_options = unpublished.options;
		
	var published = fetchById(section + 'published');
	var published_options = published.options;
	var published_index = published.selectedIndex;
	
	if (published_index > -1 && unpublished.multiple == false && published.multiple == false) {
		unpublished_options[unpublished_options.length] = new Option(published[published_index].text, published[published_index].value);
		published[published_index] = null;

		sortSelect(unpublished);
	}
}

function sortSelect(selectObject) {
	var list = new Array();
	for (var i = 0; i < selectObject.options.length; i++) {
		list[list.length] = new Option( selectObject.options[i].text, selectObject.options[i].value) ;
	}

	list = list.sort( 
		function(a,b) { 
			if ((a.text+"") < (b.text+"")) { return -1; }
			if ((a.text+"") > (b.text+"")) { return 1; }
			return 0;
			} 
		);

	for (var i = 0; i < list.length; i++) {
		selectObject.options[i] = new Option(list[i].text, list[i].value);
	}
}

function sendCard() {
	if ( (fetchById('sender').value == '') || (fetchById('sender_email').value == '') || (fetchById('recipient').value == '') || (fetchById('recipient_email').value == '') || (fetchById('message').value == '') ) {
		alert('All fields are required!');
	}
	else {
		fetchById('ecards').submit();
	}
}

// general form stuff
function doConfirm(message, url) {
	if (confirm(message)) {
		location.href = url;
	}
}

function submitonce(theform) {
	if (fetchById) {
		for (i=0;i<theform.length;i++) {
			var tempobj=theform.elements[i];
			if(tempobj.type.toLowerCase()=="submit"||tempobj.type.toLowerCase()=="reset") {
				tempobj.disabled=true;
			}
		}
	}
}

function clearText(thefield) {
	if (thefield.defaultValue==thefield.value)
	thefield.value = ""
}

function setInnerHTML(element, toValue) {
	if (typeof(element.innerHTML) != 'undefined') {
		element.innerHTML = toValue;
	}
	else {
		var range = document.createRange();
		range.selectNodeContents(element);
		range.deleteContents();
		element.appendChild(range.createContextualFragment(toValue));
	}
}

function fetchById(id) {
	return document.getElementById ? document.getElementById(id) : document.all[id];
}

function check_delete() {
	if (fetchById) {
		var count = 0;
		for (i = 0; i < fetchById('mailing').length; i++) {
			var tempobj = fetchById('mailing').elements[i]
			if (tempobj.type.toLowerCase() == "checkbox") {
				if (tempobj.checked == true) {
					count++;
				}
			}
		}
		return count;
	}	
}


function MM_reloadPage(init) { 
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);