/*
Use: http://dean.edwards.name/packer/
for minification
*/
function cci_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){
		cci_chklogin();
	}
}


function cci_chklogin() {
	var RememberMe;
	var ContinueLogin;
	var LoginCode;
	var PassWordCode;

	LoginCode		= $('#inpLoginCode').val();
	PassWordCode	= $('#inpLoginPwd').val();
	ContinueLogin	= (LoginCode.length > 0);
	if (!ContinueLogin){
		cci_showerrorlogin(labels[0]);
	}

	if (ContinueLogin){
		ContinueLogin = (PassWordCode.length > 0);
		if (!ContinueLogin){
			cci_showerrorlogin(labels[1]);
		}
	}

	if (ContinueLogin) {
		RememberMe = 0;
		if ($('#inpLoginRemember:checked').val() != null){
			RememberMe	= 1;
		}
		cci_showmessage(labels[10]);
		cci_login(String('/phplib/cci_login_xml.php?username=' + Url.encode(LoginCode) + '&password=' + Url.encode(PassWordCode) + '&remember=' + RememberMe));
	}
}


function cci_login(url){
	if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
		req.onreadystatechange = cci_handleloginxml;
        req.open("GET", url, true);
        req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = cci_handleloginxml;
			req.open("GET", url, true);
            req.send();
        }
    }
}

function cci_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			= labels[11];

	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 cci_showerrorlogin(sError){
	var oLoginTable	= document.getElementById("tblLogin");
	var icounter	= 0;
	var irows		= oLoginTable.rows.length;

	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			= labels[6] ;

	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			= labels[2];

	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 id="btnloginagain" class="cci_btn" OnClick="cci_showformlogin()">'+ labels[7] +'</button>';
	
	var ologinagain	= document.getElementById("btnloginagain");
	if (ologinagain){
		ologinagain.focus();	
	}
}

function cci_showformlogin(){
	var oLoginTable = document.getElementById("tblLogin");
	var icounter	= 0;
	var irows		= oLoginTable.rows.length;
	var irow		= 0;

	for(icounter=0; icounter<irows;icounter++){
		oLoginTable.deleteRow(-1);
	}

	var oRow				= oLoginTable.insertRow(irow);
	var oCell				= oRow.insertCell(0);
	oCell.colSpan			= 3;
	oCell.style.fontFamily	= 'Tahoma, Verdana, Arial, Sans-Serif';
	oCell.style.fontSize	= '9pt';
	oCell.style.fontWeight	= 'bold';
	oCell.style.textAlign	= "center";
	oCell.innerHTML			= labels[8];

	irow	+= 1;
	var oRow				= oLoginTable.insertRow(irow);
	var oCell				= oRow.insertCell(0);
	oCell.style.fontFamily	= 'Tahoma, Verdana, Arial, Sans-Serif';
	oCell.style.fontSize	= '8pt';
	oCell.style.paddingLeft	= '4px';
	oCell.innerHTML			= labels[3];
	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 cci_keypress(event)" class="cci_input" type="text" size="20" maxlength="20">';

	irow	+= 1;
	var oRow				= oLoginTable.insertRow(irow);
	var oCell				= oRow.insertCell(0);
	oCell.style.fontFamily	= 'Tahoma, Verdana, Arial, Sans-Serif';
	oCell.style.fontSize	= '8pt';
	oCell.style.paddingLeft	= '4px';
	oCell.innerHTML			= labels[4] ;
	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 cci_keypress(event)" CLASS="cci_input" type="password" size="20" maxlength="20">';

	irow	+= 1;
	var oRow				= oLoginTable.insertRow(irow);
	var oCell				= oRow.insertCell(0);
	oCell.style.fontFamily	= 'Tahoma, Verdana, Arial, Sans-Serif';
	oCell.style.fontSize	= '8pt';
	oCell.style.paddingLeft	= '4px';
	oCell.innerHTML			= labels[5];
	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 cci_keypress(event)" type="checkbox" name="remember">';

	irow	+= 1;
	var oRow				= oLoginTable.insertRow(irow);
	var oCell				= oRow.insertCell(0);
	oCell.colSpan			= 3;
	oCell.style.fontFamily	= 'Tahoma, Verdana, Arial, Sans-Serif';
	oCell.style.fontSize	= '8pt';
	oCell.style.textAlign	= "right";
	oCell.innerHTML			= '<button tabindex = "4" id="btnLogin" CLASS="cci_btn" OnClick="cci_chklogin()">'+ labels[9] +'</button>';

	var ologincode 			= document.getElementById("inpLoginCode");
	ologincode.focus();
}

function cci_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');
							cci_showerrorlogin(sError);
						}
					} else {
						top.location.href = '/phplib/cci_language.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;
	}

}