// JavaScript Document

// Auto-submit directed search form
function directedSearchAutoSubmit() {
	// Look for all selects
	var selects=document.getElementsByTagName('select');
	for (s=0;s<selects.length;s++) {
		// See if a value has already been selected (before full page load) and submit form if so
		if ((selects[s].className=='directed_search_dropdown')&&(selects[s].value!='')) {
			var form=selects[s].parentNode;
			form.submit();
		}
		// Create a function to submit the form whenever the dropdown is changed
		if (selects[s].className=='directed_search_dropdown') {
			selects[s].onchange=function() {
				var form=this.parentNode;
				form.submit();
			}
		}
	}
	// Hide the form go buttons
	var inputs=document.getElementsByTagName('input');
	for (i=0;i<inputs.length;i++) {
		if (inputs[i].className=='ds_go') {
			inputs[i].style.display="none";
		}
	}
}






function start_slideshow(start_frame, end_frame, delay) 
{
	
	setTimeout(switch_slides(start_frame,start_frame,end_frame, delay), delay);
		
}
						
function switch_slides(frame, start_frame, end_frame, delay) 
{
	
	return (function() 
	{
		Effect.Fade('slideshow' + frame);
	
		if (frame == end_frame) 
		{ 
			frame = start_frame; 
		} 
		else 
		{ 
			frame = frame + 1; 
		}
		
		setTimeout("Effect.Appear('slideshow" + frame + "');", 700);
		setTimeout(switch_slides(frame, start_frame, end_frame, delay), delay + 700);
	})

}

/*
$(document).ready(function() {

 // $.cluetip.setup({insertionType: 'insertBefore', insertionElement: 'div:first'});

//default theme
  $('a.title').cluetip({splitTitle: '|'});


// jTip theme
  $('a.jt:eq(0)').cluetip({
    cluetipClass: 'jtip', 
    arrows: true, 
    dropShadow: false,
    sticky: true,
    mouseOutClose: true,
    closePosition: 'title',
    closeText: '<img src="cross.png" alt="close" />'
  });
  $('a.jt:eq(1)').cluetip({cluetipClass: 'jtip', arrows: true, dropShadow: false, hoverIntent: false});
  $('span[@title]').css({borderBottom: '1px solid #900'}).cluetip({splitTitle: '|', arrows: true, dropShadow: false, cluetipClass: 'jtip'});

  $('a.jt:eq(2)').cluetip({
    cluetipClass: 'jtip', 
    arrows: true, 
    dropShadow: false, 
    height: '150px', 
    sticky: true,
    positionBy: 'bottomTop'    
  });
  $('a.jt:eq(3)').cluetip({
    cluetipClass: 'jtip', arrows: true, 
    dropShadow: false, 
    onActivate: function(e) {
      var cb = $('#cb')[0];
      return !cb || cb.checked;
    }
  });
  
});
*/

// 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 = true; //OPEN EXTERNAL LINKS BY DEFAULT IN A NEW WINDOW (TRUE/FALSE)?


//GLOBALS
/*
*
* LEAVE THESES ALONE
*
*/
//FOR CONFIRM FUNCTION
var confirmed = false;
var elementClass = '';
var titleText = '';

// confirmAction()
/*
* Builds arrays where confirmation can take place
*
* @return bool
*/
function confirmAction() {

	//IS DOM SUPPORTED
	if(!document.getElementsByTagName('a')) return false;

	//GET ELEMENTS ON PAGE TO WORK WITH
	var links   = document.getElementsByTagName('a');
	var buttons = document.getElementsByTagName('input');
	
	//PROCESS ELEMENTS
	loopElements(links);
	loopElements(buttons);
	
	return false;

}

// loopElements()
/*
* Processes passed elements, gets text for confirm box and sends for confirmation
* @return bool @param array of elements
*/
function loopElements(elements) {
	
	for(var i=0; i<elements.length; i++) { //LOOP THROUGH ELEMENTS
	
		elementClass = elements[i].className; //ASSIGN CLASSNAME
		
		if(elementClass != '' && elementClass.indexOf('confirm') > -1) { //DOES CONFIRM CLASSNAME EXIST ON ELEMENT
		
			elements[i].onclick = function() { //ELEMENT CLICKED
			
				titleText = '';
			
				if(this.getAttribute('alt')) {
					
					titleText = this.getAttribute('alt'); //GET TEXT FROM ALT ATTRIBUTE ON BUTTONS
					
				}else if(this.getAttribute('title')) {
					
					titleText = this.getAttribute('title');	//GET TEXT FROM TITLE ON LINKS
					
				}
				
				if(titleText.length > 0) alertText = titleText; //APPLY TEXT TO VARIABLE
				
				confirmed = getConfirmation(alertText); //PASS TEXT TO FUNCTION
				
				return confirmed; //RETURN RESULT
			
			}
		
		}
	
	}
	
}

// getConfirmation()
/*
* Displays confirmation box returns result
* @return bool @param String of text to display
*/
function getConfirmation(text) {

	var result = confirm(text); //SHOW CONFIRMATION BOX

	return result; //RETURN RESULT

}

// externalLinks()
/*
* Allows external links to be opened in a new window without the use of target attribute
* @return bool
*/
function externalLinks() {
	
	//SETS DOMAIN OF SITE
	var domainName=document.domain;
	
	var externalLinks=document.getElementsByTagName("a"); //FIND ALL LINKS ON THE CURRENT PAGE
	
	for(var i=0; i<externalLinks.length; i++) { //LOOP THROUGH LINKS ARRAY
	
		var attribute=externalLinks[i].getAttribute("href"); //GETS CONTENT OF 'HREF' ATTRIBUTE ON CLICKED LINK
		
		var elementClass = externalLinks[i].className; //STORES CLASS NAME OF ELEMENT
		
		var contains_http=attribute.indexOf("http"); //GET VALUE http IN 'HREF' **FOR MOZILLA&&
		
		var contains_domain=attribute.indexOf(domainName); //GET VALUE domainName **FOR IE**
		
		if(newWindowLink == true) {
		
			if(contains_http>-1 && contains_domain==-1) { //DOES CONTAIN AN 'http' OR DOES NOT CONTAIN domainName
			
				setElementAttribute(externalLinks[i], 'target', '_blank');	
				
			}
		
		}
		
		if(elementClass.indexOf('new_window') > -1 ) { //FORCE LINK TO OPEN IN NEW WINDOW
			
			setElementAttribute(externalLinks[i], 'target', '_blank');
			
		}
		
	}
	
}

// setElementAttribute()
/*
* Sets elements attribute
* @return bool; @param element: Element to attach attr to, attr: The attribute to be added, val: value of attribute
*/
function setElementAttribute(element, attr, val) {

	element.setAttribute(attr, val); //SET ATTRIBUTE ON ELEMENT

	return true;

}


// getDimentions()
/*
* Gets dimentions of popup window
* @return array; @param thisClass: Classname of link clicked
*/
function getDimentions(thisClass) {

	var parts = thisClass.split('_'); //EXPLODE CLASSNAME ON UNDERSCORE _
	var winWidth = parts[parts.length-2]; //WIDTH IS SECOND LAST ELEMENT
	var winHeight = parts[parts.length-1]; //HEIGHT IS LAST ELEMENT
	
	var dimentions = new Array();
	
	dimentions['width']  = winWidth;
	dimentions['height'] = winHeight;
	
	return dimentions; //RETURN

}







// popUpWindows()
/*
* Creates a popup window
* @return bool;
*/
function popUpWindows() {
	
	var links = document.getElementsByTagName('a'); //MAKE ARRAY OF LINKS
	var url = '';
	
	for(var i=0; i<links.length; i++) { //LOOP LINKS
		
		links[i].onclick = function() { //LINK CLICKED
	
			var classes = this.className; //GET CLASS NAMES
			
			if(classes != '') {
			
				var splitClasses = classes.split(' '); //EXPLODE CLASSES ON SPACE ' '
				
				for(var x=0; x<splitClasses.length; x++) { //LOOP THROUGH MULTIPLE CLASSNAMES
				
					if(splitClasses[x].indexOf('popup_window') > -1) { //IS LINK A POPUP WINDOW
						url = this.getAttribute('href'); //FINDS WHAT PAGE WINDOW IS TO SHOW
						
						var dimentions = getDimentions(splitClasses[x]); //GET DIMENTIONS
						
						var newWindow = window.open(url, '', 'width='+dimentions['width']+', height='+dimentions['height']+', scrollbars=yes, resize=yes'); //SET NEW WINDOW
						
						newWindow; //LAUNCH
						
						return false; //STOP LINK
					
					}
				
				}
			
			}
		
		}
	
	}
	
}

function popUpWindowsBrochure() {
	
	var links = document.getElementsByTagName('a'); //MAKE ARRAY OF LINKS
	var url = '';
	
	for(var i=0; i<links.length; i++) { //LOOP LINKS
		
		links[i].onclick = function() { //LINK CLICKED
	
			var classes = this.className; //GET CLASS NAMES
			
			if(classes != '') {
			
				var splitClasses = classes.split(' '); //EXPLODE CLASSES ON SPACE ' '
				
				for(var x=0; x<splitClasses.length; x++) { //LOOP THROUGH MULTIPLE CLASSNAMES
				
					if(splitClasses[x].indexOf('full_popup_window') > -1) { //IS LINK A POPUP WINDOW
				
						url = this.getAttribute('href'); //FINDS WHAT PAGE WINDOW IS TO SHOW
						
						//var dimentions = getDimentions(splitClasses[x]); //GET DIMENTIONS
						
						var userheight = screen.height;
						var userwidth = screen.width;
					
						var newWindow = window.open(url, '', 'width='+userwidth+', height='+userheight+', scrollbars=yes, resize=yes'); //SET NEW WINDOW
						
						newWindow; //LAUNCH
						
						return false; //STOP LINK
					
					}
				
				}
			
			}
		
		}
	
	}
	
}

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;
}

var woms = new Array();


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 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 toggle(x) {
	document.getElementById(x).className='calc_content_sel';
	if (x=='calc') {
		document.getElementById('calc_help').className='calc_content';
		document.getElementById('calc_rec').className='calc_content';
		document.getElementById('tab_calc').className='tab_sel';
		document.getElementById('tab_rec').className='tab';
		document.getElementById('tab_help').className='tab';
	}
	else if (x=='calc_help') {
		document.getElementById('calc').className='calc_content';
		document.getElementById('calc_rec').className='calc_content';
		document.getElementById('tab_help').className='tab_sel';
		document.getElementById('tab_calc').className='tab';
		document.getElementById('tab_rec').className='tab';
	}
	else if (x=='calc_rec') {
		document.getElementById('calc').className='calc_content';
		document.getElementById('calc_help').className='calc_content';
		document.getElementById('tab_rec').className='tab_sel';
		document.getElementById('tab_calc').className='tab';
		document.getElementById('tab_help').className='tab';
	}
	/*
	var element = ;
	element.style.display = displayState;
	
	var tables = document.getElementsByTagName('table');
	for(i=0; i<tables.length; i++)
		{
		if((tables[i].className=='tab'))
			{
			thisid = tables[i].getAttribute('id');
			if(thisid!=x)
				{
				//window.alert(thisid);
				document.getElementById(thisid).style.display='none';
				}
			}
		}
	displayState = (displayState == 'block') ? 'none' : 'block';
*/
	}


