var newWindow = '';
function include(fileName){
    document.write('<script type="text/javascript" src="'+fileName+'"></script>');
}
function selectPhoto(imgUrl) {
    p = window.opener.document.getElementById('photoInput');
    p.value=imgUrl;
}

function highLightPhotoFrame(layer) {
    layer.className = "photoFrameHighlighted"; 
}
function restorePhotoFrame(layer) {
    layer.className = "photoFrame";
}

function openWindow(url, title, width, height) {
    var params = "height="+height+",width="+width+",toolbars=0, scrollbars=1, resizable=1";
	if (!newWindow.closed && newWindow.location) {
		newWindow.location.href = url;
	}
	else {
		newWindow= window.open(url,  title, params);
		if (!newWindow.opener) newWindow.opener = self;
	}
	if (window.focus) {newWindow.focus()}
	return false;
}

function deleteUser(userid, msg) {
	var a = confirm(msg);
	if(a) {
		window.location = 'index.php?module=user&action=delete&userID='+userid;
	}
}
function deleteAnnouncement(announcementid, msg) {
	var a = confirm(msg);
	if(a) {
		window.location = 'index.php?module=announcement&action=delete&announcementID='+announcementid;
	}
}
function deleteBook(bookid, msg) {
	var a = confirm(msg);
	if(a) {
		window.location = 'index.php?module=book&action=delete&bookID='+bookid;
	}
}
function changePassword(userid) {
	p1 = prompt('Enter Password');
	p2 = prompt('Re-Enter Password');
	if(p1 != p2) {
		alert('Passwords don\'t match');
	} else {
		if(p1.length < 4 || p1.length > 20) {
			alert('password must be between 4 and 20 characters');
		} else {
			url = 'index.php?module=user&action=changePassword&userID='+userid + '&password='+p1;
			call(url,  null, msg); 
		}
	}
}

function changeMyPassword(userid) {
	p1 = prompt('Παρακαλώ πληκτρολογήστε τον νέο κωδικό σας', ' ');
	p2 = prompt('Παρακαλώ πληκτρολογήστε τον νέο κωδικό σας ξανά για επιβεβαίωση', ' ');
    if(p1 && p2) {
        if(p1 != p2) {
            alert('Οι κωδικοί δεν ταιριάζουν');
        } else {
            if(p1.length < 4 || p1.length > 20) {
                alert('Οι κωδικοί πρέπει να είναι από 4 μέχρι 20 χαρακτήρες');
            } else {
                url = 'index.php?module=changePassword&action=change&userID='+userid + '&password='+p1;
                call(url,  null, msg); 
            }
        }
    }
}

function msg(arg) {
	alert(arg);
}
function set(el, prop, val) {
   eval("el.style."+prop+" = '"+val+"'");   
}
function addFile() {
    l = document.getElementById('userFiles');
    f = document.getElementById('uploadFile');
    c = f.length - 1; str = "userFile_" + c
    el = document.createElement("input");
	el.setAttribute("name", str);
	el.setAttribute("type", "file");
    el.style.display="block";
    l.appendChild(el);
}

function addUsers() {
    var l1 = document.getElementById('users');
    var l2 = document.getElementById('restUsers');
    for(i = l2.options.length - 1; i > - 1; i--) {
        if(l2.options[i].selected == true) {
            l1.appendChild(l2.options[i]);
        }
    }     
}


function arUser(el) {
    var l;
    if(el.parentNode.name == 'users') {
        l = document.getElementById('restUsers');
    }else {
        l = document.getElementById('users');
    }
    l.appendChild(el);
}

function removeUsers() {
    var l1 = document.getElementById('users');
    var l2 = document.getElementById('restUsers');
    for(i = l1.options.length - 1; i > - 1; i--) {
        if(l1.options[i].selected == true) {
            l2.appendChild(l1.options[i]);
        }
    }     
}
function alterUsers() {
    var l1 = document.getElementById('users');
    var str; str="";
    for(i = 0; i < l1.options.length; i++) {
        str += l1.options[i].value +",";
    }     
    var h = document.getElementById('userids');
    h.value = str.substr(0, str.length - 1);
    var form = document.getElementById('alterPersonnelForm');
    form.submit();
}



var aXmlHttp = new Array();
var aXmlResponse = new Array();
function xmlResult()
{
    for(var i=0;i<aXmlHttp.length;i++)
    {
        if(aXmlHttp[i] && aXmlHttp[i][0] && aXmlHttp[i][0].readyState==4&&aXmlHttp[i][0].responseText)
        {
            //must null out record before calling function in case
            //function invokes another xmlHttpRequest.
            var f = aXmlHttp[i][2];
            var o = aXmlHttp[i][1];
            var s = aXmlHttp[i][0].responseText;
            aXmlHttp[i][0] = null;
            aXmlHttp[i][1] = null;
            aXmlHttp[i] = null;
            f.apply(o,new Array(s));
        }
    }
}

// u -> url
// o -> object (can be null) to invoke function on
// f -> callback function
// p -> optional argument to specify POST
function call(u,o,f)
{
    var method = "GET";
    var dat;
    if (arguments.length==4){
      method = "POST";
      tmp = u.split(/\?/);
      u = tmp[0];
      dat = tmp[1];

    }
    var idx = aXmlHttp.length;
    for(var i=0; i<idx;i++)
    if (aXmlHttp[i] == null)
    {
        idx = i;
        break;
    }
    aXmlHttp[idx]=new Array(2);
    aXmlHttp[idx][0] = getXMLHTTP();

    aXmlHttp[idx][1] = o;
    aXmlHttp[idx][2] = f;
    if(aXmlHttp[idx])
    {
        aXmlHttp[idx][0].open(method,u,true);
        if(method == "POST"){
          aXmlHttp[idx][0].setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

          aXmlHttp[idx][0].send(dat);
        }
        aXmlHttp[idx][0].onreadystatechange=xmlResult;
        
       if(method =="GET"){ aXmlHttp[idx][0].send(null);}
    }
}

function getXMLHTTP()
{
    var A=null;
    if(!A && typeof XMLHttpRequest != "undefined")
    {
        A=new XMLHttpRequest();
    }
    if (!A)
    {
        try
        {
            A=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch(e)
        {
            try
            {
                A=new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(oc)
            {
                A=null
            }
        }
    }    
    return A;
}