/*global evt, event, jQuery, $, $$, xmlhttp, alert, blur, clearInterval, clearTimeout, close, closed, confirm, console, Debug, defaultStatus, document, focus, frames, getComputedStyle, getElementTextNS, history, Image, length, location, moveBy, moveTo, name, navigator, onblur, onerror, onfocus, onload, onresize, onunload, open, opener, opera, Option, parent, print, prompt, resizeBy, resizeTo, screen, scroll, scrollBy, scrollTo, self, setInterval, setTimeout, status, top, window, XMLHttpRequest */



jQuery(document).ready(function () {
	// ! run code when DOM is ready

	var mailReg = new RegExp(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,8})+$/);
	//jQuery("#registrationForm").bind('submit', validateFields);
	//jQuery("#spesamicaInfo #convalidoreSpesamica").bind('click', verifySpesamica);
	var pagename = location.href;
	pagename=pagename.substr(pagename.lastIndexOf("/")+1).split(/[?#]/)[0];
	var linkClass = jQuery("#toggle-subsection-nav1").hasClass("hidden"); 
	if(!is_home && !linkClass)
	{
		jQuery("#toggle-subsection-nav1").addClass("hidden");
		jQuery("#subsection-nav1").hide();
	}
	function validateFields() {
		var Success=checkFields();
		// console.log("resultat : " + Success);
		return Success;
	}

	function checkFields() {
		var noError=true;
		jQuery("form *[class*=chk]").removeClass("error");
		jQuery("form *[class*=chk]").each(function() {
			
			var inputValue = jQuery(this).children("input").attr("value");
			var AllClasses=jQuery(this).attr("class");
			var ChkClass=AllClasses.match(/chk([^\s]+)/);

			switch(ChkClass[0]) {
				case "chkEmail":
					var emailCorrect = inputValue.match(mailReg);
					if(!emailCorrect || inputValue.length==0) {
						jQuery(this).addClass("error");
						noError=false;
					}
					break;
				case "chkEmailMatch":
					var emailOne = jQuery(this).siblings(".chkEmail").children("input").attr("value");
					var emailTwo = inputValue;
					if(emailOne.length && emailOne!=emailTwo) {
						jQuery(this).addClass("error");
						noError=false;
					}
					break;
				case "chkPassword":
					if(jQuery(this).children("input").length < 6) {
						jQuery(this).addClass("error");
						noError=false;	
					}
					break;
				case "chkPassMatch":
					var passOne = jQuery(this).siblings(".chkPassword").children("input").attr("value");
					var passTwo = inputValue;
					if(passOne.length && passOne!=passTwo) {
						jQuery(this).addClass("error");
						noError=false;
					}
					break;
				case "chkPunto":
					if(jQuery(".trovar").is(":visible") && !inputValue) {
						jQuery(this).addClass("error");
						noError=false;
					}
					break;
				case "chkNome":
					if(jQuery("#spesamica_morefields").is(":visible") && !inputValue) {
						jQuery(this).addClass("error");
						noError=false;
					}
					break;
				case "chkCognome":
					if(jQuery("#spesamica_morefields").is(":visible") && !inputValue) {
						jQuery(this).addClass("error");
						noError=false;
					}
					break;
				case "chkDateSpes":
					if(jQuery("#spesamica_morefields").is(":visible") && !inputValue) {
						jQuery(this).addClass("error");
						noError=false;
					}
					break;
				case "chkChoice":
					if(!jQuery(this).children("input:radio:checked").length) {
						jQuery(this).addClass("error");
						noError=false;
					}
					break;
			}
		});
		return noError;
	}

	function verifySpesamica(event){
		event.stopPropagation();
		event.preventDefault();

		var Num=jQuery(".numeroSpesamica	#carta_spesamica").val();
		if(!Num) return false;

		var URLCheck_spesamica = "ajax/integration.echo_yes.html";
		var eventIdentifier = jQuery(this);
		var errorSignal = jQuery(".numeroSpesamica");

		jQuery.ajax({
			type: "POST",
			url: URLCheck_spesamica,
			data: Num,
			beforeSend: function(){
				jQuery(".tempError").remove();
				jQuery(".tempSuccess").remove();
				jQuery(".tempLoading").remove();
				jQuery(eventIdentifier).after("<img src='core/media/img/ajax-loader.gif' class='tempLoading' alt='ajax-loader' />");
			 },
			success: function(msg){
				alert( "Success - This number exists : " + msg );
				jQuery(".tempError").remove();
				jQuery(".tempSuccess").remove();
				jQuery(".tempLoading").remove();
				jQuery(errorSignal).removeClass("error");
				jQuery("#spesamica_morefields").show();
			},
			error: function(msg){
				alert( "Error - This number does not exists" );
				jQuery(".tempError").remove();
				jQuery(".tempSuccess").remove();
				jQuery(".tempLoading").remove();
				jQuery(errorSignal).addClass("error");
				jQuery("#spesamica_morefields").hide();
			}
		});

	}



});