var origHTML = "";
var objImgBox;
var bigImgLayer;

startList = function() {
	//objImgBox = document.getElementById('imageBox');
	//origHTML = objImgBox.innerHTML;	
	//bigImgLayer = document.getElementById("bigImgLayer");
	
	if (document.all&&document.getElementById) {
		
		navRoot = document.getElementById("nav");
		
		for (i=0; i<navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			if (node.nodeName=="LI") {
				node.onmouseover=function() {
					this.className+=" over";
					//toggleDropdown();
				}
				node.onmouseout=function() {
					this.className=this.className.replace(" over", "");
					//toggleDropdown();
				}
			}
		}
	}
}
window.onload=startList;



function swopImg(objImg){
	if (objImg.src.indexOf('_on.gif') > 0 ){
		objImg.src = replaceCharacters(objImg.src,'_on.gif','.gif');	
	}else{
		objImg.src = replaceCharacters(objImg.src,'.gif','_on.gif');
	}	
}

function replaceCharacters(conversionString,inChar,outChar){
	var convertedString = conversionString.split(inChar);
  	convertedString = convertedString.join(outChar);
 	return convertedString;
}


function formatText (tag) { 
	var selectedText = document.selection.createRange().text; 
	if (selectedText != "") { 
		var newText = "<" + tag + ">" + selectedText + "</" + tag + ">"; 
		document.selection.createRange().text = newText; 
	}else{
		alert('You must select some text first');
	}
}

function toggleDropdown(){
	var idDropDown = document.getElementById('selectID');
	if(idDropDown){
		if (idDropDown.style.display == 'none'){
			idDropDown.style.display = 'inline';
		}else{
			idDropDown.style.display = 'none';
		}
	}
	return true;
}


function toggleInfoLayer(){
	var oLayer = document.getElementById('infoLayer');
	if(oLayer.style.display=='none'){
		oLayer.style.display='inline';
	}else{
		oLayer.style.display='none';
	}	
}


function displayImage(imgSrc,sCap){
	bigImgLayer.innerHTML = "<div id='bigImg'><img src='" + replaceCharacters(imgSrc,'.jpg','_l.jpg') + "' width=100% height=100%></div><div style='width:210px;' class='imgCaption' style='color:steelblue;'>" + sCap + "</div>";
	document.getElementById("imageBox").style.display='none';
	bigImgLayer.style.display='inline';
	return true;	
}

function revertImageBox(){
	bigImgLayer.style.display='none';
	document.getElementById("imageBox").style.display='block';	
	return true;
}

function displayTitle(sCap){
	document.getElementById("patternCaption").innerHTML = sCap;
	document.getElementById("patternCaption").style.display='inline';	
	return true;	
}

function revertTitle(){
	patternCaption.style.display='none';
	return true;
}

function toggleRow(iRow){
	var introRow = document.getElementById('NI'+iRow);
	var contentRow = document.getElementById('NC'+iRow);
	var moreSpan = document.getElementById('more'+iRow);	
	if (contentRow.style.display == 'none'){
		contentRow.style.display = 'inline';
		moreSpan.innerHTML = 'hide';
	}else{
		contentRow.style.display = 'none';
		moreSpan.innerHTML = '... more';		
	}
	return true;
}


function limitInput(obj,iMax){
	if(obj.value.length >= iMax){
		alert('Only ' + iMax + ' characters allowed');
		return false;
	}
}

function reduceInput(obj,iMax){
	if (obj.value.length > iMax){
		alert('The intro is more than the max ' + iMax + ' characters\nThe intro will be truncated');
		obj.value = obj.value.substr(0,iMax);
	}
}


function checkOrderForm(theForm){
	
	if(!checkField(theForm.Name,'You must enter your name','text')){return false};
	if(!checkField(theForm.Email,'You must enter your email address','text')){return false};	
	if(!checkField(theForm.Street_address_1,'You must enter the Street address','text')){return false};
	if(!checkField(theForm.Town,'You must enter the Town','text')){return false};
	if(!checkField(theForm.County,'You must enter the County','text')){return false};
	if(!checkField(theForm.Country,'You must enter the Country','text')){return false};
	if(!checkField(theForm.Postcode,'You must enter the Postcode','text')){return false};
	if(!checkField(theForm.VendorTxCode,'You must enter the Quote Reference','text')){return false};	
	if(!checkField(theForm.Description,'You must select a Description','select')){return false};		
	if(!checkField(theForm.Quantity,'You must enter a quantity','text')){return false};			
	if(!checkField(theForm.Quantity,'You must enter the Quantity as number eg. 12','number')){return false};	
	if(!checkField(theForm.QuantityUnit,'You must select a Unit of measurement','select')){return false};	
	theForm.Amount.value=theForm.Amount.value.replace('£','');
	if(!checkField(theForm.Amount,'You must enter the Cost','text')){return false};	
	if(!checkField(theForm.Amount,'You must enter the Cost as a number eg. 45 or 45.50','number')){return false};		
}


function checkQuoteForm(theForm){
	if(!checkField(theForm.Name,'You must enter your name','text')){return false};
	if(!checkField(theForm.Email,'You must enter your email address','text')){return false};		
	if(!checkField(theForm.Daytime_Telephone,'You must enter your Daytime telephone number','text')){return false};
	if(!checkField(theForm.How_found,'Please tell us how you found us','select')){return false};
	if(!checkField(theForm.Quantity,'You must enter a quantity','text')){return false};			
	if(!checkField(theForm.Quantity,'You must enter the Quantity as number eg. 12','number')){return false};	
	if(!checkField(theForm.QuantityUnit,'You must select a Unit of measurement','select')){return false};	
	if(!checkField(theForm.Fire_retardant,'Please tell us if you need the item to be treated with Fire retardant','yesno')){return false};
	if(!checkField(theForm.Stain_repellant,'Please tell us if you need the item to be treated with Stain repellant','yesno')){return false};
}



function checkField(fld,msg,fType){
   switch(fType) {
      case 'text':   	if (fld.value=='') {alert(msg); fld.focus(); return false;} break
      case 'number':   	if (isNaN(fld.value)) {alert(msg); fld.focus(); return false;} break	  
      case 'select':	if (fld.selectedIndex==0) {alert(msg); fld.focus(); return false;} break
	  case 'yesno':		if(!fld[0].checked && !fld[1].checked) {alert(msg); fld[0].focus(); return false;} break
      default:			if (fld.value=='') {alert(msg); fld.focus(); return false;} break
   }
   return true
}


