// JavaScript Document

// opens print popup
function open_win(url){
	window.open(url,"my_new_window","toolbar=yes, location=yes, directories=no, status=no, menubar=yes, scrollbars=yes, resizable=yes, copyhistory=yes, width=640, height=480")
}

// checks to see if the accessories are selected
function check(setfield,new_value)
{
	if(setfield == 'card'){
		if(new_value == '1') {
				show_form_fields(setfield);
		}
		else{
				hide_form_fields(setfield);
		}
	}
	else {
		if(new_value == '1') {
				show_form_fields(setfield);
		}
		else{
				hide_form_fields(setfield);
		}
	}
}

// hide if they are set to no
function hide_form_fields(setfield) {
	if(setfield == 'card') {
		document.getElementById("card_detail").style.display='none';
	}
	else if(setfield == 'both') {
		document.getElementById("other_detail").style.display='none';
		document.getElementById("card_detail").style.display='none';
	}
	else {
		document.getElementById("other_detail").style.display='none';
	}
}

// hide if they are set to no
function show_form_fields(setfield) {
	if(setfield == 'card') {
		document.getElementById("card_detail").style.display='block';
	}
	else {
		document.getElementById("other_detail").style.display='block';
	}
}

// rounds number to X decimal places, defaults to 2
function formatNumber(number,X) {
	
	var string_value = number.toString();
	var output = '';
	
	if (string_value.indexOf(".") < 0){
		output = string_value + '.00';
	} else {
		string_value = (number.toString()) + '00';
		string_value_array=string_value.split(".");
		output = string_value_array['0'];
		output += '.' + string_value_array['1'].substring(0,2);
	}
	
	return output;
	//return 'help';
}

//change the price for fast track
function change_price() {
var old_val = document.forms['estimate'].total.value;
var num_value_array=old_val.split("£");
var total = Number(num_value_array['1']);
var new_total = formatNumber(total += 10);
var old_total = formatNumber(total -= 20);

	if(document.forms['estimate'].fasttrack_check.checked) {
		document.forms['estimate'].total.value='£'+new_total;		
	} 
	else {
		document.forms['estimate'].total.value='£'+old_total;
	}
}





// FORM HELP FUNCTIONS
function getPosition(e){
    var r=new Array(0,0);
    while(e){
        r[0]+=e.offsetLeft;
        r[1]+=e.offsetTop;
        e=e.offsetParent;
    }
    return r;
}

function form_functions(hide_elements) {
		do_hide(hide_elements);
}


// this function is called on load to hide all of our micro help divs and text links.
function do_hide(hide_elements) {
	// get user agent used if safari or opera is present.
	//detect = navigator.userAgent.toLowerCase();
	// check to see if its opera
	//if(detect.indexOf('opera') +1) return;

	for(h=0; h<hide_elements.length; h++) {
		var hide_id = hide_elements[h];
		document.getElementById(hide_id).className = "hide";
	}
}

function show_help(form_id,help_id) {
	
	// get user agent used if safari or opera is present.
	detect = navigator.userAgent.toLowerCase();
	// check to see if its opera then turn iframe off!
	if(detect.indexOf('opera') +1) {
		var i_frames=document.getElementsByTagName("iframe");
			for(x=0; x<i_frames.length; x++) {
				i_frames[x].style.display="none";
			}
	}
	
	// hide all other elements that may be showing
	do_hide(hide_elements);
	
	var coords=getPosition( document.getElementById(form_id) );
	var helpElement=document.getElementById(help_id);
	helpElement.className="help_visable";
	
	divheight = document.getElementById(help_id).offsetHeight;
	
	// alter position - height of div
	coords[1] = coords[1] -divheight;
	// alter position - speach bubble type callout
	coords[1] = coords[1] -20;
	// set position
	helpElement.style.top=coords[1]+'px';
	
}
