﻿//-------------------------------------
// styleFileInput
//-------------------------------------
function styleSelect() {
	var $Select = $(this);
	$Select.change(function(event){
		$(this).attr("class", (this.selectedIndex > 0) ? "selected" : "")
	})
}
//-------------------------------------
// styleFileInput
//-------------------------------------
function styleTextInput() {
	var $TextInput = $(this);
	$TextInput.prev().click(function(){
		$(this).next().focus();
	})
	$TextInput.focus(function() {
		$(this).prev().hide()	
	});
	$TextInput.blur(function() {		
		if($(this).val() == ""){  
			$(this).prev().show()
		}
	});
	$TextInput.map(function(){
		if($(this).val() != ""){  
			$(this).prev().hide()
		}
	});
}

//-------------------------------------
// styleFormFields
//-------------------------------------
function styleFormFields() {
    $("input:text, input:password, textarea").each(styleTextInput);
	$("select").each(styleSelect);
}
//-------------------------------------
// styleFormFields
//-------------------------------------
function setContactEvents(){
	var $Contact = $("#Contact");
	if($Contact.length > 0){
		var $Input = $('input',  $Contact);	
		displayFeedback($Contact);
		var $Button = $("a.whiteButton",  $Contact);
		$Button.map(function(){
			$(this).attr("mshref",$(this).attr("href").split("javascript:")[1]);
			$(this).attr("href","javascript:void(0)")
		});
		$Button.click(function(event) {
			checkAndSubmitLoginForm($Contact, $(this).attr("mshref"));
			return false;
		});
		
		$('select option:first-child',  $Contact).addClass("first");
	}
}
//-------------------------------------
// styleFormFields
//-------------------------------------
function setNewsletterEvents(){
	var $NewsletterSignup = $("#NewsletterSignup");
	if($NewsletterSignup.length > 0){
		var $Input = $('input',  $NewsletterSignup);
		displayFeedback($NewsletterSignup);
		
		var $Button = $("a.whiteButton",  $NewsletterSignup);
		$("a.whiteButton",  $NewsletterSignup).click(function(event) {
			checkAndSubmitLoginForm($NewsletterSignup, subscribeNewsletter);
			return false;
		});
		$("a.blackLink",  $NewsletterSignup).click(function(event) {
			checkAndSubmitLoginForm($NewsletterSignup, unsubscribeNewsletter);
			return false;
		});
	}
}
function subscribeNewsletter(){
	submitNewsletter.call(this, 'subscribe')
}
function unsubscribeNewsletter(){
	submitNewsletter.call(this, 'unsubscribe')
}
function submitNewsletter(action){
	$Container = $(this);
	var $Input = $('input', $Container);	
	displayFeedback($Container, ".loading");
	$('.form', $Container).hide();
	$.ajax({
		type: "GET",
		url: URL + "i2m/newsletter.ashx",
		data: {
			action: action,
			mail: $Input.val()
		},
		dataType: "xml",
		success: function(returnData){
			if($("success", returnData).text() == 'true'){
				$('.form', $Container).hide();				
				$('.success-text', $Container).text($("message", returnData).text())
				$('<a href="#">Tilbage</a>').appendTo('.success-text', $Container).addClass("blackLink").css("padding","10px 0").click(function(){
					displayFeedback($Container);
					$('.form', $Container).show();
					return false;
				});
				displayFeedback($Container, ".success-text");
			}else {
				$('.form', $Container).show();
				$('.error-text', $Container).text($("message", returnData).text())
				displayFeedback($Container, ".error-text");
				displayError($Input);
			}			
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){
			$('.form', $Container).show();
			displayFeedback($Container, ".error-text");
			displayError($Input);
		}
	});
}
//-------------------------------------
// setProaktivITetEvents
//-------------------------------------
function setProaktivITetEvents(){
	var $ProaktivITet = $("#ProaktivITet");
	if($ProaktivITet.length > 0){
		var $Input = $('input',  $ProaktivITet);
		displayFeedback($ProaktivITet);	
		var $Button = $("a.whiteButton",  $ProaktivITet);
		
		$Button.map(function(){
			$(this).attr("mshref",$(this).attr("href").split("javascript:")[1]);
			$(this).attr("href","javascript:void(0)")
		});
		$Button.click(function(event) {
			checkAndSubmitLoginForm($ProaktivITet, function(){
				$(this).parents("form").attr("action", "https://www.proaktivitet.dk/login_do.php")[0].submit()
			});
			return false;
		});
		
	}
}
//-------------------------------------
// displayError
//-------------------------------------
function displayFeedback($Container, msg){
		var $FeedBack = $('.feedback', $Container);		
		$('div',  $FeedBack).hide();
		if (msg) {
			$FeedBack.show();
			$(msg, $Container).show();
		} else {
			$FeedBack.hide();
		}
}
//-------------------------------------
// Check and submit form
//-------------------------------------
function checkAndSubmitLoginForm($Container, submitFunction) {
	var errorCount = 0;		
	var $TextInputs = $("input:text, input:password, textarea", $Container);
	displayFeedback($Container)
	$TextInputs.each( function(index) {
		$This = $(this);
		if($This.prev().children("em").length > 0){
			if(checkForm($This)){			
				errorCount+=1;				
				displayError(this);
			}	else {
				displayError(this, true);				
			}			
		}		
	});
	
	if(errorCount>0){
		displayFeedback($Container, ".error-text")
	} else {
		if(jQuery.isFunction(submitFunction)){
			submitFunction.call($Container)
		} else {
			eval(submitFunction);
		}		
	}	
}
function displayError(input, reset){
	if (reset) {
		$(input).parent().removeClass("error");
	} else {
		$(input).parent().addClass("error");
	}
}
//-------------------------------------
// checkForm
//-------------------------------------
function checkForm($Input) {
	if ($Input.val() == "" || $Input.val() == $Input[0].defaultValue) {
		return true;
	}
	if ($Input.attr("id").toLowerCase().indexOf("email") > -1) {
		if (checkEmail($Input.val())) {
			return true;
		}
	}
	if ($Input.attr("id").toLowerCase().indexOf("phone") > -1) {
		if (checkTelephoneNumber($Input.val())) {
			return true;
		}
	}
	return false;
}
//-------------------------------------
// checkForm
//-------------------------------------
function checkEmail(txt) {
	var RegExFilter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (RegExFilter.test(txt)) {
		if (txt.length >= 7) {
			if (txt.indexOf("@") >= 0) {
				if ((txt.indexOf("@") + 2) < txt.lastIndexOf(".")) {
					if (txt.lastIndexOf(".") < (txt.length - 2)) {
						return false;
					}
				}
			}
		}
	}
	return true;
}

//-------------------------------------
// formatTelephoneNumber
//-------------------------------------
function formatTelephoneNumber(number) {
	return number.split(" ").join("").split(".").join("").split("+").join("00")
}
//-------------------------------------
// checkForm
//-------------------------------------
function checkTelephoneNumber(number) {
	var tempNumber = formatTelephoneNumber(number);
	if (!isNaN(tempNumber) && tempNumber.toString().length > 7 && tempNumber.toString().length < 13) {
		return false;
	}
	return true;
}
//-------------------------------------
// Run form scripts
//-------------------------------------
function runFormScripts(){
	styleFormFields();
	setContactEvents();
	setNewsletterEvents();
	setProaktivITetEvents();
}
