//////////////////////////////////////////////
// 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 = "Working..."
	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;
		}
	}
	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]
	var wrong
	wrong = null
	
	//User Check
	if(frm.user.value != "demo" && frm.user.value.length != 0) {
		frm.user.value = frm.user.value.toLowerCase();
		if(document.getElementById('userajax').innerHTML == '<img src="icons/exclamation.png" alt="Wrong">&nbsp;<strong>User Exists!</strong>') {
			wrong = true;	
		}
	}
	else {
		document.getElementById('userajax').innerHTML = '<img src="icons/exclamation.png" alt="Wrong">';
		wrong = true
	}
	
	//Password Check
	if(frm.pass.value.length != 0 && frm.pass.value == frm.conpass.value && frm.conpass.value.length != 0) {
		document.getElementById('passimg').innerHTML = '<img src="icons/accept.png" alt="Accepted">';
	}
	else {
		document.getElementById('passimg').innerHTML = '<img src="icons/exclamation.png" alt="Wrong">';
		wrong = true
	}
	
	//Email Check
	apos=frm.email.value.indexOf("@");
	dotpos=frm.email.value.lastIndexOf(".");
	
	if (apos > 1 && dotpos-apos > 2 && frm.email.value.length > 4) {
		ajax("&function=emailexists&email="+this.value, "emailajax")
		if(document.getElementById('emailajax').innerHTML == '<img src="icons/exclamation.png" alt="Wrong">&nbsp;<strong>Email Exists!</strong>' || document.getElementById('emailajax').innerHTML == '<img src="icons/exclamation.png" alt="Wrong">') {
			wrong = true;	
		}
	}
	else {
		document.getElementById('emailajax').innerHTML = '<img src="icons/exclamation.png" alt="Wrong">';
		wrong = true
	}
	
	//Finish off
	if(wrong != true) {
		frm.submit()
	}
}

//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 ajaxDisplay = document.getElementById('package');
			ajaxDisplay.innerHTML = xmlHttp.responseText;
			ajax('&function=customform', 'customform')
		}
	}
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}