/**
*  Function to go to a certain picture category.
*  Input: the directory name of the category and the chosen language.
*/

function showCat(dir,lang) {
	var location = "pics/picpages.php?path=" + dir + "&page=1&lang=" + lang;
	window.location = location;
}



/**
* Function to show a large picture that fits in the window size.
*/

function showPic(id,path,nopics,lang)
{
	
	 var width;
	 var height;
	 
	 // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	 
	 if (typeof window.innerWidth != 'undefined')
	 {
		  width = window.innerWidth,
		  height = window.innerHeight
	 }
	 
	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
	
	 else if (typeof document.documentElement != 'undefined'
		 && typeof document.documentElement.clientWidth !=
		 'undefined' && document.documentElement.clientWidth != 0)
	 {
		   width = document.documentElement.clientWidth,
		   height = document.documentElement.clientHeight
	 }
	 
	 // older versions of IE
	 
	 else
	 {
		   width = document.getElementsByTagName('body')[0].clientWidth,
		   height = document.getElementsByTagName('body')[0].clientHeight
	 }
	
	var location = "pics.php?id=" + id + "&path=" + path + "&nopics=" + nopics + "&cWidth=" + width + "&cHeight=" + height + "&lang=" + lang;
	window.location = location;
}


/**
* Function to show a large picture that fits in the window size.
*/

function showPic2(id,path,nopics,lang)
{
	 var width;
	 var height;
	 
	 // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	 
	 if (typeof window.innerWidth != 'undefined')
	 {
		  width = window.innerWidth,
		  height = window.innerHeight
	 }
	 
	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
	
	 else if (typeof document.documentElement != 'undefined'
		 && typeof document.documentElement.clientWidth !=
		 'undefined' && document.documentElement.clientWidth != 0)
	 {
		   width = document.documentElement.clientWidth,
		   height = document.documentElement.clientHeight
	 }
	 
	 // older versions of IE
	 
	 else
	 {
		   width = document.getElementsByTagName('body')[0].clientWidth,
		   height = document.getElementsByTagName('body')[0].clientHeight
	 }
	
	var location = "http://www.andersbp.dk/pics/pics2.php?id=" + id + "&path=" + path + "&nopics=" + nopics + "&cWidth=" + width + "&cHeight=" + height + "&lang=" + lang;
	window.open(location,'','fullscreen=yes,status=no,toolbar=no,menubar=no,scrollbars=no,resizable=no');
}



/**
* Function to listen for arrow keys when going through pictures in an album.
*/

function getArrows(ev,id,path,nopics,lang) {

	arrows=((ev.which)||(ev.keyCode));

	switch(arrows) {
	 case 37:
	 	if(id == 1) {prev = nopics} else {prev = id - 1}
		showPic(prev,path,nopics,lang);
	 	break;
	
	 case 39:
	 	if(id == nopics) {next = 1} else {next = id + 1}
		showPic(next,path,nopics,lang);
	 	break;
	}
}



/**
 * DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("V\u00E6r venlig at indtaste en gyldig emailadresse")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("V\u00E6r venlig at indtaste en gyldig emailadresse")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("V\u00E6r venlig at indtaste en gyldig emailadresse")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("V\u00E6r venlig at indtaste en gyldig emailadresse")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("V\u00E6r venlig at indtaste en gyldig emailadresse")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("V\u00E6r venlig at indtaste en gyldig emailadresse")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("V\u00E6r venlig at indtaste en gyldig emailadresse")
		    return false
		 }

 		 return true					
	}

function ValidateEmail(form){
	var emailID=form.email
	
	if ((emailID.value==null)||(emailID.value=="")){
		alert("V\u00E6r venlig at indtaste en gyldig emailadresse")
		emailID.focus()
		return false
	}
	if (echeck(emailID.value)==false){
		emailID.value=""
		emailID.focus()
		return false
	}
	return true
 }
 
 
/**
*  Function to validate a URL in a form:
*/

function ValidateURL(form) {
	/*
	var v = new RegExp(); 
    v.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$");
	v.compile("/^(http|ftp)\:\/\/\w+([\.\-]\w+)*\.\w{2,4}(\:\d+)*([\/\.\-\?\&\%\#]\w+)*\/?$/i");
	v.compile("/^(ht|f)tps?:\/\/[a-z0-9-\.]+\.[a-z]{2,4}\/?([^\s<>\#%\,\{\}\\|\\\^\[\]`]+)?$/");
    if (!v.test(form.homepage)) {
	*/
	if (!form.homepage.match(/^(ht|f)tps?:\/\/[a-z0-9-\.]+\.[a-z]{2,4}\/?([^\s<>\#%\,\{\}\\|\\\^\[\]`]+)?$/)) {
		alert("Du har indtastet en ugyldig hjemmeside. Pr\u00F8v igen!")
		form.homepage.focus()
		return false
	}
	return true
}


/**
*  Function to set a border on the avatar selected.
*  Input: the image (avatar) to be changed and the number of pictures (avatars) in total.
*/

function setAvatarFocus(img,nopics){
	img.style.border="3px solid black"
	var num = "0"
	for(var i=0; i<nopics; i++) {
		if(i<10) {
			num = "0"+i
		}
		else {
			num = ""+i
		}
		if(num != img) {
			document.getElementById("avatar"+num+".jpeg").style.border="none"
		}
	}
}


/**
*  Funtion to calculate the number of remaining characters allowed in an HTML textarea.
*  Input: the field to be checked (form), the name of the field to be updated (remain) and
*  		  the maximum number of characters allowed in form.
*/

function calcRemaining(form,remain,maxchars) {
	var content = form.value
	var len = content.length
	if(len > maxchars) {
		form.value = content.substring(0,maxchars)
	}
	else {
		document.getElementById(remain).value = maxchars - len
	}
}


/**
*  Function to set a rating when clicking on a certain star.
*/

var rating = 0;

function setRating(name,maxval,value) {
	var i = 1;
	for (i=1;i<=maxval;i++) {
		document.getElementById(""+name+i).src = "graphics/star-empty.png";
	}
	i=1;
	while (i<value+1) {
		document.getElementById(""+name+i).src = "graphics/star-full.png";
		i++;
	}
	rating = i-1;
	document.getElementById('rateButton').disabled = "";
}

/**
*  Function to find the set rating.
*/

function getRating() {
	return rating;
}

/**
*  Function to set the rating of a post.
*/

function rate(id) {
	//alert("Sorry. Denne funktion er stadig under udvikling...");
	var location = "rate.php?id=" + id + "&rating=" + rating;
	window.location = location;
}

/**
*	Function to toggle a CSS popup:
*/

function popup(name,width,height) {
	blanket_size(name,width,height);
	window_pos(name,width,height);
	var el = document.getElementById(name);
	if (el.style.display == "block")
		el.style.display = "none";
	else
		el.style.display = "block";
	el = document.getElementById('blanket');
	if (el.style.display == "block")
		el.style.display = "none";
	else
		el.style.display = "block";
}

/**
*	Function to center a CSS popup horisontally:
*/

function window_pos(popUpDivVar,width,height) {
	if (typeof window.innerWidth != 'undefined') {
		viewportwidth = window.innerHeight;
	} else {
		viewportwidth = document.documentElement.clientHeight;
	}
	if ((viewportwidth > document.body.parentNode.scrollWidth) && (viewportwidth > document.body.parentNode.clientWidth)) {
		window_width = viewportwidth;
	} else {
		window_width = document.body.parentNode.clientWidth;
	}
	var popUpDiv = document.getElementById(popUpDivVar);
	window_width = window_width/2-(width/2);
	popUpDiv.style.left = window_width + 'px';
}

/**
*	Function to center a CSS popup vertically:
*/

function blanket_size(popUpDivVar,width,height) {
	var top = document.body.scrollTop
	    ? document.body.scrollTop
	    : (window.pageYOffset
	        ? window.pageYOffset
	        : (document.body.parentElement
	            ? document.body.parentElement.scrollTop
	            : 0
	        )
	    );
	//alert("Top: "+top);
	if (typeof window.innerWidth != 'undefined') {
		viewportheight = window.innerHeight;
	} else {
		viewportheight = document.documentElement.clientHeight;
	}
	if ((viewportheight > document.body.parentNode.scrollHeight) && (viewportheight > document.body.parentNode.clientHeight)) {
		blanket_height = viewportheight;
	} else {
		blanket_height = document.body.parentNode.scrollHeight;
	}
	var blanket = document.getElementById('blanket');
	blanket.style.height = blanket_height + 'px';
	var popUpDiv = document.getElementById(popUpDivVar);
	popUpDiv_height = blanket_height/2-(height/2);
	popUpDiv.style.top = (75+top) + 'px';
}

// Function to load map into IFRAME after page load for correct centering:

function showMap() {
	document.getElementById('mapFrame').src = document.getElementById('mapFrame').src;
	//document.getElementById('mapFrame').contentDocument.location.reload(true);
	popup('popUpDiv',500,425);
}

