window.addEvent('domready', function(){
	//Call to function sort generic links
	if($('proddetslinks')){
		$$('#proddetslinks a').addEvent('click', function(e){showhidediv(this,e);});
	}
	// if they click the read the review link
	if($('prodreviewread')){
		$('prodreviewread').addEvent('click', function(e){showhidediv($('prodrev_link'),e);});
		var scrolltoread = new Fx.SmoothScroll({duration:1000, links: '#prodreviewread'});
	}	
	// if they click the write a review link
	if($('prodreviewwrite')){
		var linktowrite = $('prodreviewwrite').addEvent('click', function(e){showhidediv($('prodwrev_link'),e);});	
		var scrolltowrite = new Fx.SmoothScroll({duration:1000, links: '#prodreviewwrite'});
	}
	// if they click spec link
	if($('prodspecarea')){
		var linktowrite = $('prodspecarea').addEvent('click', function(e){showhidediv($('prodspec_link'),e);});	
		var scrolltospec = new Fx.SmoothScroll({duration:1000, links: '#prodspecarea'});
	}
	// hide relevant divs on page load if javascript is on
	if($('proddetslinks')){
		$$('.prodmaindetails').addClass('hide');
		$('proddesc').removeClass('hide');
	}
	
	// Bookmark the page
	if($('prodHelp')){
		var pgLoc = window.location; //get the page address
		var pgTitle = document.title; //get the page Title
		$('bookpg').addEvent('click', function(e){bookmark_us(e,pgLoc,pgTitle);}); //call bookmark feature when clicked
	}

});

// function for hiding / showing divs
function showhidediv(link, e){
	e.stop(); // stop the a tag
	var linkID = link.id; // get the links id
	var cleanLink = linkID.replace('_link',''); // get the name of the ID to be revealed
	$$('.prodmaindetails').addClass('hide'); // apply a hide class to all the generic informational divs
	$(cleanLink).removeClass('hide'); // remove the hide class from the relevant div
	setTab(linkID);
}
// function for colouring tabs up correctly
function setTab(linkID){
	$$('.genalt_nav a').removeClass('gennavon');
	$(linkID).addClass('gennavon');
}
// function to set products per page and sort by mode
function perPageSortBy(formnum) {
	if(formnum == 1) { document.products.submit(); }
	else if(formnum == 2) { document.products2.submit(); }
}
// function for bookmarking page
function bookmark_us(e, url, title){
	e.stop();
	if(window.sidebar){ // firefox
   		window.sidebar.addPanel(title, url, "");
   	}	
	else if(window.opera && window.print){ // opera
	    var elem = document.createElement('a');
	    elem.setAttribute('href',url);
	    elem.setAttribute('title',title);
	    elem.setAttribute('rel','sidebar');
	    elem.click();
	} 
	else if(document.all){// ie
	    window.external.AddFavorite(url, title);
	}
	else{ //Should never use this but just incase
		alert('Sorry, This feature is not currently supported by your browser. To bookmark this page please use the standard browser process.');
	}
}

function singleOptionCheck(opt_id, count_id, total_count) {
	var price_change = 0.00;
	var tmps;
	var tmp;
	
	for(i=1; i<=total_count; i++) {
		if(i != count_id) {
			if($('option_'+opt_id+'_'+i).checked == true) {
				tmps = $('option_'+opt_id+'_'+i).value;
				tmp = tmps.split('|');
				price_change = price_change - parseFloat(tmp[1]);
			}
			$('option_'+opt_id+'_'+i).checked = false;	
		}
	}
	
	if($('option_'+opt_id+'_'+count_id).checked == true) {
		tmps = $('option_'+opt_id+'_'+count_id).value;
		tmp = tmps.split('|');
		price_change = price_change + parseFloat(tmp[1]);	
	} else {
		tmps = $('option_'+opt_id+'_'+count_id).value;
		tmp = tmps.split('|');
		price_change = price_change - parseFloat(tmp[1]);
	}
	
	withOptionPrice(price_change);
}

function singleServiceCheck(serv_id, count_id, total_count) {
	
	for(i=1; i<=total_count; i++) {  
		if(i != count_id) {
			$('service_'+serv_id+'_'+i).checked = false;	
		}
	}
}

function withOptionPrice(price) {
	var base_price = parseFloat($('base_price').value);
	var full_price = parseFloat($('full_price').value);
	var new_price  = Math.max(base_price, full_price + parseFloat(price)); new_price = new_price.toFixed(2);
	$('full_price').value = new_price;
	if(new_price > base_price) {
		$('optPrice').set('html', new_price);
		$('optPriceBox').removeClass('hide');
		$('theOpts').setStyle('margin-bottom', '5px');
	} else {
		$('optPriceBox').addClass('hide');
		$('theOpts').setStyle('margin-bottom', '0');
	}
}
