//////////////////////////////////////////////
// Javascript + Ajax Functions
// cP Creator
//////////////////////////////////////////////
var xmlHttp

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}
function ajax(url, id) {
	document.getElementById(id).innerHTML = "<img src='tpl/swirl.gif'>"
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null) {
		alert ("Browser does not support HTTP Request")
		return
	}
	var url="ajax.php?ajax=1"+url
	xmlHttp.onreadystatechange = function()	{	
		if(xmlHttp.readyState == 4){
			var ajaxDisplay = document.getElementById(id);
			ajaxDisplay.innerHTML = xmlHttp.responseText;
			return true;
		}
	}
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}

function getnews() { 
	document.getElementById("newstxt").innerHTML = "Loading news items..."
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null) {
		alert ("Browser does not support HTTP Request")
		return
	}
	var url="ajax.php?ajax=1&function=checknews"
	xmlHttp.onreadystatechange = function()	{	
		if(xmlHttp.readyState == 4){
			var ajaxDisplay = document.getElementById("newstxt");
			ajaxDisplay.innerHTML = xmlHttp.responseText;
		}
	}
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}

function GetXmlHttpObject() {
	var xmlHttp=null;
	try {
		 // Firefox, Opera 8.0+, Safari
		 xmlHttp=new XMLHttpRequest();
	 }
	catch (e) {
		 //Internet Explorer
		 try {
			  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		  }
		 catch (e) {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		  }
	 }
	return xmlHttp;
}

function Disable() {
	frm=document.forms[0]
	if(frm.agree.checked) {
		frm.next1.disabled=false
	}
	else {
		frm.next1.disabled=true
	}
}

function CheckForm() {
	frm=document.forms[0]
	frm.check.value = "Checking Form"
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null) {
		alert ("Browser does not support HTTP Request")
		return
	}
	var url="ajax.php?ajax=1&function=checkform"
	xmlHttp.onreadystatechange = function()	{	
		if(xmlHttp.readyState == 4){
			if(xmlHttp.responseText == "OK") {
				frm.check.value = "Next Step"	
				frm.submit();
			}
			else {
				frm.check.value = "Details Invalid"	
			}
		}
	}
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}
function CheckHostForm() {
	frm=document.forms[0]
	frm.next.value = "Checking Form"
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null) {
		alert ("Browser does not support HTTP Request")
		return
	}
	var url="ajax.php?ajax=1&function=checkhostform"
	xmlHttp.onreadystatechange = function()	{	
		if(xmlHttp.readyState == 4){
			if(xmlHttp.responseText == "OK") {
				frm.next.value = "Next Step"	
				frm.submit();
			}
			else {
				frm.next.value = "Details Invalid"	
			}
		}
	}
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}

//Checks Pass
function checkPass (id, pass) {
	document.getElementById('ajaxerrors').innerHTML = 'Working.....';
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null) {
		alert ("Browser does not support HTTP Request")
		return
	}
	var url="ajax.php?ajax=1&function=checkpass&pass="+pass+"&id="+id
	xmlHttp.onreadystatechange = function()	{	
		if(xmlHttp.readyState == 4){
			var ajaxDisplay = document.getElementById('ajaxerrors');
			ajaxDisplay.innerHTML = xmlHttp.responseText;
			var frm=document.pass
			if(document.getElementById('ajaxerrors').innerHTML == "Password Correct") {
				document.getElementById('ajaxerrors').innerHTML = 'Click Validate to create your account!';	
				frm.button.disabled=false
			}
			else {
				frm.button.disabled=true
			}
		}
	}
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}

//Validates + Creates
function validate (id, pass) {
	document.getElementById('valbox').innerHTML = '<center><img src="tpl/ajax-loader.gif"></center>';
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null) {
		alert ("Browser does not support HTTP Request")
		return
	}
	var url="ajax.php?ajax=1&function=validate&pass="+pass+"&id="+id
	xmlHttp.onreadystatechange = function()	{	
		if(xmlHttp.readyState == 4){
			var ajaxDisplay = document.getElementById('valbox');
			ajaxDisplay.innerHTML = xmlHttp.responseText;
		}
	}
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}

//Change Package on signup form
function changePack () {
	if(document.getElementById('changepack').style.display == "none") {
		document.getElementById('changepack').style.display = "";
	}
	else {
		document.getElementById('changepack').style.display = "none";	
	}
}

//Change Package Submit
function changePackSubmit() {
	var frm=document.user
	document.getElementById('changepack').style.display = 'none'
		document.getElementById('package').innerHTML = "Working..."
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null) {
		alert ("Browser does not support HTTP Request")
		return
	}
	var url="ajax.php?ajax=1&function=changepack&package="+frm.newpack.value
	xmlHttp.onreadystatechange = function()	{	
		if(xmlHttp.readyState == 4){
			var HTML = xmlHttp.responseText;
			ajax('&function=customform', 'customform')
			document.getElementById('package').innerHTML = HTML;
		}
	}
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}