/*
*********************************************************************
** OPRYDNINGSPLADS!!! HER UNDER SKAL FUNKTIONER SOM BRUGES LIGGE
** EFTERHÅNDEN SOM VI STØDER PÅ DEM
*/

var err_missing_selected = "You need to select first.";


/* used to open popups */
var windowPopup;
function OpenWindow(action,height,width,center,windowName) {
	var x = (screen.width - width) / 2;
	var y = (screen.height - height) / 2;
	var windowNameThis;
	if (!windowName)
	{
		windowNameThis = "popup";
	} else {
		windowNameThis=windowName;
	}
	if (center)
	{
		windowPopup = window.open(action, windowName, "width=" + width + ",height=" + height + ",top="+y+",left="+x+",scrollbars=1,resizable=1");
	} else {
		windowPopup = window.open(action, windowName, "width=" + width + ",height=" + height + ",scrollbars=1,resizable=1");
	}
	
	windowPopup.focus();
}

function TLPopup(objID) {
	var x = 250;
	var y = 80;
	
	var obj = document.getElementById(objID);
	var theLeft = 0;
	var theTop = 0;
	if (document.documentElement) {
		theLeft = document.documentElement.scrollLeft;
		theTop = document.documentElement.scrollTop;
	}	else if (document.body) {
		theLeft = document.body.scrollLeft;
		theTop = document.body.scrollTop;
	}
	theLeft += x;
	theTop += y;
	obj.style.left = theLeft + 'px' ;
	obj.style.top = theTop + 'px' ;
	
	display(objID,'block');
}



/* date function */
function writeDate(inputDate,format) {
	
	//var months = ['januar','februar','marts','april','maj','juni','juli','august','september','oktober','november','december'];
	//var months_short = ['jan','feb','mar','apr','maj','jun','jul','aug','sep','okt','nov','dec'];

	var months       = Calendar.Months;
	var months_short = Calendar.Months_short;
	var aTemp = inputDate.split(" ");
	var date = aTemp[0].split("-");
	var time;
	if (aTemp.length > 1)
	{
		time = aTemp[1].split(":");
	}

	if (date[0].length == 4)
	{
		var outY = date[0];
		var outYshort = outY.substr(2,2);
		var outM = date[1];
		var outD = date[2];
	} else {
		var outY = date[2];
		var outM = date[1];
		var outD = date[0];
	}
	

	if (time) {
		var outT = time[0];
		var outMi = time[1];
		var outS = time[2];
	}

	

	if (format == 1) // dd. abc. 2007 12:00
	{
		if ((typeof outD == "undefined") || (typeof outM == "undefined") ||(typeof outY == "undefined") || (typeof outT == "undefined") || (typeof outMi == "undefined"))
			return "";
		
		if (inputDate == "0000-00-00") {
			return "??. ??. ???? ??:??";
		}
		else {	
			return outD + ". " + months_short[outM-1] + ". " + outY + " " + outT + ":" + outMi;
		}
	} 
	else if (format == 2) // dd. abc
	{
		if (inputDate == "0000-00-00") {
			return "??. ??";
		}
		else {	
			return outD + ". " + months_short[outM-1];
		}
	}
	else if (format == 3) // dd. abc. 2007
	{
		if (inputDate == "0000-00-00") {
			return "??. ??. ????";
		}
		else {	
			return outD + ". " + months_short[outM-1] + ". " + outY;
		}
	}
	else if (format == 4) // dd. januar 2007
	{
		if (inputDate == "0000-00-00") {
			return "??. ?? ????";
		}
		else {	
			return outD + ". " + months[outM-1] + " " + outY;
		}
	}
	else if (format == 5) // dd/mm-07 12:00   or // dd/mm-07
	{
		if (time) {
			if (inputDate == "0000-00-00") {
				return "??/??-?? ??:??";
			}
			else {	
				return outD + "/" + outM + "-" + outYshort + " " + outT + ":" + outMi;
			}
		}
		else {
			if (inputDate == "0000-00-00") {
				return "??/??-??";
			}
			else {	
				return outD + "/" + outM + "-" + outYshort;
			}
		}
	} 
	else if (format == 6) // dd. abc. 07
	{
		if (inputDate == "0000-00-00") {
				return "??. ??. ??";
		}
		else {
			return outD + ". " + months_short[outM-1] + ". " + outYshort;
		}
	}
	else if (format == 7) // HH:MM
	{
		if (time) {
			return outT + ":" + outMi;
		}
		else { 
			return " NO DATE"; 
		}
	} 
	else return inputDate;

}


function initDate(where) {
	var n=new Date();
	
	if (document.getElementById(where).value == "")
	{
		document.getElementById(where).value = n.getDate() + "-" + (n.getMonth()+1) + "-" + n.getFullYear();
	}
	

}


/*
**********************************************************************
**                       Common functions
*/

function check(obj) {
	var classN = obj.className;
	if (classN.indexOf('Off') != -1) {
		obj.className = "checkboxOn";
		obj.firstChild.value = '1';

	} else {
		obj.className = "checkboxOff";
		obj.firstChild.value = '0';
	}
	
	return false;	
}

function isdefined( variable)
{
    return (typeof(window[variable]) == "undefined")?  false: true;
}

function getObj(name)
{
	if (document.getElementById)
	{
		this.obj = document.getElementById(name);
		if ( this.obj != null) {
			this.style = document.getElementById(name).style;
		}
	}
	else if (document.all)
	{
		this.obj = document.all[name];
		this.style = document.all[name].style;
	}
	else if (document.layers)
	{
		this.obj = document.layers[name];
		this.style = document.layers[name];
	}
	else {
		this.obj = null;
		this.style = null;
	}
}


/*
** getCurrentTime(){
** make a timestamp
*/
function getCurrentTime(){
	var now=new Date();
	return now.getTime();
}

/*
** now
** maybe not in use // HUSK
*/
//now=new Date();
//now=now.getTime();




/*
   ** visibility(obj,vis)
   ** hide visibility
   **
   ** obj:  string: the object
   ** vis:  string: visibility - hidden or visible

*/

function visibility(obj,vis)
{
	var x = new getObj(obj);
	x.style.visibility='' + vis
}

function display(obj,vis)
{
	var x = new getObj(obj);
	x.style.display='' + vis;
	x.style.zindex='1';
}

function toggleObj(obj)
{
	var x = new getObj(obj);
	var d = x.style.display;
	x.style.display=(d == "none") ? "block" : "none";

}
function toggleObjV(obj)
{
	var x = new getObj(obj);
	if (x.obj != null) {
		var d = x.style.visibility;
		x.style.visibility=(d == "hidden") ? "visible" : "hidden";
	}
}
function toggleActive(obj,color)
{
	var x = document.getElementById(obj);
	if (x.className == "active")
	{
		x.className = "";
		x.style.borderBottom = "0";
	} else {
		x.className = "active";
		x.style.borderBottom = "1px dashed "+color;
	}
	

}




/*
   ** makeString(number,formname,action)
   ** make string of checkboxes and change location
   **
   
*/
function makeString(number,formname,action)
	{
		
		
		var string="";
		var counter=0;
	for (i = 0; i<number; i++) {
			
		
		box = eval("document." + formname + ".checkbox" + i);
		
		//var checkbox="checkbox" + i;
			//var x = new getObj(checkbox);
			
		if(box.checked){
				string = string + box.value + ","
				//box.checked = "false";
				counter++;
			}
	}	
		if(counter!=0){
			if(action=="sletbeskeder"){
				document.location.href = htmlroot+"edit/messages:page=deletemessages:messagelist=" + string;	
			}
		else if (action=="sletmails") {
			hiddenf = eval("document." + formname + ".mailstring");
			hiddenf.value=string;
			hiddenftwo = eval("document." + formname + ".erase");
			hiddenftwo.value="erasemails";
			submitform=eval("document." + formname);
			submitform.submit();
		}
		else if (action=="sendmails") {
				subject = eval("document." + formname + ".subject.value");
				texten = eval("document." + formname + ".text.value");
			if(!subject){
				alert(err_missing_subject);
				subject = eval("document." + formname + ".subject");
				subject.focus();
			}
			else if(!texten){
				alert(err_missing_text);
				texten = eval("document." + formname + ".text");					
				texten.focus();
			}
			else {
				hiddenf = eval("document." + formname + ".mailstring");
				hiddenf.value=string;
				hiddenftwo = eval("document." + formname + ".send");
				hiddenftwo.value="sendmails";
				submitform=eval("document." + formname);
				submitform.submit();
			}
		}
		}
		else
		{
			alert(err_missing_selected);	
		}
	}
      

	
function NewWindow(url, name, width, height) {
	var x = (screen.width - width) / 2;
	var y = (screen.height - height) / 2;
	var options = "width="+width+",height="+height+",top="+y+",left="+x;
	options += ",menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=no,resizeable=no,dependent";
	win = window.open(url,name,options);
	win.focus();
}

function start_progress() {
	upload_active = true;
	NewWindow(htmlroot+'generel/upload_progress.html','progress',300,80);
}  

/**
 * 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";
    }
}



/***** REGION + COUNTRY STUFF *****/

function update_regions(country_id, select_id) {
	// Do ajax to retrieve countrylist
	var xmlHttp;
	var data;
	var obj;
	var url = htmlroot+"site/regionlist";
	var data = "country_id="+country_id;
	
	try {
  	// Firefox, Opera 8.0+, Safari
  		xmlHttp=new XMLHttpRequest();
	}
	catch (e) {
  		// Internet Explorer
  		try {
    		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
  		catch (e) {
    		try {
      			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      		}
    		catch (e) {
      			alert("Your browser does not support AJAX!");
      			return false;
			}
    	}
  	}
	xmlHttp.onreadystatechange=function() {
			if(xmlHttp.readyState==4) {
    			if (obj = document.getElementById(select_id)) {
					obj.innerHTML = xmlHttp.responseText;
				}
			}
   		}
  
	xmlHttp.open("POST",url,true);
  	xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
  	xmlHttp.send(data);

}
function update_countries(region_id, select_id) {
	// Do ajax to retrieve countrylist
	var countrylist = "";
	
	var xmlHttp;
	var data;
	var obj;
	var url = htmlroot+"site/countrylist";
	var data = "region_id="+region_id;
	
	try {
  	// Firefox, Opera 8.0+, Safari
  		xmlHttp=new XMLHttpRequest();
	}
	catch (e) {
  		// Internet Explorer
  		try {
    		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
  		catch (e) {
    		try {
      			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      		}
    		catch (e) {
      			alert("Your browser does not support AJAX!");
      			return false;
			}
    	}
  	}
	xmlHttp.onreadystatechange=function() {
			if(xmlHttp.readyState==4) {
    			if (obj = document.getElementById(select_id)) {
					obj.innerHTML = xmlHttp.responseText;
				}
    		}
    	}
  
	xmlHttp.open("POST",url,true);
  	xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
  	xmlHttp.send(data);

}








