$(document).ready(function(){
	$("body").prepend(modal_html());
	$("body").prepend(modal_html_reg());
    $("form#vkiss_form").submit(function() {
		if ($("input#to").val() == "Enter Twitter name" || $("input#to").val() == "") {
			alert("Please enter the Twitter name of the person you want to send a virtual kiss to.");
			return false;
		}
	});
	
	$.get("down.asp", function(data){
  document.getElementById('memberlink').innerHTML = data;
});			

	
	$.preloadImages(
		"/common/images/modal_box_bottom.png",
		"/common/images/modal_box_top.png",
		"/common/images/close.png",
		"/common/images/sign_in_form.png",
		"/common/images/cancel.png",
		"/common/images/avatar.png",
		"/common/images/smiley.png",
		"/common/images/return_home.png",
		"/common/images/get_notified_hover.png",
		"/common/images/submit_survey_hover.png",
		"/common/images/cancel_hover.png",
		"/common/images/sign_in_form_hover.png",
		"/common/images/return_home_hover.png",
		"/common/images/send_virtual_kiss_hover.png"
	);
	$("input#site").labelify({ labelledClass: "dim" });
	$("input.email").labelify({ labelledClass: "dim" });
	$("#survey input.text").labelify({ labelledClass: "dim" });
	
	$("#news").newsticker();
	
	$.each($("#country option"), function(){
		if($(this).attr("value") == $.localise.defaultLanguage.substr(3, 2)) {
			$(this).attr("selected", "selected");
		}
	});
	
	$("#sign_in").jqm({
		trigger: '.m_sign_in',
		closeClass: 'close',
		overlay: 50
	});
	
	$("#Register").jqm({
		trigger: '.m_register',
		closeClass: 'close',
		overlay: 50
	});
	
	$("#thank_you").jqm({
		closeClass: 'close_action',
		overlay: 50
	});
	
	$("#error").jqm({
		closeClass: 'close_action',
		overlay: 50
	});
	$("#error1").jqm({
		closeClass: 'close_action',
		overlay: 50
	});
	$(".get_notified").hover(
		function() {
			this.src = this.src.replace("get_notified.png","get_notified_hover.png");
		},
		function() {
			this.src = this.src.replace("get_notified_hover.png","get_notified.png");
		}
	);
	$(".visit_our_blog").hover(
		function() {
			this.src = this.src.replace("visit_our_blog.png","visit_our_blog_hover.png");
		},
		function() {
			this.src = this.src.replace("visit_our_blog_hover.png","visit_our_blog.png");
		}
	);
	$(".m_submit_survey").hover(
		function() {
			this.src = this.src.replace("submit_survey.png","submit_survey_hover.png");
		},
		function() {
			this.src = this.src.replace("submit_survey_hover.png","submit_survey.png");
		}
	);
	$(".cancel").hover(
		function() {
			this.src = this.src.replace("cancel.png","cancel_hover.png");
		},
		function() {
			this.src = this.src.replace("cancel_hover.png","cancel.png");
		}
	);
	$("#sign_in .sign_in").hover(
		function() {
			this.src = this.src.replace("sign_in_form.png","sign_in_form_hover.png");
		},
		function() {
			this.src = this.src.replace("sign_in_form_hover.png","sign_in_form.png");
		}
	);
	$("#Register .sign_in").hover(
		function() {
			this.src = this.src.replace("register_homer.png","register_homer_hover.png");
		},
		function() {
			this.src = this.src.replace("register_homer_hover.png","register_homer.png");
		}
	);
	$(".return_home").hover(
		function() {
			this.src = this.src.replace("return_home.png","return_home_hover.png");
		},
		function() {
			this.src = this.src.replace("return_home_hover.png","return_home.png");
		}
	);
	$(".send_virtual_kiss").hover(
		function() {
			this.src = this.src.replace("send_virtual_kiss.png","send_virtual_kiss_hover.png");
		},
		function() {
			this.src = this.src.replace("send_virtual_kiss_hover.png","send_virtual_kiss.png");
		}
	);
	
	
	$("#error img").click(
						  function()
						  {
							  $('#Register').jqmShow();
						  }
						  );
	$("#error1 img").click(
						  function()
						  {
							  $('#sign_in').jqmShow();
						  }
						  );
	$("#thank_you img").click(
						  function()
						  {
							  location.href="/download/";
						  }
						  );
	function isEmail(str){ 
	res = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/; 
	var re = new RegExp(res);
	return !(str.match(re) == null); 
	}
	/* Register signup */
	$("#Register form").submit(function(){
	  var action = $(this).attr("action");
	  var email = $(this).find("input[name='email']");
	  var password = $(this).find("input[name='password']");
	  var repassword = $(this).find("input[name='repassword']");
	  var company = $(this).find("input[name='company']");
	  var job = $(this).find("input[name='job']");
	  var telephone = $(this).find("input[name='telephone']");
	  var city = $(this).find("input[name='city']");
	  var country = $(this).find("input[name='country']");
	  var keep = $(this).find("input[name='keep']");
	  //alert(email.attr("value"));
	  if(!isEmail(email.attr("value")))
		{
			alert("Email error!");
			email.focus();
			return false;
		}
		if(password.attr("value") == "")
		{
			alert("password not empty!");
			$("#password").focus();
			return false;
		}
		if(repassword.attr("value") == "")
		{
			alert("Confirm Password not empty!");
			$("#repassword").focus();
			return false;
		}
		if(password.attr("value") != repassword.attr("value"))
		{
			alert("Two password the importation is inconsistent!");
			repassword.attr("value", "");
			$("#repassword").focus();
			return false;
		}
		if(telephone.attr("value") == "")
		{
			alert("Telephone not empty!");
			$("#telephone").focus();
			return false;
		}
		if(isNaN(telephone.attr("value")))
		{
			alert("Must be a number!");
			$("#telephone").focus();
			return false;
		}
		
	  $.post(action, {ajax:true, email: email.attr("value"),password:password.attr("value"),company:company.attr("value"),job:job.attr("value"),telephone:telephone.attr("value"),city:city.attr("value"),country:country.attr("value"),keep:keep.attr("checked")}, function(response){
																	   //alert(response);
	    switch(response) {
	      case "SUCCESS":
	        $('#thank_you').jqmShow();
			$('#Register').jqmHide();
	      break;
	      case "EMAIL_EXISTS":
          	modal_error("That email addresss already exists", "Thanks again for signing up! If you haven't already, you should receive a confirmation email shortly.");
		  	$('#Register').jqmHide();
	      break;
	      case "INVALID_EMAIL":
          modal_error("Invalid Email", "The email you entered is invalid, please try again.");
		  $('#Register').jqmHide();
	      break;
	      default:
	        modal_error("Error", "An unknown error occurred.");
			$('#Register').jqmHide();
	      break;
	    }
	    email.attr("value", "");
	    $("input.email").labelify({ labelledClass: "dim" });
	  });
	  return false;
	});
	
	/* Sign in form */
	$("#sign_in form").submit(function(){
	  var action = $(this).attr("action");
	  var email = $(this).find("input[name='email']");
	  var password = $(this).find("input[name='password']");
	  //var keep = $(this).find("input[name='keep']");
	  //alert(email.attr("value"));
	  if(email.attr("value") == "")
		{
			alert("用户名不能为空!");
			email.focus();
			return false;
		}
		if(password.attr("value") == "")
		{
			alert("密码不能为空!");
			password.focus();
			return false;
		}
	  $.post(action, {ajax:true, email: email.attr("value"),password:password.attr("value")}, function(response){
																	   //alert(response);
	    switch(response) {
	      case "SUCCESS":
		  	modal_success("登录成功!","谢谢你再次登录。","");
	        //$('#sign_in #thank_you').jqmShow();
			$('#sign_in').jqmHide();
	      break;
	      case "EMAIL_EXISTS":
          	modal_error1("用户名不存在", "请与网站管理员联系。");
		  	$('#sign_in').jqmHide();
			email.attr("value", "");
	      break;
	      case "INVALID_EMAIL":
			  modal_error1("密码错误", "请重先输入密码.");
			  $('#sign_in').jqmHide();
			  password.attr("value", "");
	      break;
	      default:
	        modal_error1("Member Lock", "Your member has not yet opened, please contact the administrator with the background.");
			$('#sign_in').jqmHide();
	      break;
	    }
	    $("input.email").labelify({ labelledClass: "dim" });
	  });
	  return false;
	  
	});
	
});
function modal_error1 (heading, paragraph) {
	$("#error1").find("h2").text(heading);
	$("#error1").find("p").text(paragraph);
	$('#error1').jqmShow();
}

function modal_error (heading, paragraph) {
	$("#error").find("h2").text(heading);
	$("#error").find("p").text(paragraph);
	$('#error').jqmShow();
}

function modal_success (heading, paragraph, h2_class) {
	$("#thank_you").find("h2").text(heading);
	$("#thank_you").find("p").text(paragraph);
	if(h2_class) {
		$("#thank_you").find("h2").addClass(h2_class);
	}
	$('#thank_you').jqmShow();
}
$.preloadImages = function() {
	for(var i = 0; i<arguments.length; i++) {
		$("<img>").attr("src", arguments[i]);
	}
}
