// JavaScript Document

function SelectAll(id)
{
    document.getElementById(id).focus();
    document.getElementById(id).select();
}

function submitForm(src)	{
	document.getElementById(src).submit();
}

var collapse_status = 0;
var total_faqs;

function setTotalFaqs(src)	{
	total_faqs = src;	
}


// Macromedia JS functions
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function MM_validateForm() { //v4.0
  if (document.getElementById){
    var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
    for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);
      if (val) { nm=val.name; if ((val=val.value)!="") {
        if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
          if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
        } else if (test!='R') { num = parseFloat(val);
          if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
          if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
            min=test.substring(8,p); max=test.substring(p+1);
            if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
      } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
    } if (errors) alert('Please fill in all fields marked *');
    document.MM_returnValue = (errors == '');
} }

// ********************************************************* AJAX functions **********************************************************************

var xmlHttp
var targetDiv;

function update_service_filters(src) {
	switch (src) {
		case 1:
			var cat		= document.getElementById("Manufacture");
			targetDiv = "modeldiv";
			document.getElementById(targetDiv).innerHTML = "<img src=\"/Images/loader.gif\" alt=\"Seaward\" name=\"loader\" id=\"loader\" style=\"margin-left:60px;\" />";			
			var url="/Includes/ajax_output_r_models.php?manu=" + escape(cat.value);
			// blank price values
			document.getElementById("pricediv").innerHTML = "";			
			break;
		case 2:
			var cat		= document.getElementById("Manufacture");		
			var sub_cat	= document.getElementById("Model");		
			targetDiv = "servicediv";		
			document.getElementById(targetDiv).innerHTML = "<img src=\"/Images/loader.gif\" alt=\"Seaward\" name=\"loader\" id=\"loader\" style=\"margin-left:60px;\" />";			
			var url="/Includes/ajax_output_r_service.php?manu=" + escape(cat.value)+"&model=" + escape(sub_cat.value);
			// blank price values
			document.getElementById("pricediv").innerHTML = "";	
									
			break;
		case 3:
			var cat		= document.getElementById("Manufacture");		
			var sub_cat	= document.getElementById("Model");	
			var service	= document.getElementById("Service");					
			targetDiv = "pricediv";		
			document.getElementById(targetDiv).innerHTML = "<img src=\"/Images/loader.gif\" alt=\"Seaward\" name=\"loader\" id=\"loader\" style=\"margin-left:60px;\" />";			
			var url="/Includes/ajax_output_r_price.php?manu=" + escape(cat.value)+"&model=" + escape(sub_cat.value)+"&service="+escape(service.value);
						
			break;			
		default:
		break;
	}
		
	xmlHttp=GetXmlHttpObject(stateChanged)
	xmlHttp.open("GET", url , true)
	xmlHttp.send(null)

}


function get_email() {
	targetDiv = "update_msg";	
	document.getElementById(targetDiv).innerHTML = "<img src=\"Images/loader.gif\" alt=\"Seaward\" name=\"loader\" id=\"loader\" style=\"margin-left:0px; margin-top:5px;\" />";

	var obj;	
	obj	= document.getElementById('email');	
	var url="/Includes/out_email_help.php?email=" + escape(obj.value);
	update_after = false;	
	
	xmlHttp=GetXmlHttpObject(stateChanged)
	xmlHttp.open("GET", url , true)
	xmlHttp.send(null)
}

function change_password()	{ 
	var u_email				= document.getElementById("u_email").value;
	var u_old_password		= document.getElementById("u_old_password").value;
	var u_new_password		= document.getElementById("u_new_password").value;
	var u_new_password_c	= document.getElementById("u_new_password_c").value;	
	targetDiv = "update_result";
	document.getElementById(targetDiv).innerHTML = "<img src=\"/Images/loader.gif\" alt=\"Seaward\" name=\"loader\" id=\"loader\" style=\"margin-left:0px; margin-top:5px;\" />";
	
	url = "/Includes/ajax-update-password.php?u_email="+escape(u_email)+"&u_old_password="+escape(u_old_password)+"&u_new_password="+escape(u_new_password)+"&u_new_password_c="+escape(u_new_password_c);
	//alert(url);
	update_after = false;
	xmlHttp=GetXmlHttpObject(stateChanged)
	xmlHttp.open("GET", url , true)
	xmlHttp.send(null)
}

function update_downloads()	{
	var cat		= document.getElementById("filter");
	targetDiv = "downloads";
	document.getElementById(targetDiv).innerHTML = "<img src=\"Images/loader.gif\" alt=\"Seaward\" name=\"loader\" id=\"loader\" style=\"margin-top:5px;\" />";	
	
	var url="Includes/out_downloads.php?id=" + cat.value;

	xmlHttp=GetXmlHttpObject(stateChanged)
	xmlHttp.open("GET", url , true)
	xmlHttp.send(null)	
}

// Add to Basket
function add(u_id,product_id,option_count)	{ 
	url = "http://www.seaward.co.uk/Includes/ajax_add_to_basket.php?u_id=" + 
	encodeURIComponent(u_id) + 
	"&product_id=" +
	encodeURIComponent(product_id);

	if (option_count >= 0)	{
		option_count = option_count - 1;
		url = url + "&t_count=" + option_count;
		for (x = 0; x <=option_count; x++) {
			t_options = document.getElementById('t_options['+x+']');
			url = url + "&t_option_"+x+"=" + escape(t_options.value);
		}
	}
	targetDiv = "items";
	xmlHttp=GetXmlHttpObject(stateChanged)
	xmlHttp.open("GET", url , true)
	xmlHttp.send(null)
	document.getElementById("targetDiv2").innerHTML = '<p><em>Item Added</em></p>';	

}

// Update basket
function update_cart(u_id,product_id,uType,c_id)	{ 
	url = "../Includes/ajax_output_basket.php?u_id="+u_id+"&product_id="+product_id+"&uType="+uType+"&c_id="+c_id;
	targetDiv = "basket";
	xmlHttp=GetXmlHttpObject(stateChanged)
	xmlHttp.open("GET", url , true)
	xmlHttp.send(null)
}

function fetch_news(page,no_stories, category)	{ 
	url = "../Includes/ajax_output_news.php?page="+page+"&no_stories="+no_stories+"&news_category="+category;
	targetDiv = "displayed_stories";
	
	xmlHttp=GetXmlHttpObject(stateChanged)
	xmlHttp.open("GET", url , true)
	xmlHttp.send(null)
}

function fetch_faqs(page,no_stories, page_id, cat_id)	{ 

	url = "../Includes/ajax_output_faqs.php?page="+page+"&no_stories="+no_stories+"&page_id="+page_id+"&faq_category="+cat_id;
	targetDiv = "displayed_stories";
	
	xmlHttp=GetXmlHttpObject(stateChanged)
	xmlHttp.open("GET", url , true)
	xmlHttp.send(null)
}

function update_product_list()	{ 
	// show loader
	targetDiv = "results";
	document.getElementById(targetDiv).innerHTML = "<img src=\"Images/loader.gif\" alt=\"Seaward\" name=\"loader\" id=\"loader\" />";
	
	var cat_id		= document.getElementById("cat_id").value;
	var suffix		= document.getElementById("suffix").value;
	var path		= document.getElementById("path").value;
	var version_id	= document.getElementById("version_id").value;
	var page_id		= document.getElementById("page_id").value;	

	var x=document.getElementById("filters");
	var str = "";

	for (var i=0;i<x.length;i++)	{
		if (x[i].checked)	{
			str = str + "&"+x.elements[i].name + "=" + x.elements[i].value;
		}
	}
	
	url = "../Includes/ajax_output_products.php?suffix="+suffix+"&cat_id="+cat_id+"&path="+path+"&version_id="+version_id+"&page_id="+page_id+str;

	xmlHttp=GetXmlHttpObject(stateChanged)
	xmlHttp.open("GET", url , true)
	xmlHttp.send(null)
}

// Do not change below this point

function stateChanged() { 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")	{ 
		document.getElementById(targetDiv).innerHTML=xmlHttp.responseText
	} 
} 

function GetXmlHttpObject(handler)	{ 
	var objXmlHttp=null

	if (navigator.userAgent.indexOf("Opera")>=0)	{
		alert("This example doesn't work in Opera") 
		return 
	}

	if (navigator.userAgent.indexOf("MSIE")>=0)	{ 
		var strName="Msxml2.XMLHTTP"
		if (navigator.appVersion.indexOf("MSIE 5.5")>=0)	{
			strName="Microsoft.XMLHTTP"
		} 
		try
			{ 
			objXmlHttp=new ActiveXObject(strName)
			objXmlHttp.onreadystatechange=handler 
			return objXmlHttp
			} 
		catch(e)
			{ 
			alert("Error. Scripting for ActiveX might be disabled") 
			return 
			} 
		} 

	if (navigator.userAgent.indexOf("Mozilla")>=0)	{
		objXmlHttp=new XMLHttpRequest()
		objXmlHttp.onload=handler
		objXmlHttp.onerror=handler 
		return objXmlHttp
	}
} 
