function ccsi_keypress(e){
	var keynum;
	var keychar;
	var numcheck;

	if(window.event){
		// IE
		keynum = e.keyCode;
	} else if (e.which) {
		// Mozilla
		keynum = e.which;
	}
	if (keynum == 13){
		ccsi_chklogin();
	}
}

function ccsi_chklogin() {
	// Check the login for the ccsi website
	var slogincode;
	var spassword;
	var bremember;
	var surl;
	var bcontinue;
	var ologincode 		= document.getElementById("inpLoginCode");
	var ologinpass 		= document.getElementById("inpLoginPwd");
	var ologinremeber 	= document.getElementById("inpLoginRemember");

	slogincode = ologincode.value;
	spassword = ologinpass.value;

	bcontinue = (slogincode.length > 0);
	if (!bcontinue){
		ccsi_showerrorlogin('Mandatory: Login name.');
	}

	if (bcontinue){
		bcontinue = (spassword.length > 0);
		if (!bcontinue){
			ccsi_showerrorlogin('Mandatory: Password.');
		}
	}

	if (bcontinue) {
		bremember = 0;
		if (ologinremeber.checked){
			bremember	= 1;
		}
		ccsi_showmessage('Processing');
		surl = '/phplib/ccsi_login_xml.php'; //?username=' + Url.encode(slogincode) + '&password=' + Url.encode(spassword) + '&remember=' + bremember;
		ccsi_login(surl, slogincode, spassword, bremember);
	}
}

function ccsi_showmessage(sMessage){
	var oLoginTable	= document.getElementById("tblLogin");
	var icounter;
	var irows = oLoginTable.rows.length;
	icounter = 0;


	for(icounter=0; icounter<irows;icounter++){
		oLoginTable.deleteRow(-1);
	}

	var oRow				= oLoginTable.insertRow(0);
	var oCell				= oRow.insertCell(0);
	oCell.colSpan			= 2;
	oCell.style.fontFamily	= 'Tahoma, Verdana, Arial, Sans-Serif';
	oCell.style.fontSize	= '8pt';
	oCell.style.fontWeight	= 'bold';
	oCell.style.textAlign	="center";
	oCell.innerHTML			= sMessage ;

	var oRow				= oLoginTable.insertRow(1);
	var oCell				= oRow.insertCell(0);
	oCell.colSpan			= 2;
	oCell.style.textAlign	= 'center';
	oCell.style.fontFamily	= 'Tahoma, Verdana, Arial, Sans-Serif';
	oCell.style.fontSize	= '8pt';
	oCell.innerHTML			= 'One moment please';

	var oRow				= oLoginTable.insertRow(2);
	var oCell				= oRow.insertCell(0);
	oCell.colSpan			= 2;
	oCell.style.fontFamily	= 'Tahoma, Verdana, Arial, Sans-Serif';
	oCell.style.fontSize	= '8pt';
	oCell.innerHTML			= '&nbsp;';

	var oRow				= oLoginTable.insertRow(3);
	var oCell				= oRow.insertCell(0);
	oCell.colSpan			= 2;
	oCell.style.fontFamily	= 'Tahoma, Verdana, Arial, Sans-Serif';
	oCell.style.fontSize	= '8pt';
	oCell.innerHTML			= '&nbsp;';

}


function ccsi_login(url, username, password, remember){
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
        req.open("POST", url, true);
		req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		req.onreadystatechange = ccsi_handleloginxml;
		var param = 'username=' + username + '&password=' + password + '&remember='+remember;
        req.send(param);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
			req.open("POST", url, true);
			req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			req.onreadystatechange = ccsi_handleloginxml;
			var param = 'username=' + username + '&password=' + password + '&remember='+remember;
			req.send(param);
        }
    }
}

function ccsi_showerrorlogin(sError){
	var oLoginTable = document.getElementById("tblLogin");
	var icounter;
	var irows = oLoginTable.rows.length;
	icounter = 0;


	for(icounter=0; icounter<irows;icounter++){
		oLoginTable.deleteRow(-1);
	}

	var oRow = oLoginTable.insertRow(0);
	var oCell = oRow.insertCell(0);
	oCell.colSpan = 2;
	oCell.style.fontFamily = 'Tahoma, Verdana, Arial, Sans-Serif';
	oCell.style.fontSize = '8pt';
	oCell.style.fontWeight = 'bold';
	oCell.style.textAlign="center";
	oCell.innerHTML = 'Login failed';

	var oRow = oLoginTable.insertRow(1);
	var oCell = oRow.insertCell(0);
	oCell.colSpan = 2;
	oCell.style.fontFamily = 'Tahoma, Verdana, Arial, Sans-Serif';
	oCell.style.fontSize = '8pt';
	oCell.innerHTML = 'An error occured';

	var oRow = oLoginTable.insertRow(2);
	var oCell = oRow.insertCell(0);
	oCell.colSpan = 2;
	oCell.style.fontFamily = 'Tahoma, Verdana, Arial, Sans-Serif';
	oCell.style.fontSize = '8pt';
	oCell.innerHTML = sError;

	var oRow = oLoginTable.insertRow(3);
	var oCell = oRow.insertCell(0);
	oCell.colSpan = 2;
	oCell.style.fontFamily = 'Tahoma, Verdana, Arial, Sans-Serif';
	oCell.style.fontSize = '8pt';
	oCell.innerHTML = '&nbsp;';

	var oRow = oLoginTable.insertRow(4);
	var oCell = oRow.insertCell(0);
	oCell.style.fontFamily = 'Tahoma, Verdana, Arial, Sans-Serif';
	oCell.style.fontSize = '8pt';
	oCell.colSpan = 2;
	oCell.style.textAlign="right";
	oCell.innerHTML = '<button tabindex = "1" id="retrybutton" CLASS="ccsi_btn" OnClick="ccsi_showformlogin(true)">Retry</button>';

	var oRetry 			= document.getElementById("retrybutton");
	if (oRetry != null && oRetry.style.display != 'none'){
		oRetry.focus();
	}
}

function ccsi_showformlogin(focus){
	var oLoginTable = document.getElementById("tblLogin");
	var icounter;
	var irows = oLoginTable.rows.length;
	icounter = 0;


	for(icounter=0; icounter<irows;icounter++){
		oLoginTable.deleteRow(-1);
	}
	var rownumber = 0;

	var oRow = oLoginTable.insertRow(rownumber);
	var oCell = oRow.insertCell(0);
	oCell.colSpan	=	3;
	oCell.style.fontFamily = 'Tahoma, Verdana, Arial, Sans-Serif';
	oCell.style.fontSize = '8pt';
	oCell.innerHTML = '&nbsp;';

	rownumber += 1;

	var oRow = oLoginTable.insertRow(rownumber);
	var oCell = oRow.insertCell(0);
	oCell.style.fontFamily = 'Tahoma, Verdana, Arial, Sans-Serif';
	oCell.style.fontSize = '8pt';
	oCell.innerHTML = 'Login';
	var oCell = oRow.insertCell(1);
	oCell.style.fontFamily = 'Tahoma, Verdana, Arial, Sans-Serif';
	oCell.style.fontSize = '8pt';
	oCell.colSpan = 2;
	oCell.innerHTML	= '<input tabindex = "1" id="inpLoginCode" onkeypress="return ccsi_keypress(event)" class="ccsi_input" type="text" size="13" maxlength="20">';
//	oCell.innerHTML = '<input id="inpLoginCode" CLASS="ccsi_input" type="text" size="13" maxlength="20">';

	rownumber += 1;
	var oRow = oLoginTable.insertRow(rownumber);
	var oCell = oRow.insertCell(0);
	oCell.style.fontFamily = 'Tahoma, Verdana, Arial, Sans-Serif';
	oCell.style.fontSize = '8pt';
	oCell.innerHTML = 'Password';
	var oCell = oRow.insertCell(1);
	oCell.style.fontFamily = 'Tahoma, Verdana, Arial, Sans-Serif';
	oCell.style.fontSize = '8pt';
	oCell.colSpan = 2;
	oCell.innerHTML	= '<input tabindex = "2" id="inpLoginPwd" onkeypress="return ccsi_keypress(event)" class="ccsi_input" type="password" size="13" maxlength="20">';	
//	oCell.innerHTML = '<input id="inpLoginPwd" CLASS="ccsi_input" type="password" size="13" maxlength="20">';

	rownumber += 1;
	var oRow = oLoginTable.insertRow(rownumber);
	var oCell = oRow.insertCell(0);
	oCell.style.fontFamily = 'Tahoma, Verdana, Arial, Sans-Serif';
	oCell.style.fontSize = '8pt';
	oCell.innerHTML = 'Remember';
	var oCell = oRow.insertCell(1);
	oCell.style.fontFamily = 'Tahoma, Verdana, Arial, Sans-Serif';
	oCell.style.fontSize = '8pt';
	oCell.innerHTML	= '<input tabindex = "3" id="inpLoginRemember" onkeypress="return ccsi_keypress(event)" type="checkbox" name="remember">';
//	oCell.innerHTML = '<input id="inpLoginRemember" type="checkbox" name="remember">';

	var oCell = oRow.insertCell(2);
	oCell.style.fontFamily = 'Tahoma, Verdana, Arial, Sans-Serif';
	oCell.style.fontSize = '8pt';
	oCell.style.textAlign="left";
	oCell.innerHTML = '<button id="btnLogin" CLASS="ccsi_btn" OnClick="ccsi_chklogin()">Login</button>';


	if (focus){
		var ologincode 			= document.getElementById("inpLoginCode");
		if (ologincode != null && ologincode.style.display != 'none'){
			ologincode.focus();
		}
	}
}

function ccsi_handleloginxml(){
	// ok, let's evaluate the xml
	var oNodes;
	var sError;

	if (req.readyState == 4) { // Complete
		if (req.status == 200) { // OK response

			xml =  req.responseXML;
			if(xml!=null) {
				oNodes = xml.getElementsByTagName("entity");
				sText = "";
				if(oNodes==null){
					alert('Houston we have a problem');
				} else {
					if (oNodes.length>0) {
						icounter = 0;
						for (icounter=0; icounter<oNodes.length; icounter++ )
						   {
							sError 	= oNodes[icounter].getAttribute('error');
							ccsi_showerrorlogin(sError);
						}
					} else {
						top.location.href = '/index.php';
					}
				}
			}
		}
	}
}

var Url = {

	// public method for url encoding
	encode : function (string) {
		return escape(this._utf8_encode(string));
	},

	// public method for url decoding
	decode : function (string) {
		return this._utf8_decode(unescape(string));
	},

	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";

		for (var n = 0; n < string.length; n++) {

			var c = string.charCodeAt(n);

			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}

		}

		return utftext;
	},

	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;

		while ( i < utftext.length ) {

			c = utftext.charCodeAt(i);

			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}

		}

		return string;
	}

}