var req;
var dom;

/*******************************************************************************
 * getAttributeList - Create an attribute list as XML
 *******************************************************************************/
function getAttributeList(job_attribute)
{
	var xml = "<AttributeList>\n";
	for (var i in job_attribute)
	{
		var vals = job_attribute[i];
		var len = vals.length;
		for (var x=0;x<len;x++)
		{
			if (vals[x])
				xml += "<Attribute id=\""+i+"\">"+vals[x].replace('&','&amp;').replace('&amp;amp;','&amp;')+"</Attribute>\n";
		}
	}
	xml += "</AttributeList>\n";
	return xml;
}

/*******************************************************************************
 * getAttributesFromForm - Gets the attributes on the form into a list
 *******************************************************************************/
function getAttributesFromForm(form)
{
	var job_attribute = new Array();

// Iterate over all of the controls
	var v = '';
	var len = form.elements.length;
	for (var i=0;i<len;i++)
	{
		var ctl = form.elements[i];

	// If the control doesn't have a value, skip it
		if (!ctl.value || ctl.disabled)
			continue;

	// If the control is a radio, checkbox, or menu, give it special handling
		v = '';
		switch (ctl.type)
		{
		case 'select':
		// If it's a menu, look up the value
			v = ctl.options[ctl.selectedIndex].value;
			break;
		case 'radio':
		case 'checkbox':
		// If it's a radio or checkbox but not checked, skip it
			if (!ctl.checked)
				continue;
		default:
			v = ctl.value;
			break;
		}

	// Get the name without the brackets
		var n = ctl.name;
		var pos = n.indexOf('[]');
		if (pos > -1)
			n = n.substring(0,pos);

	// Add the attribute/attribute value to the array
		if (!job_attribute[n])
			job_attribute[n] = new Array();
		var ja_len = job_attribute[n].length;
		job_attribute[n][ja_len] = v;
	}
	return job_attribute;
}

/*******************************************************************************
 * formatCurrency - from javascript.internet.com
 *******************************************************************************/
function formatCurrency(num,currency)
{
// Get the currency symbol
	if (!currency)
		currency = 'USD';
	var symbol;
	var symbol_before;
	var round_to;
	switch (currency)
	{
	case 'AUD':
		symbol_before = true;
		symbol = '$';
		round_to = 2;
		break;
	case 'CAD':
		symbol_before = true;
		symbol = 'C$';
		round_to = 2;
		break;
	case 'EUR':
		symbol_before = false;
		symbol = 'EUR';
		round_to = 2;
		break;
	case 'GBP':
		symbol_before = true;
		symbol = '£';
		round_to = 2;
		break;
	case 'JPY':
		symbol_before = true;
		symbol = 'ï¿¥';
		round_to = 0;
		break;
	case 'USD':
		symbol_before = true;
		symbol = '$';
		round_to = 2;
		break;
	default:
		symbol = '$';
		symbol_before = true;
		round_to = 2;
		break;
	}
	if (round_to > 4)
		round_to = 2;

// Weed out extraneous symbols
	num = num.toString().replace(/\$|\,/g,'');

// If it's not a number, use 0
	if(isNaN(num))
		num = "0";

// Get the sign
	sign = (num == (num = Math.abs(num)));

// Round to the nearest cent
	var d = Math.pow(10,round_to);
	num = Math.round(num*d)/d;

// Separate the dollars from cents
	cents = Math.round((num*d)%d);
	cents = cents.toString();
	dollar = Math.floor(num);
	dollar = dollar.toString();

// Add a leading zero if cents is less that ten
	if (cents < 10)
		cents = '0'+cents;

// Add the commas to the dollar
	var buffer = cents;
	var len = dollar.length-1;
	var sep = '.';
	for (var i=len;i>-1;i--)
	{
		if (((len-i) % 3) == 0)
		{
			buffer = sep+buffer;
			sep = ',';
		}
		buffer = dollar.charAt(i)+buffer;
	}

// Add the symbol
	if (symbol_before)
		buffer = symbol+buffer;
	else
		buffer = buffer+symbol;
	return buffer;

//	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
//		num = num.substring(0,num.length-(4*i+3))+','+ num.substring(num.length-(4*i+3));
//	return (((sign)?'':'-') + '$' + num + '.' + cents);
}

function xmlify(s)
{
// Iterate over the string and translate non-ASCII to entities
	var buffer = '';
	for (var i=0;i<s.length;i++)
	{
		var c = s.charAt(i);
		if (c == '&')
			buffer += '&amp;';
		else
		{
			var o = s.charCodeAt(i);
			buffer += (o > 31 && o < 127) ? c : '&#'+o+';';
		}
	}
	return buffer;
}

/*******************************************************************************
 * parseXMLIntoDOM - parse the XML string into a DOM object
 *******************************************************************************/
function parseXMLIntoDOM(xml)
{
	try {
	// Re-request from local using a text/xml header
		var url = "data:text/xml;charset=utf-8," + encodeURIComponent(xml);
		req = new XMLHttpRequest();
		req.open("GET",url);
		req.onreadystatechange = handleResponse;
		req.send(null);
	} catch (ex) {
		activeXParseXMLIntoDOM(xml);
	}
}

/*******************************************************************************
 * activeXParseXMLIntoDOM - get the XML object ActiveX style
 *******************************************************************************/
function activeXParseXMLIntoDOM(xml)
{
	try {
		dom = new ActiveXObject("Microsoft.XMLDOM");
		dom.async = false;
		dom.loadXML(xml);
	} catch (ex) {}
}

/*******************************************************************************
 * handleResponse - get the XML object
 *******************************************************************************/
function handleResponse()
{
	if (!req)
		return;
	if (req.readyState == 4)
	{
	// Complete
		if (req.status == 200)
		{
		// OK response
		    dom = req.responseXML;
		}
	}
}

/*******************************************************************************
 * get_base_url - returns the base url with http or https and the domain of the parent page
 *******************************************************************************/
function get_base_url()
{
	var pos = document.location.href.substring(8,document.location.href.length).indexOf('/');
	var base = (pos > -1) ? document.location.href.substring(0,pos+8) : document.location.href;
	return  base;
}