﻿function initSubscribeForm(){ 
	oSFLink = $("a#subscribeform_button");
	sSFQuery = oSFLink.attr('href');
	oSFContainer = $("#sf_container");
	oSFLoader = $("#sf_loader");
	oSFData = $("#sf_data");
	oSFLink.toggle(
		  function () {
			oSFLink.addClass("active");
			show_sf_popup(oSFContainer,function(){
				// make AjaxReques
				oSFData.html('<img src="/f/1/global/i-wait.gif" alt="" vspace="33" class="i-loading"/>');
				$.ajaxSetup({
					url: sSFQuery,
					cache: false,
					type: "POST"
				});
				$.get(sSFQuery,{jsHttpRequest: "23"},
					function(data){
						oSFLoader.html(data);
						oSFData.animate(
							{ "height" : ""+oSFLoader.height()+"px"	}, 
							{
								duration: 1000, 
								easing: 'easeout', 
								complete: function(){
									oSFLoader.html("");
								}
							}						
						);
						oSFLoader.html("");
						oSFData.html(data);
						$("#subscribe_form").submit(submit_sf_form);
						initSubscribeFormCheck('subscribe_form');
					}
				);
				//
			})
			return false;
		  },
		function () {
			close_sf_popup($(oSFContainer));
			oSFLink.removeClass("active");
			return false;
		}
	);
}
/*
$(document.body).click(
	function(evt){
		documentClick(evt);
	}
);


function documentClick(evt){
	var oTarget = evt.target;
	var oTargetParent = evt.target.parentNode;
	var isPanelClick = false;
	if($(oTarget).hasClass("clicker") || $(oTarget).parents(".clicker").length){
		isPanelClick = true;
	}

	if(isSFOpend && !isPanelClick){
		oSFLink.click();
	}
};
*/

function submit_sf_form(){
	$(this).ajaxSubmit({
		target: oSFLoader,
		beforeSubmit: function(){},
		success: function(data){
			oSFData.html('');
			oSFData.animate(
				{ 
					"height" : ""+oSFLoader.height()+"px"
				}, 
				{
					duration: 1000,
					easing: 'easeout',
					complete: function(){
						oSFLoader.html("");
					}
				}
			);
			oSFData.html(oSFLoader.html());
			oSFLoader.html("");
			$("#subscribe_form").submit(submit_sf_form);
		}
	});
	return false;
}

function show_sf_popup(jPopup,onfinished){
	isSFOpend = true;
	jPopup.fadeIn("fast",onfinished)
}

function close_sf_popup(jPopup,onfinished){
	isSFOpend = false;
	jPopup.fadeOut("fast",
		function(){
			$(this).css("display","none");
			if(onfinished) onfinished();
			
			oSFData.html("");
			oSFData.css("height","auto");
		}
	)
}

// submit disabling and checkbox handling
var eSubscribeSubmit = null;
var iSubscribeCheckboxesMarked = 0;
var isEmailCorrect = false;
function initSubscribeFormCheck(sFormName) {
	var eForm = document.forms[sFormName];
	if (eForm) {
		iSubscribeCheckboxesMarked = 0;
		for (var i = 0; i < eForm.elements.length; i++) {
			if (eForm.elements[i].type == 'checkbox') {
				eForm.elements[i].onclick = checkSubscribeSubmit;
				if (eForm.elements[i].checked) iSubscribeCheckboxesMarked++;
			}
			if(eForm.elements[i].name == 'email'){
				eForm.elements[i].onchange = eForm.elements[i].onkeyup = checkSubscribeSubmit;
			}
			if (eForm.elements[i].type == 'submit') eSubscribeSubmit = eForm.elements[i];
		}
		checkSubscribeSubmit('init');
	}
}
function checkSubscribeSubmit(isInit) {
	if (eSubscribeSubmit) {
		if (!isInit || isInit != 'init') {
			if(this.type == 'checkbox'){
				if (this.checked) iSubscribeCheckboxesMarked++;
				else iSubscribeCheckboxesMarked--;
			}
			else {
				if(this.name=='email' && this.value.match(new RegExp( ".+@.*\\.[A-Za-z]{2,4}" )) ){
					isEmailCorrect = true
				} else {
					isEmailCorrect = false;
				}
			}
		}
		eSubscribeSubmit.disabled = !(isEmailCorrect && (iSubscribeCheckboxesMarked > 0));
	}
}


function xFormat_email( eInput ){
	var sValue = eInput.value.replace( /[^\w\-\d\.@]/g, "" );
	sValue = sValue.replace( /^[^a-z\d]/i, "" );
	sValue = sValue.replace( /(@.*)@/g, "$1" );
	sValue = sValue.replace( /@\./, "@" );
	sValue = sValue.replace( /[^\w\d\-\.@]/g, "" );
	if( eInput.value != sValue ) eInput.value = sValue;
}

