/*****************************************************************************************************

	Javascript functions for the Broadplace website pages.
	
	Copyright 2006 Broadplace Advertising Ltd. / Pretentious Ltd.

*****************************************************************************************************/

/*
	Used to dynamically change images on the site.
*/
function broadplace_user_changeimage(image, url)
{
	image.src = url;
}


/*
	Used to dynamically show quotes on the client page.
	
	Parameters:
	
		client_id		The ID for the element on the page containing the quote.
		client_show	"true" to show the quote, "false" to hide it.
*/
function broadplace_user_clientquote(client_id, client_show)
{
	if (client_show)
	{
		Effect.Appear('client_quote_'+client_id, { duration : 0.8 } );
	}
	else
	{
		Effect.Fade('client_quote_'+client_id, { duration : 0.8 } );
	}
}


/*
	Submits the approval form.
*/
function broadplace_user_order_approval_submit()
{
	if (document.getElementById('form_contact_email').value == '')
	{
		alert('Please confirm your e-mail address prior to submitting the order.');
	}
	else if (!document.getElementById('accept_terms').checked)
	{
		alert('You must accept the terms & conditions before you can submit this order');
	}
	else if (!document.getElementById('accept_payment').checked)
	{
		alert('You must agree to us using your card details before you can submit this order');
	}
	else
	{
		document.approval_form.submit();
	}
}


// Keeps track of how many service drop downs are currently on a page.
var broadplace_user_order_services = 0;


/*
	Called whenever the order page is displayed.
*/
function broadplace_user_order_service_init()
{
	// Reset the counter.
	broadplace_user_order_services = document.getElementById('input_num_services').value;
	
	// Add the first drop down to the page.
	broadplace_user_order_service_add();
}


/*
	Used to add a drop down of services to the request form
*/
function broadplace_user_order_service_add()
{
	// Increment the counter.
	broadplace_user_order_services++;
	document.getElementById('input_num_services').value = broadplace_user_order_services;
	
	// Send a request for a list of services offered by Broadplace.
	var service_xml = broadplace_user_request('services.ajax', null);
	
	// This will be filled in below with new HTML to be appended to the order form.
	var html_options = "<option class=\"form_option\" value=\"ignore\">Select Service</option>";
	
	// Go through each service one by one.
	var services = service_xml.getElementsByTagName('service');
	for (var i = 0; i < services.length; i ++)
	{
		try
		{
			var service_id = services[i].attributes[0].value;
			var service_name = services[i].attributes[1].value;
			html_options += "<option class=\"form_option\" value=\""+service_id+"\">"+service_name+"</option>";
		}
		catch (e) { }
	}

	// Add the new drop down to the form.	
	html_extra = "";
	html_extra += "<div id=\"area_services_"+broadplace_user_order_services+"_select\">";
		html_extra += "<table cellpadding=\"0\" cellspacing=\"4\" border=\"0\" width=\"400\"><tr>";
			html_extra += "<td align=\"left\" valign=\"top\" width=\"120\">Service #"+broadplace_user_order_services+"</td>";
			html_extra += "<td valign=\"top\" align=\"left\"><select id=\"area_services_"+broadplace_user_order_services+"_select_select\" class=\"form_select\" name=\"service_"+broadplace_user_order_services+"\" onchange=\"broadplace_user_order_service_select('"+broadplace_user_order_services+"', this.value);\">"+html_options+"</select>&nbsp;&nbsp;(<a class=\"link_normal\" href=\"javascript:broadplace_user_order_service_remove('"+broadplace_user_order_services+"');\">Remove</a>)</td>";
		html_extra += "</tr></table>";
	html_extra += "</div>";
	html_extra += "<div id=\"area_services_"+broadplace_user_order_services+"_options\">";
	html_extra += "</div>";
	document.getElementById('area_services').innerHTML += html_extra;
}


/*
	Used to remove a drop down of services from the request form
*/
function broadplace_user_order_service_remove(area_id)
{
	document.getElementById('area_services_'+area_id+'_select').innerHTML = "<input type=\"hidden\" name=\"service_"+area_id+"\" value=\"ignore\" />";
	document.getElementById('area_services_'+area_id+'_options').innerHTML = '';
}


/*
	When a user has selected a service, put in appropriate detail parameters and create the next drop down.
*/
function broadplace_user_order_service_select(area_id, service_id)
{
	// This will be filled in below with new HTML to be appended to the order form.
	var html_inputs = '';
	
	// Send a request for a list of services offered by Broadplace.
	var details_xml = broadplace_user_request('services.ajax', 'id='+service_id);
	
	// Go through each service one by one.
	var details = details_xml.getElementsByTagName('detail');
	for (var i = 0; i < details.length; i ++)
	{
		try
		{
			var detail_id = details[i].attributes[0].value;
			var detail_type = details[i].attributes[1].value;
			var detail_name = details[i].attributes[2].value;
			
			if (detail_type == 'request')
				html_inputs += "<tr><td align=\"left\" valign=\"top\" width=\"120\">&nbsp;</td><td valign=\"middle\" align=\"left\">"+detail_name+"&nbsp;&nbsp;<input class=\"form_text_small\" type=\"text\" name=\"detail_"+area_id+"_"+detail_id+"\"></td></tr>";
		}
		catch (e) { alert(e); }
	}

	// Add the new drop down to the form.	
	html_extra = "";
	html_extra += "<table cellpadding=\"0\" cellspacing=\"4\" border=\"0\" width=\"400\">";
		html_extra += html_inputs;
	html_extra += "</table>";
	document.getElementById('area_services_'+area_id+'_options').innerHTML = html_extra;
}


/*
	Submits the order form.
*/
function broadplace_user_order_service_submit()
{
	document.request_form.submit();
}


/*
	Sends an HTTP request and returns the resulting XML object.
*/
function broadplace_user_request(url, parameters)
{
	// Send the HTTP request.
	if (window.XMLHttpRequest) 
	{
		request = new XMLHttpRequest();
		request.open("POST", url, false);
		request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		request.send(parameters);
	}
	else if (window.ActiveXObject) 
	{
		request = new ActiveXObject("Microsoft.XMLHTTP");
		request.open("POST", url, false);
		request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		request.send(parameters);
	}

	// Process response on a successful return.
	if (request.status == 200)
	{
		var xml_response = '';
		if (window.ActiveXObject) 
		{
			xml_response = new ActiveXObject("Microsoft.XMLDOM");
			xml_response.loadXML(request.responseText);
		}
		else
		{
			xml_response = (new DOMParser()).parseFromString(request.responseText, "text/xml");
		}
		
		return xml_response;
	}
}
