function increase_quantity(myname,dosub,button)
{
	var thing=myGetElementById(myname);
	thing.value=parseInt(thing.value);
	if(thing.value < 1 || isNaN(thing.value))
	{
		thing.value=0;
	}
	thing.value++;
	// add in action
	if(dosub==1)
	{
		var a = document.createElement('input');
		a.setAttribute('type', 'hidden');
		a.setAttribute('value', button);
		a.setAttribute('name', 'action');
		document.forms['basket_form'].appendChild(a);
		document.forms['basket_form'].submit();
	}
}

function decrease_quantity(myname,dosub,button)
{
	var thing=myGetElementById(myname);
	thing.value=parseInt(thing.value);
	if(thing.value > 0)
	{
		thing.value--;
	}
	else
	{
		thing.value=0;
	}
	if(dosub==1)
	{
		var a = document.createElement('input');
		a.setAttribute('type', 'hidden');
		a.setAttribute('value', button);
		a.setAttribute('name', 'action');
		document.forms['basket_form'].appendChild(a);
		document.forms['basket_form'].submit();
	}
}

function remove_item(myname,button)
{
	var thing=myGetElementById(myname);
	thing.value=parseInt(thing.value);
	thing.value=0;

	// add in action
	var a = document.createElement('input');
	a.setAttribute('type', 'hidden');
	a.setAttribute('value', button);
	a.setAttribute('name', 'action');
	document.forms['basket_form'].appendChild(a);
	document.forms['basket_form'].submit();
}

function doPopUp(myUrl,winWidth,winHeight)
{
	newWindow=window.open(myUrl,'productImageDetail','height='+winHeight+',width='+winWidth+',toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no');
	newWindow.resizeTo(winWidth+2,winHeight+30);
	newWindow.focus();
}

// check what key was entered to trigger the form submission and if necessary add
// form variable
function check_keycode(myname)
{
	var keycode = window.event.keyCode;
	function kH(e)
	{
		var pK = e ? e.which : window.event.keyCode;
		return pK != 13;
	}
}

function genUniqID()
{
	var id = 'u_';
	for(x=0; x < 30; x++)
	{
		id = id + String.fromCharCode(Math.floor(Math.random() * 26) + 65);
	}
	return id;
}

function set_distressing(myID,targetID)
{
	var m = myGetElementById(myID);
	if(m)
	{
		var typeID = prod_material_list[(m[m.selectedIndex].value)]["finish"];
		var t = myGetElementById(targetID);
		if(typeID == 1 || typeID == 3)
		{
			t.options.length=3;
			t.options[0].value='';
			t.options[0].text='none';
			t.options[1].value='light';
			t.options[1].text='light';
			t.options[2].value='heavy';
			t.options[2].text='heavy';
		}
		else
		{
			t.options.length=1;
			t.options[0].value='';
			t.options[0].text='none';
		}
	}
	else
	{
		alert('BAD ID ' + myID);
	}

}


function populate_Colors(parentID,childID,gchildID)
{
	// var typeID = prod_material_list[materialID]["finish"];
	var c = myGetElementById(childID); // main colour
	var gc = myGetElementById(gchildID); // 2nd colour
	var m = myGetElementById(parentID); //material
	// form not always present
	if(c)
	{
		// alert(typeID);
		c.length=1;
		c.options[0].value='';
		c.options[0].text='Choose a colour. . .';
	}
	if(gc)
	{
		gc.length=1;
		gc.options[0].value='';
		gc.options[0].text='n/a';
	}

	if(m)
	{
		var typeID = prod_material_list[(m[m.selectedIndex].value)]["finish"];

		// now cycle through global style
		if(prod_colour_list[typeID] != null)
		{

			//alert(ColorID);
			if(prod_colour_list[typeID].length>0)
			{
				for(var x=0; x < prod_colour_list[typeID].length; x++)
				{
					c.length++;
					c.options[c.length-1].value = prod_colour_list[typeID][x]['id'];
					c.options[c.length-1].text = prod_colour_list[typeID][x]['title'];
				}
			}
		}

		show_colour_layer('mc_' + typeID );
	}
}

function populate_Colors2(parentID,childID)
{
	// var typeID = prod_material_list[materialID]["finish"];
	var c = myGetElementById(childID);
	var p = myGetElementById(parentID);
	var ci = false;

	var typeID = 0;

	// alert( p[p.selectedIndex].value );

	// first find parent
	// alert(p[p.selectedIndex].text + ' ' + p[p.selectedIndex].value);
	for(var x=1; x<4; x++)
	{
		if( prod_colour_list[x].length > 0)
		{
			for(y=0; y < prod_colour_list[x].length; y++)
			{
				if( prod_colour_list[x][y]['id'] == parseInt(p[p.selectedIndex].value) )
				{
					typeID = x;
					//alert( 'Got type: ' + typeID );
					// highlight colour that has been selected
					break;
				}
			}
		}
	}
	// see if the primary colour is paint
	if(typeID == 3)
	{

		c.length=1;
		c.options[0].value='';
		c.options[0].text='Choose a stain colour. . .';

		for(var x=0; x < prod_colour_list[1].length; x++)
		{
			if(prod_colour_exclusive_list.find(p[p.selectedIndex].value) && prod_colour_exclusive_list.find(prod_colour_list[1][x]['id'])) continue;

			c.length++;
			c.options[c.length-1].value = prod_colour_list[1][x]['id'];
			c.options[c.length-1].text = prod_colour_list[1][x]['title'];

		}
	}

	if(typeID)
	{
		highlight_colour(parentID);
	}
}


function highlight_colour(parentID)
{
	var p = myGetElementById(parentID);
	if(!p) return false;

	for(x=1; x < p.length; x++)
	{
		//alert('selected: ' + p[x].value);
		// now highlight whatever we need to
		if(p[x].value != "" && ! isNaN(p[x].value))
		{
			ci = myGetElementById('mc_colour_' + parseInt(p[x].value));
		}
		if(ci)
		{
			if(p[x].value == p[p.selectedIndex].value)
			{
				ci.style.borderColor='black';
				//ci.style.float='left';
				// ci.style.padding='.2em';
				ci.style.borderStyle='solid';
				ci.style.borderBottomWidth='thin';
				//ci.style.borderWidth='.2em';
			}
			else
			{
				// alert('mc_colour_' + parseInt(prod_colour_list[x][y]['id']));
				ci.style.borderColor='transparent';
				ci.style.borderBottomWidth='thin';
				// ci.style.float='left';
				// ci.style.padding='.2em';
				//ci.style.borderWidth='.2em';
				//ci.style.borderStyle='solid';
			}
		}
	}
}

function set_material_selection(myID,mySelection)
{
	var m = myGetElementById(myID);

	if(m)
	{
		for(x=0; x < m.length; x++)
		{
			if(m[x].value == mySelection)
			{
				m.selectedIndex = x;
				return true;
			}
		}
	}
}

function set_material_underline(myID)
{

	for( id in prod_material_list)
	{
		if(id != false && id != 'find' && id != 0)
		{

			o = myGetElementById('mc_url_' + id);
			if( o )
			{
				if(id == parseInt(myID))
				{
					o.style.textDecoration = 'none';
				}
				else
				{
					o.style.textDecoration = 'underline';
				}
			}
		}
	}
}

function show_colour_layer(myID)
{
	// alert('MYID=' + myID);
	// var c = myGetElementById(myID);
	var c = MM_findObj(myID);
	//var o = false;
	var colour_div_list = Array( 'mc_1' , 'mc_2' , 'mc_3');

	for(x=0; x<colour_div_list.length; x++)
	{
		if(myID != colour_div_list[x] && colour_div_list[x] != 'find')
		{
			MM_changeProp(colour_div_list[x],'style.display','none')
			/*
			o = myGetElementById(colour_div_list[x]);
			if(o)
			{
				// alert('setting to none ID=' + colour_div_list[x]);
				// o.style.display = 'none';
				if(!isNaN(o.setStyle))
				{
					o.setStyle("visibility", "hidden");
					o.setStyle("display", "none");
				}
				else
				{
					o.style.display = 'none';
					o.style.visibility = 'hidden';
				}
			}
			*/
		}
	}
	if(c)
	{
		MM_changeProp(myID,'style.display','block')
		// c.style.display = 'block';
		// c.style.visibility = 'visible';

		if(myID == 'mc_3')
		{
			var o = MM_findObj('mc_1');
			// o = myGetElementById('mc_1');
			if(o)
			{
				MM_changeProp('mc_1','style.display','block')
				/*
				if(!isNaN(o.setStyle))
				{
					// o.style.display = 'block';
					o.setStyle("visibility", "visible");
					o.setStyle("display", "block");
				}
				else
				{
					o.style.display = 'block';
					o.style.visibility = 'visible';
				}
				*/
			}
		}
	}
}

function MM_findObj(n, d)
{ //v4.01
	var p,i,x;
	if(!d) d=document;
	if((p=n.indexOf("?"))>0 && parent.frames.length)
	{
		d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);
	}
	if(!(x=d[n])&&d.all) x=d.all[n];
	for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
	if(!x && d.getElementById) x=d.getElementById(n);
	return x;
}

function MM_changeProp(objName,theProp,theValue)
{ //v6.0
	var obj = MM_findObj(objName);
	if (obj && (theProp.indexOf("style.")==-1 || obj.style))
	{
		if (theValue == true || theValue == false) eval("obj."+theProp+"="+theValue);
		else eval("obj."+theProp+"='"+theValue+"'");
	}
}


function myGetElementById(divID)
{
	if( document.getElementById )
	{
    		return document.getElementById(divID);
    	}
	if( document.all )
	{
		return document.all[divID];
	}
	if( !oDoc ) { oDoc = document; }
	if( document.layers ) {
		if( oDoc.layers[divID] ) { return oDoc.layers[divID]; }
		else
		{
			//repeatedly run through all child layers
			for( var x = 0, y; !y && x < oDoc.layers.length; x++ )
			{
			//on success, return that layer, else return nothing
				y = getRefToDiv(divID,oDoc.layers[x].document);
			}
			return y;
		}
	}
	return false;
}

function showDiv(divID_as_a_string)
{
  	//get a reference as above ...
	myReference = getRefToDiv(divID_as_a_string);
	if( !myReference )
	{
		window.alert('Nothing works in this browser');
		return; //don't go any further
	}
	//now we have a reference to it
	if( myReference.style ) {
		//DOM & proprietary DOM
		myReference.style.visibility = 'visible';
	}
	else
	{
		//layers syntax
		myReference.visibility = 'show';
	}
}

function hideDiv(divID_as_a_string)
{
  	//get a reference as above ...
	myReference = getRefToDiv(divID_as_a_string);
	if( !myReference )
	{
		window.alert('Nothing works in this browser');
		return; //don't go any further
	}
	//now we have a reference to it
	if( myReference.style ) {
		//DOM & proprietary DOM
		myReference.style.visibility = 'hidden';
	}
	else
	{
		//layers syntax
		myReference.visibility = 'hide';
	}
}


function verify_order(materialID,colourID,colour2ID)
{
	// make sure that form details are completed
	var err = 0;
	m = myGetElementById(materialID);
	if(parseInt(m[m.selectedIndex].value)<1 || isNaN(parseInt(m[m.selectedIndex].value)))
	{
		alert( 'Please select a material.' );
		err = 1;
	}
	else
	{
		c = myGetElementById(colourID);
		if(parseInt(c[c.selectedIndex].value)<1 || isNaN(parseInt(c[c.selectedIndex].value)))
		{
			alert( 'Please select a colour.' );
			err = 1;
		}
	}
	if(err) return false;
	else return true;
}

function verify_quote()
{
	// make sure that form details are completed
	var err = 0;

	m = myGetElementById('product_material_1');
	c = myGetElementById('product_colour_1');
	qe = myGetElementById('quote_email');
	qn = myGetElementById('quote_name');
	qp = myGetElementById('quote_phone');
	if(parseInt(m[m.selectedIndex].value)<1 || isNaN(parseInt(m[m.selectedIndex].value)))
	{
		alert( 'Please select a material.' );
		err = 1;
	}
	else if(parseInt(c[c.selectedIndex].value)<1 || isNaN(parseInt(c[c.selectedIndex].value)))
	{
		alert( 'Please select a colour.' );
		err = 1;
	}
	else if(! /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(qe.value))
	{
		alert( 'Please enter a valid email address.');
		qe.value='';
		qe.focus();
		err = 1;
	}
	else if(qn.value.length<5)
	{
		alert( 'Please enter your name.' );
		qn.focus();
		err = 1;
	}
	else if(qp.value.length<7)
	{
		alert( 'Please enter your phone number.' );
		qp.focus();
		err = 1;
	}
	if(err) return false;
	else return true;
}

Array.prototype.find = function (s)
{
	for(var i=0;i<this.length;i++) if(this[i] == s) return true;
	return false;
};

function doSubmitIf(myform,myid,myvalue,mysubmit)
{
	m = myGetElementById(myid);
	if(m[m.selectedIndex].value==myvalue)
	{
		var a = document.createElement('input');
		a.setAttribute('type', 'hidden');
		a.setAttribute('value', mysubmit);
		a.setAttribute('name', 'action');
		document.forms[myform].appendChild(a);
		document.forms[myform].submit();
	}

}
