//Create a boolean variable to check for a valid Internet Explorer instance.
var xmlhttp = false;
var scrolling = false;
var focused = false;
var scroll_timer = false;
//Check if we are using IE.
try
{
	//If the Javascript version is greater than 5.
	xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
	//If not, then use the older active x object.
	try
	{
		//If we are using Internet Explorer.
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	catch (E)
	{
		//Else we must be using a non-IE browser.
		xmlhttp = false;
	}
}

if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
{
	xmlhttp = new XMLHttpRequest();
}

function init()
{
	scroll_obj = $('scroller_content');
}

function make_request(serverPage, objID)
{
	xmlhttp.open("GET", serverPage);
	if (objID)
	{
		var obj = document.getElementById(objID);
		xmlhttp.onreadystatechange = function()
		{
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
			{
				obj.innerHTML = xmlhttp.responseText;
				document.body.focus();
				xmlhttp.onreadystatechange = function(){};
			}
		}
	}
	xmlhttp.send(null);
	return false;
}

function $(obj_id)
{
	return document.getElementById(obj_id);
}

function submit_form(form_id,ajax_file,div_id)
{
	function getform(form_obj)
	{
		var return_string = "";
		if (form_obj.hasChildNodes() && form_obj.tagName != "undefined" && form_obj.tagName != "")
		{
			for (var i=0; i<form_obj.childNodes.length; i++)
			{
				if (form_obj.childNodes[i].hasChildNodes() && form_obj.childNodes[i].tagName != "undefined" && form_obj.childNodes[i].tagName != "")
				{
					return_string += getform(form_obj.childNodes[i]);
				}
				if (form_obj.childNodes[i].tagName == "INPUT")
				{
					if (form_obj.childNodes[i].type == "text" || form_obj.childNodes[i].type == "password" || form_obj.childNodes[i].type == "hidden")
					{
						return_string += form_obj.childNodes[i].name + "=" + escape(form_obj.childNodes[i].value) + "&";
		            }
		            else if (form_obj.childNodes[i].type == "checkbox")
					{
						if (form_obj.childNodes[i].checked)
						{
							return_string += form_obj.childNodes[i].name + "=" + escape(form_obj.childNodes[i].value) + "&";
						}
						else
						{
							return_string += form_obj.childNodes[i].name + "=&";
		               	}
					}
		            else if (form_obj.childNodes[i].type == "radio")
		            {
		            	if (form_obj.childNodes[i].checked)
		            	{
		                	return_string += form_obj.childNodes[i].name + "=" + escape(obj.childNodes[i].value) + "&";
		               	}
		            }
				}
				else if (form_obj.childNodes[i].tagName == "TEXTAREA")
				{
					return_string += form_obj.childNodes[i].name + "=" + escape(form_obj.childNodes[i].value) + "&";
				}
				else if (form_obj.childNodes[i].tagName == "SELECT")
				{
					var sel = form_obj.childNodes[i];
					return_string += sel.name + "=" + escape(sel.options[sel.selectedIndex].value) + "&";
				}
			}
			return return_string;
		}
	}

	var getstr = "?";

	obj = document.getElementById(form_id);

	getstr += getform(obj);

    make_request('ajax/'+ajax_file+''+getstr,div_id);
}

function open_window(page)
{
	window.open(page, 'window_1', 'right=1, top=120, width=300, height=350, menu=no, statusbar=no, toolbar=no, scrollbars=yes, resizable=no')
}
function key_check(e)
{
	var code;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	return(code);
}
function pin_check(num)
{
	for (x = 1; x < num; x++)
	{
		document.login_form['pin'+x].onkeypress = function (e)
		{
			var id = this.id.substr(3);
			if (!e) var e = window.event;
			var key_code = key_check(e);
			if (key_code > 46 && key_code < 58) this.onkeyup = function()
			{
				var id = this.id.substr(3);
				var next_id = (id*1)+1;
				document.login_form['pin'+next_id].focus();
				this.onkeyup = null;
			};
		}
	}

	document.login_form['pin'+num].onkeypress = function(e)
	{
		if (!e) var e = window.event;
		var key_code = key_check(e);
		if (key_code == "13")
		{
			submit_now = true;
			for (x = 1; x <= num; x++)
			{
				if (submit_now && document.login_form['pin'+x].value) submit_now = true;
				else submit_now = false;
			}

			document.login_form.submit();
		}
	};
}

function pin_check2(form_name,pin_var)
{
	num = document[form_name][pin_var+'[]'].length;
	for (x = 1; x < num; x ++)
	{
		document[form_name][pin_var+x].onkeypress = function (e)
		{
			if (!e) var e = window.event;
			var key_code = key_check(e);
			if (key_code > 46 && key_code < 58) this.onkeyup = function()
			{
				var id = this.id.split(pin_var);
				var id = (id[1]*1)+1;
				document[form_name][pin_var+id].focus();
				this.onkeyup = null;
			};
		}
	}

/*
	document[form_name][pin_var+'['+num+']'].onkeypress = function(e)
	{
		if (!e) var e = window.event;
		var key_code = key_check(e);
		if (key_code == "13")
		{
			submit_now = true;
			for (x = 1; x <= num; x++)
			{
				if (submit_now && document[form_name][pin_var+'['+x+']'].value) submit_now = true;
				else submit_now = false;
			}

			document.login_form.submit();
		}
	};*/
}

function debug(debug_id,debug_var)
{
	if (!(debug_element = document.getElementById('debug_'+debug_id)))
	{
		var debug_element = document.createElement("div");
		debug_element.style.position = "fixed";
		debug_element.style.right="10px";
		debug_element.style.height="15px";
		debug_element.style.backgroundColor="#E0E0E0";
		debug_element.style.font="13px/17px verdana";
		debug_element.id = "debug_"+debug_id;
		debug_element.style.bottom = 10+(debug_id*20)+"px";
		document.body.appendChild(debug_element);
	}
	debug_element.innerHTML = debug_var;
}
function scroll(a)
{
	if (!a) return false;
	if (scrolling) return false;
	amount = a;
	from = scroll_obj.scrollLeft;
	if (from == 0 && a < 0) return false;
	if (from + a >= scroll_obj.scrollWidth) return false;
	scroller_unfocus();
	scrolling = true;
	scroll_it();
}
function scroll_it()
{
	var offset = 0;
	if (amount < 0) offset = -12;
	else offset = 12;

	if (amount > 0)
	{
		if (scroll_obj.scrollLeft+offset < from+amount && (scroll_obj.scrollLeft + offset + scroll_obj.clientWidth) < scroll_obj.scrollWidth)
		{
			scroll_obj.scrollLeft+=offset;
			setTimeout(scroll_it,20);
		}
		else
		{
			scroll_obj.scrollLeft = from+amount;
			scrolling = false;
		}
	}
	else
	{
		if ((scroll_obj.scrollLeft + offset) < 0)
		{
			scroll_obj.scrollLeft = 0;
			scrolling = false;
		}
		else if (scroll_obj.scrollLeft+offset > from+amount)
		{
			scroll_obj.scrollLeft+=offset;
			setTimeout(scroll_it,20);
		}
		else
		{
			scroll_obj.scrollLeft = from+amount;
			scrolling = false;
		}
	}
}
function scroller_focus(num)
{
	if (!scrolling && !scroll_timer)
	{
		if (focused != num)
		{
			to_focus = num;
			if (focused)
			{
				a = num - focused;
				scroller_unfocus();
				scroll(a * 128);
			}
			else
			{
				scroll(((num-1) * 128) - scroll_obj.scrollLeft);
			}
			scroll_timer = setInterval(function ()
			{
				if (!scrolling)
				{
					$('scroller_focus').style.display = "";
					$('focus_text_'+to_focus).style.display = "";
					if ($('yes_no'+to_focus)) $('yes_no'+to_focus).style.display = "none";
					focused = to_focus;
					clearInterval(scroll_timer);
					scroll_timer = false;
				}
			},100);
		}
	}
}
function scroller_unfocus()
{
	if ($('scroller_focus')) $('scroller_focus').style.display = "none";
	if (focused)
	{
		$('focus_text_'+focused).style.display = "none";
		focused = false;
	}
}

var qmarks = new Array("",
					"let us tell you when there are new berries to be earned, how to enter to win cool prizes, or send you more info or cool offers you've for asked for whilst jamming - we'll always include an unsubscribe option :-)",
					"because we carry +18's content, we need to verify your age",
					"to help us support you",
					"to help us support you",
					"essential to send you the link to download the getjam player to your mobile - we will only SMS you when we have to, otherwise we'll email you - we don't like lots of random SMSs either!",
					"soon, we'll filter stuff so that you only get info on things local to you",
					"forgotten PIN",
					"you need to 'set up' a 4 digit pin you'll remember");

function mouse_coords(ev)
{
	if(ev.pageX || ev.pageY)
	{
		return {x:ev.pageX, y:ev.pageY};
	}
	return {
		x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
		y:ev.clientY + document.body.scrollTop  - document.body.clientTop
	};
}

function mouse_move(ev)
{
	ev = ev || window.event;

	var mouse_pos = mouse_coords(ev);

	mousex = mouse_pos.x;
	mousey = mouse_pos.y;

	if (help_on)
	{
		if (qnum <= 4) document.getElementById("help_box").style.left = (mousex + 25)+"px";
		else if (qnum == 7)
		{
			help_element.style.left = (mousex - 55)+"px";
		}
		else document.getElementById("help_box").style.left = (mousex - 225)+"px";
		if (qnum == 7) document.getElementById("help_box").style.top = (mousey - 35)+"px";
		else document.getElementById("help_box").style.top = (mousey - 10)+"px";
	}
}

function show_help(num)
{
	if (num)
	{
		qnum = num;
		help_element = document.createElement("div");
		help_element.id = "help_box";
		help_element.innerHTML = qmarks[num];
		if (num <= 4) help_element.style.left = (mousex + 25)+"px";
		else if (num == 7)
		{
			help_element.style.left = (mousex - 55)+"px";
			help_element.className = "short_help";
		}
		else help_element.style.left = (mousex - 225)+"px";
		if (num == 7) help_element.style.top = (mousey - 35)+"px";
		else help_element.style.top = (mousey - 10)+"px";
		document.body.appendChild(help_element);
		help_on = true;
	}
	else
	{
		document.body.removeChild(document.getElementById("help_box"));
		help_on = false;
	}
}

document.onmousemove = mouse_move;
var mousex = 0;
var mousey = 0;
var help_on = false;
var qnum = 0;

function update(obj,what,ex1)
{
	v = obj.value;
//	alert(v);
	if(ex1)
	{
		make_request('/cmp/plans.php?model='+ex1+'&network='+v,what);
	}
	else
	{
		make_request('/cmp/models.php?brand='+v,what);
	}
}