(function($){
	$.fn.loadFields = function(option){
		$("#signupForm > p").hide();
		
		var getEmailSubscription = function(){
			var optOut = ($("#unsubscribe").is(":checked")) ? 1 : 0;
			$("#emailOptOut").val(optOut);
			
			if(optOut == 0) {
				var txt = "";
				$("input.newsletter[type=checkbox]:checked").each(function(){
					txt += ($(this).attr("id") == "corporate") ? "Corp; " : "";	
					txt += ($(this).attr("id") == "business") ? "NBS; " : "";
					txt += ($(this).attr("id") == "entertainment") ? "NES; " : "";
					txt += ($(this).attr("id") == "merchant") ? "NMS; " : "";
					txt += ($(this).attr("id") == "press") ? "Press; " : "";
				});
				
				$("#00N20000001nl3E").val(txt);
			}
		};
		
		for(key in option.fields)
		{
			var selector = $('#'+option.fields[key]);
			
			if(selector.length > 0) {
				if(option.fields[key] == "newsletterBox") {
					$(this).find("input.newsletter").each(function(){	
						$(this).click(function(){
											   
							if($(this).attr("id") == "unsubscribe")					
								$("input.newsletter[type=checkbox]").attr("checked", false);					
							else if($(this).attr("id") == "subscribe")					
								$("input.newsletter[type=checkbox]").attr("checked", true);
							
							if($(this).attr("type") == "checkbox") {
								$("#unsubscribe").attr("checked", false);
								$("#subscribe").attr("checked", true);
							}			
						});														 
					});
					
					$(this).find("#subscribe").trigger("click");
					$("#signupForm").submit(function(){  
						getEmailSubscription();						 
						return true;
					});
					
					selector.show();
				}
				else if (option.fields[key] == "comments") { 
					$("#comments").textlimit("span.counter", 300); 
					selector.parent("p").show(); 
				}
				else {	
					var param = $(document).getUrlParam(option.fields[key]);
					if(param != null) selector.val(param.replace(/\%20/g," "));
					
					selector.parent("p").show(); 
				}
				
			}
		}
		
		$(".required").each(function(){ 
			if($(this).parent("p").css("display") == "none") 
				$(this).removeClass("required");	
		});
		
		$("#signupForm > p:hidden, #signupForm > div:hidden").remove();
		
		if(option.salesforceId != undefined) {
			$.ajax({ url: "../template/formular/salesforce/" + option.salesforceId + ".htm", success: function(data){ $("#signupForm").append(data); } });

		}
	};
	
	$.fn.getContactForm = function(){
	
		var contactForm = new Object();
		contactForm.Title		= $(this).find("#salutation").val();
		contactForm.FirstName 	= $(this).find("#firstname").val();
		contactForm.LastName 	= $(this).find("#lastname").val();
		contactForm.Email 		= $(this).find("#email").val();
		contactForm.Country		= $(this).find("#country").find('option:selected').text();
		contactForm.Market		= $(this).find("#market").val();
		contactForm.Company		= $(this).find("#company").val();
		contactForm.JobTitle	= $(this).find("#title").val();
		contactForm.Phone		= $(this).find("#phone").val();
		contactForm.Mobile		= $(this).find("#mobile").val();
		
		return contactForm;
	}
	
	$.fn.total = function (){
		var shipping = 0;
		var unitPrice = parseFloat($(this).find("#unit").text());		
		var quantity = parseFloat($(this).find("#quantity").val());
		
		if(quantity == 1) shipping = 13.5;
		else if(quantity == 2) shipping = 23;
		else if(quantity == 3) shipping = 31.5;
		else if(quantity > 3) shipping = 0;
		
		var total = unitPrice*quantity; 
		$(this).find("#unitTotal").text(total);
		$(this).find("#shipping").text((shipping==0)? 'Free' : shipping);
		$(this).find("#total").text(total+shipping);	
	}
	
	
})(jQuery);

function getLegal() {
	var result;
	$.ajax({ async: false, url: "../template/formular/legal.html", success: function(data){ result = data; } });
	
	return result;
}

function getForm() {
	var result;
	$.ajax({ async: false, url: "../template/formular/form.html", success: function(data){ result = data; } });
	
	return result;
}

function sendMail(formContact, async)
{
	var result = false;
	async = (async == undefined) ?  false : true;
	$.ajax({ type: "POST",	dataType: "json",	data: formContact,		async: async,
		url: "../NetsizeWebSite/Mail/Send",
		success: function(data)	{ 
				result = true; 
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) { 
				result = false; 
		}
	});
	
	return result;
}

function support(formContact, async)
{
	var result = false;
	async = (async == undefined) ?  false : true;
	$.ajax({ type: "POST",	dataType: "json",	data: formContact,		async: async,
		url: "../NetsizeWebSite/Mail/Support",
		success: function(data)	{ 
				result = true; 
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) { 
				result = false; 
		}
	});
	
	return result;
}


function getUrlContact(selector)
{
	var salutation = $(selector).getUrlParam("salutation");
	var firstname = $(selector).getUrlParam("firstname");
	var lastname = $(selector).getUrlParam("lastname");
	var email = $(selector).getUrlParam("email");
	var company = $(selector).getUrlParam("company");
	var jobtitle = $(selector).getUrlParam("jobtitle");

	
	var contact = new Object();
	if (salutation != null){ contact.salutation = salutation.replace(/\%20/g," "); }
	if (firstname != null){ contact.firstname = firstname.replace(/\%20/g," "); }
	if (lastname != null){ contact.lastname = lastname.replace(/\%20/g," "); }
	if (email != null){ contact.email = email; }
	if (company != null){ contact.company = company.replace(/\%20/g," "); }
	if (jobtitle != null){ contact.jobtitle = jobtitle.replace(/\%20/g," "); }
	
	
	return contact;
}



//jquery.textlimit.js
(function(jQuery) {
	jQuery.fn.textlimit=function(counter_el, thelimit, speed) {
		var charDelSpeed = speed || 15;
		var toggleCharDel = speed != -1;
		var toggleTrim = true;
		var that = this[0];
		var isCtrl = false; 
		updateCounter();
		
		function updateCounter(){
			if(typeof that == "object")
				jQuery(counter_el).text(thelimit - that.value.length+" characters remaining");
		};
		
		this.keydown (function(e){ 
			if(e.which == 17) isCtrl = true;
			var ctrl_a = (e.which == 65 && isCtrl == true) ? true : false; // detect and allow CTRL + A selects all.
			var ctrl_v = (e.which == 86 && isCtrl == true) ? true : false; // detect and allow CTRL + V paste.
			// 8 is 'backspace' and 46 is 'delete'
			if( this.value.length >= thelimit && e.which != '8' && e.which != '46' && ctrl_a == false && ctrl_v == false)
				e.preventDefault();
		})
		.keyup (function(e){
			updateCounter();
			if(e.which == 17)
				isCtrl=false;

			if( this.value.length >= thelimit && toggleTrim ){
				if(toggleCharDel){
					// first, trim the text a bit so the char trimming won't take forever
					// Also check if there are more than 10 extra chars, then trim. just in case.
					if ( (this.value.length - thelimit) > 10 )
						that.value = that.value.substr(0,thelimit+100);
					var init = setInterval
						( 
							function(){ 
								if( that.value.length <= thelimit ){
									init = clearInterval(init); updateCounter() 
								}
								else{
									// deleting extra chars (one by one)
									that.value = that.value.substring(0,that.value.length-1); jQuery(counter_el).text('Trimming... '+(thelimit - that.value.length));
								}
							} ,charDelSpeed 
						);
				}
				else this.value = that.value.substr(0,thelimit);
			}
		});
		
	};
})(jQuery);


