//////////////////////////////////////////////////////////////////////////////////////////////////
// www.hanseandersson.com
// Hans E Andersson, 2008-01-25
// ISO Latin 1

//////////////////////////////////////////////////////////////////////////////////////////////////
// START: AJAX //
var ie4_check = (document.all && !document.getElementById) ?     true : false;
var ie5_check = (document.all && document.getElementById) ?     true : false;
var req;
var winIE_client;
var message;
var url;
var t;

function getHTTPObject() {
	var req = false;
	//Use IE's ActiveX items to load the file.
	if(typeof ActiveXObject != 'undefined') {
		try { req = new ActiveXObject("Msxml2.XMLHTTP"); }
		catch (e) {
			try { req = new ActiveXObject("Microsoft.XMLHTTP"); }
			catch (E) { req = false; }
		}
		winIE_client = true;
	//If ActiveX is not available, use the XMLHttpRequest of Firefox/Mozilla etc. to load the document.
	} else if (XMLHttpRequest) {
		try { req = new XMLHttpRequest(); }
		catch (e) { req = false; }
	}
	return req;
}

var req = getHTTPObject();
// SKICKA POST
function sendXMLPostData(url,params,bstring) {
	req.onreadystatechange = processReqChange;
	req.open('POST', url, true);
	req.setRequestHeader("Content-type", "multipart/form-data; boundary=\"" + bstring + "\"");
	req.setRequestHeader("Connection", "close");
	req.setRequestHeader("Content-length", params.length);
	req.send(params);
}
// SKICKA GET
function sendXMLGetData(url) {
	req.open("GET", url, true);
	if (winIE_client) { req.send(); }
	else { req.send(null); }
	req.onreadystatechange = processReqChange;
	
}

// START: Ta emot request //
function processReqChange() {
	var message = document.getElementById("BBstatus");
	message.style.color = "#789";
	if (req.readyState == 0) {
		message.innerHTML = 'idle';
	}
	if (req.readyState == 1) {
		message.innerHTML = 'loading';
	}
	if (req.readyState == 3) {
		message.innerHTML = 'interactive';
	}
	
	if (req.readyState == 4) {
		var result;
		var str;
		var list;
		
		message.innerHTML = 'COPMLETE';
		
		if (req.status == 200) {
			var stat      = req.responseXML.documentElement;
			var method    = stat.getElementsByTagName('dosomeajax')[0].firstChild.data;
			var str = '';
			if (stat.getElementsByTagName('result')[0].firstChild.data != '0') {
				result    = stat.getElementsByTagName('result')[0].firstChild.data;
				str = method + "('" +preview_bild+ "','','"  +result+ "')";
				//alert("Resp: " +str);
				eval(str);
			}
			//if (stat.getElementsByTagName('resultlist')[0].firstChild.data != '') {
			//	list  = stat.getElementsByTagName('list');
			//	//alert(method + '(list)');
			//	//alert(list);
			//	eval(method + '(list)');	
			//}
			//if (method == "getNyckelord") {
			//	//var list  = stat.getElementsByTagName('list');
			//	//alert("Metod:" +method);
			//}
			if (method == "noData") {
				alert(req.responseText);
			}
		} else {
			var txt = req.statusText;
			alert("Det gick inte att hämta data!");
		}
	}
}
// END: Ta emot request //




function createNewSession() {
	c = "<table><tr>";
	c = c+ "<td colspan=\"3\"><b>Skapa Ny Session</b></td></tr>";
	c = c+ "<tr><td>Session Namn</td>";
	c = c+ "<td><input type=\"text\" name=\"new_session\" class=\"fields\" /></td>";
	c = c+ "<td><input type=\"submit\" class=\"fancybutt\" value=\"Skapa\" /></td>";
	c = c+ "</tr></table>";
	//c = c+ "<input type=\"hidden\" name=\"usr_id\" class=\"fancybutt\" value=\"$user_id\" />";
	document.getElementById('transplatta').innerHTML = c;
	document.getElementById('transplatta').style.display = "block";
}

function activateSession(session_id,user_id,resultat) {
	var url = "ajax.xin";
	var mesg = document.getElementById('ajaxStat');
	if (resultat != '') {
		//alert(resultat);
		mesg.innerHTML = resultat;
		window.location.reload();
	}
	if (user_id != '') {
		if (session_id != '') {
			url = url + "?setSession=" +session_id+ "&user_id=" + user_id;
			//alert("Sänder: " +url);
			sendXMLGetData(url);
		}
	}
}

function deleteSession(session_id,user_id,resultat) {
	var url = "ajax.xin";
	var mesg = document.getElementById('ajaxStat');
	if (resultat != '') {
		//alert(resultat);
		mesg.innerHTML = resultat;
		window.location.reload();
	}
	if (user_id != '') {
		if (session_id != '') {
			
			
			if (confirm("Är det säkert att du vill slänga session " +session_id+ "?\n")) {
				document.getElementById('prompt').innerHTML = "Det ante mig att du ville slänga " +session_id+ ".";
				url = url + "?deleteSession=" +session_id+ "&user_id=" + user_id;
				//alert("Sänder: " +url);
				sendXMLGetData(url);
			} else { 
				document.getElementById('prompt').innerHTML = "OK, behåller session " +session_id+ "!";
			}
			
			
			
		}
	}
}

function changeStyle(style_id,user_id,resultat) {
	var url = "ajax.xin";
	var mesg = document.getElementById('styleStat');
	if (resultat != '') {
		//alert(resultat);
		mesg.innerHTML = resultat;
		window.location.reload();
	}
	if (user_id != '') {
		if (style_id != '') {
			url = url + "?changeStyle=" +style_id+ "&user_id=" + user_id;
			//alert("Sänder: " +url);
			sendXMLGetData(url);
		}
	}
}

function addToCart(user_id,id,resultat) {
	var url = "ajax.xin";
	var mesg = document.getElementById('BBstatus');
	if (resultat != '') {
		//alert(resultat);
		mesg.innerHTML = resultat;
	}
	if (user_id != '') {
		if (id != '') {
			url = url + "?addToCart=" +id+ "&user_id=" + user_id;
			//alert("Sänder: " +url);
			sendXMLGetData(url);
		}
	}
}

// END: AJAX //
//////////////////////////////////////////////////////////////////////////////////////////////////

// Kolla om användaren är inloggad
// Om inloggad, skicka request vidare
// annars uppmana att logga in eller avbryt.
function checkLoginstat(str) {
	//var x = document.cookie;
	var x = getCookie("imsysuser");
	var rezu = '';
	
	var htbox = document.getElementById('loginbox');
	
	if (x) { rezu = "yes"; }
	
	if (rezu != '') {
		window.location = str;
	} else {
		
		var ut = "<br>This file requires login.<br>Continue to <a href=\"" +str+ "\">login page</a> or <a href=\"javascript:clearLoginstat()\">cancel</a>.<br>";
		//var ut = "<p><div style=\"border: 1px solid #999; padding: 3px;\">This file requires login.<br>";
		//ut += "Continue to <a href=\"" +str+ "\">login page</a> or ";
		//ut += "<a href=\"javascript:clearLoginstat()\">cancel</a>.</div></p>";
		//alert("This file requires login!" + ht);
		document.getElementById("loginbox").innerHTML = ut;
	}
	
}

function clearLoginstat() {
	document.getElementById('loginbox').innerHTML = '';
}

function getCookie(cookie_name) {
	
	var resultats = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );
	
	if (resultats) {
		return ( unescape ( resultats[2] ) );
	} else {
		return null;
	}
	
}
function passwordValidator(inp,pv) {
	//'password','pview'
	var pview = document.getElementById(pv);
	var inpf = document.signIn.password;
	var invalid = " "; // Invalid character is a space
	var minLength = 8; // Minimum length
	var pw = inpf.value;
	
	var doSubmit = "<input type=\"hidden\" name=\"register\" value=\"do\" />";
	doSubmit += "<input class=\"fancybutt\" name=\"login\" value=\"Send and wait for confirmation\" tabindex=\"5\" type=\"submit\" />";
	
	// check for minimum length
	var ts = "";
	if (inpf.value.length < minLength) {
		ts = 'Your password must be at least ' + minLength + ' characters long.';
		doSubmit = "";
	}
	// check for spaces
	if (inpf.value.indexOf(invalid) > -1) {
		ts = "Sorry, spaces are not allowed.";
		doSubmit = "";
	}
	
	//pview.innerHTML = "New password: <b>" +inpf.value+ "</b> " + ts;
	pview.innerHTML = "<b>" +inpf.value.length+ " characters</b> " + ts;
	document.getElementById("submit").innerHTML = doSubmit;
}
function confimrPassword(inp,pv) {
	var cpview = document.getElementById(pv);
	var inpf = document.signIn.password;
	var cinpf = document.signIn.cpassword;
	
	var doSubmit = "<input type=\"hidden\" name=\"register\" value=\"do\" />";
	doSubmit += "<input class=\"fancybutt\" name=\"login\" value=\"Send and wait for confirmation\" tabindex=\"5\" type=\"submit\" />";
	
	var ts = "";
	if (inpf.value == '' || cinpf.value == '') {
		ts = 'Please enter your password twice.';
		doSubmit = "";
	}
	if (inpf.value != cinpf.value) {
		ts = "You did not enter the same new password twice. Please re-enter your password.";
		doSubmit = "";
	} else {
		ts = "OK!";
	}
	
	cpview.innerHTML = ts;
	document.getElementById("submit").innerHTML = doSubmit;
}
function printConfirmation(msg) {
	newDoc=window.open('','printer','toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,fullscreen=no,width=640,height=400');
	newDoc.document.open();
	txt = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n";
	txt +="<html><head><title>Print Order</title></head>";
	txt += "<link rel=\"stylesheet\" rev=\"stylesheet\" href=\"/schnitt/print.css\" type=\"text/css\" />";
	txt += "<body><form>";
	txt +="<table width=\"100%\"><tbody><tr><td><img src=\"/schnitt/ImsyslogoBWLiten.gif\" alt=\"Imsys\"></td><td align=\"right\">";
	txt += "<b style=\"font-size:18pt\">Imsys Online Shop</b></td></tr></tbody></table>";
	txt += "<hr size=1><table width=\"100%\"><tbody><tr>";
	txt += "<td> </td>";
	txt += "<td align=\"right\"><input type=\"button\" class=\"fancybutt\" onclick=\"javascript:window.print();\" value=\"Print\" /></td>";
	txt += "</tr></tbody></table>";
	txt += "<pre>" +msg+ "</pre><hr size=1>";
	txt += "<table width=\"100%\"><tbody><tr>";
	txt += "<td> </td>";
	txt += "<td align=\"right\"><input type=\"button\" class=\"fancybutt\" onclick=\"javascript:window.close();\" value=\"Close\" /></td>";
	txt += "</tr></tbody></table>";
	txt += "</form></body></html>\n";
	newDoc.document.write(txt);
	newDoc.document.close();
	return false;
}

function douserLogout() {
	var ht = document.getElementById('sidePopperLoginPrompt');
	document.cookie = "imsysuser=; expires=; path=/";
	// Ändra https:// till http://
	var s = new String(window.location);
	if (s == "https://www.imsystech.com/cgi-bin/useraccount.pl") {
		s = "https://www.imsystech.com/cgi-bin/useraccount.pl?logout=yes";
	}
	if (s == "https://www.imsystech.com/cgi-bin/useraccount.pl?editaccount=ok") {
		s = "https://www.imsystech.com/cgi-bin/useraccount.pl?logout=yes";
	} else {
		s = s.replace("https","http");
	}
	alert("You are now leaving secure mode.");
	ht.innerHTML = '<a href=\"/cgi-bin/login.pl\">Login</a>';
	window.location = (s);
}

function showLoc() {
	var txt = document.getElementById('loc');
	//var lok = window.location;
	//var ana = document.URL;
	//txt.innerHTML = "winloc=" + lok + ana;
	//s = new String(document.URL);
	var s = new String(window.location);
	s = s.replace("https","http");
	txt.innerHTML = "new=" + s;
}