// JavaScript Document

//Clear field
window.onload = onStart;
		function onStart() {
			clearField(document.getElementById('e-post_newsletter'), document.getElementById('e-post_newsletter').value);
			clearField(document.getElementById('cn'), document.getElementById('cn').value);
			
		}
		
		function clearField(field, value) {
			field.value = value;
			
			field.onfocus = function () {
				if (field.value == value) {
					field.value = '';
				}
				else {
					field.select();
				}
			};
		
			field.onblur = function () {
				if (field.value == '') {
					field.value = value;
				}
			};
		}



	$(document).ready(function(){
				
		$('#annonser').innerfade({
			animationtype: 'fade',
			speed: 'slow',
			timeout: 9000,
			type: 'random',
			containerheight: '70px'
			//runningclass: 'innerfade'
		});
		
		$('.fade_double').innerfade({
			animationtype: 'fade',
			speed: 'slow',
			timeout: 9000,
			type: 'sequence',
			containerheight: '275px'
			//runningclass: 'innerfade'
		});
		
		jQuery('#calc_accordion').accordion({
			alwaysOpen: false,
			active: true,
			autoheight: false
		});
		
		jQuery('#profile_accordion').accordion({
			autoheight: false
		});
		//$(".barometer").tablesorter( {sortList: [[0,0], [1,0]]} ); 
		
		// call the tablesorter plugin 
		/*$(".barometer").tablesorter({ 
			// set forced sort on the fourth column and i decending order. 
			sortList: [[1,0]] 
		}); */
		
		// extend the default setting to always include the zebra widget. 
		$.tablesorter.defaults.widgets = ['zebra']; 
		// extend the default setting to always sort on the fifth column 
		//$.tablesorter.defaults.sortList = [[1,0]] , headers: 6 {sorter: 'numeric'}; 
		// call the tablesorter plugin 
		$(".barometer").tablesorter(); 
		
		/*$(".barometer").tablesorter({
        sortList: [[1,0]],headers: 6 {sorter: 'numeric'};*/
        
          /*$('.amount_value').keyup(function(){
    		if(this.value.length%4==3) this.value = this.value+" ";
  		  });*/
  		  
  		  
  		  
$('.amount_value').keyup(function(){
  // Regex som fjerner alt som ikke er tall (matcher alt bortsett fra \d)
  var reg1 = /[^\d]/g;
  this.value = this.value.replace(reg1,"");

  // Regex som fordeler ut på tusenskillet
  var reg2 = /(-?\d+)(\d{3})/;

  // Iterere gjennom hvert treff og legge inn mellomrom
  while(reg2.test(this.value)) {
     this.value = this.value.replace(reg2,'$1 $2');
  }

});  		  
        
        

	
	});
/*
	jQuery(function(){
		var options = {
			exact:"exact",
			style_name_suffix:false,
			//highlight:"tr",
			keys:""
		}
		jQuery(document).SearchHighlight(options);
	});
*/
	
	jQuery(function(){

		var options = {
			exact:"exact",
			highlight:".highlightable",
			nohighlight:".not_highlightable",
			keys:""
			
		}
		jQuery(document).SearchHighlight(options);
		$('td:contains("keys")').parent().addClass('highlight'); 
		 
	});	
	
	
	
  	  var options = {};
      jQuery(function(){
        reset_highlight();
        jQuery("input:not(:text)").click(reset_highlight)
        .filter("[@name=keys_enabled]").click(function(){
          var disable = !jQuery(this).is(":checked");
          jQuery("input[@name=keys],input[@name=keys_button]").each(function(){
            this.disabled = disable;
          });
        });
        jQuery("input:text").change(reset_highlight);
      });
      function reset_highlight() {
        var disable = !jQuery("input[@name=keys_enabled]").is(":checked");
        jQuery("input[@name=keys],input[@name=keys_button]").each(function(){
          this.disabled = disable;
        });
        jQuery("span[@class^=hilite]").each(function(){
          var hilite = jQuery(this);
          var txt_el = hilite[0].previousSibling;
          if(txt_el && txt_el.nodeType==3)
            txt_el.data += hilite.text();
          else {
            hilite.before(hilite.text());
            txt_el = hilite[0].previousSibling;
          }
          if(hilite[0].nextSibling && hilite[0].nextSibling.nodeType==3) {
            txt_el.data += hilite[0].nextSibling.data;
            $(hilite[0].nextSibling).remove();
          }
          hilite.remove();
        });
        jQuery("#code").text(build_options());
        jQuery(document).SearchHighlight(options);
        
      }
      function build_options() {
        var code = "\tjQuery(function(){\r\n\t\tvar options = {\r\n\t\t\t";

        options.debug_referrer = jQuery("input[@name=referrer]").val();
        
        options.exact = jQuery("input[@name=exact]:checked").val();
        code += 'exact:"'+options.exact+'"';
        options.style_name_suffix = jQuery("input[@name=suffix]").is(":checked");
        if(!options.style_name_suffix)
          code += ',\r\n\t\t\tstyle_name_suffix:'+options.style_name_suffix;
        if(jQuery("input[@name=Highlight]").is(":checked")) {
          options.highlight = ".highlightable";
          code += ',\r\n\t\t\thighlight:".highlightable"';
        } else {
          delete(options.highlight);
        }
        if(jQuery("input[@name=NoHighlight]").is(":checked")) {
          options.nohighlight = ".not_highlightable";
          code += ',\r\n\t\t\thighlight:".not_highlightable"';
        } else {
          delete(options.nohighlight);
        }        
        if(jQuery("input[@name=keys_enabled]").is(":checked")) {
          options.keys = jQuery("input[@name=keys]").val();
          code += ',\r\n\t\t\tkeys:"'+options.keys+'"';
        } else {
          delete(options.keys);
        }
        
        code += "\r\n\t\t}\r\n\t\tjQuery(document).SearchHighlight(options);\r\n\t});";
        return code;
      }
	
	

//Tips en venn ajax
function XHConn(){ 

    var xmlhttp= window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP"); 

    this.connect = function(mURL, mMethod, data, fnDone, fnError){ 

        xmlhttp.open(mMethod, mURL, true); 
        xmlhttp.setRequestHeader("connection", "close"); 
        xmlhttp.setRequestHeader('User-Agent','XMLHTTP/1.0'); 
        xmlhttp.setRequestHeader("X-Requested-With", "XMLHttpRequest"); 

        if(mMethod == "POST"){ 
            xmlhttp.setRequestHeader("Method", "POST "+mURL+" HTTP/1.1"); 
            xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
            xmlhttp.setRequestHeader('Content-Length', data.length); 
        } 

        xmlhttp.onreadystatechange = function(){ 

            if (xmlhttp.readyState == 4){ 
                if(xmlhttp.status == 200){ 
                    fnDone(xmlhttp); 
                } 

                else{ 
                    fnError(xmlhttp); 
                } 
            } 
        } 

        xmlhttp.send(data); 

    } 

}; 




/*Login functions*/
	
            function toggleLostPassword() {
                if(document.getElementById('login-form-lostpassword-container').style.display == 'block') {
                    // if lostpassword is not visible, display it
                    document.getElementById('login-form-lostpassword-container').style.display = 'none';
                    document.getElementById('login-form-container').style.display = 'block';
                } else {
                    // lostpassword id visible, hide it
                    document.getElementById('login-form-lostpassword-container').style.display = 'block';
                    document.getElementById('login-form-container').style.display = 'none';
                }
            }
			
			function toggleChangePasswordForm() {
				if(document.getElementById('login-form-changepassword').style.display == 'block') {
					document.getElementById('login-form-changepassword').style.display = 'none';
					document.getElementById('login-form-changepassword-btn').style.display = 'block';
				} else {
					document.getElementById('login-form-changepassword').style.display = 'block';
					document.getElementById('login-form-changepassword-btn').style.display = 'none';
				}
			}
			function toggleChangeProfileForm() {
				if(document.getElementById('login-form-changeprofile').style.display == 'block') {
					document.getElementById('login-form-changeprofile').style.display = 'none';
					document.getElementById('login-form-changeprofile-btn').style.display = 'block';
				} else {
					document.getElementById('login-form-changeprofile').style.display = 'block';
					document.getElementById('login-form-changeprofile-btn').style.display = 'none';
				}
			}			
	
/*Newsletter popup*/

 
function NewsletterPop(page, form)
   {   
   form.setAttribute("target", "popup");
   OpenWin = this.open(page,"popup","toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=460,height=350,left = 500,top = 200");
 }
 
function LegalizPop(page, form)
   {   
   form.setAttribute("target", "popup_legaliz");
   OpenWin = this.open(page,"popup_legaliz","toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=785,height=450,left = 500,top = 200");
 }
 
 
 //Add input
 
/*function addInput(el){
var b, o, p;
	if ( document.createElement
		&& (o=document.createElement('input'))
		&& (p=el.parentNode)){
		o.type = 'file';
		o.name = 'dokumentasjon_' + (el.form.elements.length - 1) + '';
		b = document.createElement('br');
		p.appendChild(b);
		p.appendChild(o);
		//p.appendChild(document.createElement(o.name));
	}
	return false;
}*/

var current_input = 3;
function addInput() {
	//current keeps track of how many fonds we have.
	current_input++;
	
	var strToAdd = '<li><label for="dokumentasjon_'+current_input+'">Dokument '+current_input+'</label><input id="dokumentasjon_'+current_input+'" name="dokumentasjon_'+current_input+'" class="text" type="file">'



	$('#extra_fields').append(strToAdd)
}

$(document).ready(function(){
	$('#addInput').click(addInput)
});








//Callback script

function Validate(form) {
  var cn = form.cn.value;
  var pos = 0;
  var ok = true;
  while (pos < cn.length) {
    if ((cn.charAt(pos) < '0') || (cn.charAt(pos) > '9')) {
      cn = cn.slice(0, pos) + cn.slice(pos+1, cn.length);
      ok = false;
    } else
      pos++;
  }
  form.cn.value = cn;
  if (!ok)
    ok = confirm('Telefonnummeret du skrev inn inneholdt ugyldige tegn. Er "'
      + cn + '" nummeret du vil at vi skal ringe?');
  if (ok) {
    if ((cn.length < 5)||(cn.length > 15))
        alert("Telefonnummeret m&aring; ha 5 til 15 siffer!")
    else
        return true;
  }
  return false;
}


//Jquery add input
/*
$(document).ready(function() {
	$("a#add_input").click(function() {
		
		//Write new input field
		
		
		
	});
});*/


/*
count = 5;
function createExtraElement(area,field,limit){
	if(!document.getElementById) return; //Prevent older browsers from getting any further.
		
		var field_area = document.getElementById(area);
		count += 1;
		//var template = "Fond "+(count)+"</legend><ol><li><label for=\"Rep"+(count)+"_Medlemsnummer\">Medlemsnummer: <em>*</em></label> <input class=\"text\" id=\"Rep"+(count)+"_Medlemsnummer\" name=\"Rep"+(count)+"_Medlemsnummer\" value=\"\" type=\"text\"></li><li><label for=\"Rep"+(count)+"_Navn\">Navn: <em>*</em></label> <input class=\"text\" id=\"Rep"+(count)+"_Navn\" name=\"Rep"+(count)+"_Navn\" value=\"\" type=\"text\"></li><li><label for=\"Rep"+(count)+"_Adresse\">Adresse: <em>*</em></label> <input class=\"text\" id=\"Rep"+(count)+"_Adresse\" name=\"Rep"+(count)+"_Adresse\" value=\"\" type=\"text\"></li><li><label for=\"Rep"+(count)+"_Postnummer\">Postnr./sted: <em>*</em></label> <input class=\"text short\" id=\"Rep"+(count)+"_Postnummer\" name=\"Rep"+(count)+"_Postnummer\" value=\"\" type=\"text\"> <input class=\"text medium\" id=\"Rep"+(count)+"_Sted\" name=\"Rep"+(count)+"_Sted\" value=\"\" type=\"text\"></li><li><label for=\"Rep"+(count)+"_epost\">E-post privat: <em>*</em></label> <input class=\"text\" id=\"Rep"+(count)+"_epost\" name=\"Rep"+(count)+"_epost\" value=\"\" type=\"text\"></li><li><label for=\"Rep"+(count)+"_Tillitsverv\">Tillitsverv i styret: <em>&nbsp;</em></label> <input class=\"text\" id=\"Rep"+(count)+"_Tillitsverv\" name=\"Rep"+(count)+"_Tillitsverv\" value=\"\" type=\"text\"></li><li><fieldset><legend>Ønsker overnatting? <em>*</em></legend><label><input name=\"Rep"+(count)+"_overnatting\" value=\"Ja\" checked=\"checked\" type=\"radio\"> Ja</label><label><input name=\"Rep"+(count)+"_overnatting\" value=\"Nei\" type=\"radio\"> Nei</label></fieldset></li></ol></fieldset>";
		var template = "<li><label for=\"fond"+(count)+"\">Fond "+(count)+" <em>&nbsp;</em></label> <input id=\"fond"+(count)+"\" name=\"fond"+(count)+"\" class=\"text half2\" /> <input id=\"verdi"+(count)+"\" name=\"verdi"+(count)+"\" class=\"text short2\" /></li>";
		
		if(document.createElement) { //W3C Dom method.
			field_area.innerHTML += (template);
		} else { //Older Method
			field_area.innerHTML += (template);
		}
	}

count = 4;
count2 = 5;
function createExtraElement2(area,field,limit){
	if(!document.getElementById) return; //Prevent older browsers from getting any further.
		
		var field_area = document.getElementById(area);
		count += 1;
		count2 += 1;
		
		
		var template = "<tr><th scope=\"row\" abbr=\"Fond "+(count2)+"\">Fond "+(count2)+"</th><td><input id=\"fond["+(count)+"][navn]\" name=\"fond["+(count)+"][navn]\" class=\"text fond_navn\" /></td><td><input id=\"fond["+(count)+"][verdi]\" name=\"fond["+(count)+"][verdi]\" class=\"text short2\" /></td><!--td><select id=\"fond["+(count)+"][valuta]\" name=\"fond["+(count)+"][valuta]\"><option value=\"NOK\">NOK</option><option value=\"SEK\">SEK</option><option value=\"DKK\">DKK</option><option value=\"EUR\">EUR</option><option value=\"GBP\">GBP</option><option value=\"USD\">USD</option></select></td--></tr>";


		
		
		/*if(document.createElement) { //W3C Dom method.
			field_area.innerHTML += (template);
		} else { //Older Method
			field_area.innerHTML += (template);
		}
		
		field_area.innerHTML += (template);

	}
*/
//Jquery

var current = 5;
var current2 = 4;

function addFond() {
	//current keeps track of how many fonds we have.
	current++;
	current2++;
	//var strToAdd = '<p><label for="firstname"'+current+'">Name</label> <em>*</em><input id="firstname'+current+'" name="firstname'+current+'" size="25" /> <input id="lastname'+current+'" name="lastname'+current+'" size="25" />'
	
	var strToAdd = '<tr><th scope="row" abbr="Fond '+current+'">Fond '+current+'</th><td><input id="fond['+current2+'][navn]" name="fond['+current2+'][navn]" class="text fond_navn" /></td><td><input id="fond['+current2+'][verdi]" name="fond['+current2+'][verdi]" class="text short2 amount_value" /></td><!--td><select id="fond['+current2+'][valuta]" name="fond['+current2+'][valuta]"><option value="NOK">NOK</option><option value="SEK">SEK</option><option value="DKK">DKK</option><option value="EUR">EUR</option><option value="GBP">GBP</option><option value="USD">USD</option></select></td--></tr>'

	//strToAdd += '<p><label for="email'+current+'">Email</label>	<em>*</em><input id="email'+current+'" name="email'+current+'" size="25" /></p>'
	$('#extra_fields_fond').append(strToAdd)
}

$(document).ready(function(){
	$('#addFond').click(addFond)
});






