var W3CDOM = (document.getElementsByTagName && document.createElement);
var PCGTimer = null;
var useRPCs = true;
var cVcTimer = false;
var useRPCs = false;

function newWin(url,name,width,height){window.open(url,name,'status=0,width='+width+',height='+height+',top='+(screen.availHeight/2 - height/2)+',left='+(screen.availWidth/2 - width/2));return false;}

function e(e)
{
	return $(e);
}
function $(e)
{
	return document.getElementById(e);
}
function getHTTPObject()
{
	var xmlhttp;
	/*@cc_on
	@if(@_jscript_version >= 5)
	try{xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");}
	catch(e){try{xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}catch(E){xmlhttp=false;}}
	@else
	xmlhttp=false;
	@end @*/
	if(!xmlhttp&&typeof XMLHttpRequest!='undefined'){try{xmlhttp=new XMLHttpRequest();}catch(e){xmlhttp=false;}}
	return xmlhttp;
}
function rpcCall(url, vars, callback, method)
{
	if(httpOb.readyState==4 || httpOb.readyState==0)
	{
		method = (typeof method == 'undefined') ? 'POST' : method;
		url = (url.indexOf('http') != -1 ? url : '/' + url + '/');// + (url.indexOf('?') != -1 ? '&' : '?') + 'rand='+Math.floor(Math.random()*1000000);
		//alert(url + ', ' + method + ', ' + vars);
		httpOb.open(method, url, true);
		httpOb.onreadystatechange=callback;
		if(method == 'POST') httpOb.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
		httpOb.send(vars);
	}
}
var httpOb=getHTTPObject();

function getPage(page)
{
	if(!useRPCs) return true;
	rpcCall('RPC', 'page='+page, function(){if(httpOb.readyState==4 && httpOb.status==200) $('main3').innerHTML = httpOb.responseText;});
	return false;
}

function sendContact()
{
	if(!useRPCs) return true;
	rpcCall('Contact/Sent', 'type=' + e('type').value + '&' + 'name=' + e('name').value + '&' + 'email=' + e('email').value + '&' + 'message=' + e('message').value, function(){if(httpOb.readyState==4 && httpOb.status==200) getPage('ContactRPCDone');});
	return false;
}

function formStep(s)
{
	$('nxts').value=s;
	$('gsform').submit();
}

// addEvent function from http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html
function addEvent(obj, type, fn)
{
	if (obj.addEventListener) obj.addEventListener(type, fn, false);
	else if (obj.attachEvent)
	{
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent("on"+type, obj[type+fn]);
	}
}

function getPostcodeData(pc)
{
	if(pc == '') return false;
	rpcCall('PC-RPC', 'postcode='+pc, function()
	{
		if(httpOb.readyState==4 && httpOb.status==200)
		{
			response=httpOb.responseText;
			if(response=='FAILED' || response=='0') return;
			else
			{
				pc_data=eval('('+response+')');
				if(pc_data && pc_data['address'] && pc_data['postcode'])
				{
					var pcO = '<p>';
					if($('business_name').value != '') pcO+= $('business_name').value + '<br />';
					if($('building_no').value != '') pc_data['address'] = $('building_no').value + ' ' + pc_data['address'];
					for(var i in pc_data)
					{
						if(pc_data[i]!='') pcO+=pc_data[i]+'<br />';
						$(i+'_man').value=pc_data[i];
					}
					$('postcode').value = pc_data['postcode'];
					$('postcode_output').innerHTML = pcO+'</p>';
					$('postcode_output_cont').style.backgroundImage = 'url(/images/pc_output.png)';
				} else
				{
					$('postcode_output').innerHTML = '';
					$('postcode_output_cont').style.backgroundImage = 'url(/images/pc_not_found.png)';
				}
			}
		}
	});
}

function manualAddress()
{
	$('postcode_output_cont').style.background = '';
	$('postcode_output').style.display = 'none';
	$('manual_address').style.display = 'block';
	$('manual_address_button').style.display = 'none';
	$('manual_address_field').value = 'true';
	$('building_no').value='';
	$('building_no').disabled='disabled';
	$('postcode').value='';
	$('postcode').disabled='disabled';
	$('address_man').focus();
	
	if(W3CDOM)
	{
		$('building_no').parentNode.previousSibling.style.color = '#ccc';
		$('postcode').parentNode.previousSibling.style.color = '#ccc';
		if($('building_no').hasError) removeObjError($('building_no'));
		if($('postcode').hasError) removeObjError($('postcode'));
	}
}

function checkVerificationCode(vc)
{
	rpcCall('Signup', 'vc_rpc='+vc, function()
	{
		if(httpOb.readyState==4 && httpOb.status==200)
		{
			if(httpOb.responseText == 'VERIFIED')
			{
				$('verified').style.display = 'block';
				$('unverified').style.display = 'none';
			} else
			{
				$('verified').style.display = 'none';
				$('unverified').style.display = 'block';
			}
		}
	});
}

function replaceChars(needle,replacement,haystack)
{
	temp = "" + haystack; 
	while (temp.indexOf(needle)>-1)
	{
		pos= temp.indexOf(needle);
		temp=(temp.substring(0,pos)+replacement+temp.substring((pos+needle.length),temp.length));
	}
	return temp;
}

function checkStep(s)
{
	if(s == 1)
	{
		fields=['registered_user','business_name','building_no','postcode','tel'];
		msg=['Please enter the registered users name','Please enter the business name','Please enter the building name or no','Please enter the postcode','Please enter the telephone number'];
		
		fields2=['address_man','address2_man','address3_man','town_man','county_man','postcode_man'];
		msg2=['Please enter the first line of address','Please enter the second line of address','Please enter the third line of address','Please enter the town','Please enter the county','Please enter the postcode'];
		
		if($('manual_address_field').value == 'true')
		{
			fields=fields.concat(fields2);
			msg=msg.concat(msg2);
		}
	} else if(s == 2)
	{
		if($('unverified').style.display == 'none') return true;
		fields=['mobile','verification_code'];
		msg=['Please enter your mobile number', 'Please enter the verification code'];
	}

	for(i=0; i < fields.length; i++)
	{
		if(!$(fields[i]).disabled && replaceChars(' ','',$(fields[i]).value)=='')
		{
			writeError($(fields[i]), msg[i]);
			$(fields[i]).focus();
			return false;
		}
	}
	return true;
}

function writeError(obj,message)
{
	if (obj.hasError) return;
	if (W3CDOM)
	{
		obj.className += ' error';
		obj.onchange = removeError;
		var sp = document.createElement('span');
		sp.className = 'error';
		sp.appendChild(document.createTextNode(message));
		obj.parentNode.appendChild(sp);
		obj.hasError = sp;
	} else
	{
		errorstring += obj.name + ': ' + message + '\n';
		obj.hasError = true;
	}
	//if (!firstError) firstError = obj;
}

function removeError()
{
	this.className = this.className.substring(0,this.className.lastIndexOf(' '));
	this.parentNode.removeChild(this.hasError);
	this.hasError = null;
	this.onchange = null;
}

function removeObjError(obj)
{
	obj.className = obj.className.substring(0,obj.className.lastIndexOf(' '));
	obj.parentNode.removeChild(obj.hasError);
	obj.hasError = null;
	obj.onchange = null;
}

function startPCGTimer()
{
	if(PCGTimer) window.clearTimeout(PCGTimer);
	if($('postcode').value.length > 3) PCGTimer = window.setTimeout(function(){getPostcodeData($('postcode').value);}, 3500);
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if(a[i].indexOf("#")!=0){d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

var winHeight = 0;
function getWindowHeight() //From Google Maps
{
	if(window.self && self.innerHeight) return self.innerHeight;
	if(document.documentElement && document.documentElement.clientHeight) return document.documentElement.clientHeight;
	return 0;
}

function setContentHeight()
{
	if(typeof noResize != 'undefined') return false;
	lWH = getWindowHeight();
	if(lWH != winHeight)
	{
		$('main').style.height = (getWindowHeight() - 190) + 'px';
		$('main2').style.height = (getWindowHeight() - 190) + 'px';
	}
	winHeight = lWH;
}

addEvent(window, 'load', function()
{
	//var so = new SWFObject("/images/mp3player.swf?playlist=http://www.libritech.com/PlayList/", "line", "300", "300", "7", "#CCFF66");
	//so.write("tunes");
	//setContentHeight();
	//window.setInterval("setContentHeight();", 200);
	MM_preloadImages('/images/pc_output.png','/images/pc_not_found.png');
	
	
	/*rpcCall('http://www.jibbering.com/2002/4/test.txt', null, function()
	{
		if(httpOb.readyState==4 && httpOb.status==200)
		{
			//$('uptime').innerHTML = httpOb.responseText;
			alert(httpOb.responseText);
		}
	}, 'GET');*/

	
});