// JavaScript Document

function makeStatistics() {	

var xmlHttp;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	} catch (e) {
			alert("Your browser does not support AJAX!");
			return false;
			}
		}
	}

	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
			document.getElementById("statistics").innerHTML=xmlHttp.responseText;
		} else {
			document.getElementById("statistics").innerHTML='<div class="center"><img src="images/loading_medium.gif" vspace="15"></div>';
		}
	}
	xmlHttp.open("POST", "ajax/statistics.php", true);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlHttp.send();
}


//Register User
function register(username, fname, lname, male, female, pass1, pass2, email1, email2, country, region, address, mobile, telephone, fax, regional_emails, xtreme_news, regional_sms, agreement) {	

var username=document.getElementById(username);
var fname=document.getElementById(fname);
var lname=document.getElementById(lname);
var male=document.getElementById(male);
var female=document.getElementById(female);
var pass1=document.getElementById(pass1);
var pass2=document.getElementById(pass2);
var email1=document.getElementById(email1);
var email2=document.getElementById(email2);
var country=document.getElementById(country);
var region=document.getElementById(region);
var address=document.getElementById(address);
var mobile=document.getElementById(mobile);
var telephone=document.getElementById(telephone);
var fax=document.getElementById(fax);
var regional_emails=document.getElementById(regional_emails);
var xtreme_news=document.getElementById(xtreme_news);
var regional_sms=document.getElementById(regional_sms);
var agreement=document.getElementById(agreement);

var xmlHttp;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	} catch (e) {
			alert("Your browser does not support AJAX!");
			return false;
			}
		}
	}

	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
			document.getElementById("registrationNote").innerHTML=xmlHttp.responseText;
		} else {
			document.getElementById("registrationNote").innerHTML='<div class="center"><img src="images/loading_red.gif" vspace="15"></div>';
		}
	}
	xmlHttp.open("POST", "ajax/register.php", true);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlHttp.send("mode=process&user="+username.value+"&fname="+fname.value+"&lname="+lname.value+"&male="+male.checked+"&female="+female.checked+"&pass1="+pass1.value+"&pass2="+pass2.value+"&email1="+email1.value+"&email2="+email2.value+"&country="+country.value+"&region="+region.value+"&address="+address.value+"&mobile="+mobile.value+"&telephone="+telephone.value+"&fax="+fax.value+"&regional_emails="+regional_emails.checked+"&xtreme_news="+xtreme_news.checked+"&regional_sms="+regional_sms.checked+"&agreement="+agreement.checked);
}


//Make Login Form
function makeLogin() {	

var xmlHttp;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	} catch (e) {
			alert("Your browser does not support AJAX!");
			return false;
			}
		}
	}

	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
			document.getElementById("loginForm").innerHTML=xmlHttp.responseText;
		} else {
			document.getElementById("loginForm").innerHTML='<div class="center"><img src="images/loading_medium.gif" vspace="15"></div>';
		}
	}
	xmlHttp.open("POST", "ajax/login.php", true);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlHttp.send("mode=make");
}

//Login Authentication
function doLogin(usr, psw) {
	var usr=document.getElementById(usr);
	var psw=document.getElementById(psw);

var xmlHttp;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	} catch (e) {
			alert("Your browser does not support AJAX!");
			return false;
			}
		}
	}

	if(usr.value=="" || usr.value=="Username") {
		alert("You have to specify a username!");
	} else if(psw.value=="") {
		alert("You have to specify a password!");
	} else {
		xmlHttp.onreadystatechange=function() {
			if(xmlHttp.readyState==4) {
				if(xmlHttp.responseText=="Bad Login!") {
					alert("Bad login attempt! Check your username/password and try again!");
					makeLogin();
				} else if(xmlHttp.responseText=="Inactive User!") {
					alert("Your username is inactive! Make sure to check your email and click on the Confirmation Link to activate your account!");
					makeLogin();
				} else {
					document.getElementById("loginForm").innerHTML=xmlHttp.responseText;
					makeJoin("user");
					makeSubscription("user");
				}
			} else {
				document.getElementById("loginForm").innerHTML='<div class="center"><img src="images/loading_medium.gif" vspace="15"></div>';
			}
		}
		xmlHttp.open("POST", "ajax/login.php", true);
		xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xmlHttp.send("mode=login&usr="+usr.value+"&psw="+psw.value);
	}
}

//Take control of the "Join" box
function makeJoin(mode) {

var xmlHttp;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	} catch (e) {
			alert("Your browser does not support AJAX!");
			return false;
			}
		}
	}

	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
			document.getElementById("join").innerHTML=xmlHttp.responseText;
		} else {
			document.getElementById("join").innerHTML='<div class="center"><img src="images/loading_medium.gif" vspace="15"></div>';
		}
	}
	xmlHttp.open("POST", "ajax/join.php", true);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlHttp.send("mode="+mode);
}

//Take control of the "Subscription" box
function makeSubscription(mode) {

var xmlHttp;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	} catch (e) {
			alert("Your browser does not support AJAX!");
			return false;
			}
		}
	}

	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
			document.getElementById("subscription").innerHTML=xmlHttp.responseText;
		} else {
			document.getElementById("subscription").innerHTML='<div class="center"><img src="images/loading_medium.gif" vspace="15"></div>';
		}
	}
	xmlHttp.open("POST", "ajax/subscription.php", true);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlHttp.send("mode="+mode);
}

//User already logged in
function makeLoggedIn(usrid) {

var xmlHttp;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	} catch (e) {
			alert("Your browser does not support AJAX!");
			return false;
			}
		}
	}

	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
			if(xmlHttp.responseText=="Bad Login!") {
				alert("Bad login attempt! Check your username/password and try again!");
				makeLogin();
			} else {
				document.getElementById("loginForm").innerHTML=xmlHttp.responseText;
				makeJoin("user");
				makeSubscription("user");
			}
		} else {
			document.getElementById("loginForm").innerHTML='<div class="center"><img src="images/loading_medium.gif" vspace="15"></div>';
		}
	}
	xmlHttp.open("POST", "ajax/login.php", true);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlHttp.send("mode=loggedin&usrid="+usrid);
}

//Checks username validity
function validateRegistration(mode, val, spanName) {

var xmlHttp;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	} catch (e) {
			alert("Your browser does not support AJAX!");
			return false;
			}
		}
	}
	
	if(mode=='pass2') {
		var val=val+"/"+document.getElementById('pass1').value;
	} else if(mode=='email2') {
		var val=val+"/"+document.getElementById('email1').value;
	}

	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
			document.getElementById(spanName).innerHTML=xmlHttp.responseText;
		}
	}
	xmlHttp.open("POST", "ajax/register.php", true);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlHttp.send("mode="+mode+"&val="+val);
}

//change subscription settings
function changeSubscription(emails,news,sms,mobilecode) {

var xmlHttp;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	} catch (e) {
			alert("Your browser does not support AJAX!");
			return false;
			}
		}
	}
	
	var emails=document.getElementById(emails).checked;
	var news=document.getElementById(news).checked;
	var sms=document.getElementById(sms).checked;
	var code=document.getElementById(mobilecode).value;

	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
			document.getElementById("subNotice").innerHTML=xmlHttp.responseText;
			makeSubscription('user');
		} else {
			document.getElementById("subNotice").innerHTML='<div class="center"><img src="images/loading_small.gif" vspace="15"></div>';
		}
	}
	xmlHttp.open("POST", "ajax/subscription.php", true);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlHttp.send("mode=change&emails="+emails+"&news="+news+"&sms="+sms+"&code="+code);
}

//generate email campaign boxes
function makeEmailCampaigns(uid, month, year) {

var xmlHttp;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	} catch (e) {
			alert("Your browser does not support AJAX!");
			return false;
			}
		}
	}
	
	if(!uid || uid=="" || uid<=0) {
		window.location='index.php';
	}
	
	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
			document.getElementById("campaigns").innerHTML=xmlHttp.responseText;
		} else {
			document.getElementById("campaigns").innerHTML='<div class="center"><img src="images/loading_medium.gif" vspace="15"></div>';
		}
	}
	xmlHttp.open("POST", "ajax/emailCampaigns.php", true);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlHttp.send("mode=make&id="+uid+"&month="+month+"&year="+year);
}
