$(function(){
	$(".input").click(function () {
		$('.field-hint').addClass('inactive');
	 
    var id = $(this).attr('name');
    if (!$(this).val()){
			$('#hint_'+id).removeClass('inactive');
			$('#error_'+id).html('');
		}
		else $('#hint_'+id).addClass('inactive');
  });
  
  $(".input").focus(function () {
		$('.field-hint').addClass('inactive');
	 
    var id = $(this).attr('name');
		if (!$(this).val()){
			$('#hint_'+id).removeClass('inactive');
			$('#error_'+id).html('');
		}
		else $('#hint_'+id).addClass('inactive');
  });

  $(".input").blur(function () {
		var id = $(this).attr('name');
		
		if (id == 'name') valid_name();
		if (id == 'email') valid_email();
		if ((id == 'pass') || (id == 'pass2')) valid_pass();
		if (id == 'domain') valid_domain();
  });
  
  $('#pass2').keyup(function (){
  	valid_pass();
	});
  
});


function selectdomain(type){
	if (type == "1"){
		$('#ourdomain').show();
		$('#yourdomain').hide();
		$('#paydomain').hide();
		
		$('#domain1').attr('name','domain');
		$('#domain2').attr('name','domain1');
		$('#domain3').attr('name','domain2');
	}
	if (type == "2"){
		$('#ourdomain').hide();
		$('#yourdomain').show();
		$('#paydomain').hide();
		
		$('#domain1').attr('name','domain1');
		$('#domain2').attr('name','domain');
		$('#domain3').attr('name','domain2');
	}
	if (type == "3"){
		$('#ourdomain').hide();
		$('#yourdomain').hide();
		$('#paydomain').show();
		
		$('#domain1').attr('name','domain1');
		$('#domain2').attr('name','domain2');
		$('#domain3').attr('name','domain');
	}
}
function showreinvoice(){
	$('#resend').show();
}

function valid_name(){
	var val = $('#name').val();
	if (val){
		if (!(val.length >= 6 && val.length <= 30)){
			$('#error_name').html(lang['error_name2']);
			$('#name').addClass('input_error');
			return false
		}
	
		$('#name').removeClass('input_error');
		$('#error_name').html('');		
		return true;
	}	else {
		$('#error_name').html(lang['error_name']);
		$('#name').addClass('input_error');
		return false;
	}
}

function valid_email(){
	var val = $('#email').val();
	if (!val){
		$('#error_email').html(lang['error_email']);
		$('#email').addClass('input_error');
		return false;
	}
	
	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	var elem = document.getElementById('email');
	if(!elem.value.match(emailExp)){
		$('#error_email').html(lang['error_email2']);
		$('#email').addClass('input_error');
		return false;
	}
	
	$('#error_email').html('');
	$('#email').removeClass('input_error');
	return true;
}

function valid_pass(){
	var val = $('#pass').val();
	var val2 = $('#pass2').val();
	if (!val){
		$('#error_pass').html(lang['error_pass']);
		$('#pass').addClass('input_error');
		return false
	}
	if (val != val2){
		$('#error_pass').html(lang['error_pass2']);
		$('#pass').addClass('input_error');
		$('#pass2').addClass('input_error');
		return false;
	}
	
	var elem = document.getElementById('pass');
	var uInput = elem.value;
	if (!(uInput.length >= 6 && uInput.length <= 30)){
		$('#error_pass').html(lang['error_pass3']);
		$('#pass').addClass('input_error');
		$('#pass2').addClass('input_error');
		return false
	}
	
	$('#error_pass').html('');
	$('#pass').removeClass('input_error');
	$('#pass2').removeClass('input_error');
	return true;
}

function valid_domain(){
	if ($('#selour').attr('checked')){
		var elem = document.getElementById('domain1');
		var alphaExp = /^[0-9a-zA-Z]+$/;
		if(!elem.value.match(alphaExp)){
			$('#error_domain').html(lang['error_domain']);
			$('#domain1').addClass('input_error');
		}
		
		if (!(elem.value.length >= 6 && elem.value.length <= 30)){
			$('#error_domain').html(lang['error_domain2']);
			$('#domain1').addClass('input_error');
			return false
		}
		
		$('#error_domain').html('');
		$('#domain1').removeClass('input_error');
		return true;
	}
	
	if ($('#selyour').attr('checked')){
		var elem = document.getElementById('domain2');
		var alphaExp = /^[0-9a-zA-Z.]+$/;
		if(!elem.value.match(alphaExp)){
			$('#error_domain').html(lang['error_domain']);
			$('#domain2').addClass('input_error');
		}
		
		if (!(elem.value.length >= 6 && elem.value.length <= 30)){
			$('#error_domain').html(lang['error_domain2']);
			$('#domain2').addClass('input_error');
			return false;
		}
		
		$('#error_domain').html('');
		$('#domain2').removeClass('input_error');
		return true;
	}
	
	return false;	
}

function valid(){
	$('.field-hint').addClass('inactive');
	var vn = valid_name();
	var ve = valid_email();
	var vp = valid_pass();
	var vd = valid_domain();
	if (!vn)
	{
		$('#name').focus();
	}
	else
	{
		if (!ve)
		{
			$('#email').focus();
		}
		else
		{
			if (!vp)
			{
				$('#pass').focus();
			}
			else
			{
				if (!vd)
				{
					if ($('#selour').attr('checked')){$('#domain1').focus();}
					if ($('#selyour').attr('checked')){$('#domain2').focus();}
				}
			}
		}
	}
	return vn && ve && vp && vd;
}


function activateThisColumn(column) {
  $('#back_'+column).show();
	$('#select_'+column).hide();
	$('#plan').val(column);
	
	$('#pricetable th').each(function(i){
  	$(this).removeClass('on');
  	if (!$(this).hasClass('side')) $(this).hide();
 	});
	$('#pricetable td').each(function(i){
  	$(this).removeClass('on');
  	if (!$(this).hasClass('side')) $(this).hide();
 	});
  $('.choice_'+column).each(function(i){
  	$(this).addClass('on');
  	//$(this).show();
		$(this).fadeIn("fast");
 	});
  
  //$('#formcontainer').show('slow');
  $('#formcontainer').fadeIn("fast");
		
}

function hideTheForm(column) {
	$('#back_'+column).hide();
	$('#select_'+column).show();
	$('#formcontainer').hide();	
	
	
	$('#pricetable th').each(function(i){
  	$(this).removeClass('on');
  	//$(this).show();
  	$(this).fadeIn("fast");
 	});
 	$('#pricetable td').each(function(i){
  	$(this).removeClass('on');
  	//$(this).show();
  	$(this).fadeIn("fast");
 	});
}


function installShop(){
	if (!validateInstall()) return false;
	shop_name = $('#shop_name').val();
	shop_description = $('#shop_description').val();
	shop_keywords = $('#shop_keywords').val();
	shop_currency = $('#shop_currency').val();
	shop_addcurrency = '';
	
	$('.add_currency').each(function(i){
  	if ($(this).attr('checked')){
			shop_addcurrency += '|'+$(this).val();
		}
 	});
 	
 	country = $('#country').val();
 	region = $('#region').val();
 	address = $('#address').val();
	postal_code = $('#postal_code').val();
	domain = $('#domain').val();
	locale = $('#locale').val();
	
	$('#install').fadeOut('normal');
	$('#loading').fadeIn('normal');
	
	$.post(
				"/ajax/installshop/", 
				{
					shop_name: shop_name,
					def_meta_description: shop_description,
					def_meta_keywords: shop_keywords,
					default_currency: shop_currency,
					allow_currency: shop_addcurrency,
					address_country: country,
					address_region: region,
					address_city: address,
					address_postcode: postal_code,
					domain: domain,
					locale: locale
				},
			  function(data){
			  	$('#loading').fadeOut('normal');
					$('#result').html(data);			  	
			  }
	);	
}

function validateInstall(){
	var ret = true;
	shop_name = $('#shop_name').val();
	if (!shop_name){
		$('#shop_name').addClass('input_error');
		ret = false;
	} else $('#shop_name').removeClass('input_error');
	
	shop_description = $('#shop_description').val();
	if (!shop_description){
		$('#shop_description').addClass('input_error');
		ret = false;
	} else $('#shop_description').removeClass('input_error');
	
	address = $('#address').val();
	if (!address){
		$('#address').addClass('input_error');
		ret = false;
	} else $('#address').removeClass('input_error');
	
	return ret;	
}
