String.prototype.trim = function() { return jQuery.trim(this); };
String.prototype.removeCarriage = function() { 
	o="";
	for (i=0;i<this.length;i++)
	{
		if (this.charCodeAt(i)== 10)
		{o+="  "}
		else {o+=this.charAt(i);}
	}
	return o; 
	};
String.prototype.replaceAll = function(reallyDo, replaceWith, ignoreCase) {
	if (!RegExp.prototype.isPrototypeOf(reallyDo)) {
	    return this.replace(new RegExp(reallyDo, (ignoreCase ? "gi": "g")), replaceWith);
	} else {
	    return this.replace(reallyDo, replaceWith);
	}
}
String.prototype.addslashes = function() { 
	str=this.replaceAll('\'','\'\'');
	return str; 
	};
String.prototype.stripslashes = function() { 
	str=this.replaceAll('\'\'','\'');
	return str; 
	};
Array.prototype.inArray = function( value ){
  var i;
  for(i=0; i < this.length; i++){
    if(this[i] === value)
      return true;
  };
  return false;
};	


//MyValidation
function required(input,texts,m,style)
{
	$("#"+m).text("");
	for (i=0;i<input.length;i++)
	{
		$("#"+input[i]).css({ border: "" });
		if ($("#"+input[i]).val().trim()=="")
		{
			$("#"+input[i]).focus().css({ border: style });
			$("#"+m).html(texts[i]);
			return false;
		}
	}
	return true;
}

//POPUP
function mypopup( url )
{
	window.open( url, 'nuovaFinestra','width=400,height=400'); 
	return false;
}

//FUNZIONE DI CONTROLLO EMAIL
function checkValidEmail(email)
{
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(email))
	{return false;}
	return true;
}

function checkValidUsername(input)
	{
		if (input.length<2) {return false;}
		var numaric = input.trim();
		for(var j=0; j<numaric.length; j++)
			{
			  var alphaa = numaric.charAt(j);
			  var hh = alphaa.charCodeAt(0);
			  if((hh > 47 && hh<59) || (hh > 64 && hh<91) || (hh > 96 && hh<123))
			  {
			  	k=0;
			  }
			  else{ return false; }
			}
	 return true;
	}

$(document).ready(function() {
	$("a[rel='external']").attr("target","_blank");
	$("input.emptyonclick").click( function(){ 
		if ( this.value.toLowerCase()=="username" || this.value.toLowerCase()=="password" || this.value.toLowerCase()=="la sposa"  )
		{
			this.value='';
		}
	})
	$(".mouseoverfade").mouseover( function(){ this.src=this.src.replace("rosa","blu");}).mouseout( function(){ this.src=this.src.replace("blu","rosa");});
});


/* FUNCTIONS */
function tryLogin( formname, dest )
{
	if ( $("#form_"+formname+"_username").val().toLowerCase()=="username" || $("#form_"+formname+"_password").val().toLowerCase()=="password" )
	{
		$("#form_"+formname+"_message").html("Inserire username e password");
		return;
	}
	i=[ "form_"+formname+"_username" , "form_"+formname+"_password" ];	 		
	t=[ "Inserisci la username",
		"Inserisci la password"
		];
	if (!required( i , t , "form_"+formname+"_message" , "dotted red" )) {return;}
	var remember=0;
	if( $("#form_"+formname+"_remember:checked").val() == 'on' )
	{
		remember=1;
	}
	//POST
	$.post(WEBROOT+"business/user.php",
  		{ action:"TRYLOGIN", 
  			u: $("#form_"+formname+"_username").val() , 
  			p: $("#form_"+formname+"_password").val() ,
  			r: remember
  			},
  				function(data){
	  				var myObj = eval('(' + data + ')');
	 				if (myObj.result=="OK")
	 				{
	 					if ( formname=="loginpage" )
	 					{
	 						document.location.href = dest;
	 					} else {
	 						$("#header_login").load(WEBROOT+"components/header/userdata.php");
	 					}
	 				}
	 				else {
	 					$("#form_"+formname+"_message").html(myObj.message);
	 				}
  				}
			);
}

function tryLogout()
{
	//CALL
	$.post(WEBROOT+"business/user.php",
  		{ action:"TRYLOGOUT" },
  				function(data){
  					var myObj = eval('(' + data + ')');
	 				if (myObj.result=="OK")
	 				{
	 					//$("#left_login").load(WEBROOT+"components/left/login_form.php");
	 					document.location.href = document.location.href;
	 				}
	 				else {
	 					$("#msg").html("Si e' verificato un errore");
	 				}
  				}
			);
}

function tryRegister()
{
	i=[ "form_register_name", "form_register_surname", "form_register_username", "form_register_p", "form_register_p2", "form_register_email" ];	 		
	t=[ "Inserisci il tuo nome", "Inserisci il tuo cognome", "Scegli una username", "Scegli una password", "Inserisci la conferma della password", "Inserisci la tua email"	];
	if (!required( i , t , "form_message_container" , "dotted #6699cc" )) {return;}
	var nl=0;
	var tc=0;
	var g='';
	if( $("#form_register_tc:checked").val() != 'on' )
	{
		$("#form_message_container").html("Occorre accettare i termini e le condizioni d'utilizzo");
		return;
	} else {
		tc=1;
	}
	if( $("#form_register_newsletter:checked").val() == 'on' )
	{
		nl=1;
	}
	//check username contains just alphanumeric values
	if ( !checkValidUsername( $("#form_register_username").val() ) )
	{
		$("#form_message_container").html("La username può contenere solo caratteri alfanumerici (a-z o 1-9 e _)");
		$("#form_register_username").focus().css({ border: "dotted #6699cc" });
		return;
	}
	//check passwords match
	if ( $("#form_register_p").val() != $("#form_register_p2").val() )
	{
		$("#form_message_container").html("Le due password non coincidono");
		return;
	}
	//check valid email
	if ( !checkValidEmail( $("#form_register_email").val() ) )
	{
		$("#form_message_container").html("Inserisci un indirizzo email valido");
		return;
	}
	//check sex
	if( $("#form_register_man:checked").val()=='m' ) {
		g='m';
	} else if ($("#form_register_man:checked").val()=='f') {
		g='f';	
	}
	//CALL
	$.post(WEBROOT+"business/user.php",
  		{ action:"TRYREGISTER",
  		  n: $("#form_register_name").val(),
  		  s: $("#form_register_surname").val(),
  		  u: $("#form_register_username").val(),
  		  p: $("#form_register_p").val(),
  		  e: $("#form_register_email").val(),
  		  b: $('#form_register_birthdate').val(),
  		  ph: $("#form_register_phone").val(),
  		  g: g,
  		  nl: nl,
  		  tc: tc
  		 },
  				function(data){
  					var myObj = eval('(' + data + ')');
	 				if (myObj.result=="OK")
	 				{
	 					document.location.href = WEBROOT+"confirmRegistration.php";
	 				}
	 				else {
	 					$("#form_message_container").html(myObj.text);
	 				}
  				}
			);
}

function switchSiteToEdit( sid )
{
	if (sid=="" || typeof(sid)=="undefined" ) 
	{ return; }
	$.post(WEBROOT+"business/site.php",
  		{ action:"SWITCHSITETOEDIT",
  		  sid: sid
  		 },
  		 	function(data){
  					var myObj = eval('(' + data + ')');
	 				if (myObj.result=="OK")
	 				{
	 					document.location.href = WEBROOT+"sitePages.php";
	 				}
  				}
			);
}

function trySearch( from )
{
	bn = $("#search_bride_name").val();
	gn = $("#search_groom_name").val();
	p = $("#search_place").val();
	df = $("#search_day_from").val();
	dt = $("#search_day_to").val();
	//check data box
	if ( from=='box' && 
			( bn=='' || bn=='undefined' ) &&
			( gn=='' || gn=='undefined' )
		)
	{
		$.blockUI({ 
			theme:     true, 
			title:    'Ricerca', 
			message:  '<p>Inserisci i parametri di ricerca</p>',
			timeout: 2500
		});
		return;
	}
	//check data page
	if ( from=='page' && 
			( bn=='' ||  bn=='undefined' ) &&
			( gn=='' || gn=='undefined' ) &&
			( p=='' || p=='undefined' ) &&
			( df=='' || df=='undefined' ) &&
			( dt=='' || dt=='undefined' )
		)
	{
		$.blockUI({ 
			theme:     true, 
			title:    'Ricerca', 
			message:  '<p>Inserisci i parametri di ricerca</p>',
			timeout: 2500
		});
		return;
	}
	//query string
	params = [ "bn", "gn", "p", "df", "dt" ];
	queryString = "";
	for( i=0;i<params.length;i++)
	{
		thisVar = eval(params[i]);
		if ( typeof(thisVar)!='undefined' && thisVar!='' && thisVar!='undefined')
		{
			queryString += params[i]+"="+thisVar+"&";
		}
	}
	if ( from=="box" )
	{
		document.location.href=WEBROOT+"search.php?"+queryString;
	} else if ( from=="page" )
	{
		$("#search_results").load(WEBROOT+"components/boxes/searchResults.php?"+queryString);
	}
}

//Function post to Facebook
function fbs_click( url, title ) {
	window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(url)+'&t='+encodeURIComponent(title),'sharer','toolbar=0,status=0,width=626,height=436');
	return false;
}

