// JScript File
function activate(obj, text){
    obj.value = '';
    document.getElementById('input_text' + text).className = 'input_text_hover';
    document.getElementById('input_left' + text).className = 'input_left_hover';
    document.getElementById('input_right' + text).className = 'input_right_hover';
}

function deactivate (obj, text){
	document.getElementById('input_text' + text).className = 'input_text';
    document.getElementById('input_left' + text).className = 'input_left';
    document.getElementById('input_right' + text).className = 'input_right';
}

jQuery(document).ready(function() {
    repos();

	jQuery("#options div > a").click(function (event) { 
		// First disable the normal link click 
		event.preventDefault(); 
 
		// Remove all list and links active class. 
		jQuery('#options .active').removeClass("active"); 
 
		// Grab the link clicks ID 
		var id = this.id;
 
		// The id will now be something like "link1" 
		// Now we need to replace link with option (this is the ID's of the checkbox) 
		var newselect = id.replace('link', 'option'); 
 
		// Make newselect the option selected. 
		jQuery('#'+newselect).attr('checked', true);
 
		// Now add active state to the link and list item 
		jQuery(this).addClass("active").parent().addClass("active"); 
 
		return false; 
	});

});

jQuery(window).resize(function() { 
	repos() ;
});

function repos(){
    var window_height = jQuery(window).height();
    var footer_height = jQuery('.footer_container').height();
    var content_height = window_height - footer_height - jQuery('.top').height() - jQuery('.menu_top_container').height();
    
    if(jQuery('.content_container').height() < content_height){
        jQuery('.content_container').height(content_height);
    }
}

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;

//loading popup with jQuery magic!
 function loadPopup(){
//loads popup only if it is disabled
 if(popupStatus==0){
	jQuery("#backgroundPopup").css({
		"opacity": "0.7"
	});
	jQuery("#backgroundPopup").fadeIn("slow");
	jQuery("#popupContact").fadeIn("slow");
	popupStatus = 1;
	}
}

function loadPackshotPopup(){
 if(popupStatus==0){
	jQuery("#backgroundPopup").css({
		"opacity": "0.7"
	});
	jQuery("#backgroundPopup").fadeIn("slow");
	jQuery("#packshot_popup").fadeIn("slow");
	popupStatus = 1;
	}
}

 function loadPopupsms(){
//loads popup only if it is disabled
 if(popupStatus==0){
	jQuery("#backgroundPopupsms").css({
		"opacity": "0.7"
	});
	jQuery("#backgroundPopupsms").fadeIn("slow");
	jQuery("#popupContactsms").fadeIn("slow");
	popupStatus = 1;
	}
}

 function loadPopupFlickr(){
//loads popup only if it is disabled
 if(popupStatus==0){
	jQuery("#backgroundPopup").css({
		"opacity": "0.7"
	});
	jQuery("#backgroundPopup").fadeIn("slow");
	jQuery("#popupFlickr").fadeIn("slow");
	popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
 function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
	jQuery("#backgroundPopup").fadeOut("slow");
	jQuery("#popupContact").fadeOut("slow");
	jQuery("#popupFlickr").fadeOut("slow");
	jQuery("#packshot_popup").fadeOut("slow");
	popupStatus = 0;
	}
 }
 
 //centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = jQuery("#popupContact").height();
	var popupWidth = 308;//jQuery("#popupContact").width();
	//alert(windowWidth/2-popupWidth/2);
	//centering
	jQuery("#popupContact").css({
	"position": "absolute",
	"top": jQuery(window).scrollTop() + 100,//windowHeight/2-popupHeight/2,
	//"left": windowWidth/2-popupWidth/2
	"left": 330
	});
	//only need force for IE6

	jQuery("#backgroundPopup").css({
	"height": windowHeight
	});

}

function centerPackshotPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = jQuery("#packshot_popup").height();
	var popupWidth = jQuery("#packshot_popup").width();


	jQuery("#packshot_popup").css({
	"position": "absolute",
	"top": jQuery(window).scrollTop() + 40,//windowHeight/2-popupHeight/2,
	//"left": windowWidth/2-popupWidth/2
	"left": 100
	});
	//only need force for IE6

	jQuery("#backgroundPopup").css({
	"height": windowHeight
	});
}

function centerPopupFlickr(src, title){
	//request data for centering
	jQuery('#popupImage').html("<img src=" + src + " height='315'></img>");
	jQuery('#FlickrTitle').html("<h1>" + title + "</h1>");
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = jQuery("#popupFlickr").height();
	var popupWidth = jQuery("#popupFlickr").width();
	//centering
	jQuery("#popupFlickr").css({
	"position": "absolute",
	"top": jQuery(window).scrollTop() + 40,//windowHeight/2-popupHeight/2,
	//"left": windowWidth/2-popupWidth/2
	"left": 200
	});
	//only need force for IE6

	jQuery("#backgroundPopup").css({
	"height": windowHeight
	});

}
//disabling popup with jQuery magic!
 function disablePopupsms(){
	//disables popup only if it is enabled
	if(popupStatus==1){
	jQuery("#backgroundPopupsms").fadeOut("slow");
	jQuery("#popupContactsms").fadeOut("slow");
	popupStatus = 0;
	}
 }
 
 //centering popup
function centerPopupsms(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = jQuery("#popupContactsms").height();
	var popupWidth = jQuery("#popupContactsms").width();
	//centering
	jQuery("#popupContactsms").css({
	"position": "absolute",
	"top": jQuery(window).scrollTop() + 100,//windowHeight/2-popupHeight/2,
	//"left": windowWidth/2-popupWidth/2
	"left": 315
	});
	//only need force for IE6

	jQuery("#backgroundPopupsms").css({
	"height": windowHeight
	});

} 
 
jQuery(document).ready(function() { 

	//SEND SMS
	jQuery("#smssend").click(function(){
	    sendsms();
	});

	jQuery("#email").click(function(){
	    centerPopup();
	    loadPopup();
	});

	jQuery(".packshot_popup").each(function(i){
        jQuery(this).click(function(){
            var arr = jQuery(this).html().split('"');
			var src = '';
			var title = '';

			for(x=0;x<arr.length;x++){
				if(arr[x].indexOf('src')>0){
					src = arr[x+1];
				}

				if(arr[x].indexOf('alt')>0){
					title = arr[x+1];
				}
			}

			src = src.substring(src.indexOf('file=') + 5, src.length);
			src = src.replace('114','123');

			if(src){
				jQuery('#packshot_popup_img').attr('src', src);
				jQuery('#packshop_popup_text').html(title);
				centerPackshotPopup();
				loadPackshotPopup();
			}
			/*
            var title = jQuery(this).attr("alttext");
            var link = jQuery(this).attr("link");
            src = src.substring(src.indexOf('http'), src.indexOf('jpg') + 3);
            src = src.replace('_m.jpg', '.jpg');
	        centerPopupFlickr(src, title);
	        jQuery('#FlickrDownload').html('300 dpi <a href="' + link + '" target="_blank" class="flickrdownload">Last ned</a>');
	        loadPopupFlickr();        */

        });		
	});
    
	jQuery(".flickr").each(function(i){
        jQuery(this).click(function(){
            var src = jQuery(this).html();
            var title = jQuery(this).attr("alttext");
            var link = jQuery(this).attr("link");
            src = src.substring(src.indexOf('http'), src.indexOf('jpg') + 3);
            src = src.replace('_m.jpg', '.jpg');
	        centerPopupFlickr(src, title);
	        jQuery('#FlickrDownload').html('300 dpi <a href="' + link + '" target="_blank" class="flickrdownload">Last ned</a>');
	        loadPopupFlickr();        
        });
	});    

    jQuery("#closeFlickr").click(function(){
        disablePopup();
    });
    
    jQuery("#popupContactClose").click(function(){
        disablePopup();
    });

    jQuery("#popupPackshot_close").click(function(){
        disablePopup();
    });

    jQuery("#backgroundPopup").click(function(){
        disablePopup();
    });

	jQuery("#sms").click(function(){
	    centerPopupsms();
	    loadPopupsms();
	});
	
    jQuery("#popupContactClosesms").click(function(){
        disablePopupsms();
    });

    jQuery("#backgroundPopupsms").click(function(){
        disablePopupsms();
    });
    
	jQuery(".rec_cont table").each(function(i){
		if(i==0 && jQuery(this).attr('id') != 'noformat'){
			jQuery(this).addClass("nice");
		}
	});
	
	jQuery(".nice tr:even td").each(function(i){
	if(i==0){
		jQuery(this).addClass("rec_header");
	    }
	});

	jQuery(".nice tr:odd td").each(function(i){
		if(jQuery(this).html().indexOf('FYLL')> 0 || jQuery(this).html().indexOf('SAUS')> 0 || jQuery(this).html().indexOf('DEIG')> 0){
			jQuery(this).addClass("rec_subheading");
		} else {
			jQuery(this).addClass("rec_odd");
		}
	});
	
	jQuery(".nice tr:even td").each(function(i){
		if(i!=0){
		if(jQuery(this).html().indexOf('FYLL')> 0 || jQuery(this).html().indexOf('SAUS')> 0 || jQuery(this).html().indexOf('DEIG')> 0){
				jQuery(this).addClass("rec_subheading");
			} else {
				jQuery(this).addClass("rec_even");
			}
		} 
	});

	jQuery(".nice tr").each(function(i){
	 var n = jQuery(".nice tr").length;
		if(i==n-1){
		    //jQuery(this).addClass("rec_footer");

		    var HTML = jQuery('.rec_cont table').html();

		    HTML = HTML.replace("</tbody>", "");

		    HTML += "<tr><td class='rec_footer'>&#160;</td></tr></tbody>";
		    jQuery('.rec_cont table').html(HTML);
	    }
		
	});

});

function submit_newsletter(){
    if(!isValidEmail(document.NEWSLETTER.EMAIL.value)){
        alert('Vennligst fyll inn en gyldig epost adresse.');
    } else {
        document.NEWSLETTER.submit();
    }
}

function signoff_single_letter(){
    if(!isValidEmail(document.NEWSLETTER.EMAIL.value)){
        alert('Vennligst fyll inn en gyldig epost adresse.');
    } else {
        document.NEWSLETTER.CMD.value = 'SIGNOFF_SINGLE';
        document.NEWSLETTER.submit();
    }
}

function isValidEmail(str) {
   return (str.indexOf(".") >= 1) && (str.indexOf("@") > 0);
}

function submit_multi_newsletter(){
    var err = '';
    var checked = false;
    
    if(!isValidEmail(document.NEWSLETTER.EMAIL.value)){
        err = 'Vennligst fyll inn en gyldig epost adresse.\n';
    }
    
    for(i=0;i<=document.NEWSLETTER.NEWSLETTER_ID.length - 1;i++){
        if(document.NEWSLETTER.NEWSLETTER_ID[i].checked == true){
            checked = true;
        }
    }
    if(checked == false){
        err = err + 'Ingen nyhetsbrev er valgt.';
    }
    
    if(err != ''){
        alert(err);
        return false;
    }
    
    document.NEWSLETTER.submit();
    
    return;
    
}

function signoff_multi_letter(){
    var err = '';
    var checked = false;
    
    if(!isValidEmail(document.NEWSLETTER.EMAIL.value)){
        err = 'Vennligst fyll inn en gyldig epost adresse.\n';
    }
    
    for(i=0;i<=document.NEWSLETTER.NEWSLETTER_ID.length - 1;i++){
        if(document.NEWSLETTER.NEWSLETTER_ID[i].checked == true){
            checked = true;
        }
    }
    if(checked == false){
        err = err + 'Ingen nyhetsbrev er valgt.';
    }
    
    if(err != ''){
        alert(err);
        return false;
    }
    
    document.NEWSLETTER.CMD.value = 'SIGNOFF_MULTI';
    document.NEWSLETTER.submit();
    
    return;
}
