// Purchase Functions
var site_root = "http://www.shutterfarm.com";

	obj_login = new Object();

window.onload = function(){
	if(window.document.getElementById("loginbox")){
		 obj_login.HTML = window.document.getElementById("loginbox").innerHTML;
		 if(window.document.getElementById("username")){
			 window.document.getElementById("username").focus();
		 }
	}
}
function purchase(){
	httpPost = getPOSTRequest();
	if(!httpPost){
		window.document.creditpkg.submit();	
		return;	
	}
	
	//get the parameter
	 var params = "mid=" + encodeURI(window.document.getElementById("mid").value) +
                    "&itemDesc=" + encodeURI(window.document.getElementById("item_name").value);
	
	url = "purchaselog.php";
	httpPost.onreadystatechange = function(){
		if (httpPost.readyState == 4 || httpPost.readyState=="complete") {
		data = httpPost.responseText;
			if(data == "loged"){
				window.document.creditpkg.submit();	
			}else{
				window.document.creditpkg.submit();	
			}
		}		
	}
	httpPost.open('POST', url, true);
	httpPost.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	httpPost.setRequestHeader("Content-length", params.length);
	httpPost.setRequestHeader("Connection", "close");
	httpPost.send(params);
}
function updatecredits(pid){
	var name = window.document.getElementById("name_"+pid).innerHTML;
	var price = window.document.getElementById("price_"+pid).innerHTML;
	var mid = window.document.getElementById("mid").value;
	//set form information
	
	window.document.getElementById("item_name").value= name+" ShutterFarm Credits";
	window.document.getElementById("item_number").value = pid+"-"+mid;
	window.document.getElementById("amount").value = price;
}

function login(){
	httpPost = getPOSTRequest();
	
	
	//get the parameter
	 var params = "username=" + encodeURI( window.document.getElementById("username").value ) +
                    "&password=" + encodeURI( window.document.getElementById("password").value );
	
	url = "dologin.php";
	httpPost.onreadystatechange = chklogin;
	httpPost.open('POST', url, true);
	httpPost.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	httpPost.setRequestHeader("Content-length", params.length);
	httpPost.setRequestHeader("Connection", "close");
	httpPost.send(params);
	
}

function chklogin(){
	if (httpPost.readyState == 4) {
		data = httpPost.responseText;
		if(data == "loged"){
			window.location = "index.php";			
		}else if(data=="2"){
			window.document.getElementById("loginbox").innerHTML = "<div class=\"validateerrors\">Authenication Failed. Please enter your correct username or password. Username and password are case sensitive</div>";
			window.setTimeout("resetloginbox()",6000);
		}else if(data=="3"){
			window.document.getElementById("loginbox").innerHTML = "<div class=\"validateerrors\">This account is no longer active.</div>";
			window.setTimeout("resetloginbox()",6000);
		}
	}	
}

function resetloginbox(){
	window.document.getElementById("loginbox").innerHTML = obj_login.HTML;	
}
function getPOSTRequest() {
	http_request = false;
	if (window.XMLHttpRequest) {
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/html');
		}
	} else if(window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
			   http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
			
			}
		}
	}
	if (!http_request) {
		//cannot make the ajax so go to the login page
		window.location = site_root+"/accounts";
		return false;
	}
	
	return http_request;
}
function getPOSTRequest2() {
	http_request = false;
	if (window.XMLHttpRequest) {
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/html');
		}
	} else if(window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
			   http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
			
			}
		}
	}
	if (!http_request) {
		//cannot make the ajax so go to the login page
		return false;
	}
	
	return http_request;
}