//On load page, init the timer which check if the there are anchor changes each 300 ms
$().ready(function(){
	setInterval("checkAnchor()", 300);
});
var currentAnchor = null;
var idIE = false;
//Function which chek if there are anchor changes, if there are, sends the ajax petition
function checkAnchor(){
	//Check if it has changes
	if(currentAnchor != document.location.hash){
		currentAnchor = document.location.hash;
		//if there is not anchor, the loads the default section
		if(!currentAnchor)
			query = "section=home";
		else
		{
			//Creates the  string callback. This converts the url URL/#main&id=2 in URL/?section=main&id=2
			var splits = currentAnchor.substring(1).split('&');
			//Get the section
			var section = splits[0];
			delete splits[0];
			//Create the params string
			var params = splits.join('&');
			var query = "section=" + section + params;
		}
		//Send the petitions
		//alert(query);
		$.get("callbacks.php",query, function(data){
			$("#content").html(data);
			var ua = navigator.userAgent.toLowerCase();
			isIE = (ua.indexOf("msie") != -1 ); //&& ua.indexOf("opera") == -1 && ua.indexOf("webtv") == -1);
			if (!isIE)
				page_init();
		});
	}
}

function enterSite(){
	var login = $("#vuser").val();
	var pw    = $("#vpassword").val();
	if(login.length && pw.length){
		$.post("modules/userajax.php?action=enter", $("#enterSite").serialize(),function(data){
				if(data=="authed")
					document.aform.submit();//window.location = 'http://vendacerecords.com';
				else
					alert('Login or password incorrect');
		});
	}
	else
		alert('Fill all (both) fields, please.');
}

function removeFromCart(id,type){
	$.get("modules/userajax.php?action=removecart&id="+id+"&type="+type ,function(){
			$("#account_section").load("modules/userajax.php?action=load&section=cart");
	});
}

function reg(){
	$("#authreg").load("modules/reg.php");
}

function checkEx(login){
	$("#subResult").load("modules/userajax.php?action=checkex&login="+encodeURIComponent(login));
	return 1;
}

function registerUser(){
	$.post("modules/userajax.php?action=registerUser", $("#reginfo").serialize(),function(data){
			alert(data);
	});
}

function pregEmail (email)
{
 return (/^([a-z0-9_\-]+\.)*[a-z0-9_\-]+@([a-z0-9][a-z0-9\-]*[a-z0-9]\.)+[a-z]{2,4}$/i).test(email);
}
function checkReg(){
	var login = $("#rlogin").val();
	var password = $("#rpass").val();
	var passrepeat = $("#rpassrepeat").val();
	var email = $("#email").val();
	var emailrepeat = $("#emailrepeat").val();
	var fail = 0;
	if(!login.length){
		alert('Please, enter login');
		fail++;
		return 0;
    }	
	if(!email.length){
		alert('Please, enter e-mail');
		fail++;
		return 0;
	}
	var emailValid = pregEmail(email);
	if(!emailValid){
		alert('Incorrect e-mail format');
		return 0;
	}
	if(!emailrepeat.length){
		fail++;
		alert('Please, enter e-mail confirm');
		return 0;
	}
	var emailRValid = pregEmail(emailrepeat);
	if(!emailRValid){
		alert('Incorrect e-mail confirm format');
		return 0;
	}
	if(email!=emailrepeat){
		fail++;
		alert('Entered e-mails do not match');
		return 0;
	}
	if(password.length<8){
		alert('Password should be at least 8 characters long');
		fail++;
		return 0;
	}
	if(passrepeat.length<8){
		alert('Password confirm is too short');
		fail++;
		return 0;
	}
	if(password!=passrepeat){
		alert("Entered passwords do not match");
		fail++;
		return 0;
	}	
	if(!fail){
			$.post("modules/ajax.reg.php?action=register", { login: login, email: email,password:password}, function (data){
				if(data=='repeat')
					alert("Unfortunately, there's user with same login in our database already. Please, try another login.");
				else{
					$("#accstep1").fadeOut("500",function(){
						$("#fancybox-inner").load("modules/signup2.php?id="+data,function(){
						$.fancybox.resize();
						});
					});
				
				}
				
			
			
			}
			);		
	}
	
	
}



function buy_item(type,id){
	if(type=='release')
		$("#ajaxTemp").load("modules/userajax.php?action=buy&type=release&id="+id,function(){
			basket_updater();
		});
	else
		$("#ajaxTemp").load("modules/userajax.php?action=buy&type=track&id="+type,function(){
			basket_updater();
		});
	
}
function showBasket(){
}
function exitSite(){
	$.cookie("auth", "");
}

function regme(){
	
}

function logout_(){
	$.cookie("vendacer", null);
	$.get("modules/logout.php",function(data){
		window.location='http://vendacerecords.com/';
	});
}
function nextFancyBox(){
	$("#authclick").click();
}
function fillLater(uid){
	$.post("modules/ajax.reg.php?action=fillLater",{uid:uid},function(){
		$.fancybox.close();
	});
}
function finReg(uid){
	var firstname = $("#firstname").val();
	var lastname = $("#lastname").val();
	var street = $("#street").val();
	var city = $("#city").val();
	var postal = $("#postal").val();
	var country = $("#country").val();
	if(firstname.length && lastname.length && street.length && city.length && postal.length && country.length){
		$.post("modules/ajax.reg.php?action=finalstep",{uid:uid,firstname:firstname,lastname:lastname,street:street,city:city,postal:postal,country:country},function(data){
			if(data=="good"){
				alert("Thanks for your time, you've just successfully completed registration! You can edit your info at MyAcoount page (probably)");
				
				$("#secondstep").fadeOut("500",function(){
						$.fancybox.close();
						window.setTimeout('nextFancyBox()',500);
						
				});
				
			}
			if(data=="fail")
				alert("We have some technical problems, try to click 'finish' again or just fill all information later at MyAccount page.");
			if(data=="params")
				alert("There's some technical issues, try again later, please (at MyAccount page)");
		});
	}else
		alert('Please, fill all fields.');
}

function myacc(){

}

function account_sections(section){
	$(".account_menu_item_active").removeClass("account_menu_item_active");
	$("#account_section").load("modules/userajax.php?action=load&section="+section,function(){
	
	});
	
}

function edit_account(){
	
}

function signup_main(){
	$('#signupbutt_main').fancybox({width:'auto',height:'auto',scrolling:'no',padding:0,overlayOpacity:0.7,overlayColor:"black"});
	$.fancybox.resize();
}


function changeformat(type,id){
	if(type=='track'){
		var format = $("#format"+id).val();

		$.get("modules/userajax.php?action=changeformat&format="+format+"&id="+id,function(){
			
			if(format=='wav'){
				getTotal();
				$("#trcost"+id).html('$1.49');
			}else
			{
				$("#trcost"+id).html('$0.99');
				getTotal();
				}
		});
	}else
		alert('Release format changing is not possible at the moment.');
}

function getTotal(){
	var total = 0;
	$.get("modules/userajax.php?action=counttotal",function(answer){
		$("#cart_total_summ").html(answer);
		$("#cart_total_summ_vat").html(answer);

	});
	return total;
	
}

function makeCheckout(){
	$("#epicform").load("modules/userajax.php?action=makecheckout",function(pay){
		$("#payit").submit();
	});
}

function selectCountry(){
	var selected = $("#country_select").val();

}

function step2(){
	var country = $("#country_select").val();
	var city = $("#city").val();
	var postal = $("#postal").val();
	var street = $("#street").val();
	var name = $("#name").val();
	if(!country.length){
		alert('Please, select the country.');
	}else{
		if(!city.length){
			alert('Please, enter city');
		}else
		{
			if(!street.length){
				alert('Please, enter street');
			}else
			{
				if(!name.length){
					alert('Please, enter your name');
				}else
				{
					var send = confirm('We will deliver your CD to this address.Confirm?');
					if(send){
						$.post('modules/do.order.php',{country:country,city:city,postal:postal,street:street,name:name},
							function(data){
								$("#cd-order-content").html(data);
							}
						);	
					}	
				}	
			}		
		}
		
	}
	
}
