var product = "";


$(document).ready(function() {	
						   
	//select all the a tag with name equal to modal
	$('a[name=modal]').click(function(e) {
		//Cancel the link behavior
		e.preventDefault();
		
		//Get the A tag
		var id = $(this).attr('href');
		product = $(this).attr('product');
	
		//Get the screen height and width
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
	
		//Set heigth and width to mask to fill up the whole screen
		$('#mask').css({'width':maskWidth,'height':maskHeight});
		
		//transition effect		
		$('#mask').fadeIn(0);	
		$('#mask').fadeTo("fast",0.5);	
	
		//Get the window height and width
		var winH = $(window).height();
		var winW = $(window).width();
              
		//Set the popup window to center
		$(id).css('top',  winH/2-$(id).height()/2);
		$(id).css('left', winW/2-$(id).width()/2);
	
		if (id == '#signupdialog' && _loggedinuserid != "") {
		document.location = "/register/orderproduct.aspx?product="+product;
		} else {
		//transition effect
		$(id).fadeIn(0); 
		}
	
	});
	
	//if close button is clicked
	$('.popup-box .close').click(function (e) {
		//Cancel the link behavior
		e.preventDefault();
		
		$('#mask').hide();
		$('.popup-box').hide();
	});		
	
	//if mask is clicked
	$('#mask').click(function () {
		$(this).hide();
		$('.popup-box').hide();
	});
	
	//if signup button is clicked
	$('input[name=signupbutton]').click(function() {
    signup();
	});
	
	//if signin button is clicked
	$('input[name=signinbutton]').click(function() {
    signin();
	});
	
	//if newsletter form is submitted
	$('form[name=newsletterform]').submit(function(e) {
	signupnewsletter();
	return false;
	});

	
	//if sign in form is submitted
	$('form[name=signinform]').submit(function(e) {
	signin();
	return false;
	});

	
	//if forgot password link is clicked
	$('a[name=forgotpassword]').click(function() {
   		document.location = "/settings/forgotpassword.aspx";
	});
	
	//if forgot password link is clicked
	$('a[name=termsandconditions]').click(function() {
   		window.open('/website-streaming/terms-and-conditions.aspx');
	});


	//if download layer is clicked
	$('.download').click(function () {
		document.location = "/music-downloads/overview.aspx";
	});
	
	// if download layer is hovered
	$('.download').hover(function() { 
     $('.download-on').show();
	 $('.download-off').hide();
   }, function() { 
    $('.download-on').hide();
	$('.download-off').show();
   }); 
	
	//if advert layer is clicked
	$('.advert').click(function () {
		document.location = "/website-streaming/overview.aspx";
	});
	
	// if advert layer is hovered
	$('.advert').hover(function() { 
     $('.advert-on').show();
	 $('.advert-off').hide();
   }, function() { 
    $('.advert-on').hide();
	$('.advert-off').show();
   }); 
	
	//if store layer is clicked
	$('.store').click(function () {
		document.location = "/in-store-music/overview.aspx";
	});
	
	// if store layer is hovered
	$('.store').hover(function() { 
     $('.store-on').show();
	 $('.store-off').hide();
   }, function() { 
    $('.store-on').hide();
	$('.store-off').show();
   }); 

	
});

// key code functions
function enter_pressed(e){
	var keycode;
	if (window.event) keycode = window.event.keyCode; 
	else if (e) keycode = e.which; 
	else return false; 
	return (keycode == 13); 
}

// sign up functions
function signup() {

	if  (validateSignupFields()) {

   		var strEmail = document.getElementById("email").value;
  		var strPassword = document.getElementById("password").value;
  		var bAccept = document.getElementById("accept").checked;
  		var strDataString = "email="+escape(strEmail);
   		strDataString += "&password="+escape(strPassword);
  		strDataString += "&accept="+escape(bAccept);
		
   		$.ajax({
  		 type: "GET",
  		 url: "/customers/signup.ashx",
  		 data: strDataString,
		 error: function(xhr, status, error) {
         	alert("error:" + status);
  		 },success: function(xml) {
   	   		$(xml).find('response').each(function(){
   				var intErrorCode = $(this).find("errorcode").text();
   	   			if (parseInt(intErrorCode) == 0) {
   		 			document.location = "/register/orderproduct.aspx?product="+product;
					
   				} else {
   		 			generateSignupError(parseInt(intErrorCode));
   				}
   		});
   }
 });
 }
}

function validateSignupFields() {
	var strEmail = document.getElementById("email").value;
	var strPassword = document.getElementById("password").value;
	var bAccept = document.getElementById("accept").checked;
	
	if ((strEmail == "") || (strPassword == "")) {
		generateSignupError(1);
		return false;
	} else if (!bAccept) {
		generateSignupError(3);
	} else {
		return true;
	}
}

function getSignupErrorDescription(intErrorCode) {
	var strErrorText = "";
	switch(intErrorCode)
	{
	case 1:
		strErrorText = "Please fill out all form fields.";
		break;
	case 2:
		strErrorText = "Email address is already registered.";
		break;
	case 3:
		strErrorText = "You must accept the terms and conditions";
		break;
	default:
		strErrorText = "Please fill out all form fields.";
	}
	return strErrorText;
}

function generateSignupError(intErrorCode) {
	var strErrorText = getSignupErrorDescription(intErrorCode);

   	document.getElementById("signupstatustext").innerHTML = "<li>" + strErrorText + "</li>";
   	$('#signupstatusbox').show();
}

// sign in functions

function signin() {
	if  (validateSigninFields()) {
   		var strEmail = document.getElementById("signin_email").value;
  		var strPassword = document.getElementById("signin_password").value;
  		var bRememberMe = document.getElementById("remember-me").checked;

  		var strDataString = "email="+escape(strEmail);
   		strDataString += "&password="+escape(strPassword);
  		strDataString += "&rememberme="+escape(bRememberMe);
  		
  		$.ajax({
  		 type: "GET",
  		 url: "/customers/signin.ashx",
  		 data: strDataString,
  		 success: function(xml) {
   	   		$(xml).find('response').each(function(){
   				var intErrorCode = $(this).find("errorcode").text();
   	   			if (parseInt(intErrorCode) == 0) {

					if (bRememberMe) {
						setCookie("rememberlogin","1",300);
						setCookie("email",strEmail,300);
						setCookie("password",strPassword,300);
					} else {
						setCookie("rememberlogin","0",1);
						setCookie("email","",1);
						setCookie("password","",1)
					}
					document.location = "/settings/loggedin.aspx";
					
   				} else {
   		 			generateSigninError(parseInt(intErrorCode));
   				}
   		});
   		
   }
 });
 }
}


// newsletter functions
function signupnewsletter() {

	if  (validateNewsletterSignupFields()) {
   		var strNewsletterName = document.getElementById("newsletter_name").value;
   		var strNewsletterEmail = document.getElementById("newsletter_email").value;
  		var strDataString = "name="+escape(strNewsletterName);
   		strDataString += "&email="+escape(strNewsletterEmail);
		
   		$.ajax({
  		 type: "GET",
  		 url: "/customers/newslettersignup.ashx",
  		 data: strDataString,
  		 error: function(xhr, status, error) {
         alert("error:" + status);
  		 },success: function(xml) {
   	   		$(xml).find('response').each(function(){
				
   				var intErrorCode = $(this).find("errorcode").text();
   	 			displayNewsletterMessage(parseInt(intErrorCode));
   		});
   }
 });
 }
}

function validateNewsletterSignupFields() {
	var strNewsletterName = document.getElementById("newsletter_name").value;
	var strNewsletterEmail = document.getElementById("newsletter_email").value;
	
	if (strNewsletterName == "") {
		displayNewsletterMessage(1);
		return false;
	} else if (strNewsletterEmail == "") {
		displayNewsletterMessage(2);
		return false;
	} else {
		return true;
	}
}

function displayNewsletterMessage(intErrorCode) {
	var strMessage = "";
	switch(intErrorCode)
	{
	case 0:
		strMessage = "Thank you for signing up. Please check your inbox.";
		break;
	case 1:
		strMessage = "Please write your name";
		break;
	case 2:
		strMessage = "Please write your e-mail address";
		break;
	case 3:
		strMessage = "E-mail address is already registered.";
		break;
	default:
		strMessage = "Please fill out all form fields.";
	}
	
	document.getElementById("newslettertext").innerHTML = strMessage;
}

  		
function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "";
}

function validateSigninFields() {

	var strEmail = document.getElementById("signin_email").value;
	var strPassword = document.getElementById("signin_password").value;
	var bRememberMe = document.getElementById("remember-me").checked;
	
	if ((strEmail == "") || (strPassword == "")) {
		generateSigninError(1);
		return false;
	} else {
		return true;
	}
}

function getSigninErrorDescription(intErrorCode) {
	var strErrorText = "";
	switch(intErrorCode)
	{
	case 1:
		strErrorText = "Please fill out all form fields.";
		break;
	case 2:
		strErrorText = "Wrong email address or password.";
		break;
	default:
		strErrorText = "Please fill out all form fields.";
	}
	return strErrorText;
}

function generateSigninError(intErrorCode) {
	var strErrorText = getSigninErrorDescription(intErrorCode);

   	document.getElementById("signinstatustext").innerHTML = "<li>" + strErrorText + "</li>";
   	$('#signinstatusbox').show();
}