/** COOKIES **/
/**
 * Sets a Cookie with the given name and value.
 *
 * name       Name of the cookie
 * value      Value of the cookie
 * [expires]  Expiration date of the cookie (default: end of current session)
 * [path]     Path where the cookie is valid (default: path of calling document)
 * [domain]   Domain where the cookie is valid
 *              (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *              secure transmission
 */
function setCookie(name, value, expires, path, domain, secure)
{
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
function getCookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

/**
 * Deletes the specified cookie.
 *
 * name      name of the cookie
 * [path]    path of the cookie (must be same as path used to create cookie)
 * [domain]  domain of the cookie (must be same as domain used to create cookie)
 */
function deleteCookie(name, path, domain)
{
    if (getCookie(name))
    {
        document.cookie = name + "=" + 
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

function cookieTest(message){
setCookie('test','1');
var isCookie = getCookie('test');
if (isCookie==1){
return true;
}else{
alert(message);
window.location = '/setCookies.php';
}
}

/** END OF COOKIES **/


function checkVersion() {
if (navigator.appVersion.charAt(0)>=3)
return true; 
else return false;
}

function hilite(imgsrc, imgname) {
if (checkVersion()) {
document.images[imgname].src = imgsrc;
}
return true;
}

function loadiFrame(source, name,element,bgcolor,clicked){
var theiFrame = document.getElementById(name);
var theElement = document.getElementById(element);
var thelink = document.getElementById(clicked);
theiFrame.src = source;
theElement.style.background = bgcolor;
thelink.blur();
}

//function to write check box on or off depending on user cookie
function writeBox(imgName,imgSrc){
var theStatus = getCookie(imgName);
		if (theStatus == 0 || theStatus == null){
		document.write("<img src=\""+imgSrc+".gif\" border=\"0\" name=\""+imgName+"\" />");
		
		}else if (theStatus == 1){
		document.write("<img src=\""+imgSrc+"-on.gif\" border=\"0\" name=\""+imgName+"\" />");
		}
		}
/*alternative function to create single cookie for checkboxes. this cookie needs to be used to check the status of each box and to build the SQL querie in the php page*/

function writeBox2(imgName,imgSrc,sectionValue){
var theStatus = getCookie(imgName);
if (!theStatus || theStatus == null){
	document.write("<img src=\""+imgSrc+".gif\" border=\"0\" name=\""+imgName+"\" align=\"absmiddle\" /> ");
}else if (theStatus){
	document.write("<img src=\""+imgSrc+sectionValue+".gif\" border=\"0\" name=\""+imgName+"\" align=\"absmiddle\" /> ");
	}
}
		
		
//function for self-exclusive radio buttons
function swapRadioBtn(theImage,imgName, imgTot, partialSource, pickMonth) {
if (checkVersion()) {
var a = new Array();
var s="";
//Create an array with all the images:
for (i = 0; i<imgTot; i++){
myImg = imgName + i;
a[i] = myImg;
s = s + myImg +"\n";
}
//Remove the image that has to stay the same from the array

var pattern = /[0-9]+/;//regular expression to get the number only
var position = theImage.match(pattern);
var onImage = a[position];//removes the one image from the array
//I create the b array because splice() does not work under IE5.5
var out = parseFloat(position)+1;
a1 = a.slice(out);
a2 = a.slice(0,position);
var b = new Array();
b = b.concat(a2,a1);



var t="";
l = b.length;
for (i = 0;i<l;i++){
t = b[i];
j = i+1;
if (i<position){
document.images[t].src = partialSource+i+'.gif';
}else{
document.images[t].src = partialSource+j+'.gif';
}
theSource = partialSource+position+"-on.gif";
document.images[onImage].src = theSource;
/*alert ('position: '+position+'\n'+t+'removed Image: '+onImage);*/
document.forms[0].month.value = pickMonth;
}
return true;
}
}

//function to pop confirmatin window before deleting user (admin)
function confirmDeleteUser(username, id){
	var string1 = "Do you really want to delete '"+username+"' from the database?";
	var string2 = "admin-deleteUserAsk.php?id="+id;
	var answer = confirm (string1);
    if (answer){
		window.location = string2;
	}else{
		return false;
	}
}
		


//function for independant radio buttons
function independantButton(imgName, imgSrc, value) {
var cvalue = value+'-';
if (checkVersion()) {
var valuesArray = new Array();
var status = getCookie(imgName);
var theDate = new Date();
var theYear = theDate.getFullYear();
theYear++;
theDate.setYear(theYear);
if (status == 0 || status == null){
setCookie(imgName,cvalue,theDate);
document.images[imgName].src = imgSrc+value+'.gif';
document.images[imgName].blur();/*prevents IE blue outline. MUST be last statement as not understood by some other browsers (safari etc)*/
}else if (status){
deleteCookie(imgName);
document.images[imgName].src = imgSrc+'.gif';
document.images[imgName].blur();
}
}


}

function swapYear(theImage,firstYear, imgTot, onSource, pickYear) {

if (checkVersion()) {
var a = new Array();

//Create an array with all the images:
for (i = 0; i<imgTot; i++){
firstYear = Number(firstYear);
year = firstYear + i;
a[i] = 'year'+year;
}

//turn all year images off:
for (i=0;i<imgTot;i++){
offImage = 'year'+i;
theYear = firstYear+i;
offSource = 'images/calendar/small/calsm-year'+theYear+'.gif';
document.images[offImage].src = offSource;
}

//turn the 'on' image back on:
document.images[theImage].src = onSource;
document.forms[0].year.value = pickYear;

return true;
}
}

function checkCalForm(){
var theYear = document.forms[0].year.value;
var theMonth = document.forms[0].month.value;
if (!theMonth && theYear){
alert('Please select a month...');
}else if (theMonth && !theYear){
alert('Please select a year...');
}else if (!theMonth && !theYear){
alert('Please select a month and a year...');
}else{
document.forms[0].submit();
}
}

function changeMonth(theMonth){
var year = getCookie('year');
var oldMonth = getCookie('month');
if (8<oldMonth && oldMonth<13 && 0<theMonth && theMonth<9){
	year = Number(year)+1;
	}else if (0<oldMonth && oldMonth<9 && 8<theMonth && theMonth<13){
	year = Number(year)-1;
	}
var url = "calendar.php?day=1&month="+theMonth+"&year="+year;
window.location = url;
}

function plusMonth(){
var year = getCookie('year');
var oldMonth = getCookie('month');
if (oldMonth<12){
	month = Number(oldMonth)+1;
}else{
	month = 1;
	year = Number(year)+1;
}
var url = "calendar-month.php?day=1&month="+month+"&year="+year;
window.location = url;
}

function minusMonth(){
var year = getCookie('year');
var oldMonth = getCookie('month');
if (oldMonth>1){
	month = Number(oldMonth)-1;
}else{
	month = 12;
	year = Number(year)-1;
}
var url = "calendar-month.php?day=1&month="+month+"&year="+year;
window.location = url;
}

function plusMonthPrint(){
var year = getCookie('year');
var oldMonth = getCookie('month');
if (oldMonth<12){
	month = Number(oldMonth)+1;
}else{
	month = 1;
	year = Number(year)+1;
}
var url = "calendar-monthp.php?day=1&month="+month+"&year="+year;
window.location = url;
}

function minusMonthPrint(){
var year = getCookie('year');
var oldMonth = getCookie('month');
if (oldMonth>1){
	month = Number(oldMonth)-1;
}else{
	month = 12;
	year = Number(year)-1;
}
var url = "calendar-monthp.php?day=1&month="+month+"&year="+year;
window.location = url;
}

function changeMonthadmin(theMonth){
var year = getCookie('year');
var oldMonth = getCookie('month');
if (8<oldMonth && oldMonth<13 && 0<theMonth && theMonth<9){
	year = Number(year)+1;
	}else if (0<oldMonth && oldMonth<9 && 8<theMonth && theMonth<13){
	year = Number(year)-1;
	}
var url = "admin-calendar.php?day=1&month="+theMonth+"&year="+year;
window.location = url;
}

function toggleChangeYear(value){
setCookie('toggleYear', value);
}

function addYear(){
var year = getCookie('year');
var nextYear = Number(year) + 1;
var theMonth = getCookie('month');
var url = "calendar.php?day=1&month="+theMonth+"&year="+nextYear;
window.location = url;
}

function substractYear(){
var year = getCookie('year');
var theMonth = getCookie('month');
var nextYear = Number(year) - 1;
var url = "calendar.php?day=1&month="+theMonth+"&year="+nextYear;
window.location = url;
}

function addYearAdmin(){
var year = getCookie('year');
var nextYear = Number(year) + 1;
var theMonth = getCookie('month');
var url = "admin-calendar.php?day=1&month="+theMonth+"&year="+nextYear;
window.location = url;
}

function substractYearAdmin(){
var year = getCookie('year');
var theMonth = getCookie('month');
var nextYear = Number(year) - 1;
var url = "admin-calendar.php?day=1&month="+theMonth+"&year="+nextYear;
window.location = url;
}


function selectDate(myMonth,myDay,mySection,myHourStart){

document.forms[0].month[myMonth].selected='true';
document.forms[0].day[myDay].selected='true';
document.forms[0].year[1].selected='true';
var theSection = document.getElementById(mySection);
theSection.selected='true';
var theHourStart = document.getElementById(myHourStart);
}

function openDiv(element,theLink,size)
{
var indexBox = document.getElementById(element);
var myIndexLink = document.getElementById(theLink);
	indexBox.style.height=size;
	myIndexLink.style.height="1px";
	myIndexLink.style.overflow="hidden";
}

function closeDiv(element)
{
	var indexBox = document.getElementById("index");
	var myIndexLink = document.getElementById("indexLink");
	indexBox.style.height="1px";
	myIndexLink.style.height="30px";
	indexBox.style.clip="rect(auto auto auto auto)";
}

function confirmThis(message,url)
{
 var where_to= confirm(message);
 if (where_to== true)
 {
   window.location=url;
 }
}

function printHelp(url)
{
	var printHelpStatus = getCookie('printHelp');
	if (printHelpStatus !=1){
//trying to disable caching of the linked document;
	var today=new Date()
	var time=today.getSeconds()
//
	myUrl = 'printHelp.php?url='+ url+'?'+time.valueOf();
		window.open(myUrl);
	}else{
		window.open(url);
	}
	
}

function printHelpPublic(url)
{
	var printHelpStatus = getCookie('printHelp');
	if (printHelpStatus !=1){
//trying to disable caching of the linked document;
	var today=new Date()
	var time=today.getSeconds()
//
	myUrl = 'printHelp-public.php?url='+ url+'?'+time.valueOf();
		window.open(myUrl);
	}else{
		myUrl = 'downloads-public'+url;
		window.open(myUrl);
	}
	
}

var popUpWin=0;
function popUpWindow(URLStr, left, top, width, height)
{
  if(popUpWin)
  {
    if(!popUpWin.closed) popUpWin.close();
  }
  popUpWin = open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
}
