function textCounter(field, countfield, maxlimit) {
if (field.value.length > maxlimit) // if too long...trim it!
field.value = field.value.substring(0, maxlimit);
else 
countfield.value = maxlimit - field.value.length;
$("#charsRemain").html(maxlimit - field.value.length);
}

/*
function searchMagazine()
{

	var data = $("#search_form").serialize();

     	$.ajax({
		   type: "POST",
		   dataType: "html",
		   error: function(response){ showMessage('error ' + response.status, 'Error while getting data...'); },
		   url: "index.php?do=magazines",
		   data: data,
		   success: function(response)
               {
						$("#search_input").slideUp();
						$("#search_results_div").html(response);
						$("#show_search").show();	
               }
		});

}
*/


function add2Cart(id)
{

    $("#cartIcon_"+id).effect(
			'spectrum', 
			{	dest: $("#cart_head"), autoScroll:false },
			1000,
			function(){
				add2CartComplete(id);
			}
	);
}

function saveOrder()
{
	$.ajax({
		type: "POST",
		url: "index.php",
		data: "do=save_order",
		dataType: "html",
		complete: function(){},
		success: function(response){
			$("#custom").val(response);
			document.forms.pay_pal_form.submit();
			return true;		
		},
		error: function()
		{
			alert('Export operation failed!');
			return false;
		}
	});

	return false;
}


function add2CartComplete(id)
{
	$.ajax({
		type: "POST",
		url: "index.php",
		data: "do=add2cart&id="+id,
		dataType: "html",
		complete: function(){},
		success: function(response){
			$("#cart_div").html(response);					
		},
		error: function()
		{
			//alert('Export operation failed!');
		}
	});
}


function deleteFromCart(id)
{

	$.ajax({
		type: "POST",
		url: "index.php",

		data: "do=add2cart&del=1&id="+id,
		dataType: "html",
		complete: function(){},
		success: function(response){
			$("#cart_tr_"+id).remove(); 

			var result = response.split("|");
						
			if (result[0] == 0)
			{
				alert("Your shopping cart is empty, click OK to continue shopping");
				window.location.href="http://www.freelance-market.net/advanced_search.html";
			}

			$("#total_span").html(result[0]);					
			$("#total_span_left").html(result[0]);
			$("#can_get_span").html(result[1]);	
			$("#cart__amount").val(result[0]);					
											
		},
		error: function()
		{
			//alert('Export operation failed!');
		}
	});
}

function searchMagazine()
{

	if ("do=magazines&keywords=" == $("#search_form").serialize() || "do=magazines" == $("#search_form").serialize())
	{
		$.growlUI('System Notification', 'Search Criteria can not be empty!'); 
   }else{
		$("#search_form").submit();
	}
//   var url = "index.php?do=magazines&" + data;
//	window.location.href= url;
}


function paginateArticles(form)
{

	var data = $("#"+form).serialize();

     	$.ajax({
		   type: "POST",
		   dataType: "html",
		   error: function(response){ showMessage('error ' + response.status, 'Error while getting data...'); },
		   url: "index.php?do=articles.ajax",
		   data: data,
		   success: function(response)
               {
                 $("#articles_div").html(response);
               }
		});
}



function addFavorites(id)
{

    var data;
     	$.ajax({
		   type: "GET",
		   dataType: "html",
           beforeSend: function(){document.getElementById('wait').innerHTML = '<img src="images/wait.gif">&nbsp;Сохранение  данных...';},
           complete: function(){document.getElementById('wait').innerHTML = '';},
		   error: function(response){ showMessage('error ' + response.status, 'Error while getting data...'); },
		   url: "index.php?do=ajax.add.favorites&id="+id,
		   data: data,
		   success: function(response)
               {
                  //  $("#callback").html(response);
               }
		});


}

function addRecommended(id)
{
    var data = "";
     	$.ajax({
		   type: "POST",
		   dataType: "html",
		   error: function(response){ showMessage('error ' + response.status, 'Error while getting data...'); },
		   url: "index.php?do=recommended&id="+id,
		   data: data,
		   success: function(response)
               {
                 $("#recommend_"+id).html(response);
               }
		});
}




function loadURL(dest) {


    var data;
     	$.ajax({
		   type: "GET",
		   dataType: "html",
           beforeSend: function(){document.getElementById('wait').innerHTML = '<img src="images/wait.gif">&nbsp;Загрузка данных...';},
           complete: function(){document.getElementById('wait').innerHTML = '';},
		   error: function(response){ showMessage('error ' + response.status, 'Error while getting data...'); },
		   url: dest,
		   data: data,
		   success: function(response)
               {
                    $("#callback").html(response);
               }
		});

}



function submitFormAjax(dest) {


    var data = $("#theForm").serialize();
     	$.ajax({
		   type: "GET",
		   dataType: "html",
           beforeSend: function(){document.getElementById('wait').innerHTML = '<img src="images/wait.gif" align=absmiddle>&nbsp;Обработка данных...';},
           complete: function(){document.getElementById('wait').innerHTML = '';},
		   error: function(response){ showMessage('error ' + response.status, 'Error while getting data...'); },
		   url: dest,
		   data: data,
		   success: function(response)
               {
                    $("#callback").html(response);
               }
		});

}




function validateUserFormAjax()
{

	if (!document.getElementById("username").value)
	{
	  alert("Поле Логин не может быть пустым.");
	  document.getElementById("username").focus();
	  return (false);
	}

	if (!document.getElementById("pass").value)
	{
	  alert("Пожалуйста, введите пароль");
	  document.getElementById("pass").focus();
	  return (false);
	}
	
	if (document.getElementById("pass_confirm").value && document.getElementById("pass").value != document.getElementById("pass_confirm").value)
	{
	  alert("Несовпадение пароля и его подтверждения!");
	  document.getElementById("pass_confirm").focus();
	  return (false);
	}
	
	if (!document.getElementById("email").value)
	{
	  alert("Необходимо ввести корректный email адрес.");
	  document.getElementById("email").focus();
	  return (false);
	}
	
	var re_check_email = new RegExp("[A-Za-z0-9_]+([-+.][A-Za-z0-9_]+)*@[A-Za-z0-9_]+([-.][A-Za-z0-9_]+)*\\.[A-Za-z0-9_]{2,}([-.][A-Za-z0-9_]+)*");
	var ares = re_check_email.exec(document.getElementById("email").value);
	if( ares == null )
	{
	  alert("Необходимо ввести корректный email адрес.");
	  document.getElementById("email").focus();
	  return (false);
	}

   submitFormAjax("index.php?do=ajax.user.profile");

}


function validateLoginForm()
{

	
	if (!document.getElementById("username").value)
	{
	  alert("Необходимо ввести Имя пользователя.");
	  document.getElementById("username").focus();
	  return (false);
	}

	if (!document.getElementById("pass").value)
	{
	  alert("Пожалуйста, введите пароль");
	  document.getElementById("pass").focus();
	  return (false);
	}

	return true;
}

function validateUserForm(hideCaptcha)
{

	document.getElementById('userEnter').value = 1;

	if (!document.getElementById("username").value)
	{
	  alert("Поле Логин не может быть пустым.");
	  document.getElementById("username").focus();
	  return (false);
	}

	if (!document.getElementById("pass").value)
	{
	  alert("Пожалуйста, введите пароль");
	  document.getElementById("pass").focus();
	  return (false);
	}
	
	if (document.getElementById("pass_confirm").value && document.getElementById("pass").value != document.getElementById("pass_confirm").value)
	{
	  alert("Несовпадение пароля и его подтверждения!");
	  document.getElementById("pass_confirm").focus();
	  return (false);
	}
	
	if (!document.getElementById("email").value)
	{
	  alert("Необходимо ввести корректный email адрес.");
	  document.getElementById("email").focus();
	  return (false);
	}
	
	var re_check_email = new RegExp("[A-Za-z0-9_]+([-+.][A-Za-z0-9_]+)*@[A-Za-z0-9_]+([-.][A-Za-z0-9_]+)*\\.[A-Za-z0-9_]{2,}([-.][A-Za-z0-9_]+)*");
	var ares = re_check_email.exec(document.getElementById("email").value);
	if( ares == null )
	{
	  alert("Необходимо ввести корректный email адрес.");
	  document.getElementById("email").focus();
	  return (false);
	}

	if (hideCaptcha != 1)
	{
		if(document.getElementById('txtCaptcha').value == '')
		{
		  alert("Необходимо ввести код защиты от роботов.");
		  document.getElementById("txtCaptcha").focus();
		  return (false);
		}

		getParam(document.sign_up_form); 
	}else{
     document.forms.sign_up_form.submit()	;
	}



}

function checkSignUpForm()
{
	var theForm = document.forms.sign_up_form;

	if (!theForm.user_email.value)
	{
		alert("Please, enter your e-mail.");
		theForm.user_email.focus();
		return false;
	}

	if( !checkEmailAddress(theForm.user_email.value) )
	{
		alert("Please, enter valid email address.");
		theForm.user_email.focus();
		return false;
	}

	if (!theForm.user_password.value)
	{
		alert("Please, enter your password.");
		theForm.user_password.focus();
		return false;
	}

	if (theForm.user_password.value != theForm.user_password2.value)
	{
		alert("Please verify your password - the password you entered in both fields does not match.");
		theForm.user_password2.focus();
		return false;
	}

	if (!theForm.txtCaptcha.value)
	{
		alert("Please, enter confirmation code.");
		theForm.txtCaptcha.focus();
		return false;
	}

	return true;
}

function checkEmailAddress(emailStr)
{
	var re_check_email = new RegExp("[A-Za-z0-9_]+([-+.][A-Za-z0-9_]+)*@[A-Za-z0-9_]+([-.][A-Za-z0-9_]+)*\\.[A-Za-z0-9_]{2,}([-.][A-Za-z0-9_]+)*");
	var ares = re_check_email.exec(emailStr);
	if( ares == null )
	{
	  return false;
	}
	return true;
}


function generateNewCaptcha()
{
	document.getElementById('userEnter').value = '';
	getParam(document.sign_up_form); 

}


function validateCommentForm(hideCaptcha)
{

	document.getElementById('userEnter').value = 1;

	if (!document.getElementById("username").value)
	{
	  alert("Поле Имя не может быть пустым.");
	  document.getElementById("username").focus();
	  return (false);
	}


	if (document.getElementById("email").value != "")
	{

	
	var re_check_email = new RegExp("[A-Za-z0-9_]+([-+.][A-Za-z0-9_]+)*@[A-Za-z0-9_]+([-.][A-Za-z0-9_]+)*\\.[A-Za-z0-9_]{2,}([-.][A-Za-z0-9_]+)*");
	var ares = re_check_email.exec(document.getElementById("email").value);
	if( ares == null )
	{
	  alert("Необходимо ввести корректный email адрес.");
	  document.getElementById("email").focus();
	  return (false);
	}

	}

	if (!document.getElementById("content_1").value)
	{
	  alert("Текст сообщения не может быть пустым.");
	  document.getElementById("content_1").focus();
	  return (false);
	}


	/*
	if (hideCaptcha != 1)
	{
		if(document.getElementById('txtCaptcha').value == '')
		{
		  alert("Необходимо ввести код защиты от роботов.");
		  document.getElementById("txtCaptcha").focus();
		  return (false);
		}

		getParam(document.sign_up_form); 
	}else{
	*/
     document.forms.sign_up_form.submit()	;
	//}
}



function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


			function checkEmail(e) {
				ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";

				for(i=0; i < e.length ;i++){
					if(ok.indexOf(e.charAt(i))<0){ 
						return (false);
					}	
				} 

				if (document.images) {
					re = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
					re_two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
					if (!e.match(re) && e.match(re_two)) {
						return (-1);		
					} 

				}

			}		
		

				function checkForm() {

					if (document.fmVContactUs.message.value.length < 1) {
						alert('Пожалуйста, введите данные в поле "Сообщение".');
						document.fmVContactUs.message.focus();
						return;
					}
					document.fmVContactUs.vfy.value = document.fmVContactUs.pre_vfy.value;
					
					document.fmVContactUs.submit();
				}				


function saveNote()
{

    var data = $("#noteForm").serialize();

     	$.ajax({
		   type: "GET",
		   dataType: "html",
           beforeSend: function(){document.getElementById('wait').innerHTML = '<img src="images/wait.gif">&nbsp;Сохранение...';},
           complete: function(){document.getElementById('wait').innerHTML = '';},
		   error: function(response){ showMessage('error ' + response.status, 'Error while getting data...'); },
		   url: "index.php",
		   data: data,
		   success: function(response)
               {
                    $("#callback").html(response);
               }
		});



}