/****************************************************************
* design and development - christopher aalholm / apt as - 2001 	*
*   adapted to fit this site - yonas sandbęk / apt as - 2002    *
****************************************************************/
var menu = false;
var content = new Array();


 //window.captureEvents(event.RESIZE);     


window.onresize=reloadMain;

function reloadMain(){
	//alert('hepp');
	location.reload();
}

function setFrame(){
	t=top.location.href
	s=self.location.href
	if (s == t){
		top.location.href='/index.php';
	}
}

function setStyle(){
	// set the font-size to 9px if msie
	if(document.getElementById){
		var str;
			str = 'textarea { border: 1px #949295 solid;scrollbar-base-color : #ffffff;';
			str += 'scrollbar-shadow-color : #ffffff;scrollbar-3d-light-color : #ffffff;';
			str += 'scrollbar-darkshadow-color : #ffffff;scrollbar-face-color : #ffffff;';
			str += 'scrollbar-highlight-color : #ffffff;scrollbar-arrow-color : #000000;}.capt{padding-left:0px;}';
		document.writeln('<style>a.menu,td {font-size: 9px;}input,textarea{border: 1px solid #949295;}.nuWhite{font-size:9px;}'+str+'</style>');
	}
}
function swapOtherImg(img){
	// mouseOver-script for the menu
	document.images[img].src = '../elements/'+img+'_over.gif';
	return false;
}
function swapOtherImgBack(img){
	// mouseOver-script for the menu
	document.images[img].src = '../elements/'+img+'.gif';
	return false;
}

function swapImg(img){
	// mouseOver-script for the menu
	resetImg();
	document.images[img].src = '../elements/meny/'+img+'_over.gif';
	return false;
}

function resetImg(){
	// restores all pictures to default state
	var i;
	for(i=1;i<7;i++){
		document.images['m_0'+i].src = '../elements/meny/m_0'+i+'.gif';
	}
}
/****************************************************
* function that validate emailadress				*
*****************************************************/
/****************************************************
* notes: 'exclude' checks 5 conditions:				*
* a) characters that should not be in the address	*
* b) characters that should not be at the start		*
* c) & d) characters that shouldn't be together		*
* e) there's not more than one '@'					*
* 'check' checks there's at least one '@', later 	*
* followed by at least one '.'						*
* 'checkend' checks the address ends with a period 	*
* followed by 2 or 3 alpha characters				*
****************************************************/
function validate(email){
	
	var exclude=/[^@\-\.\w]|^[_@\.\-]|[\._\-]{2}|[@\.]{2}|(@)[^@]*\1/;
	var check=/@[\w\-]+\./;
	var checkend=/\.[a-zA-Z]{2,3}$/;

	if(((email.search(exclude) != -1)||(email.search(check)) == -1)||(email.search(checkend) == -1)){
		return true;
	} else {
		return false;
	}
}



/****************************************
* functions that operate the menu		*
****************************************/
function showMenu(item){
	// function: show menu
	// returns : false if menu isn't loaded
	// calls   : hideMenu()
	
	hideMenu();
	
	// check if menu is loaded	
	if(content){ 
		if(document.getElementById){ // ie 5.0, nn6
		
			document.getElementById('menubg'+item).style.visibility = 'visible';
			// opera-fix
			for(var j=0;j<content[item].length;j++){
				document.getElementById('menu'+item+'_'+j).style.visibility = 'visible';
			}
			document.getElementById('bg').style.visibility = 'visible';
			document.getElementById('bgtop').style.visibility = 'visible';
			
		}else if(document.layers){ // ns 4.x
		
			tmp = eval("document.layers.menubg"+item);
			tmp.visibility = 'show';
			document.layers.bg.visibility = 'show';
			document.layers.bgtop.visibility = 'show';
			
		}else if(document.all){ // ie 4.x
		
			document.all(item).style.visibility = 'visible';
			document.all('bg').style.visibility = 'visible';
			
		}
	}
	return false;
}

function hideMenu(){
	// function: hide menu
	// returns : nothing
	// calls   : hideBg()
	
	var i;
	if(menu){
		for(i=0;i<content.length;i++){
			if(document.getElementById){ // ie 5.x.nn6
				document.getElementById('menubg'+i).style.visibility = 'hidden';
				// opera-fix
				for(var j=0;j<content[i].length;j++){
					document.getElementById('menu'+i+'_'+j).style.visibility = 'hidden';
				}
			
			}else if(document.layers){ // nn 4.x
				tmp = eval('document.layers.menubg'+i);
				tmp.visibility = 'hide';
			
			}else if (document.all){ // ie 4.x
				document.all('menubg'+i).style.visibility = 'hidden';
			}
		}
		hideBg();
	}
}

function hideBg(){
	// function: hide the closing backgroundlayers
	// returns : nothing
	
	if(document.getElementById){ // ie 5.x,nn6
		document.getElementById('bg').style.visibility = 'hidden';
		document.getElementById('bgtop').style.visibility = 'hidden';
		
	}else if(document.layers){ // nn 4.x
		document.layers.bg.visibility = 'hide';
		document.layers.bgtop.visibility = 'hide';
		
	}else if(document.all){ // ie 4.x
		document.all('bg').style.visibility = 'hidden';
		document.all('bgtop').style.visibility = 'hidden';
	}
}

function hiLite(obj){
	// function: change background-color in a layer
	// returns : nothing
	
	if((document.getElementById)||(document.all)){
		obj.style.backgroundColor = '#BECFD6';
	} else if(document.layers){
		obj.document.bgColor = '#BECFD6';
	}
}

function loLite(obj){
	// function: change background-color in a layer
	// returns : nothing
	
	if((document.getElementById)||(document.all)){
		obj.style.backgroundColor = "#D9E3E5";
	} else if(document.layers){
		obj.document.bgColor = '#D9E3E5';
	}
}

function checkFormSave(myForm){
	// function: 
	// returns :

	var i;
	var j;
	var k;
	var send = true;	
	var found_first = false;
	var group_val = "";
	var komma = "";
	if(arguments.length == 3){
		myForm.elements[arguments[1]].value = arguments[2];
	}
	for(i=0;i<myForm.elements.length;i++){
	// find the fieldname for the first dynamic field
		if((!found_first)&&(myForm.elements[i].name.indexOf('field_')!=-1)){
			found_first = myForm.elements[i].name;
		}
	
	// check fieldtype
		switch(myForm.elements[i].type){
			case"checkbox":
			// check if it's in a group..
				if(myForm.elements[myForm.elements[i].name].length){
				
					group_val = "";
					komma = "";
					k = i;
					
				// loop through the collection of checkboxes
					for(j=0;j<myForm.elements[myForm.elements[k].name].length;j++){
						group_val += komma + checkBox(myForm.elements[i]);
						komma = ",";
						// increment the counter..
						i++;
					}
					
				// return the compiled value to the last element in the group
					myForm.elements[i-1].value = group_val;
					
				// decrement the counter
					i--;					
				
				}else{
				// it's not..
					myForm.elements[i].value = checkBox(myForm.elements[i]);
				}
				break;
				
			case"radio":
			// check if it's in a group
				if(myForm.elements[myForm.elements[i].name].length){
					group_val = "";
					komma = "";
					k = i;
				// loop through the collection of radiobuttons
					
					for(j=0;j<myForm.elements[myForm.elements[k].name].length;j++){						
						group_val += komma + radioBox(myForm.elements[i]);
						komma = ",";
												
					// increment the counter
						i++;
					}
				// return the compiled value to the last element in the group
					myForm.elements[i-1].value = group_val;
					myForm.elements[i-1].checked = true;
					
				// decrement the counter
					i--;
					//alert(myForm.elements[k].name + ' ' + myForm.elements[k].value);
				
				}else{
				
				// it's not..
					myForm.elements[i].value = radioBox(myForm.elements[i]);
				}
				
				break;
				
			case"select-one":				
				myForm.elements[i].value = selectBox(myForm.elements[i]);	
				break;
				
			default:
				myForm.elements[i].value = textBox(myForm.elements[i]);
				break;			
		}
	}
	
// check the dateinput..

	if(myForm.elements['Startdate']){
		if(nonValidDate(myForm.elements['Startdate'].value)){
			// not valid date
			alert('Feil datoformat! (dd.mm.yyyy)');
			send = false;
			return;
		}else{		
		// valid, format it..
			myForm.elements['Startdate'].value = formatDate(myForm.elements['Startdate'].value);
			
		}
		
		if(nonValidDate(myForm.elements['Stopdate'].value)){
		// not valid date
			alert('Feil datoformat! (dd.mm.yyyy)');
			send = false;
			return;
		}else{
		// valid, format it..
			myForm.elements['Stopdate'].value = formatDate(myForm.elements['Stopdate'].value);
		}
	}
	
// copy value from first dynamic field to instname
	if(myForm.elements['instname']){
		myForm.elements['instname'].value = myForm.elements[found_first].value;
	}
	
	if(send){
		myForm.submit();
	}
}

function checkBox(myObj){
	// function: check if a checkbox/radio is checked or not
	// returns : string, will also check the box
		
	if(myObj.checked){
		return myObj.value;
	}else{
		myObj.checked = true;
		if(myObj.name == 'Active'){
			return 'n'
		}else{
			return '0';
		}
	}
}

function radioBox(myObj){
	// function: check if a radio is checked or not
	// returns : string, checks the last box
		
	if(myObj.checked == true){		
		return myObj.value;
	}else{
		//myObj.checked=true;
		return '0';
	}
}

function selectBox(myObj){
	// function: return the value of a select
	// returns : string, value of selectedIndex
	
	return myObj[myObj.selectedIndex].value;
}

function textBox(myObj){
	// function: return the value from a text-input or text-area
	// returns : string
	return myObj.value;
}

function formatDate(str){
	// function: to format a date to valid input into the database
	// returns : string, formatted as yyyy.mm.dd
	
	var output = str;
	var day, month, year, tall;
	var dato = new Date();
	
	if(str.length == 10){
		day   = str.substring(0,2);
		month = str.substring(3,5);
		year  = str.substring(6,10);
		output = year + "." + month + "." +day;
	}else{
		day   = str.substring(0,2);
		month = str.substring(2,4);
		year  = str.substring(4,8);
		output = year + "." + month + "." +day;
	}	
		
	return output;
}

function isEmpty(str){
	// function: check if a string is empty
	// returns : boolean
	
	if(str == "" || str == null || str.length == 0){
		return true;
	}else{
		return false;
	}
}

function nonValidDate(str){
	// function: check if a date is valid (dd.mm.yyyy,dd-mm-yyyy,dd/mm/yyyy or ddmmyyyy)
	// returns : boolean
	
	var svar, day, month, year;
	svar = true;
	
	if(!isEmpty(str)){
		if(str.length == 10){
			if(str.charAt(2) == '/' || str.charAt(2) == '.' || str.charAt(2) == '-'){
				if(str.charAt(5) == '/' || str.charAt(2) == '.' || str.charAt(2) == '-'){
					if(str.substring(0,1) == "0"){
						day = str.substring(1,2);
					}else {
						day = str.substring(0,2);
					}
					month = str.substring(3,5);
					year = str.substring(6,10);
				}
			}
		}else if(str.length == 8){
			if(str.substring(0,1) == "0"){
				day = str.substring(1,2);
			} else {
				day = str.substring(0,2);
			}
			month = str.substring(2,4);
			year = str.substring(4,8);
		}
		
		day = parseInt(day);
		month = parseInt(month);
		year = parseInt(year);
		
		if(!isNaN(day) || !isNaN(month) || !isNaN(year)){
			if(month <= 12 && year < 3000){
				if(validateDay(day,month,year)){
					svar = false;
				}
			}
		}
	}
	
	return svar;
}

function validateDay(day,month,year){
	// function: validate that a day exists within a given month/year
	// returns : boolean
	
	day = parseInt(day);
	month = parseInt(month);
	var monthMAX = new Array(31,31,28,31,30,31,30,31,31,30,31,30,31);
	
	if(year%4 == 0){
		monthMAX[2] = 29;
	}
	
	var topp = monthMAX[month];
	
	if(day >= 1 && day <= topp ){
		return true;
	}else{
		return false;
	}
}