// JavaScript Document
function swap_towel(colour_index,img_name,color_menuID,path){
	var colour_menu=document.getElementById(color_menuID);
	document[img_name].width="150";
	document[img_name].length="150";
	document[img_name].border="1px";
	document[img_name].src =path+colour_menu.options[colour_index].title+'.jpg';
}
function swap_colour(colour_index,img_name,color_menuID,mat){
	var colour_menu=document.getElementById(color_menuID);
	if(mat=='Spectra Plain'){
		mat="Spectra";
	}else if(mat=='Damask Rose'){
		mat="Rose";
	}else if(mat=='Ivy Leaf'){
		mat="Ivy";
	}else if(mat=='Roma Glitter'){
		mat="Roma";
	}else if(mat=='Chinese Fan'){
		mat="Chinese";
	}else if(mat=='Shield'){
		mat="Shield";
	}else if(mat=='Alamo'){
		mat="Alamo";
	}
	document[img_name].border="1px";
	document[img_name].src ='../../images/pix/table_linen/Colour_Scans/'+mat+'_'+colour_menu.options[colour_index].value+'.jpg';
}
function swap_colour2(colour_index,img_name,color_menuID,mat,path){
	var colour_menu=document.getElementById(color_menuID);
	document[img_name].width="150";
	document[img_name].length="150";
	document[img_name].border="1px";
	document[img_name].src =path+'/'+mat+'_'+colour_menu.options[colour_index].value+'.jpg';
}
function standinaIMG_swapper(img_name,standina){
	document[img_name].border="1px";
	document[img_name].src ='pix/Table%20linen/Standina/'+standina+'.jpg';
}
function quant_discount(quant,hprice_id,dprice_id,oprice,sprice){
	hprice=document.getElementById(hprice_id);
	dprice=document.getElementById(dprice_id);
	
	if(quant>=50){
		hprice.value=sprice;
		dprice.innerHTML='&euro;'+sprice;
	}else{
		hprice.value=oprice;
		dprice.innerHTML='&euro;'+oprice;
	}
}

var head=""; var colour_mat=""; var img=""; var cmenu_id="";
function change_material(img_name,fabricID,colorPath,menu_id,big_sample_in){
	img=img_name;
	fabric=fabricID;
	cmenu_id=menu_id;
	big_sample=big_sample_in;
	if(fabric=='ByStretch'){
		head='>bystretch'; colour_mat="By";
	}else if(fabric=='Spectra Plain'){
		head='>spectra'; colour_mat="Spectra";
	}else if(fabric=='Damask Rose'){
		head='>rose'; colour_mat="Rose";
	}else if(fabric=='Ivy Leaf'){
		head='>ivy'; colour_mat="Ivy";
	}else if(fabric=='Roma Glitter'){
		head='>roma'; colour_mat="Roma";
	}else if(fabric=='Ottoman'){
		head='>ottoman'; colour_mat="Ottoman";
	}else if(fabric=='Italia'){
		head='>italia'; colour_mat="Italia";
	}else if(fabric=='Chinese Fan'){
		head='>chinese'; colour_mat="Chinese";
	}else if(fabric=='Mediterranean'){
		head='>mediterranean'; colour_mat="Mediterranean";
	}else if(fabric=='Polycotton'){
		head='>poly'; colour_mat="Polycotton";
	}else if(fabric=='Organza'){
		head='>organza'; colour_mat="Organza";
	}else if(fabric=='Shield'){
		head='>shield'; colour_mat="Shield";
	}else if(fabric=='Gingham'){
		head='>gin'; colour_mat="Gingham";
	}else if(fabric=='Alamo'){
		head='>alamo'; colour_mat="Alamo";
	}
	var flag=false;
	if(document.getElementById && document.getElementsByTagName && document.createElement){
        // load data file
        sendRequest(colorPath);
    }
	//$(document).ready(function(){
		//if(flag){}
	//});

	
}
// initialize XMLHttpRequest object
var xmlobj=null;
// initialize global variables
var data=new Array();
var i=0;

// send http request
function sendRequest(doc){
    // check for existing requests
    if(xmlobj!=null&&xmlobj.readyState!=0&&xmlobj.readyState!=4){
        xmlobj.abort();
    }
    try{
        // instantiate object for Firefox, Nestcape, etc.
        xmlobj=new XMLHttpRequest();
    }
    catch(e){
        try{
            // instantiate object for Internet Explorer
            xmlobj=new ActiveXObject('Microsoft.XMLHTTP');
        }
        catch(e){
            // Ajax is not supported by the browser
            xmlobj=null;
            return false;
        }
    }
    // assign state handler
    xmlobj.onreadystatechange=stateChecker;
    // open socket connection
    xmlobj.open('GET',doc,true);
    // send request
    xmlobj.send(null);
}

// check request status
function stateChecker(){
    // if request is completed
    if(xmlobj.readyState==4){
        // if status == 200 display text file
        if(xmlobj.status==200){
            // create data container
            //createDataContainer();
            // display data into container
            data=xmlobj.responseText.split('\n');
			//text=xmlobj.responseText;
			displayData();
        }
        else{
            alert('Failed to get response :'+ xmlobj.statusText);
        }
    }else{
		//alert(xmlobj.readyState);
	}
}
// display data at a given time interval
function displayData(){
	var select_menu=document.getElementById(cmenu_id);
	select_menu.options.length=0;
	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();
			for(w=0;w<colours.length;w++){
				var pattern=/^(\w+).*Out of Stock.*$/;
				var colour="";
				if(matches=colours[w].match(pattern)){
					colour=matches[1];
				}else{
					colour=colours[w];
				}
				select_menu.options[w]=new Option(colours[w],colour, false, false);
			}
		}
	}
	swap_colour(0,img,cmenu_id,colour_mat);
	if(big_sample){
		swap_colour(0,big_sample,cmenu_id,colour_mat);
	}
}
// curvy corners =>  overlock only!
function checkFinish(corner,finish_id){
	//var corner_menu=document.getElementById(corner_id);
	var finish_menu=document.getElementById(finish_id);
	finish_menu.options.length=0;;
	if(corner.match(/^Sharp/)){
		finish_menu.options[0]=new Option('Overlocked','Overlocked', false, false);
		finish_menu.options[1]=new Option('Hemmed','Hemmed', false, false);
	}else if(corner.match(/^Curvy|Round/i)){
		finish_menu.options[0]=new Option('Overlocked','Overlocked', false, false);
	}
}
function login(showhide){
if(showhide == "show"){
	document.getElementById('popupbox').style.visibility="visible"; /* If the function is called with the variable 'show', show the login box */
}else if(showhide == "hide"){
	document.getElementById('popupbox').style.visibility="hidden"; /* If the function is called with the variable 'hide', hide the login box */
}
}

var nbsp = 160;		// non-breaking space char
var node_text = 3;	// DOM text node-type
var emptyString = /^\s*$/ ;
var global_valfield;	// retain valfield for timer thread

// --------------------------------------------
//                  trim
// Trim leading/trailing whitespace off string
// --------------------------------------------

function trim(str)
{
  return str.replace(/^\s+|\s+$/g, '');
}

// --------------------------------------------
//            commonCheck
// Common code for all validation routines to:
// (a) check for older / less-equipped browsers
// (b) check if empty fields are required
// Returns true (validation passed), 
//         false (validation failed) or 
//         proceed (don't know yet)
// --------------------------------------------

var proceed = 2;  

function commonCheck    (valfield,   // element to be validated
                         infofield,  // id of element to receive info/error msg
                         required)   // true if required
{
  if (!document.getElementById) 
    return true;  // not available on this browser - leave validation to the server
  var elem = document.getElementById(infofield);
  if (!elem.firstChild) return true;  // not available on this browser 
  if (elem.firstChild.nodeType != node_text) return true;  // infofield is wrong type of node  

  if (emptyString.test(valfield.value)) {
    if (required) {
      msg (infofield, "style22", "ERROR: required");  
      setfocus(valfield);
      return false;
    }
    else {
      msg (infofield, "style22", "");   // OK
      return true;  
    }
  }
  return proceed;
}

// --------------------------------------------
//                  setfocus
// Delayed focus setting to get around IE bug
// --------------------------------------------

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 );
}


// --------------------------------------------
//                  msg
// Display warn/error message in HTML element.
// commonCheck routine must have previously been called
// --------------------------------------------

function msg(fld,     // id of element to display message in
             msgtype, // class to give element ("warn" or "error")
             message) // string to display
{
  // setting an empty string can give problems if later set to a 
  // non-empty string, so ensure a space present. (For Mozilla and Opera one could 
  // simply use a space, but IE demands something more, like a non-breaking space.)
  var dispmessage;
  if (emptyString.test(message)){
    dispmessage = String.fromCharCode(nbsp);    
  }else{
    dispmessage = message;
  }

  var elem = document.getElementById(fld);
  //dispmessage.blink();
  elem.firstChild.nodeValue = dispmessage;
  
  elem.className = msgtype;   // set the CSS class to adjust appearance of message
}