/*
function trim(str)
{
    return str.replace(/^\s+|\s+$/g, '');  
}
*/
  
function clearInput(e, compare)
{
    f = jQuery(e);
	f.removeClass('inputblank');   // remove grayed out color  
    if (f.val() == compare) {  
        f.val('');                // clear default text when clicked  
    }
}
  
function restoreInput(e, value)
{
	f = jQuery(e);
    if (jQuery.trim(f.val()) == '') {  
        f.val(value);             // reset default text when unclicked  
        f.addClass('inputblank');  // display grayed out color  
    }  
}

function showSubMenu(big_sister, element_name)
{
	if(navigator.appName == "Microsoft Internet Explorer")	{
		topp_add = 50;
	} else {
		topp_add = 5;
	}
	r = document.getElementById("innerramme");
	s_menu = document.getElementById(element_name);
	s_menu.style.display= "inline";
	if(!positions[element_name])	{
		positions[element_name] = r.offsetLeft+150;
		positionstop[element_name] = big_sister.offsetTop+topp_add;
	}
	s_menu.style.left = (positions[element_name])+"px";
	s_menu.style.top = (positionstop[element_name])+"px";
}

function hideSubMenu(element_name)
{
	s_menu = document.getElementById(element_name);
	s_menu.style.display= "none";
}

function showSubMenuFromSubmenu(element_name)
{
	s_menu = document.getElementById(element_name);
	s_menu.style.display= "inline";
}

function hideSubMenuFromSubmenu(element_name)
{
	hideSubMenu(element_name);
}

function validateEmailAddress()
{
	ret = true;
	if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.getElementById('emailaddress').value))){
		alert(labels[391]);
		ret = false;
	}
	/*if(requireReference==1 && rv!=document.getElementById('referenceNo').value && agreementNo==document.getElementById('agreementNo').value && document.getElementById('agreementNo').value.length>0){
		alert(labels[401]);
		ret = false;
	}*/
	return ret;
}

function selectNationality()
{
	
	e = document.getElementById('selectcountry');
	r = e.options[e.selectedIndex].value;
	f = document.getElementById('nationality').selectedIndex;
	
	display = 'none';
	if(f!=e.selectedIndex && f!=0 && e.selectedIndex!=0){
		display = 'block';
	}
	
	if (undefined == selectNationality.fields) {
		selectNationality.fields = new Array(
			'languagelabel',
			'languageinput',
			'languageinfo'
		);
	}
	
	for ( var i=0, len=selectNationality.fields.length; i<len; ++i ) {
		name = selectNationality.fields[i]
		t = document.getElementById(name);
		if (null != t) {
			t.style.display = display;
		}
	}
}

function docSave()
{
	window.location=labels['url'] + "?close=1";
	return false;
}

function phoneModel2()
{
	verdi = document.getElementById('phoneManufacturerSelect').selectedIndex-1;
	list = modeller[verdi];
	box2 = document.getElementById('phoneModelSelect');
	box2.options.length = 0;
	box2.options[0] = new Option(labels[318], -1);
	for(i=0;i<list.length;i+=2)
	{
		if(list[i+1]!=''){
			box2.options[(i+2)/2] = new Option(list[i],list[i+1]);
		}
	}
	display = 'none';
	if(list[1]!=''){
		display = 'block';
	}
	
	if (undefined == phoneModel2.fields) {
		phoneModel2.fields = new Array('pm', 'pm1', 'pmi');
	}
	
	for (var i = 0, len = phoneModel2.fields.length; i < len; ++i) {
		name = phoneModel2.fields[i];
		element = document.getElementById(name);
		if (null != element) {
			element.style.display = display;
		}
	}
}

function doIt(element_name)
{
	document.getElementById(element_name).checked = 1;
	document.getElementById(element_name+"_1").style.display = "block";
	if(prev != 0){
		document.getElementById(prev+"_1").style.display = "none";
	}
	if(prev == "PAYMENT_CC"){
		document.getElementById("expiryMonth").selectedIndex = 0;
		document.getElementById("expiryYear").selectedIndex = 0;
		document.getElementById("cardNo").value = "";
		document.getElementById("cvc2").value = "";
	}
	if(prev == "PAYMENT_AUTO_WITHDRAWAL"){
		document.getElementById("accountNo").value = "";
		document.getElementById("bankNo").value = "";
	}
	prev = element_name;
	r = document.getElementById("ramme");
	r.style.height = document.getElementById("innholdtabell").offsetHeight+175;
}

function emergencyCheck()
{
	r = document.getElementById("selectcountry");
	
	if ('undefined' != typeof(nextOfKinNumber)) {
		//dinside
		gammelfeil = 0;
		for(i=0;i<ant;i++){
			given = document.getElementById("givenName"+i).value;
			family = document.getElementById("familyName"+i).value;
			mobile = document.getElementById("mobile"+i).value;
			phone = document.getElementById("phone"+i).value;
			if(given=='' || family=='' || (mobile=='' && phone=='')){
				gammelfeil = 1;
			}
		}
		r = document.getElementById("selectcountry");
		given = document.getElementById("givenName").value;
		family = document.getElementById("familyName").value;
		mobile = document.getElementById("mobile").value;
		phone = document.getElementById("phone").value;
		country = r.selectedIndex;
		if(given=='' || family=='' || (mobile=='' && phone=='') || country==0 || gammelfeil==1){
			alert(labels[98]);
			return false;
		}
		else{
			return true;
		}
	} else {
		//index
		given = document.getElementById("givenName").value;
		family = document.getElementById("familyName").value;
		mobile = document.getElementById("mobile").value;
		if(given=='' || family=='' || mobile==''){
			alert(labels[98]);
			return false;
		}
		else{
			this.form.submit();
			return true;
		}
	}
}

function emergencyCheckSave()
{
	gammelfeil = -1;
	
	ant = 2;
	if ('undefined' != typeof(nextOfKinNumber)) {
		ant = nextOfKinNumber;
	}
	
	for(i=0;i<ant;i++){
		given = document.getElementById("givenName"+i).value;
		family = document.getElementById("familyName"+i).value;
		mobile = document.getElementById("mobile"+i).value;
		phone = document.getElementById("phone"+i).value;
		if(i==0){
			r = document.getElementById("selectcountry");
		}
		else{
			r = document.getElementById("selectcountry"+i);
		}
		country = r.selectedIndex;
		if((given=='' || family=='' || country==0 || (mobile=='' && phone=='')) && gammelfeil==-1){
			gammelfeil = i;
		}
		if(given=='' && family=='' && mobile=='' && country==0 && phone=='' && gammelfeil==i){
			gammelfeil = -1;
		}
	}
	if(gammelfeil>-1){
		alert(labels[98]);
		return false;
	}
	else{
		return true;
	}
}

function agreeCheck()
{
	if(document.getElementById("agree").checked){
		if(document.getElementById('registersteiger').value == 0){
			return true;
		}
		else if(document.getElementById('registersteiger').value == 1){
			if(document.getElementById("jamed").checked || document.getElementById("allerede").checked || document.getElementById("jauten").checked){
				if(document.getElementById("steigerterms").checked){
					return true;
				}
				else{
					alert(labels[399]);
					return false;
				}
			}
			else{
				alert(labels[323]);
				return false;
			}
		}
	}
	else{
		alert(labels[230]);
		return false;
	}
}

function agreeCheck2()
{
	if(godta==1){
		if(document.getElementById('registersteiger').value == 0){
			return true;
		}
		else if(document.getElementById('registersteiger').value == 1){
			if(document.getElementById("jamed").checked || document.getElementById("allerede").checked || document.getElementById("jauten").checked){
				if(document.getElementById("steigerterms").checked){
					return true;
				}
				else{
					alert(labels[399]);
					return false;
				}
			}
			else{
				alert(labels[323]);
				return false;
			}
		}
	}
	else{
		alert(labels[230]);
		return false;
	}
}

function updateAgreeCheck()
{
	switch (MAIN_PAGE) {
		case 'index':
			if(document.getElementById("agree").checked){
				if(document.getElementById('registersteiger').value == 0){
					return true;
				}
				else if(document.getElementById('registersteiger').value == 1){
					if(document.getElementById("jamed").checked || document.getElementById("allerede").checked || document.getElementById("jauten").checked){
						if(document.getElementById("steigerterms").checked){
							return true;
						}
						else{
							alert(labels[399]);
							return false;
						}
					}
					else{
						alert($labels[323]);
						return false;
					}
				}
			}
			else{
				alert(labels[230]);
				return false;
			}
			break;
		case 'dinside':
			var xScroll, yScroll;
			
			if (window.innerHeight && window.scrollMaxY) {	
				xScroll = document.body.scrollWidth;
				yScroll = window.innerHeight + window.scrollMaxY;
			} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
				xScroll = document.body.scrollWidth;
				yScroll = document.body.scrollHeight;
			} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
				xScroll = document.body.offsetWidth;
				yScroll = document.body.offsetHeight;
			}
			
			var windowWidth, windowHeight;
			if (self.innerHeight) {	// all except Explorer
				windowWidth = self.innerWidth;
				windowHeight = self.innerHeight;
			} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
				windowWidth = document.documentElement.clientWidth;
				windowHeight = document.documentElement.clientHeight;
			} else if (document.body) { // other Explorers
				windowWidth = document.body.clientWidth;
				windowHeight = document.body.clientHeight;
			}	
			
			// for small pages with total height less then height of the viewport
			if(yScroll < windowHeight){
				pageHeight = windowHeight;
			} else { 
				pageHeight = yScroll;
			}
		
			// for small pages with total width less then width of the viewport
			if(xScroll < windowWidth){	
				pageWidth = windowWidth;
			} else {
				pageWidth = xScroll;
			}
		
			if(document.getElementById("agree").checked){
					document.getElementById("waitmsg").style.height = pageHeight+"px";
					document.getElementById("waitmsg").style.width = pageWidth+"px";
					document.getElementById("waitmsg").style.display = "block";
					document.getElementById("msgcontainer").style.display = "block";
					document.getElementById("msgcontainer").style.left = ((pageWidth-732)/2)+"px";
					topp = 0;
					if((pageHeight-windowHeight)>0){
						topp = pageHeight-windowHeight;
					}
					topp = topp+(windowHeight/2)-50;
					document.getElementById("msgcontainer").style.top = topp+"px";
					return true;
			}
			else{
				alert(labels[230]);
				return false;
			}
			break;
	}
}

function toggleValue()
{
	if(godta==0){
		godta=1;
	}
	else{
		godta.value=0;
	}
}

function emergencyCheck2()
{
	return confirm(labels[98]);
}

function textCounter(field, maxlimit, passes)
{
	this.passed;

	if (undefined == this.passed) {
		this.passed = 0;
	}

	passed = true;
	if ((undefined != passes) && !isNaN(passes) && (passes <= this.passed)) {
		passed = false;
	}
	
	if ((field.value.length > maxlimit) && passed) {
		alert(labels[96]);
		this.passed++;
	}
}

function sjekkValgt(docid)
{
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}
	
	var checkedYes = jQuery("#completeyes:checked").length;
	var checkedNo = jQuery("#completeno:checked").length;
	if (checkedYes || checkedNo){
		if ((docid.length > 0) && checkedYes) {
			alert(labels[263]);
		}
		document.getElementById("waitmsg").style.height = pageHeight+"px";
		document.getElementById("waitmsg").style.width = pageWidth+"px";
		document.getElementById("waitmsg").style.display = "block";
		document.getElementById("msgcontainer").style.display = "block";
		
		containerWidth = ((pageWidth-732)/2);
		containerWidth += "px";
		document.getElementById("msgcontainer").style.left = containerWidth;
		
		if (MAIN_PAGE == 'index') {
			topp = 0;
			if ((pageHeight-windowHeight) >0) {
				topp = pageHeight-windowHeight;
			}
			topp = topp+(windowHeight/2)-50;
			document.getElementById("msgcontainer").style.top = topp+"px";
		}
		return true;
	} else {
		alert(labels[97]);
		return false;
	}
}

function sjekkSokStr(elem)
{
	console.log(typeof elem);
	var typeOfElem = typeof elem;
	var sokElem;
	var codeElem;
	if (typeOfElem != undefined) {
		console.log('sjekkSokStr not undefined');
		var element = jQuery(elem).closest('form');
		sokElem = element.find('input#sok_str');
		codeElem = element.find('input#code');
	} else {
		console.log('sjekkSokStr undefined');
		sokElem = jQuery('input#sok_str');
		codeElem = jQuery('input#code');
	}
	var str = sokElem.val().length;
	var cstr = codeElem.val().length;
	/*
	r = document.getElementById("sok_str");
	str = r.value.length;
	c = document.getElementById("code");
	cstr = c.value.length;
	*/
	console.log('str ' + str + '. cstr ' + cstr);
	return false;
	if((str >= 3) || (cstr >= 1)){
		return true;
	} else {
		alert(labels[262]);
		return false;
	}
}

function getMousePosition(mouse)
{
	//currentLeft = mouse.clientX;
	//currentTop  = mouse.clientY;
	if (document.layers) {
		currentLeft = mouse.clientX+10;
        currentTop = mouse.clientY;
	} else if (document.all) {
		currentLeft =  mouse.clientX+document.body.scrollLeft+10;
        currentTop =  mouse.clientY+document.documentElement.scrollTop;
    } else if (document.getElementById) {
		currentLeft = mouse.clientX+10;
        currentTop = mouse.clientY;
	}
}

function visInfo(elementnavn,tekst)
{
	switch (MAIN_PAGE) {
		case 'index':
		  	var myWidth = 0, myHeight = 0;
			if( typeof( window.innerWidth ) == 'number' ) {
			    //Non-IE
			    myWidth = window.innerWidth;
			    myHeight = window.innerHeight;
			} else if( (undefined !=document.documentElement) && ( (undefined !=document.documentElement.clientWidth) || (undefined !=document.documentElement.clientHeight) ) ) {
			    //IE 6+ in 'standards compliant mode'
			    myWidth = document.documentElement.clientWidth;
			    myHeight = document.documentElement.clientHeight;
			} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			    //IE 4 compatible
			    myWidth = document.body.clientWidth;
			    myHeight = document.body.clientHeight;
			}
			
			var scrOfX = 0, scrOfY = 0;
			if( typeof( window.pageYOffset ) == 'number' ) {
			    //Netscape compliant
			    scrOfY = window.pageYOffset;
			    scrOfX = window.pageXOffset;
			} else if ( (undefined != document.body) && ( (undefined != document.body.scrollLeft) || (undefined != document.body.scrollTop) ) ) {
			    //DOM compliant
			    scrOfY = document.body.scrollTop;
			    scrOfX = document.body.scrollLeft;
			}
			else if ((undefined != document.documentElement) && ((undefined != document.documentElement.scrollLeft) || (undefined != document.documentElement.scrollTop)))
			{
				
			    /*IE6 standards compliant mode*/
			    
			    scrOfY = document.documentElement.scrollTop;
			    scrOfX = document.documentElement.scrollLeft;
			    
			}
			
			document.getElementById(elementnavn).style.display = "block";
			if((currentLeft + 250) > myWidth){
				currentLeft = currentLeft - 260 + scrOfX;
			}
			document.getElementById(elementnavn).style.left = currentLeft + "px";
			document.getElementById(elementnavn).style.top = (currentTop) + "px";
			document.getElementById(elementnavn).innerHTML = tekst;
			break;
		case 'dinside':
			document.getElementById(elementnavn).style.display="block";
			document.getElementById(elementnavn).style.left=currentLeft+"px";
			document.getElementById(elementnavn).style.top=(currentTop)+"px";
			document.getElementById(elementnavn).innerHTML=tekst;
			break;
	}
}

function skjulInfo(elementnavn)
{
	document.getElementById(elementnavn).style.display="none";
}

function lengthCheck()
{
	r = document.getElementById("searchname");
	str = r.value.length;
	c = document.getElementById("member-ID");
	cstr = c.value.length;
	if(str<3 && cstr<1){
		alert(labels[262]);
		return false;
	}
	else{
		return true;
	}
}

function resizeMe()
{
    r = document.getElementById("ramme");
	r.style.height = document.getElementById("innholdtabell").offsetHeight+175;
}

function logout()
{
	window.location = configData['sikkerurl'] + '/index.php?logout=logout';
}

function goToOnWeb()
{
	window.location = configData['sikkerurl'] + '/dinside.php';
}

// MENU SLIDE
$(document).ready(function(){
$('#meny ul li').hover(
function() { $('ul', this).show(); },
function() { $('ul', this).hide(); });
});

//liveSearch
var liveSearchElements = {"medications":["medication","medicationText"],"diagnoses":["diagnose","diagnoseText"]};
function checkLiveSearch(element, type)
{
	var formElement = jQuery(element).closest('form');
	//var type = formElement.attr('type');
	var temp;	
	for (var x in liveSearchElements[type]) {
		temp = formElement.find('input[name=' + liveSearchElements[type][x] + ']').val();
		if ((temp == '') || (temp == labels["455"])) {
			return false;
		} 
	}
	formElement.removeAttr('type');
	return true;
}

//on page ready
jQuery(function() {
	jQuery('.noreturn').keypress(function (e) {
	    if (e.which == 13) { return false; }
	});
});