// JavaScript Document
var emptyString = /^\s*$/ ;
var global_valfield;	// retain valfield for timer thread

function check_quant(quant,shape){
	if(emptyString.test(quant)){
		alert("Please provide quantity.");
		return false;
	}
	if(isNaN(quant)){
		alert("Quantity must be in numbers only.");
		return false;
	}
	if(shape=='Square End With Tassels' && quant<2){
		confirm("At least 2 pairs of tassels are required per Square End Runner. Please increase the quantity.");
		return false;
	}
	if(quant<1){
		alert("Tassels must be purchased, one pair of tassels must be purchased per runner.");
		return false;
	}
}

function change_shape(shape_index, img_name){
	if(shape_index==2){
		document[img_name].src = '../../images/pix/table_linen/Runners/Salim_Runner_Square_End.jpg';
		document[img_name].width="100";
		document[img_name].length="100";
		document.getElementById('buy_tassle').innerHTML='';
	}else if(shape_index==1){
		document[img_name].src = '../../images/pix/table_linen/Runners/Salim_Runner_With_Tassel.jpg';
		document[img_name].width="100";
		document[img_name].length="100";
		document.getElementById('buy_tassle').innerHTML='IMPORTANT:<br />Please note that tassels are NOT machine washable.';
	}else if(shape_index==0){
		document[img_name].src = '../../images/pix/table_linen/Runners/Salim_Runner_Without_Tassel.jpg';
		document[img_name].width='100';
		document[img_name].length='100';
		document.getElementById('buy_tassle').innerHTML='';
	}else if(shape_index==3){
		document[img_name].src = '../../images/pix/table_linen/Runners/Salim_Runner_Square_End_With_Tassel.jpg';
		document[img_name].width="100";
		document[img_name].length="100";
		document.getElementById('buy_tassle').innerHTML='IMPORTANT:<br />Please note that tassels are NOT machine washable.';
	}else{
		document[img_name].src = '../../images/pix/table_linen/Runners/test.jpg';
		document[img_name].width='100';
		document[img_name].length='100';
		document.getElementById('buy_tassle').innerHTML='';
	}	
}
var head=""; var colour_mat=""; var img="";
function change_material(img_name){
	img=img_name;
	//shape_index=document.getElementById('material').selectedIndex;
	var material=document.getElementById('material').value;
	if(material.match(/damas/i)){
		head='>rose'; colour_mat="Rose";
	}else if(material.match(/spectra/i)){
		head='>spectra'; colour_mat="Spectra";
	}else if(material.match(/polycotton/i)){
		head='>poly'; colour_mat="Polycotton";
	}else if(material.match(/ivy/i)){
		head='>ivy'; colour_mat="Ivy";
	}else if(material.match(/bystretch/i)){
		head='>bystretch'; colour_mat="ByStretch";
	}else if(material.match(/gingham/i)){
		head='>gin'; colour_mat="Gingham";
	}else if(material.match(/ottoman/i)){
		head='>ottoman'; colour_mat="Ottoman";
	}else if(material.match(/italia/i)){
		head='>italia'; colour_mat="Italia";
	}else if(material.match(/medit/i)){
		head='>mediterranean'; colour_mat="Mediterranean";
	}else if(material.match(/chinese/i)){
		head='>chinese'; colour_mat="Chinese";
	}else if(material.match(/roma/i)){
		head='>roma'; colour_mat="Roma";
	}else if(material.match(/organza/i)){
		head='>organza'; colour_mat="Organza";
	}else if(material.match(/repellent/i)){
		head='>repellent'; colour_mat="Repellent";
	}else if(material.match(/taffeta/i)){
		head='>taffeta'; colour_mat="Taffeta";
	}else if(material.match(/chenille/i)){
		head='>chenille'; colour_mat="Chenille";
	}else if(material.match(/madrid/i)){
		head='>madrid'; colour_mat="Madrid";
	}else if(material.match(/orchard/i)){
		head='>orchard'; colour_mat="Orchard";
	}else if(material.match(/circle/i)){
		head='>circle'; colour_mat="Circle";
	}else if(material.match(/check/i)){
		head='>check'; colour_mat="Check";
	}else if(material.match(/wild/i)){
		head='>wild'; colour_mat="Wild";
	}else if(material.match(/suede/i)){
		head='>suedette'; colour_mat="Suedette";
	}else if(material.match(/cosmos/i)){
		head='>cosmos'; colour_mat="Cosmos";
	}
	
	//var flag=false;
	if(document.getElementById && document.getElementsByTagName && document.createElement){
        // load the out-of-stock colours for the customisation
		xmlreqGET('../cgi-bin/outofstock.list',false);
		// load data file
        xmlreqGET('../cgi-bin/colours.list',true);
    }
}
var xmlreqs = new Array();

function CXMLReq(freed) {
	this.freed = freed;
	this.xmlhttp = false;
	if (window.XMLHttpRequest) {
		this.xmlhttp = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
}

function xmlreqGET(url,display) {
	var pos = -1;
	for (var i=0; i<xmlreqs.length; i++) {
		if (xmlreqs[i].freed == 1) { pos = i; break; }
	}
	if (pos == -1) { pos = xmlreqs.length; xmlreqs[pos] = new CXMLReq(1); }
	if (xmlreqs[pos].xmlhttp) {
		xmlreqs[pos].freed = 0;
		xmlreqs[pos].xmlhttp.open("GET",url,true);
		xmlreqs[pos].xmlhttp.onreadystatechange = function() {
			if (typeof(xmlhttpChange) != 'undefined') { xmlhttpChange(pos,display); }
		}
		if (window.XMLHttpRequest) {
			xmlreqs[pos].xmlhttp.send(null);
		} else if (window.ActiveXObject) {
			xmlreqs[pos].xmlhttp.send();
		}
	}
}

function xmlreqPOST(url,data) {
	var pos = -1;
	for (var i=0; i<xmlreqs.length; i++) {
		if (xmlreqs[i].freed == 1) { pos = i; break; }
	}
	if (pos == -1) { pos = xmlreqs.length; xmlreqs[pos] = new CXMLReq(1); }
	if (xmlreqs[pos].xmlhttp) {
		xmlreqs[pos].freed = 0;
		xmlreqs[pos].xmlhttp.open("POST",url,true);
		xmlreqs[pos].xmlhttp.onreadystatechange = function() {
			if (typeof(xmlhttpChange) != 'undefined') { xmlhttpChange(pos); }
		}
		xmlreqs[pos].xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xmlreqs[pos].xmlhttp.send(data);
	}
}
var outofstock=new Array();
var g_oos=new Array();
function xmlhttpChange(pos,display) {
	if (typeof(xmlreqs[pos]) != 'undefined' && xmlreqs[pos].freed == 0 && xmlreqs[pos].xmlhttp.readyState == 4) {
		if (xmlreqs[pos].xmlhttp.status == 200 || xmlreqs[pos].xmlhttp.status == 304) {
			if(display==true){
				displayData(xmlreqs[pos].xmlhttp.responseText.split('\n'));
			}else{
				// load up the out-of-stock colours
				outofstock=xmlreqs[pos].xmlhttp.responseText.split('\n');
				for(i=0;i<outofstock.length;i+=2){
					var mat=outofstock[i].split(/[\s\n]+/);
					//alert(mat[0]+"::"+head);
					if(mat[0]==head){
						var oos_colours=new Array();
						oos_colours=outofstock[i+1].split(",");
						oos_colours.pop(); g_oos[head]=oos_colours;
					}
				}
			}
		} else {
			handle_error();
		}
		xmlreqs[pos].freed = 1;
	}
}
function displayData(data){
	var select_menu=document.getElementById('colour_menu');
	select_menu.options.length=0;
	//alert(outofstock);
	for(i=0;i<data.length;i+=2){
		var mat=data[i].split(" ");
		if(mat[0]==head){
			//alert(data[i]);
			var colours=data[i+1].split(",");
			colours.pop();
			// initiate an index adjustment variable
			var adjust=0;
			for(w=0;w<colours.length;w++){
				var pattern=/^(\w+).*Out of Stock.*$/i;
				var colour=""; //var ar=oos_colours[head];
				//alert(g_oos);
				if(typeof(g_oos[head])!= 'undefined'){
					if(g_oos[head].has(colours[w])){
						adjust++;
						continue;
					}
				}
				if(matches=colours[w].match(pattern)){
					colour=matches[1];
				}else{
					colour=colours[w];
				}
				select_menu.options[w-adjust]=new Option(colours[w],colour_mat+"_"+colour, false, false);
			}
		}
	}
	change_colour(0,img);
}
function handle_error(){
	alert("An error has occurred!");
}
function change_colour(colour_index,img_name){
	var colour_menu=document.getElementById('colour_menu');
	document[img_name].width="100";
	document[img_name].length="100";
	document[img_name].border="1px";
	document[img_name].src ='../../images/pix/table_linen/Colour_Scans/'+colour_menu.options[colour_index].value+'.jpg';
	document.getElementById('img_path').value='../../images/pix/table_linen/Colour_Scans/'+colour_menu.options[colour_index].value+'.jpg';
}
function change_tcolour(colour_index,img_name){
	var colour_menu=document.getElementById('tcolour');
	document[img_name].width="80";
	document[img_name].length="30";
	document[img_name].border="1px";
	document[img_name].src ='../../images/pix/table_linen/Colour_Scans/Tassel_'+colour_menu.options[colour_index].value+'.jpg';
	document.getElementById('timg_path').value='../../images/pix/table_linen/Colour_Scans/Tassel_'+colour_menu.options[colour_index].value+'.jpg';
}

function happy(field, infofield){
	var returnval;
	var num = parseFloat(field.value);
	var para=document.getElementById(infofield);
	para.style.color="#cc3333";
	if (emptyString.test(field.value)) {
      para.innerHTML="&nbsp;&nbsp;&nbsp;ERROR: required";
      setfocus(field);
      return false;
    }
	if(!isNaN(field.value)){
		returnval = true;
		field.value=num.toFixed(0);
		para.innerHTML="";
	}else{
		para.innerHTML="&nbsp;&nbsp;&nbsp;ERROR, Invalid input";
		setfocus(field);
		returnval = false;
	}
	return returnval;
}
function setFocusDelayed()
{
  global_valfield.focus();
}

function setfocus(valfield)
{
  // save valfield in global variable so value retained when routine exits
  global_valfield = valfield;
  setTimeout( 'setFocusDelayed()', 100 );
}
function validate(){
	var returnval=false;
	//var shape=parseInt(document.step2.shape.value);
	var objects = new Array();
	objects[0]=document.getElementById('length').value;
	objects[1]=document.getElementById('width').value;
	objects[2]=document.getElementById('quantity').value;
	
	for(var i=0;i<objects.length;i++){
		if(emptyString.test(objects[i])){
			alert("One or More Fields Are Empty");
			return false;
		}
		if(isNaN(objects[i])){
			alert("One or More Fields Need Correcting Before Submitting");
			return false;
		}
		if(objects[i]<1){
			alert("The length, width or quantity cannot equal zero!!");
			return false;
		}
	}

}
function previous_form(form,url){
	form.action = url;
	form.submit();
}
Array.prototype.has = function(value) {
var i;
for (var i = 0, loopCnt = this.length; i < loopCnt; i++) {
	if (this[i] === value) {
	return true;
	}
}
return false;
};