// JavaScript Document
//========================================================================0
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx0
//========================================================================0
/*					JAVASCRIPT  FUNCTIONS
#00- function encodeToHex(str)
#01- functions nl2br and br2nl  -PROTOTYPE-
#02- function hidelayer(lay)
#03- function showlayer(lay)
#05- function Validate_Pop_Login(logincheck)
#06- function Validate_Login(logincheck)
#07- function Validate_LoginHomepage()

#08- functions for popups
#09- AJAX functions
*/
//========================================================================0
//#00-               encodeToHex(str)             xxxxxxxxxxxx0
//========================================================================0
function encodeToHex(str){
    var r="";
    var e=str.length;
    var c=0;
    var h;
    while(c<e){
        h=str.charCodeAt(c++).toString(16);
        while(h.length<3) h="0"+h;
        r+=h;
    }
    return r;
}
//========================================================================0
//#01-                functions nl2br and br2nl               xxxxxxxxxxxx0
//========================================================================0

String.prototype.nl2br = function() {
	var br;
	if( typeof arguments[0] != 'undefined' ) {
		br = arguments[0];
	}
	else {
		br = '<br />';
	}
	return this.replace( /\r\n|\r|\n/g, br );
}
 
String.prototype.br2nl = function() {
	var nl;
	if( typeof arguments[0] != 'undefined' ) {
		nl = arguments[0];
	} 
	else {
		nl = '\r\n';
	}
	return this.replace( /\<br(\s*\/|)\>/g, nl );
}
//========================================================================0
//#02-                function hidelayer(lay)                 xxxxxxxxxxxx0
//========================================================================0
// function used to show and hide layers passed as paramenters....
function hidelayer(lay) {
if (ie4) {document.all[lay].style.visibility = "hidden";}
if (ns4) {document.layers[lay].visibility = "hide";}
if (ns6) {document.getElementById([lay]).style.display = "none";}
if (my_bros.ie5) {document.getElementById([lay]).style.display = "none";}
if (my_bros.ie6) {document.getElementById([lay]).style.display = "none";}
}

//========================================================================0
//#03-                function showlayer(lay)                 xxxxxxxxxxxx0
//========================================================================0
function showlayer(lay) {
if (ie4) {document.all[lay].style.visibility = "visible";}
if (ns4) {document.layers[lay].visibility = "show";}
if (ns6) {document.getElementById([lay]).style.display = "block";}
if (ie5) {document.getElementById([lay]).style.display = "block";}
if (ie6) {document.getElementById([lay]).style.display = "block";}
}
//========================================================================0
//#05-                function Validate_Pop_Login()           xxxxxxxxxxxx0
//========================================================================0
// #05-
  function Validate_Pop_Login(logincheck) {
     // variables definition
	var username = document.pop_login.pop_username.value;
	var password = document.pop_login.pop_password.value;
	var tabdiag = document.getElementById("pop_id_tabdiag");
	var cella1 = document.getElementById("pop_id_cella1");
	var cella2 = document.getElementById("pop_id_cella2");
	var msg = "";
	var submsg = "";
	
	var filterLogin = /^[a-zA-Z0-9]+$/;
	var filterLogin2 = /^[a-zA-Z0-9]+([a-zA-Z0-9\_\-]+)$/;
		//-------------------------------------------------------------------------
        //check the username
        if ((username == "") || (username == undefined)) {
			document.getElementById("id_pop_username").className="errlabel";
			if (msg == "") {
				document.pop_login.pop_username.focus();
				msg="<ul>"; //begin the Unordered List
			}
			msg = msg  + "<li>" + "<b>Username</b> is required" + "</li>";
        }
		else
		{
			if (filterLogin2.test(username) ==false) {
				document.getElementById("id_pop_username").className="errlabel";
				if (msg == "") {
					document.pop_login.pop_username.focus();
					msg="<ul>"; //begin the Unordered List
				}
				msg = msg  + "<li>" + "<b>Username</b>: only letters and numbers" + "</li>";
			}
        }
		//-------------------------------------------------------------------------
        //check the password
        if ((password == "") || (password == undefined)) {
			document.getElementById("id_pop_password").className="errlabel";
			if (msg == "") {
				document.pop_login.pop_password.focus();
				msg="<ul>"; //begin the Unordered List
			}
			msg = msg  + "<li>" + "<b>Password</b> is required" + "</li>";
        }
		//------FINAL CHECK             -------------------------------------------
		if (msg == "") {
			if ((logincheck == "") || (logincheck == undefined)) {
				// back to the ajax call
				return true;
			}else{
				//submit data: document.login.action = "00-logincheck.php";
				document.pop_login.action = logincheck;
				document.pop_login.submit();
				return true;
			}
		}else{ 
			// errors, back to the login page...
			submsg = "Please, check the hilighted fields:" + msg + "</ul>";
			cella2.innerHTML = submsg;
			tabdiag.className="errtable";
			cella1.className="errlogo";
			cella2.className="errmsg";
			return false; 
        }
  } // end function  Validate_Pop_Login()


//========================================================================0
//#06-                function Validate_Login()               xxxxxxxxxxxx0
//========================================================================0
// #06-
  function Validate_Login(logincheck) {
     // variables definition
	var username = document.login.username.value;
	var password = document.login.password.value;
	var tabdiag = document.getElementById("id_tabdiag");
	var cella1 = document.getElementById("id_cella1");
	var cella2 = document.getElementById("id_cella2");
	var msg = "";
	var submsg = "";
	
	var filterLogin = /^[a-zA-Z0-9]+$/;
	var filterLogin2 = /^[a-zA-Z0-9]+([a-zA-Z0-9\_\-]+)$/;
		//-------------------------------------------------------------------------
        //check the username
        if ((username == "") || (username == undefined)) {
			document.getElementById("id_username").className="errlabel";
			if (msg == "") {
				document.login.username.focus();
				msg="<ul>"; //begin the Unordered List
			}
			msg = msg  + "<li>" + "<b>Username</b> is required" + "</li>";
        }
		else
		{
			if (filterLogin2.test(username) ==false) {
				document.getElementById("id_username").className="errlabel";
				if (msg == "") {
					document.login.username.focus();
					msg="<ul>"; //begin the Unordered List
				}
				msg = msg  + "<li>" + "<b>Username</b>: only letters and numbers" + "</li>";
			}
        }
		//-------------------------------------------------------------------------
        //check the password
        if ((password == "") || (password == undefined)) {
			document.getElementById("id_password").className="errlabel";
			if (msg == "") {
				document.login.password.focus();
				msg="<ul>"; //begin the Unordered List
			}
			msg = msg  + "<li>" + "<b>Password</b> is required" + "</li>";
        }
		//------FINAL CHECK             -------------------------------------------
		if (msg == "") {
			if ((logincheck == "") || (logincheck == undefined)) {
				// back to the ajax call
				return true;
			}else{
				//submit data: document.login.action = "00-logincheck.php";
				document.login.action = logincheck;
				document.login.submit();
				return true;
			}
		}else{ 
			// errors, back to the login page...
			submsg = "Please, check the hilighted fields:" + msg + "</ul>";
			cella2.innerHTML = submsg;
			tabdiag.className="errtable";
			cella1.className="errlogo";
			cella2.className="errmsg";
			return false; 
        }
  } // end function  Validate_Login()

//========================================================================0
//#07-                function Validate_LoginHomepage()               xxxxxxxxxxxx0
//========================================================================0
// #07-
  function Validate_LoginHomepage() {
     // variables definition
	var username = document.login.username.value;
	var password = document.login.password.value;
	//var tabdiag = document.getElementById("id_tabdiag");
	//var cella1 = document.getElementById("id_cella1");
	//var cella2 = document.getElementById("id_cella2");
	var msg = "";
	var submsg = "";
	
	var filterLogin = /^[a-zA-Z0-9]+$/;
	var filterLogin2 = /^[a-zA-Z0-9]+([a-zA-Z0-9\_\-]+)$/;
		//-------------------------------------------------------------------------
        //check the username
        if ((username == "") || (username == undefined)) {
			if (msg == "") {
				//document.login.username.focus();
				msg="<ul>"; //begin the Unordered List
			}
			msg = msg  + "<li>" + "<b>Username</b> is required" + "</li>";
        }
		else
		{
			if (filterLogin2.test(username) ==false) {
				if (msg == "") {
					msg="<ul>"; //begin the Unordered List
				}
				msg = msg  + "<li>" + "<b>Username</b>: only letters and numbers" + "</li>";
			}
        }
		//-------------------------------------------------------------------------
        //check the password
        if ((password == "") || (password == undefined)) {
			if (msg == "") {
				//document.login.password.focus();
				msg="<ul>"; //begin the Unordered List
			}
			msg = msg  + "<li>" + "<b>Password</b> is required" + "</li>";
        }
		//------FINAL CHECK             -------------------------------------------
		if (msg == "") {
			//send data
			document.login.action = "/uptest/login/10-logincheck.php";
			document.login.submit();
			return true;
			}
        else { 
			// errors, sent to the login page...
			document.login.action = "/uptest/login/?up=err";
			document.login.submit();
			return false; 
        }
  } // end function  Validate_LoginHomepage()


//========================================================================0
//#08-                functions for popups
//========================================================================0
function getBrowserWidth(){
if (window.innerWidth){
return window.innerWidth;}
else if (document.documentElement && document.documentElement.clientWidth != 0){
return document.documentElement.clientWidth; }
else if (document.body){return document.body.clientWidth;}
return 0;
} 
function getBrowserHeight(){
if (window.innerHeight){
return window.innerHeight;}
else if (document.documentElement && document.documentElement.clientHeight != 0){
return document.documentElement.clientHeight; }
else if (document.body){return document.body.clientHeight;}
return 0;
} 

var state = 'none'; 
var visib = 'hidden'; 

function modalToggle(layer_ref) { 
	/*
	if (window.frames['ifrm'].document.getElementById('id_cella2')) { 
		var childFrame = window.frames['ifrm'];
		childFrame.document.getElementById('id_cella2').innerHTML = '';
		childFrame.document.getElementById('id_tabdiag').className = '';
		childFrame.document.getElementById('id_upfile').className = '';
	}
	*/

	if (state == 'block') { 
	state = 'none';
	visib = 'hidden';
	} 
	else { 
	state = 'block'; 
	visib = 'visible';
	} 
	
	var screen_height = getBrowserHeight();
	var screen_width = getBrowserWidth();

if (document.getElementById("poppy_box")) {
    var poppy_box = document.getElementById("poppy_box");
    var curr_width = parseInt(poppy_box.style.width);
    var curr_height = parseInt(poppy_box.style.height);
}else{
    var curr_width = 400; //default popup width
    var curr_height = 200; //default popup height
}



	
	if (document.all) { //IS IE 4 or 5 (or 6 beta) 
		//alert("Type 1 - "+screen_width +" x "+ screen_height);
		eval( "document.all." + layer_ref + ".style.display = state"); 
		eval( "document.all." + layer_ref + ".style.visibility = visib"); 
		eval( "document.all." + "modalDisable" + ".style.display = state"); 
		eval( "document.all." + "modalDisable" + ".style.visibility = visib");
		eval( "document.all." + layer_ref + ".style.top = ((screen_height - 200) / 2) + 'px'"); 
		eval( "document.all." + layer_ref + ".style.left = ((screen_width - 400) / 2) + 'px'");
		
		doc_width = document.body.clientWidth;
		doc_height = document.body.clientHeight;
		eval( "document.all." + "modalDisable" + ".style.position = 'absolute'");
		eval( "document.all." + "modalDisable" + ".style.top = 0 + 'px'"); 
		eval( "document.all." + "modalDisable" + ".style.left = 0 + 'px'"); 
		eval( "document.all." + "modalDisable" + ".style.width = doc_width + 'px'"); 
		eval( "document.all." + "modalDisable" + ".style.height = doc_height + 'px'"); 
	} 
	if (document.layers) { //IS NETSCAPE 4 or below 
		document.layers[layer_ref].display = state; 
		document.layers[layer_ref].visibility = visib; 
		document.layers['modalDisable'].display = state; 
		document.layers['modalDisable'].visibility = visib;
		document.layers[layer_ref].top = ((screen_height - 200) / 2) + 'px';
		document.layers[layer_ref].left = ((screen_width - 400)/ 2) + 'px';
	} 
	if (document.getElementById &&!document.all) { 
	
		//var screen_height = window.document.documentElement.scrollHeight;
		//var screen_width = window.document.documentElement.scrollWidth;
		//alert(screen_width + " " + screen_height );
	
		var hza = document.getElementById(layer_ref); 
		var hzmod = document.getElementById('modalDisable'); 
		hza.style.display = state; 
		hza.style.visibility = visib;
		//hza.style.top = ((screen_height - 200) / 2) + 'px';
		//hza.style.left = ((screen_width - 600)/ 2) + 'px';
		hza.style.top = ((screen_height - curr_height) / 2) + 'px';
		hza.style.left = ((screen_width - curr_width)/ 2) + 'px';
		hzmod.style.display = state; 
		hzmod.style.visibility = visib;
	} 
} // end function modalToggle(layer_ref)
//========================================================================0
//#09-                AJAX Functions
//========================================================================0
function getXMLObject()  //XML OBJECT
{
   var xmlHttp = false;
   try {
     xmlHttp = new ActiveXObject("Msxml2.XMLHTTP")  // For Old Microsoft Browsers
   }
   catch (e) {
     try {
       xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")  // For Microsoft IE 6.0+
     }
     catch (e2) {
       xmlHttp = false   // No Browser accepts the XMLHTTP Object then false
     }
   }
   if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
     xmlHttp = new XMLHttpRequest();        //For Mozilla, Opera Browsers
   }
   return xmlHttp;
}
//==================================================== 
var xmlhttp = new getXMLObject();
//==================================================== 
function ajaxLogin(pgm_path) {
  var getdate = new Date();
  if(xmlhttp) { 
  	//var myurl = "../login/20-logincheck.php"+ "?date=" + getdate;
  	var myurl = pgm_path + "?date=" + getdate;
  	var username = document.getElementById("pop_username");
  	var password = document.getElementById("pop_password");
	var params = "username=" + username.value+"&password=" + password.value;
	//
    xmlhttp.open("POST",myurl,true);
    xmlhttp.onreadystatechange  = handleServerResponse_Login;
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.setRequestHeader("Content-length", params.length);
	xmlhttp.setRequestHeader("Connection", "close");
    xmlhttp.send(params);
  }
}
 
function handleServerResponse_Login() {
   if (xmlhttp.readyState == 4) {
     if(xmlhttp.status == 200) {
		var app_res = xmlhttp.responseText;
		app_res = jQuery.trim(app_res);
		//var apphex = encodeToHex(app_res);
		//alert (app_res.indexOf("OK"));
		if (app_res == 'LOGGEDOK') {
			hide_mdb("#mdb_login");
			window.location.reload(true);
		}else{
			document.getElementById("pop_id_cella2").innerHTML=app_res;
		}
	   		
     }
     else {
        alert(xmlhttp.status+" Error during AJAX call. Please try again");
     }
   }
}
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
//$$$$$$$$$$$$$$$$ NOT USED $$$$$$$$$$$$$$$$$$$$$$$
function getAjaxObject() {
   if (window.XMLHttpRequest) {
      return new XMLHttpRequest()
   } else {
      return new ActiveXObject("Microsoft.XMLHTTP");
   }
}
function postAjaxData (callback, data, url, mode) {
   var req = getAjaxObject();
   //var req = new getXMLObject();
	            alert("before bc ");
   req.callbackfunc = callback;
	            alert("after bc ");
   req.onreadystatechange = function () {
      if (req.readyState == 4){
         if (req.status == 200) {
            req.callbackfunc();
         } else {
            alert("Server error " + req.status);
         }
      }
   }
   req.open("post", url, mode);
   var myType = "application/x-www-form-urlencoded; charset=UTF-8";
   req.setRequestHeader("Content-Type", myType);
   req.send(data);
   return req;
}
var AJAX;
function encryptCallback () {
   //document.getElementById("encrypted").innerHTML = AJAX.responseText;
   alert ("call back OK = " + AJAX.responseText);
}
function encrypt (id) {
   //var url = this.action;
   var url = "uptest/ges10/95-pull_lyrics.php";
   var mode = true;
   //var txt = document.getElementById("txt").value;
   //var method = getRadioValue(this, "method");
   var data = "";
   data = "id=" + id;
   //data += "t=" + encodeURIComponent(txt);
   //data += "&m=" + encodeURIComponent(method);
   AJAX = postAjaxData (encryptCallback, data, url, mode);
   return false;
}
//$$$$$$$$$$$$$$$ END NOT USED $$$$$$$$$$$$$$$$$$$$$$$$
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

