/***********************************************
			JavaScript functions for 
		www.vertical-blinds-direct.co.uk
				CJH Oct2004
***********************************************/

// make sure the widths and drops are within range
function validate(form, min_width, max_width, min_drop, max_drop, metric)
{
	var width, drop;
	
	if ( isNaN(parseFloat(form.width.value)) || isNaN(parseFloat(form.drop.value)) ) {
		alert('Please enter numbers only.');
		return false;
	}
	
	if (!metric)
	{
		width = (parseInt(form.width.value) + form.width_fraction.value / 8) * 2.54;
		drop = (parseInt(form.drop.value) + form.drop_fraction.value / 8) * 2.54;
	}
	else
	{
		width = parseFloat(form.width.value);
		drop = parseFloat(form.drop.value);
	}
	if (width < min_width) {
		alert('Width must be greater than ' + min_width + 'cm. Please re-enter and try again.');
		return false;
	}
	if (width > max_width) {
		alert('Width must be less than ' + max_width + 'cm. Please re-enter and try again.');
		return false;
	}
	if (drop < min_drop) {
		alert('Drop must be greater than ' + min_drop + 'cm. Please re-enter and try again.');
		return false;
	}
	if (drop > max_drop) {
		alert('Drop must be less than ' + max_drop + 'cm. Please re-enter and try again.');
		return false;
	}
	return true;
}
	
// scroll the price into view
function scrollToPrice()
{
	//priceField = document.getElementById("price")
	if (!document.getElementById) return;
	addToCart = document.getElementById("addtocart");
	if (addToCart) addToCart.scrollIntoView(false);
	else if (document.location.href.indexOf('width=') > 0){
		priceField = document.getElementById("options")
		priceField.scrollIntoView(false);
	}
}


// Shows pop-up property description when property image is hovered over
function showDescription(elem, chars)
{
	e = elem.parentNode.childNodes[2];
	//alert(e.className);
	e.style.display="block";
	//height = (chars/31)*20 + 'px';
	//alert(height);
	//e.style.height = 'auto';//height;
	rhsDiv = document.getElementById("properties");
	var width = rhsDiv.offsetWidth-50;
	//alert(width);
		e.style.width = width;//'200px';//width;
	//e.offsetWidth = width; //'200px';
	//alert(width);
}

// Hides pop-up property description
function hideDescription(elem)
{
	e = elem.parentNode.childNodes[2];
	//alert(e.className);
	e.style.display="none";
	
}

function getElementHeight(Elem) {
	if (ns4) {
		var elem = getObjNN4(document, Elem);
		return elem.clip.height;
	} else {
		if(document.getElementById) {
			var elem = document.getElementById(Elem);
		} else if (document.all){
			var elem = document.all[Elem];
		}
		if (op5) { 
			xPos = elem.style.pixelHeight;
		} else {
			xPos = elem.offsetHeight;
		}
		return xPos;
	} 
}

function getElementWidth(Elem) {
	if (ns4) {
		var elem = getObjNN4(document, Elem);
		return elem.clip.width;
	} else {
		if(document.getElementById) {
			var elem = document.getElementById(Elem);
		} else if (document.all){
			var elem = document.all[Elem];
		}
		if (op5) {
			xPos = elem.style.pixelWidth;
		} else {
			xPos = elem.offsetWidth;
		}
		return xPos;
	}
}
var intervalID;
function updatePrice(frm)
{
// Update total price when option is changed

var extra = 0; // extra costs

// determine which size entry form contains the price field
if (frm.name=="rmoptions") pfield = document.getElementById("price_readymade");
else pfield = document.getElementById("price_madetomeasure");

// get the basic price from the size entry form
price = parseFloat(pfield.value.substring(pfield.value.indexOf("£")+1) );

// loop through option-select elements and see if they incur extra cost
for (var i=0; i<frm.elements.length; i++)
{
	if (frm.elements[i].type == "select-one") {
	//var regex = /\(£(\d+\.\d\d)\)/;
	var regex = /\((\+\%(\d\d)|£(\d+\.\d\d))\)/;
	var result = regex.exec(frm.elements[i].options[frm.elements[i].selectedIndex].text);
	if (result) {
		if (result[0].indexOf('%')>0) extra += parseInt(result[2])*price/100;
		else extra += parseFloat(result[3]);
	}
	}
}

// add the extra costs to the basic price
var total = (price + extra).toFixed(2);

// update the total price on the page
if (frm.name=="rmoptions") elem = document.getElementById("rmtotalprice");
else elem = document.getElementById("mtmtotalprice");
elem.firstChild.nodeValue = 'Total: £' + total;
}

// Check whether any options should be enabled/disabled according to chosen value of given option
function checkDependencies(optionElem)
{
	frm = optionElem.form;
	changedOptionId = /*parseInt*/(optionElem.name.substring(3));

	// Loop through the options for this blind and see if any of them are dependent on the option just changed
	for (var i=0; i<frm.elements.length; i++)
	{
		if (frm.elements[i].type == "select-one") {
		optionId = /*parseInt*/(frm.elements[i].name.substring(3));
		//alert(optionId + "  -->  " + changedOptionId);
		if (optionId!=changedOptionId)
		{
			var exclude_vals;
			// Check if there is an 'exclude value' for this option -i.e. a value which when chosen makes the dependent option redundant
			if ( (opt_dep[String(optionId)]))
			{
			//exclude_val = opt_dep[(optionId)][(changedOptionId)];
			exclude_vals = opt_dep[(optionId)][(changedOptionId)];
			if (exclude_vals)
			{ 
				for (var exclude_val_index in exclude_vals)
				{
					var exclude_val = exclude_vals[exclude_val_index];
					// Check if the current value for the changed option matches an exclude value
					if (optionElem.options[optionElem.selectedIndex].value==exclude_val) {
						//frm.elements[i].parentNode.parentNode.style.display='none';
						//frm.elements[i].parentNode.parentNode.style.visibility='collapse';
						frm.elements[i].parentNode.parentNode.className="unavailable-option";
						frm.elements[i].disabled=true;
						break;
						
					}
					else
					{
						//frm.elements[i].parentNode.parentNode.style.display='';
						//frm.elements[i].parentNode.parentNode.style.visibility='visible';
						frm.elements[i].parentNode.parentNode.className="";	
						frm.elements[i].disabled=false;
						
					}
				}
			}
			}
			
		}
		}

	}
}
function initOptions()
{
	// Remove any options that are not available given default option values
	var frm;
	if (frm = document.forms['mtmoptions'])
	{
		for (var i=0; i<frm.elements.length; i++)
		{
			if (frm.elements[i].type == "select-one") {
			if (frm.elements[i].name.substring(0,3)=='opt')
			{
				optionElem = frm.elements[i];
				checkDependencies(optionElem);
			}
			}
		}
	}
}


var w = null;

function showOptionImages(option_id, frm, num_values, db)//option_id, option_name, images_array)
{
// open a window to show graphical representations of available option choices
var url = 'options.php?id=' + option_id;
if (frm) url += '&frm=' + frm;
url+='&db=' + db;					// database VBD or RBD
if (w && !w.closed) { w.close();}
var height = num_values * 20 + 200;
if (!num_values) height=600;
if (height>600) height=600;
//var width = num_values * 30 + 300;
var width=700;

// Window may open in a tab, so don't try and size it!
//w=window.open(url, 'Options', 'width=' + width + ', height=' + height + ',left=200,top=180,toolbar=0,location=0,directories=0,status=0,menuBar=0,scrollBars=1,resizable=0'); 
w=window.open(url, 'Options', 'toolbar=0,location=0,directories=0,status=0,menuBar=0,scrollBars=1'); 
}

function showGallery(id)
{
var url = 'gallery.php?id=' + id;
if (w && !w.closed) { w.close();}
w=window.open(url, 'Gallery', 'width=520, height=620, left=200,top=50,toolbar=0,location=0,directories=0,status=0,menuBar=0,scrollBars=1,resizable=0'); 
}

function showDetail()
{
var elem = document.getElementById('extra_details');
//alert('test1');
if (elem.style.display!="block")
{
	//alert('test');
	//elem.style.position="absolute";
	elem.style.left="-220px";
	elem.style.display="block";
	intervalID=setInterval("slideIn()", 10);
	var link = document.getElementById('detail_link');
	link.firstChild.nodeValue="Hide Extra Details <<";
	link.setAttribute('title', 'Hide Extra Details');
}
else
{
	elem.style.display="none";
	var link = document.getElementById('detail_link');
	link.firstChild.nodeValue="Show Extra Details >>";
	link.setAttribute('title', 'Show Extra Details');
	clearInterval(intervalID);
}
}
function slideIn()
{
elem = document.getElementById('extra_details');
//var width = elem.style.width;
//for (var x=-width; x<80; x+=10)
var x = parseInt(elem.style.left);
if (x<20)
{
	x+=10;
	elem.style.left = x + 'px';
	//alert(elem.style.left);
	//var str = "slideIn(\'elem\')";
	//setTimeout(str,10);
}

}
