/* Main JavaScript file for Jesper Tullbacks Portfolio (c) 2006 Jesper Tullback, Petter Ellgård */

/*
Dependencies on other JavaScript files: None
*/

//This function takes a string in the form of an url as an argument and navigates the current window 
//to that url. By supplying a second argument you can navigate another window instead of the current. 
//Another frame for example. If you supply a second argument it must be a string that can be evaluated 
//by javascript to a target window in the browser document object model. 
//A third option is to not supply any arguments at all. In that case the function just reloads the 
//current page.
//Usage example 1: <td onClick="navigate('index.htm')">, The current window navigates to index.htm.
//Usage example 2: <td onClick="navigate('index.htm','parent.frame2')">, Frame2 navigates to index.htm.
//Usage example 3: <td onClick="navigate()">, The page in the current window is reloaded.

function navigateTo(url){
	if(arguments.length > navigateTo.length){
		if(url!='')eval(arguments[1]).location.href = url;
	}else if(arguments.length == navigateTo.length){
		if(url!='')window.location.href = url;
	}else{
		window.location.reload();
	}
}

//Use with select onChange()
//Navigates the browser to the value of an option in a select
//it supports a target if you include it in the option value property delimited with a comma like this:
//<option value="http://www.thesiteofyourchoise.com,_blank">
//currently supported targets are: Any window specified by name or _blank and _new which both opens a new
//window with a name of '' (empty string) 
function selectNav(aSelect){
	strURL = '';
	strTarget = '';
	var tmpArray;
	if(aSelect.value != ''){
		strURL = aSelect.value;
		if(strURL.indexOf(',') > -1){
			tmpArray = strURL.split(',');
			strURL = tmpArray[0];
			strTarget = tmpArray[1].toUpperCase();	
		}
		//reset the select to the first option (when clicking back from an external site the select wont reset otherwise)
		//aSelect.options[0].selected = true;
		if(strTarget != ''){
			if(strTarget == '_BLANK'||strTarget == '_NEW'){
				window.open(strURL,'');
			}else{
				window.open(strURL,strTarget);
			}
		}else{
			location.href = strURL;
		}
	}
}

//Checks the send e-mail form
function eMailCheck(aForm){

	var message="Var sn\xE4ll att r\xE4tta till dessa sm\xE5 problem:";
	var retval = true;
	
	if(aForm.from.value!=""){
		if(!checkEmail(aForm.from.value)){
			message+="\nDin e-mail m\xE5ste inneh\xE5lla ett @-tecken och minst en punkt n\xE5gonstans d\xE4refter.";
			retval=false;
		}	
	}
	
	if(aForm.subject.value==""){
		message+="\nDu har gl\xF6mt att skriva ett \xE4rende i \xE4rendef\xE4ltet.";
		retval=false;
	}	

	if(isTextAreaEmpty(aForm.body)){
		message+="\nDu har gl\xF6mt att skriva ett meddelande i meddelandef\xE4ltet.";
		retval=false;
	}
	
	if(retval){
		putCookieInJar();
		return retval;
	}else{
		alert(message);
		return retval;
	}
}

//Refresh menu (from content frame)
function refreshMenu(){
	parent.navigation.window.location.reload();
}

//This function checks and displays appropriate messages when the "delete projects form" is submitted.
function deletionWarning(aCheckboxgroup){
	if(aCheckboxgroup!=null){
		selectedItems=0;
		
		if(aCheckboxgroup.length!=undefined){
			for(i=0;i<aCheckboxgroup.length;i++)if(aCheckboxgroup[i].checked)selectedItems++;
		}else if(aCheckboxgroup.checked)selectedItems++;
		
		if(selectedItems>0)return confirm('Du h\u00E5ller på att ta bort ' + selectedItems + ' projekt.\nProjekt som du tar bort kan du inte f\u00E5 tillbaks!\n\u00C4r du s\u00E4ker p\u00E5 att du vill forts\u00E4tta?');
		else{
			alert('Du har inte valt n\u00E5got projekt att ta bort!');
			return false;
		}
	}else{
		alert('Det finns inga projekt att ta bort!');
		return false;
	}
}

//Use with body onLoad
//usage: <body onload="setFocus();"> OR <body onload="setFocus(true);">
//Sets focus to the first form element of type 'text', 'textarea' and 'password' encountered on the page.
//You may also supply an optional argument and set its value to true if you wish to also select any text in the
//affected form element. 
function setFocus(){
	for(i=0;i<document.forms.length;i++){
		for(j=0;j<document.forms[i].elements.length;j++){
			if(document.forms[i].elements[j].type == "text"||document.forms[i].elements[j].type == "textarea"||document.forms[i].elements[j].type == "password"){
				if(arguments.length > setFocus.length){
					if(arguments[0]==true)document.forms[i].elements[j].select();
				}
				document.forms[i].elements[j].focus();
				return;
			}
		}
	}
}