//var overlayDiv;
//var messageDiv;
var divHeight;
var divWidth;

function showAjaxPopUp(parameters, actions) {
    callback = function(status, req) {
        if (status == 0) {						
            var messageDiv = $("messageDiv");
            var overlayDiv = $("overlayDiv");
										
				
			var newdiv = document.createElement("div");
			newdiv.innerHTML = req.responseText;			
			messageDiv.appendChild(newdiv);										
																													
            overlayDiv.style.display = "block";
			
			overlayDiv.style.height = document.body.scrollHeight+"px";
            messageDiv.style.display = "block";
						
			if(messageDiv.offsetWidth > 1 && messageDiv.offsetHeight > 1) {
				divHeight = messageDiv.offsetHeight;
				divWidth = messageDiv.offsetWidth; 			
			}
			
            messageDiv.style.marginLeft = "-" + parseInt(divWidth  / 2) + "px";
            messageDiv.style.marginTop  = "-" + parseInt(divHeight / 2) + "px";
			            			
			eval(actions);       
        } else if (status == -1) {
            alert("Fault! Code:"+fault.exceptionCode+", Message:"+fault.exceptionMessage);
        } else {
			if(req.status==403) {
				openAjaxMessageBox('page=show-ajax-login&ajax=1', '');
			} else {
            	openMessageBox(req.statusText);
            }			
		}
    }

    ajaxCall(parameters, callback, true);	
}

function closeAjaxPopUp() {
    $("overlayDiv").style.display = "none";
	$("messageDiv").innerHTML="";
    $("messageDiv").style.display = "none";
}

function photosNotifyAboutUnwantedContent() {
	
	$('tableNotifyUnwantedContent').style.display="none";				
	
}

function sendAjaxLogin(parameters, isAjax, actionsParams) {
	callback = function(status, req) {
        if (status == 0) {
			
			if(isAjax) {					
				closeMessageBox();
			}
			if(document.getElementById("loginForm")) {
				document.getElementById("loginForm").style.display="none";
			}
			
			if(actionsParams!='') {
				eval(actionsParams);
			}
			
			document.getElementById("managementDesktopButton").style.display = 'inline';
			// favorite album add
        } else if (status == -1) {
            alert("Fault! Code:"+fault.exceptionCode+", Message:"+fault.exceptionMessage);
        } else {
            //alert("Unknown problems. Status code:"+req.status+", Status text:"+req.statusText);
			
			if(isAjax) {
	        	document.getElementById("errorsNode").style.display="inline";			
				var errorMsgs = [];
				errorMsgs.push($("1"));
				errorMsgs.push($("2"));
				errorMsgs.push($("6"));
				errorMsgs.push($("123"));
				errorMsgs.push($("m1"));
				errorMsgs.push($("m666"));						
				for(i = 0; i < errorMsgs.length; i++){
					if(req.statusText == errorMsgs[i].id){
						errorMsgs[i].style.display = "inline";
					} else {
						errorMsgs[i].style.display = "none";	
					}				
				}
			} else {
				document.location.href = "/?page=login&error="+req.statusText;
			}						
		}						
    }
	
	var pass='';
	var user='';
	if(document.getElementById("ajaxLoginUser")) {
		user = document.getElementById("ajaxLoginUser").value
	}	else  {
			user = document.getElementById("ajaxLoginUserMain").value
	}
	
	if(document.getElementById("ajaxLoginPassword")) {
			pass = document.getElementById("ajaxLoginPassword").value
	}	else  {
			pass = document.getElementById("ajaxLoginPasswordMain").value
	}
	
	parameters+="&user="+user+"&pass="+pass+"&ajax=1";   
	ajaxCall(parameters, callback, true, true);
}

//message box system

var messageBoxes = [];

function showOverlay() {
	overDiv = document.createElement("div");
	overDiv.className = "overlayDiv";
	document.body.appendChild(overDiv);
	messageBoxes.push(overDiv);
}

function openMessageBox(messageText) {
	
	showOverlay();
	
	messDiv = document.createElement("div");		
	messDiv.innerHTML = ""+
	"<table cellpadding=\"0\" cellspacing=\"0\" style=\"width: 260px; text-size: 10pt;\">"+
	    "<tr>"+
	        "<td style=\"width: 10px; height: 10px;\"><img src=\"img/popup-top-right.gif\" /></td>"+
	        "<td style=\"background-color: #e6e6e6;\"></td>"+
	        "<td style=\"width: 10px; height: 10px;\"><img src=\"img/popup-top-left.gif\" /></td>"+
	    "</tr>"+
	    "<tr>"+
	        "<td style=\"background-color: #e6e6e6;\"></td>"+
			"<td style=\"background-color: #e6e6e6;\">"+
				"<table cellpadding=\"0\" cellspacing=\"0\" style=\"width: 100%;\" >"+
					"<tr>"+
						"<td style=\"padding: 10px; text-align: center; font-size: 10px;\">"+			
							messageText+
						"</td>"+	
					"</tr>"+	
					"<tr>"+
						"<td style=\"text-align: center; padding: 10px;\" align=\"center\">"+	
						"<table cellpadding=\"0\" cellspacing=\"0\" class=\"button\" onclick=\"closeMessageBox()\" style=\"cursor: pointer;\">"+
                            "<tr>"+
                                "<td>"+
                                    "<img src=\"img/button-left.gif\" />"+
                                "</td>"+
                                "<td class=\"mid_desktop\" align=\"center\">"+
                                    "OK"+
                                "</td>"+
                                "<td>"+
                                    "<img src=\"img/button-right.gif\" />"+
                                "</td>"+
                            "</tr>"+   
                        "</table>"+ 
						"</td>"+	
					"</tr>"+
				"</table>"+
			"</td>"+
			"<td style=\"background-color: #e6e6e6;\"></td>"+
	    "</tr>"+
	    "<tr>"+
	        "<td style=\"width: 10px; height: 10px;\"><img src=\"img/popup-bottom-right.gif\" /></td>"+
	        "<td style=\"background-color: #e6e6e6;\"></td>"+
	        "<td style=\"width: 10px; height: 10px;\"><img src=\"img/popup-bottom-left.gif\" /></td>"+
	    "</tr>"+
	"</table>";
	
	
	messDiv.className = "messageDiv";		
	document.body.appendChild(messDiv);
	messDiv.style.marginLeft = "-" + parseInt(messDiv.offsetWidth/2)  + "px";
    messDiv.style.marginTop  = "-" + parseInt(messDiv.offsetHeight/2) + "px";
	messageBoxes.push(messDiv);
}

function openMessageBoxCancel(messageText) {
	
	showOverlay();
	
	messDiv = document.createElement("div");		
	messDiv.innerHTML = ""+
	"<table cellpadding=\"0\" cellspacing=\"0\" style=\"width: 260px; text-size: 10pt;\">"+
	    "<tr>"+
	        "<td style=\"width: 10px; height: 10px;\"><img src=\"img/popup-top-right.gif\" /></td>"+
	        "<td style=\"background-color: #e6e6e6;\"></td>"+
	        "<td style=\"width: 10px; height: 10px;\"><img src=\"img/popup-top-left.gif\" /></td>"+
	    "</tr>"+
	    "<tr>"+
	        "<td style=\"background-color: #e6e6e6;\"></td>"+
			"<td style=\"background-color: #e6e6e6;\">"+
				"<table cellpadding=\"0\" cellspacing=\"0\" style=\"width: 100%;\" >"+
					"<tr>"+
						"<td style=\"padding: 10px; text-align: center; font-size: 10px;\">"+			
							messageText+
						"</td>"+	
					"</tr>"+	
					"<tr>"+
						"<td style=\"text-align: center; padding: 10px;\">"+			
							"<img src=\"img/button-cancel.png\" style=\"cursor: pointer;\" onclick=\"closeMessageBox()\"/>"+	
						"</td>"+	
					"</tr>"+
				"</table>"+
			"</td>"+
			"<td style=\"background-color: #e6e6e6;\"></td>"+
	    "</tr>"+
	    "<tr>"+
	        "<td style=\"width: 10px; height: 10px;\"><img src=\"img/popup-bottom-right.gif\" /></td>"+
	        "<td style=\"background-color: #e6e6e6;\"></td>"+
	        "<td style=\"width: 10px; height: 10px;\"><img src=\"img/popup-bottom-left.gif\" /></td>"+
	    "</tr>"+
	"</table>";
	
	
	messDiv.className = "messageDiv";		
	document.body.appendChild(messDiv);
	messDiv.style.marginLeft = "-" + parseInt(messDiv.offsetWidth/2)  + "px";
    messDiv.style.marginTop  = "-" + parseInt(messDiv.offsetHeight/2) + "px";
	messageBoxes.push(messDiv);
}

function openAjaxMessageBox(parameters, actions) {				 		
	callback = function(status, req) {
        if (status == 0) {						
            showOverlay();            
            messDiv = document.createElement("div");
			
			messDiv.innerHTML = ""+
			"<table cellpadding=\"0\" cellspacing=\"0\">"+
			    "<tr>"+
			        "<td style=\"width: 10px; height: 10px;\"><img src=\"img/popup-top-right.gif\" /></td>"+
			        "<td style=\"background-color: #e6e6e6;\"></td>"+
			        "<td style=\"width: 10px; height: 10px;\"><img src=\"img/popup-top-left.gif\" /></td>"+
			    "</tr>"+
			    "<tr>"+
			        "<td style=\"background-color: #e6e6e6;\"></td>"+
					"<td style=\"background-color: #e6e6e6;\">"+									
						req.responseText+						
					"</td>"+
					"<td style=\"background-color: #e6e6e6;\"></td>"+
			    "</tr>"+
			    "<tr>"+
			        "<td style=\"width: 10px; height: 10px;\"><img src=\"img/popup-bottom-right.gif\" /></td>"+
			        "<td style=\"background-color: #e6e6e6;\"></td>"+
			        "<td style=\"width: 10px; height: 10px;\"><img src=\"img/popup-bottom-left.gif\" /></td>"+
			    "</tr>"+
			"</table>";
															
			messDiv.className = "messageDiv";	
			document.body.appendChild(messDiv);
			messDiv.style.marginLeft = "-" + parseInt(messDiv.offsetWidth/2)  + "px";
		    messDiv.style.marginTop  = "-" + parseInt(messDiv.offsetHeight/2) + "px";
			messageBoxes.push(messDiv);
				            			
			eval(actions);       
        } else if (status == -1) {
            alert("Fault! Code:"+fault.exceptionCode+", Message:"+fault.exceptionMessage);
        } else {
			if(req.status==403) {
				openAjaxMessageBox('page=show-ajax-login&ajax=1', '');
			} else {
            	openMessageBox(req.statusText);//alert("Unknown problems. Status code:"+req.status+", Status text:"+req.statusText);
            }
        }
    }

    ajaxCall(parameters, callback, true);			
}

function closeMessageBox() {	
	var messBox = messageBoxes.pop();	
	messBox.parentNode.removeChild(messBox);			
	messBox = messageBoxes.pop();	
	messBox.parentNode.removeChild(messBox);	
}

var SUCCESS = 0;
var FAULT   = -1;
var ERROR   = -2;



function ajaxCall(parameters, callback, sync, secure) {
    var eventHandlerCalled = false;
    var req = getRPC();

    function internalCallBack() {
        eventHandlerCalled = true;
        if (req.readyState == 4) {
            if (req.status == 200) {
                // only if "OK"
                callback(SUCCESS, req);
            /*
			} else if (req.status == 500) {
                // on Fault cases
                callback(FAULT, req);
            */
            } else {
                callback(ERROR, req);
            }
        }
    }
    req.onreadystatechange = internalCallBack;
    //var env = this.wrap2soap(header, body);
	
	if(!secure) secure = false;
	var url = "/";
	var newUrl = makeUrl(url, secure);
	if(newUrl != url){
		newUrl += ("?" + parameters);
	    scriptHack(newUrl, callback);
	} else {
	    req.open("POST", newUrl, sync);
	    req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	    req.setRequestHeader("Content-length", parameters.length);
		req.setRequestHeader("Connection", "close");
	   	req.send(parameters);
	    // check if synchronous call then call the callback function yourself
	    if (sync == true && eventHandlerCalled == false) {
	        internalCallBack();
	    }
	}
}

function getRPC() {
    var rpc;

    if (window.XMLHttpRequest) { // Mozilla, Safari, ...
        rpc = new XMLHttpRequest();

    } else if (window.ActiveXObject) { // IE
        try {
            rpc = new ActiveXObject("Msxml2.XMLHTTP");
        } catch ( e) {
            try {
                rpc = new ActiveXObject("Microsoft.XMLHTTP");
            } catch ( e) {}
        }
    }

    return rpc;
}


function makeUrl(url, secure){
	var curSec = window.location.protocol == 'https:';
	var host = window.location.hostname;
	var path = window.location.pathname;
	
	if(curSec != secure){
		var p = getDefaultPort(secure);
		var port = "";
		if(secure && p != 442 || p != 80)
			port = ":" + p;

		url = (secure ? 'https' : 'http') + "://" + host + port + (url.charAt(0) == '/' ? "" : path) + url;
	}
	
	return url;
}


var securePort = 443;
var insecurePort = 80;

function getDefaultPort(secure){
	return secure ? securePort : insecurePort;
}

function setDefaultPorts(secure, insecure){
	securePort = secure;
	insecurePort = insecure;
}

var oScript;
var shnum = 0;
var callbacks = [];

function putCallback(callback){
    var n = shnum++;
	callbacks[n] = [];
	callbacks[n].callback = callback;
	callbacks[n].data = undefined;
	return n;
}

function scriptHack(url, callback){
   
	scripthackdata = undefined;
	//shcallback = callback;
   
	var id = "scriptHack";
    oScript = document.getElementById(id);
    var head = document.getElementsByTagName("head").item(0);
    if (oScript) head.removeChild(oScript);

    oScript = document.createElement("script");

    // Setup the src attribute of the script tag
	var n = putCallback(callback);
	
	var sess = readCookieJSessId('session');
	/*if(!sess) {
		sess = readCookieJSessId('JSESSIONID');
	}*/
	var pg = url.indexOf("https") >= 0 ? "sh-secure" : "sh";
	var urlparts = url.split("?", 2);
	//var newUrl = urlparts[0] + ";jsessionid=" + sess + "?page=" + pg + "&num=" + n;
	var newUrl = urlparts[0] + "?page=" + pg + "&num=" + n;
	var pageSet = false;
	if(urlparts.length > 1){
		var params = urlparts[1].split("&");
		var p;
		for(p in params){
	        var ps = params[p].split("=");
	        if(ps[0] == "page"){
	            pageSet = true;
				ps[0] = "target";
	        }
			newUrl += "&" + ps[0] + (ps.length > 1 ? "=" + ps[1] : "");
	    }
	}
	if(!pageSet) newUrl += "&target=index";
	newUrl += "&session=" + sess;
	
    oScript.setAttribute("src", newUrl);
    oScript.setAttribute("id",id);
    head.appendChild(oScript);

	checkshdata(n);
}

function checkshdata(n){
    var data = callbacks[n].data;
    if(data != undefined){
		var status;
		var req = [];
		if(data.indexOf("ERR") == 0){
            var sh = data.substring(3).split(":", 2);
			var s = parseInt(sh[0]);
			var st = sh[1];
            req.status = s;
			req.statusText = st;
            req.responseText = "";
		    status = ERROR;
		} else {
            req.status = 200;
            req.statusText = "OK";
            req.responseText = data;
            status = SUCCESS;
        }
		
	    callbacks[n].callback(status, req);
		callbacks[n] = undefined;
	} else setTimeout("checkshdata('" + n + "')", 50);
}


