// JavaScript Document

// EDIT THESE VALUES IF REQUIRED
var alertText = 'Are you sure you wish to perform this action' + "\n"; //DEFAULT TEXT DISPLAYED ON CONFIRM BUTTONS/LINKS WHEN NO ALT/TITLE
var newWindowLink = false; //OPEN EXTERNAL LINKS BY DEFAULT IN A NEW WINDOW (TRUE/FALSE)?


var woms = new Array(); //CREATE ARRAY OF FUNCTIONS TO LAUNCH ONLOAD

//ONLOAD MANAGER
function womOn(){
  window.onload = womGo;
}

function womGo(){
  for(var i = 0;i < woms.length;i++)
    eval(woms[i]);
}

function womAdd(func){
  woms[woms.length] = func;
}



function CreateBookmarkLink() {

	title = document.title;
	url = window.location.href;
	
	if (window.sidebar) { 
		window.sidebar.addPanel(title, url,"");
	} else if(window.external) {
		window.external.AddFavorite( url, title); 
	} else if(window.opera && window.print) {
		return true;
	}
	
}

function objToggle(obj) { 

	var obj = document.getElementById(obj);

	if(obj.style.display == "block") { 
		obj.style.display = "none";
	} else { 
		obj.style.display = "block";
	}

}

function objToggleMenu(obj, cat_id) { 

	var obj = document.getElementById(obj);
	var arrow = 'arrow_' + cat_id;
	
	if(obj.style.display == "block") { 
		obj.style.display = "none";
		document.getElementById(arrow).src = 'images/arrow_down.gif';
	} else { 
		obj.style.display = "block";
		document.getElementById(arrow).src = 'images/arrow_up.gif';
	}

}


function showFabricBoxes(location){
	
	var div = document.getElementById(location);
	var ulList = div.getElementsByTagName('ul');
	
	for(var i=0; i<ulList.length; i++) {
		
		var liList = ulList[i].getElementsByTagName('li');
		
		for (var x=0; x<liList.length; x++){
			
			if (x > 15){
				
				if (liList[x].style.display == 'block') liList[x].style.display = 'none';
				if (liList[x].style.display == 'none') liList[x].style.display = 'block';
				
			}
			
		}
		
	}
	
	document.getElementById('fabric_expander').innerHTML = '';
	
	return true;
	
}



function imageSwap() {

	if(!document.getElementById('thumb_list')) return false;

	var placeHolder  = document.getElementById('main'); //LARGE IMAGE
	var imageList    = document.getElementById('thumb_list'); //UL OF IMAGE LIST
	var listElements = imageList.getElementsByTagName('li'); //LIS IN IMAGELIST
	var ahref		 = document.getElementById('ahref');
	
	for(var i=0; i<listElements.length; i++) { //LOOP THROUGH LIS
	
		var image = listElements[i].getElementsByTagName('img'); //GET IMAGES WITHIN LIS
		
		for(var x=0; x<image.length; x++) { //LOOP IMAGES
		
			image[x].onclick = function() { //CLICKED ON AN IMAGE
			
				//THUMB FILENAME
				var file_location = this.getAttribute('src'); //GET FILE LOCATION
				var location_explode = file_location.split('/');
				var filename = location_explode[(location_explode.length-1)]; //FIND LAST VALUE
				
				//CURRENT LARGE FILENAME
				var file_location = placeHolder.getAttribute('src');
				var location_explode = file_location.split('/');
				var large_filename = location_explode[(location_explode.length-1)]; //FIND LAST VALUE
				
				var thumb = 'uploads/images/products/tinythumbs/'+large_filename; //SET NEW SRC FOR THUMBNAME
				var large = 'uploads/images/products/large/'+filename; //SET NEW SRC FOR PLACEHOLDER
				var enlargement = 'uploads/images/products/huge/'+filename;
				
				placeHolder.setAttribute('src', large); //SET ATTS
				ahref.setAttribute('href', enlargement);
				this.setAttribute('src', thumb);
			
			}
		
		}
		
	
	}
	
}

function prodAdminToggle(obj) { 
	
	if (obj == 'general'){
		document.getElementById('general').style.display = "block";			
		document.getElementById('location').style.display = "none";	
		document.getElementById('associated').style.display = "none";
		document.getElementById('amazon').style.display = "none";
		document.getElementById('promo').style.display = "none";
		document.getElementById('fabrics').style.display = "none";
		document.getElementById('image').style.display = "none";
		document.getElementById('tab').value = 'general';
	}
	
	if (obj == 'location'){
		document.getElementById('general').style.display = "none";	
		document.getElementById('location').style.display = "block";
		document.getElementById('associated').style.display = "none";	
		document.getElementById('amazon').style.display = "none";
		document.getElementById('promo').style.display = "none";
		document.getElementById('fabrics').style.display = "none";
		document.getElementById('image').style.display = "none";
		document.getElementById('tab').value = 'location';
	}
	
	if (obj == 'associated'){
		document.getElementById('general').style.display = "none";			
		document.getElementById('location').style.display = "none";	
		document.getElementById('associated').style.display = "block";
		document.getElementById('amazon').style.display = "none";
		document.getElementById('promo').style.display = "none";
		document.getElementById('fabrics').style.display = "none";
		document.getElementById('image').style.display = "none";
		document.getElementById('tab').value = 'associated';
	}
	
	if (obj == 'amazon'){
		document.getElementById('general').style.display = "none";			
		document.getElementById('location').style.display = "none";	
		document.getElementById('associated').style.display = "none";
		document.getElementById('amazon').style.display = "block";
		document.getElementById('promo').style.display = "none";
		document.getElementById('fabrics').style.display = "none";
		document.getElementById('image').style.display = "none";
		document.getElementById('tab').value = 'amazon';
	}

	if (obj == 'promo'){
		document.getElementById('general').style.display = "none";			
		document.getElementById('location').style.display = "none";	
		document.getElementById('associated').style.display = "none";
		document.getElementById('amazon').style.display = "none";
		document.getElementById('promo').style.display = "block";
		document.getElementById('fabrics').style.display = "none";
		document.getElementById('image').style.display = "none";
		document.getElementById('tab').value = 'promo';
	}
	
	if (obj == 'fabrics'){
		document.getElementById('general').style.display = "none";			
		document.getElementById('location').style.display = "none";	
		document.getElementById('associated').style.display = "none";
		document.getElementById('amazon').style.display = "none";
		document.getElementById('promo').style.display = "none";
		document.getElementById('fabrics').style.display = "block";
		document.getElementById('image').style.display = "none";
		document.getElementById('tab').value = 'fabrics';
	}
	
	if (obj == 'image'){
		document.getElementById('general').style.display = "none";			
		document.getElementById('location').style.display = "none";
		document.getElementById('associated').style.display = "none";	
		document.getElementById('amazon').style.display = "none";
		document.getElementById('promo').style.display = "none";
		document.getElementById('fabrics').style.display = "none";
		document.getElementById('image').style.display = "block";
		document.getElementById('tab').value = 'image';
	}
	
}


function validate(formobj, formtype){
		
	switch(formtype) { 
		case "contact":
			var fieldRequired = Array("enq_name", "enq_email", "enq_telephone", "enq_referral", "enq_comments");
			break;
		case "register":
			var fieldRequired = Array("account_username", "account_password", "account_confirm_password", "account_fullname", "account_email", "account_company", "account_outlet");
			break;
		case "checkout":
			var fieldRequired = Array("order_del_name", "order_del_addr1", "order_del_postcode", "order_del_tel", "order_bil_name", "order_bil_addr1", "order_bil_postcode", "order_bil_tel", "order_bil_email");
			break;
	}
	
	var error = false;
	
	var normalClass = "textfield";
	var invalidClass = "textfield_invalid";
	
	for (var i = 0; i < fieldRequired.length; i++){
		var obj = formobj.elements[fieldRequired[i]];
		if(obj) {
			obj.className = normalClass;
		}
	}
	
	for (var i = 0; i < fieldRequired.length; i++){
		var obj = formobj.elements[fieldRequired[i]];
		if (obj){
			switch(obj.type){
				case "select-one":
				if (obj.selectedIndex == 0 || obj.options[obj.selectedIndex].text == ""){
					obj.className = invalidClass;
					error = true;
				}
				break;
				case "select-multiple":
					if (obj.selectedIndex == -1){
					obj.className = invalidClass;
					error = true;
				}
				break;
				case "text":
				case "textarea":
				if (obj.value == "" || obj.value == null){
					obj.className = invalidClass;
					error = true;
				}
				break;
				default:
			}
		}
	}
	
	if (!error){
		
		return true;
		
	} else {
		
		alert("Some fields were not filled in. Please complete the highlighted fields.");
		return false;
		
	}
}



/*function getSelection(ta)
  { var bits = [ta.value,'','','']; 
    if(document.selection)
      { var vs = '#$%^%$#';
        var tr=document.selection.createRange()
        if(tr.parentElement()!=ta) return null;
        bits[2] = tr.text;
        tr.text = vs;
        fb = ta.value.split(vs);
        tr.moveStart('character',-vs.length);
        tr.text = bits[2];
        bits[1] = fb[0];
        bits[3] = fb[1];
      }
    else
      { if(ta.selectionStart == ta.selectionEnd) return null;
        bits=(new RegExp('([\x00-\xff]{'+ta.selectionStart+'})([\x00-\xff]{'+(ta.selectionEnd - ta.selectionStart)+'})([\x00-\xff]*)')).exec(ta.value);
      }
     return bits;
  }*/

function matchPTags(str)
  { str = ' ' + str + ' ';
    ot = str.split(/\[[B|U|I].*?\]/i);
    ct = str.split(/\[\/[B|U|I].*?\]/i);
    return ot.length==ct.length;
  }

function addPTag(ta,pTag)
  { bits = getSelection(ta);
    if(bits)
      { if(!matchPTags(bits[2]))
          { alert('\t\tInvalid Selection\nSelection contains unmatched opening or closing tags.');
            return;
          }
        ta.value = bits[1] + '[' + pTag + ']' + bits[2] + '[/' + pTag + ']' + bits[3];
      }
  }
  

		
function select_fabric(fabric_id, title, price, img){
		
	document.getElementById('fabric_id').value = fabric_id;
	document.getElementById('fabric_title').innerHTML = title;
	document.getElementById('request_sample_price').innerHTML = price;
	document.getElementById('fabric_options').style.display = 'none';
	document.getElementById('request_sample_chosen').style.display = 'block';
	document.getElementById('selectedimg').src = 'uploads/images/swatches/' + img;
	
}
		
function deselect_fabric(){
		
	document.getElementById('fabric_id').value = '';
	document.getElementById('fabric_title').innerHTML = '';
	document.getElementById('request_sample_price').innerHTML = '';
	document.getElementById('fabric_options').style.display = 'block';
	document.getElementById('request_sample_chosen').style.display = 'none';
	document.getElementById('selectedimg').src = '';
}


function show_fabric_selection(product_id){
	
	var id_fabric_options = 'fabric_options_' + product_id;
	document.getElementById(id_fabric_options).style.display = 'block';
	
}

function select_fabric_assoc(product_id, title, fabric_id, price, img){
	
	var id_fabric_id = 'fabric_id_' + product_id;
	var id_fabric_title = 'fabric_title_' + product_id;
	var id_request_sample_price = 'request_sample_price_' + product_id;
	var id_fabric_options = 'fabric_options_' + product_id;
	var id_request_sample_chosen = 'request_sample_chosen_' + product_id;
	var id_selectedimg = 'selectedimg_' + product_id;
	
	document.getElementById(id_fabric_id).value = fabric_id;
	document.getElementById(id_fabric_title).innerHTML = title;
	document.getElementById(id_request_sample_price).innerHTML = price;
	document.getElementById(id_fabric_options).style.display = 'none';
	document.getElementById(id_request_sample_chosen).style.display = 'block';
	document.getElementById(id_selectedimg).src = 'uploads/images/swatches/' + img;
	
}

function deselect_fabric_assoc(product_id){
	
	var id_fabric_id = 'fabric_id_' + product_id;
	var id_fabric_title = 'fabric_title_' + product_id;
	var id_fabric_price = 'fabric_price_' + product_id;
	var id_request_sample_price = 'request_sample_price_' + product_id;
	var id_fabric_options = 'fabric_options_' + product_id;
	var id_request_sample_chosen = 'request_sample_chosen_' + product_id;
	var id_selectedimg = 'selectedimg_' + product_id;
	
	document.getElementById(id_fabric_id).value = 0;
	document.getElementById(id_fabric_title).innerHTML = '';
	document.getElementById(id_request_sample_price).innerHTML = '';
	document.getElementById(id_fabric_options).style.display = 'block';
	document.getElementById(id_request_sample_chosen).style.display = 'none';
	document.getElementById(id_selectedimg).src = 'uploads/images/swatches/' + img;
	
}

function ismaxlength(obj){
var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
if (obj.getAttribute && obj.value.length>mlength)
obj.value=obj.value.substring(0,mlength)
}

function delivery_populate(){
	
	document.getElementById('delivery_firstname').value = document.getElementById('firstname').value;
	document.getElementById('delivery_surname').value = document.getElementById('surname').value;
	document.getElementById('delivery_address1').value = document.getElementById('address1').value;
	document.getElementById('delivery_address2').value = document.getElementById('address2').value;
	document.getElementById('delivery_address3').value = document.getElementById('address3').value;
	document.getElementById('delivery_town').value = document.getElementById('town').value;
	document.getElementById('delivery_county').value = document.getElementById('county').value;
	document.getElementById('delivery_postcode').value = document.getElementById('postcode').value;
	
}

function checkfabric(){
	
	if (document.getElementById('fabric_id').value == ''){
		
		alert('Please choose a fabric option');
		return false;
	
	} else {
		
		return true;	
		
	}
	
}

function checkfabric_assoc(product_id){
	
	var id_fabric_id = 'fabric_id_' + product_id;
	var id_fabric_options = 'fabric_options_' + product_id;
	var id_request_sample_chosen = 'request_sample_chosen_' + product_id;
	
	if (document.getElementById(id_fabric_options).style.display = 'none'){
		document.getElementById(id_fabric_options).style.display = 'block';
	}
	
	if (document.getElementById(id_fabric_id).value == ''){
		
		alert('Please choose a fabric option');
		return false;
	
	} else {
		document.getElementById(id_fabric_options).style.display = 'none';
		return true;	
		
	}
	
}


function sampleTallyCheck(){

	var tally = 0;
	
	var fields = document.getElementsByTagName('input');
	
	for(var i=0; i<fields.length; i++)
		{
			
			if ((fields[i].getAttribute('type')=='checkbox') && (fields[i].checked == true)){
			
				tally++;
				
				if (tally > 5) fields[i].checked = false;
				
			}
			
		}
		
	if (tally > 5){
		
		alert ('You have selected five samples.');
		
		return false;
		
	}

}


function confirmSubmit(){

	var agree=confirm("Are you sure you wish to delete this item?");

	if (agree)

		return true ;

	else

		return false ;

}

function bookmark(url,title){
  if ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4)) {
  window.external.AddFavorite(url,title);
  } else if (navigator.appName == "Netscape") {
    window.sidebar.addPanel(title,url,"");
  } else {
    alert("Press CTRL-D (Netscape) or CTRL-T (Opera) to bookmark");
  }
}


function checkAll(id, checked){
	
	var length = id.length;
	
	var fields = document.getElementsByTagName('input');
	
	for(var i=0; i<fields.length; i++)
		{
			
			if (fields[i].getAttribute('type') == 'checkbox'){
				
				var theid = fields[i].getAttribute('name');
				if (theid.substring(0, length) == id) fields[i].checked = checked;
				
			}
			
		}
		
}