var eleOffset;
var plink;
var eleWidth;
$(function(){

	// level the left and right column //
	if($("#templateid").attr("value") == 10 || $("#templateid").attr("value") == 11 || $("#templateid").attr("value") == 12 || $("#templateid").attr("value") == 19 || $("#templateid").attr("value") == 15 ){

		if($("#rightcolumn").height() > $("#leftcolumn").height()){
			$("#leftcolumn").css({height:  $("#rightcolumn").height() - ($("#leftcolumntop").height() + 20)});
		}else if(($("#leftcolumn").height() > $("#rightcolumn").height())){
			$("#rightcolumn").css({height:  $("#leftcolumn").height()-20});
		}

	}
	else if( $("#templateid").attr("value") == 24 ){

		if($("#rightcolumn").height() > $("#leftcolumn").height()){
			$("#leftcolumn").css({height:  $("#rightcolumn").height() - ($("#leftcolumntop").height() + 20)});
		}else if(($("#leftcolumn").height() > $("#rightcolumn").height())){
			$("#rightcolumn").css({height:  $("#leftcolumn").height() - 40});
		}

	}
	else if($("#templateid").attr("value") == 9 || $("#templateid").attr("value") == 8 || $("#templateid").attr("value") == 7){
		
		if($("#rightcolumn_transp").height() > $("#leftcolumn").height()){
			$("#leftcolumn").css({height:  $("#rightcolumn_transp").height()-56});
		}
	}
	

	var cpos, spos, lpos; 
	
	$('#country').mouseover(function(){
		cpos = $('#country').offset();
		$('#countryselect').css({top: cpos.top + 19, left: cpos.left});
		$('#countryselect').show(); 
		$('#searchbox,#lanuageselect').hide();
	});
	$('#search').mouseover(function(){
		var spos = $('#search').offset();
		$('#searchbox').css({top: spos.top + 19, left: spos.left - 85});
		$('#searchbox').show();
		$('#countryselect,#languageselect').hide();
	});
	$('#language').mouseover(function(){
		var lpos = $('#language').offset();
		$('#languageselect').css({top: lpos.top + 19, left: lpos.left -93});
		$('#languageselect').show(); 
		$('#searchbox,#countryselect').hide();
	});

	// Standard form submit
	$("form[id^='FORM_']").submit(function(){
		var values = $(this).serialize();
		
		$("#overlay").fadeIn('fast', function(){
			$(this).css("filter","alpha(opacity=30)");
		});
			
		$("#confirmation_std").show('slow', function(){
			$(this).load("/trms-content/ajax/standard_form.php", values);
		});
		return false;
	});

	$("#editform").live("click", function(){
		$("#confirmation_std").hide('slow', function(){
			$(this).html("");
			$("#action").val("");
		});
		$("#overlay").fadeOut();
	});

	$("#formsubmit").live("click", function(){
		$("#action").val("saveAnswer");
		var thisform  = $("#formhandle").val();

		var values = $("#" + thisform).serialize();
		$("#confirmation_std").load("/trms-content/ajax/standard_form.php", values);
	});

	$("#closeconf").live("click", function(){
		$("#confirmation_std").hide('slow', function(){
			$(this).html("");
			$("#action").val("");
		});
		$("#overlay").fadeOut();

		var thisform  = $("#formhandle").val();
		 $("#" + thisform)[0].reset();
	});

	// Nominationform  ////////////////////////////////////////////////
	
	$("#next_btn,#prev_btn,#completeform").live('click', function(event){
		var errors = 0;
		var btnerrors = 0;
		
		if($(this).attr("id") == "next_btn"){
			
			$("#action").val("nextpage");
			
			$('label').each(function(index) {

				if( $(this).text().indexOf('*') > -1 && $("#" + $(this).attr("for")).val() == "" ){
					$(this).css("background-color", "#FFCC00");
					errors++;
				}
				
				if( $(this).text().indexOf('*') > -1 && $("#" + $(this).attr("for")).is("input:radio") == true){
					btnerrors = 1;
					$("input:radio").each(function(index){ 
						if($(this).is(":checked") == true)btnerrors = 0; 
					});
					if(btnerrors > 0) $(this).css("background-color", "#FFCC00");
				}
			});
		
		}else if($(this).attr("id") == "prev_btn")
			$("#action").val("prevpage");

		if( errors > 0 || btnerrors > 0){
			alert("Please fill out the required fields marked width *");
			return;
		}else{
			if( $(this).attr("id") == "completeform"){ 
				$("#action").val("complete");
				var values = $("form[id^='NOMINATE_FORM_']").serialize();
				
				$("#overlay").fadeIn('fast', function(){
					$(this).css("filter","alpha(opacity=30)");
				});
					
				$("#confirmation_std").show('slow', function(){
					$(this).load("/trms-content/wcp/ajax/nomination_form.php", values);
				});

			}else{
				var values = $("form[id^='NOMINATE_FORM_']").serialize();
				$(".formblock").load("/trms-content/wcp/ajax/nomination_form.php", values);
			}
		}
	});

	$("#editnominationform").live("click", function(){
		$("#confirmation_std").hide('slow', function(){
			$(this).html("");
			$("#action").val("");
		});
		$("#overlay").fadeOut();
	});

	$("#submitnominationform").live("click", function(){
		$("#action").val("confirm");
		var values = $("form[id^='NOMINATE_FORM_']").serialize();
		
		$("#confirmation_std").load("/trms-content/wcp/ajax/nomination_form.php", values);

		 $('html').animate({scrollTop:0}, 'fast');
	});

	$("#closenomconf").live("click", function(){
		$(".formblock").load("/trms-content/wcp/ajax/nomination_form.php", {action: "nominationcomplete"});
		$("#confirmation_std").hide('slow', function(){
			$(this).html("");
		});
		$("#overlay").fadeOut();
		
	});

	/// Login  ////////////////////////////////////////////////////////

	$("#foot_right").click(function(){

		$("#overlay").fadeIn('fast', function(){
			$(this).css("filter","alpha(opacity=30)");
		});
		var str = $(this).text();

		if( str.search(/login/i) > -1 ){
			
			$("#confirmation_std").show('slow', function(){
				$(this).load("/trms-content/ajax/login.php", {action: "login", cid: $("#cid").val()});
				$('html').animate({scrollTop:0}, 'fast');
			});

		}else{

			$("#confirmation_std").show('slow', function(){
				$(this).load("/trms-content/ajax/login.php", {action: "logout", cid: $("#cid").val()});
				$('html').animate({scrollTop:0}, 'fast');
			});
		}
	});

	$("#login_btn").live("click", function(){
		var values = $("#logon").serialize();

		//alert(values);
		
		$("#confirmation_std").load('/trms-content/ajax/logincheck.php?', values);
		return false;
	})

	$("#logout_btn").live("click", function(){
		var values = $("#logon").serialize();
		
		$("#confirmation_std").load('/trms-content/ajax/logincheck.php?', values);
		return false;
	})

	$("#closelogin").live('click', function(){
		$("#confirmation_std").hide('slow', function(){
			$(this).html("");
		});
		$("#overlay").fadeOut();
	})


	// Language change ////////////////////////////////////////////////
	$("#languageid").change(function(){
		window.location.href = "/trms-admin/setlanguage.php?languageid=" + $(this).val() + "&returnpath=" + location.href;
	});
	
	$('#content,#lefthead,#menu,#subhead,#content,.lnks').mouseover(function(){
		$('#countryselect,#searchbox,#languageselect').hide();
	})

	$('.anythingSlider,.contentSlider').anythingSlider({
			easing: "easeInOutExpo",        // Anything other than "linear" or "swing" requires the easing plugin
			autoPlay: true,                 // This turns off the entire FUNCTIONALY, not just if it starts running or not.
			startStopped: false,            // If autoPlay is on, this can force it to start stopped
			delay: 5000,                    // How long between slide transitions in AutoPlay mode
			animationTime: 1000,             // How long the slide transition takes
			hashTags: true,                 // Should links change the hashtag in the URL?
			buildNavigation: true,          // If true, builds and list of anchor links to link to each slide
			pauseOnHover: true,             // If true, and autoPlay is enabled, the show will pause on hover
			startText: "<img src='/trms-admin/images/play.png' alt='Play'/>",                // Start text
			stopText: "<img src='/trms-admin/images/pause.png' alt='Pause'/>",               // Stop text
			navigationFormatter: formatText // Details at the top of the file on this use (advanced use)
	});

	//$('.contentSlider').data('AnythingSlider').startStop(true); 
            
    //$('.anythingSlider,.contentSlider').anythingSlider(1);
			
	$("#slide-jump").click(function(){
		$('.anythingSlider,.contentSlider').anythingSlider(1);
	});
	
	// used by template List Laurate or Countries
	var hoverIntentConfig = {    
     over: showBox, // function = onMouseOver callback (REQUIRED)    
     timeout: 200,// number = milliseconds delay before onMouseOut 
	 sensitivity: 10,
	 interval: 500,
     out: hideBox // function = onMouseOut callback (REQUIRED)    
	};
	
	
	$(".listpage > li a").mouseover(
		function(){
		eleOffset = $(this).offset();
		plink = $(this).attr("href");
		eleWidth = $(this).width();
		
		}
	).hoverIntent(hoverIntentConfig, eleOffset, plink, eleWidth);

	$("#pressphotocategoryid").live("change", function(){
		$("#pressdownloads").load('/trms-content/wcp/ajax/pressphotos_get.php', {pressphotocategoryid: $(this).val()} );
	});

	$("#subscribe-btn").click(function(){

		$("#subscribe").load('/trms-content/wcp/ajax/subscribe.php', {emailaddress: $("#emailaddress").val()})
	
		//alert($("#emailaddress").val());
	});

	/**
	 * Discussions 
	 */

	 $("#commenticon").click(function(){
		$("#commenttext").val("");
		$("#signature").val("");
		$("#discussionconfirm").fadeOut("slow"); 
		$("#discussioninput").toggle("fast");
	 });

	 $("#sendcomment").click(function(){
		if( $("#commenttext").val() != "" && $("#signature").val() != ""){
			$("#discussionlist").load("/trms-content/ajax/discussion_edit.php", {action: "savecomment", cid: $("#cid").val(), commenttext: $("#commenttext").val(), signature: $("#signature").val()})
			$("#discussioninput").fadeOut("slow", function(){
				$("#discussionconfirm").fadeIn("slow"); 
			}); 
			
		}else{
			alert("Please fill out!");
		}
	});

	/**
	 *	Indiskt språkval
	 */
	
	 $("#indianchoice").submit(function(){
		 var values = $(this).serialize();
		$("#langselect").slideUp();
		window.location.href = "/trms-admin/setlanguage.php?" + values + "&returnpath=" + location.href + "&indianchoice=1";
		return false;
	 });
});

// used by template List Laurate or Countries
function showBox(){
	$("#infobox").css({top:eleOffset.top -55, left: eleOffset.left + eleWidth + 15}).text(
		function(){
			$(this).load('/trms-content/wcp/ajax/content_get.php', {plink: plink})
		}
	).fadeIn("slow");
}

function hideBox(){
	$("#infobox").text("").fadeOut("slow");
}

// anythingSlider uses this
function formatText(index, panel) {
	  return index + "";
}

function checkDonationForm(formname){
	theForm = document.forms[formname];
	optionChecked = 0;

	str = "Input is missing for the field/s: " 
	for(i=0; i< theForm.elements.length; i++){
		if( theForm.elements[i].name.indexOf("required") != -1 && theForm.elements[i].value == ""){
			str = str + theForm.elements[i].name.substring(0, theForm.elements[i].name.indexOf("_")) +", ";
			document.getElementById(theForm.elements[i].name).style.background = "#FFCC00";
			}
	}

	// testa om Donation_to är bockad
	for(i=0;i<theForm.Donation_to.length;i++){ if(theForm.Donation_to[i].checked) optionChecked++;}
	if(optionChecked == 0){str = str + ", Donation_to"; document.getElementById("donFormPart2").style.background = "#FFCC00";} optionChecked = 0;
	
	// testa om Donation_type är bockad
	for(i=0;i<theForm.Donation_type.length;i++){ if(theForm.Donation_type[i].checked) optionChecked++;}
	if(optionChecked == 0){str = str + ", Donation_type"; document.getElementById("donFormPart3").style.background = "#FFCC00";} optionChecked = 0;

	// testa om Donation_type är bockad
	for(i=0;i<theForm.Donation_type.length;i++){ if(theForm.Donation_type[i].checked) optionChecked++;}
	if(optionChecked == 0){str = str + ", Donation_type"; document.getElementById("donFormPart3").style.background = "#FFCC00";} optionChecked = 0;

	// testa om Donation_amount är bockad
	for(i=0;i<theForm.Donation_amount.length;i++){ if(theForm.Donation_amount[i].checked) optionChecked++;}
	if(optionChecked == 0){str = str + ", Donation_amount"; document.getElementById("donFormPart4").style.background = "#FFCC00";} optionChecked = 0;

	// testa om Payment_type är bockad
	for(i=0;i<theForm.Payment_type.length;i++){ if(theForm.Payment_type[i].checked) optionChecked++;}
	if(optionChecked == 0){str = str + ", Payment_type"; document.getElementById("formPart7").style.background = "#FFCC00";} optionChecked = 0;

	if(str.length > 34){
		//alert(str);
		alert("Formuläret är ej komplett ifyllt!\nV.v. kontrollera fälten med asterixer och de orangemarkerade fälten.")
		return false;
	}
	
	sendDonationForm();
}

function sendDonationForm(){
	var amount;
	if(document.forms["donform"].Payment[0].checked){
			
		
		for(i=0;i<document.forms["donform"].Donation_amount.length;i++){
			if(document.forms["donform"].Donation_amount[i].checked) amount = document.forms["donform"].Donation_amount[i].value;
		}

		//alert(amount);
		if(amount == "Optional") amount = document.forms["donform"].Optional_amount.value;

		if(isNumeric(amount) && amount != "" && amount != "0"){

			//document.forms["donform"].submit();

			window.open("/trms-content/wcp/pay/check.php?Amount=" + amount + "&Currency=SEK" + "&OrderReference=" + document.forms["donform"].OrderReference.value + "&code=" + document.forms["donform"].code.value + "&Fullname=" + document.forms["donform"].Fullname_required.value + "&Address=" + document.forms["donform"].Address_required.value + "&City=" + document.forms["donform"].City_required.value + "&Country=" + document.forms["donform"].Country_required.value,"PAYMENT", "scrollbars=yes,resizable=yes,status=no,toolbar=no,menubar=no,location=no,height=425,width=650");
			
			
			//window.open("/trms-content/wcp/pay/payment.php?Amount=" + amount + "&Currency=SEK" + "&OrderReference=" + document.forms["donform"].OrderReference.value,"PAYMENT", "scrollbars=yes,resizable=yes,status=yes,toolbar=yes,menubar=yes,location=yes,height=425,width=650")
		}
		else if(amount == "") 
			alert("Belopp saknas!");
		else if(amount == "0") 
			alert("Belopp är 0!");
		else
			alert(amount + "\nEj korrekt belopp!\nV.v. kontrollera att det inte finns några bokstäver,\n mellanslag eller andra tecken än siffror i fältet för \nannat belopp.");
	}
	else{
		document.forms["donform"].submit();
	}
}

function checkDonationFormENG(formname){
	theForm = document.forms[formname];
	optionChecked = 0;

	str = "Input is missing for the field/s: " 
	for(i=0; i< theForm.elements.length; i++){
		if( theForm.elements[i].name.indexOf("required") != -1 && theForm.elements[i].value == ""){
			str = str + theForm.elements[i].name.substring(0, theForm.elements[i].name.indexOf("_")) +", ";
			document.getElementById(theForm.elements[i].name).style.background = "#FFCC00";
			}
	}

	// testa om Donation_to är bockad
	for(i=0;i<theForm.Donation_to.length;i++){ if(theForm.Donation_to[i].checked) optionChecked++;}
	if(optionChecked == 0){str = str + ", Donation_to"; document.getElementById("donFormPart2").style.background = "#FFCC00";} optionChecked = 0;
	
	// testa om Donation_type är bockad
	for(i=0;i<theForm.Donation_type.length;i++){ if(theForm.Donation_type[i].checked) optionChecked++;}
	if(optionChecked == 0){str = str + ", Donation_type"; document.getElementById("donFormPart3").style.background = "#FFCC00";} optionChecked = 0;

	// testa om Donation_type är bockad
	for(i=0;i<theForm.Donation_type.length;i++){ if(theForm.Donation_type[i].checked) optionChecked++;}
	if(optionChecked == 0){str = str + ", Donation_type"; document.getElementById("donFormPart3").style.background = "#FFCC00";} optionChecked = 0;

	// testa om Donation_amount är bockad
	for(i=0;i<theForm.Donation_amount.length;i++){ if(theForm.Donation_amount[i].checked) optionChecked++;}
	if(optionChecked == 0){str = str + ", Donation_amount"; document.getElementById("donFormPart4").style.background = "#FFCC00";} optionChecked = 0;

	// testa om Payment_type är bockad
	for(i=0;i<theForm.Payment_type.length;i++){ if(theForm.Payment_type[i].checked) optionChecked++;}
	if(optionChecked == 0){str = str + ", Payment_type"; document.getElementById("formPart7").style.background = "#FFCC00";} optionChecked = 0;

	if(str.length > 34){
		//alert(str);
		alert("This form is not complete\nPlease control the fields marked with orange.")
		return false;
	}
	
	sendDonationFormENG();
	//alert("ok letssubmit");
}

function sendDonationFormENG(){
	var amount;
	if(document.forms["donform"].Payment[0].checked){
		
		for(i=0;i<document.forms["donform"].Donation_amount.length;i++){
			if(document.forms["donform"].Donation_amount[i].checked){ amount = document.forms["donform"].Donation_amount[i].value;}
		}

		if(amount == "Optional") amount = document.forms["donform"].Optional_amount.value;

		if(isNumeric(amount) && amount != "" && amount != "0"){
				//document.forms["donform"].submit();
				//window.open("/trms-content/wcp/pay/payment.php?Amount=" + amount + "&Currency=USD" + "&OrderReference=" + document.forms["donform"].OrderReference.value,"PAYMENT", "scrollbars=yes,resizable=yes,status=no,toolbar=no,menubar=no,location=no,height=425,width=650")
				//window.open("/trms-content/wcp/pay/check.php?Amount=" + amount + "&Currency=USD" + "&OrderReference=" + document.forms["donform"].OrderReference.value + "&code=" + document.forms["donform"].code.value,"PAYMENT", "scrollbars=yes,resizable=yes,status=no,toolbar=no,menubar=no,location=no,height=425,width=650");

				window.open("/trms-content/wcp/pay/check.php?Amount=" + amount + "&Currency=USD" + "&OrderReference=" + document.forms["donform"].OrderReference.value + "&code=" + document.forms["donform"].code.value + "&Fullname=" + document.forms["donform"].Fullname_required.value + "&Address=" + document.forms["donform"].Address_required.value + "&City=" + document.forms["donform"].City_required.value + "&Country=" + document.forms["donform"].Country_required.value,"PAYMENT", "scrollbars=yes,resizable=yes,status=no,toolbar=no,menubar=no,location=no,height=425,width=650");
		}
		else if(amount == "") 
			alert("Amount is missing!");
		else if(amount == "0") 
			alert("Amount is 0!");
		else
			alert(amount + "\nNot correct amount!\nPlease control that there is no letters,\n spaces or other characters than digits in the field for \nother amount.");
	}
	else{
		document.forms["donform"].submit();
	}
}

function resetColor(id){
	document.getElementById(id).style.background = "#FFFFFF";
}

function resetColorInput(id){
	document.getElementById(id).style.background = "#FFFFFF";
	document.getElementById(id).style.border = "1px solid #DDDDDD";
}

function isNumeric(value) {
  if (value == null || !value.toString().match(/^[-]?\d*\.?\d*$/)) return false;
  return true;
}

function checkTicketPaymentForm(formname, lang){
	theForm = document.forms[formname];
	optionChecked = 0;

	str = "Input is missing for the field/s: " ;

	for(i=0; i< theForm.elements.length; i++){
		
		if( theForm.elements[i].name.indexOf("required") != -1 && (theForm.elements[i].value == "" || theForm.elements[i].value == 0)){
			str = str + theForm.elements[i].name.substring(0, theForm.elements[i].name.indexOf("_")) +", ";
			document.getElementById(theForm.elements[i].name).style.background = "#FFCC00";
			}
	}

	/*testa om Tickcettype är bockad
	for(i=0;i<theForm.tickettype.length;i++){ if(theForm.tickettype[i].checked) optionChecked++;}
	if(optionChecked == 0){str = str + ", Ticket_type"; document.getElementById("TicketForm2").style.background = "#FFCC00";} optionChecked = 0;
	*/
	
	if(str.length > 34){
		//alert(str);
		alert("This form is not complete\nPlease control the fields marked with orange.")
		return false;
	}

	sendTicketPaymentForm(lang);
}

function sendTicketPaymentForm(lang){
	var amount;
	
	amount = document.forms["ticketform"].Total_required.value;

	document.forms["ticketform"].submit();

	if(lang == "en"){
		window.open("/trms-content/wcp/pay/ticket_payment.php?Amount=" + amount + "&Currency=USD" + "&OrderReference=" + document.forms["ticketform"].OrderReference.value,"PAYMENT", "scrollbars=yes,resizable=yes,status=yes,toolbar=yes,menubar=yes,location=yes,height=425,width=650");
	}
	else if (lang == "sv"){
		window.open("/trms-content/wcp/pay/ticket_payment.php?Amount=" + amount + "&Currency=SEK" + "&OrderReference=" + document.forms["ticketform"].OrderReference.value,"PAYMENT", "scrollbars=yes,resizable=yes,status=yes,toolbar=yes,menubar=yes,location=yes,height=425,width=650");
	}
}

function checkForm(formname, lang){
	theForm = document.forms[formname];
	optionChecked = 0;

	str = "Input is missing for the field/s: " ;

	for(i=0; i< theForm.elements.length; i++){
		if(theForm.elements[i].name.indexOf("Total_required") > -1)continue;
		if( theForm.elements[i].name.indexOf("required") != -1 && (theForm.elements[i].value == "" || theForm.elements[i].value == 0)){
			str = str + theForm.elements[i].name.substring(0, theForm.elements[i].name.indexOf("_")) +", ";
			document.getElementById(theForm.elements[i].name).style.background = "#FFCC00";
			}
	}

	/*testa om Tickcettype är bockad
	for(i=0;i<theForm.tickettype.length;i++){ if(theForm.tickettype[i].checked) optionChecked++;}
	if(optionChecked == 0){str = str + ", Ticket_type"; document.getElementById("TicketForm2").style.background = "#FFCC00";} optionChecked = 0;
	*/
	
	if(str.length > 34){
		//alert(str);
		alert("This form is not complete\nPlease control the fields marked with orange.")
		return false;
	}else theForm.submit();
}


function testTicketPaymentForm(lang){
	var amount;
	
	amount = document.forms["ticketform"].total_required.value;

	if(lang == "en"){
		window.open("/pay/ticket_finished_test.php?Amount=" + amount + "&Currency=USD" + "&OrderReference=" + document.forms["ticketform"].OrderReference.value,"PAYMENT", "scrollbars=yes,resizable=yes,status=yes,toolbar=yes,menubar=yes,location=yes,height=425,width=650");
	}
	else if (lang == "sv"){
		window.open("/pay/ticket_finished_test.php?Amount=" + amount + "&Currency=SEK" + "&OrderReference=" + document.forms["ticketform"].OrderReference.value,"PAYMENT", "scrollbars=yes,resizable=yes,status=yes,toolbar=yes,menubar=yes,location=yes,height=425,width=650");
	}
}


