var is_ie = document.all ? true : false;
var is_ff = window.addEventListener ? true : false;

function redirect(url) {
	window.location.replace(url);
}

function get_cookie(name) {
	cookiename = name + '=';
	cookiepos = document.cookie.indexOf(cookiename);
	if(cookiepos != -1) {
		cookiestart =cookiepos+cookiename.length;
		cookieend = document.cookie.indexOf(';', cookiestart);
		if(cookieend == -1) {
			cookieend = document.cookie.length;
		}
		return unescape(document.cookie.substring(cookiestart, cookieend));
	}
	return '';
}

function set_cookie(name, value) {
	expires = new Date();
	expires.setTime(expires.getTime() + 2592000);
	document.cookie = name + "=" + value + "; path=/; expires=" + expires.toGMTString();
}

function checkLength(o,n,min,max) {	
	alert('here??');
	if ( o.val().length > max || o.val().length < min ) {
		o.addClass('ui-state-error');
		nn = n+"_error";
		$("#"+nn).addClass("ui-state-highlight");
		alert(nn);
		$("#"+nn).html("Length must be between "+min+" and "+max+".");
		return false;
	} else {		
		return true;
	}
}

function checkRegexp(o,n,regexp,s) {
	if ( !( regexp.test( o.val() ) ) ) {
		o.addClass('ui-state-error');
		//show_error(n,s);
		nn = n+"_error";
		$("#"+nn).addClass("ui-state-highlight");
		$("#"+nn).html(s);
		return false;
	} else {
		return true;
	}
}

function show_error(n,s){
	$("#"+n).addClass("ui-state-error");
	$("#"+n+"_error").addClass("ui-state-highlight");
	$("#"+n+"_error").html(s);
}

function is_valid_email(email){
	regexp = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i
	return regexp.test(email);
}

function checkEmail(){
	var email = $("#email").val();
	if (email.length == 0){
		show_error("email","email can not be blank.");
		return false;
	}
		
	if (!is_valid_email(email)){
		show_error("email","not valid format.");
		return false;
	}
	
	flag = true;	
	$.ajax({
		type:"GET",
		url:"/register/checkemail",
		data:"email="+email,
		dataType:'json',
		async:false,
		success:function(json){
			if (json.error==1){
				flag = false;
				show_error("email",json.message);
			}
	}});	
	return flag;
}

function check_register_form(){
	$(".ui-state-highlight").html("");
	$(".ui-state-highlight").removeClass("ui-state-highlight");
	$(".ui-state-error").removeClass("ui-state-error");
	
	var flag = true;
	if ($("#firstname").val().length >20 || $("#firstname").val().length < 3){
		flag = false;
		show_error("firstname","Length must be between 3 and 20");
	}
	
	if ($("#lastname").val().length >20 || $("#lastname").val().length < 3){
		flag = false;
		show_error("lastname","Length must be between 3 and 20");
	}
	
	if ($("#password").val().length >30 || $("#password").val().length < 6){
		flag = false;
		show_error("password","Length must be between 6 and 30");
	}
	
	if ($("#hearfrom").val().length >30 || $("#hearfrom").val().length < 2){
		flag = false;
		show_error("hearfrom","can not be blank.");
	}
	if (!checkEmail()){
		flag = false;
	}
	return flag;
}
function show_register(){
	if ($("#reg-dialog").length == 0){
		$.get("/register",function(data){
			$("body").append(data);
			$("#reg-dialog").dialog({
				autoOpen:false,
				height: 630,
				width: 400,
				position: ['center'],
				overlay: {opacity: 0.5,background: "black"},
				buttons:{"SUBMIT":function(){
					if (check_register_form()){
						$("#reg-form").submit();
						//$(this).dialog("close");
					}
				}},
				closeOnEscape:true,
				modal:true,
				title:"REGISTER"
			}).dialog("open");
		});
	}	else	{
		$("#reg-dialog").dialog("open");
	}
}

function show_size_guide(){
	if ($(".size-guide-div").length == 0){
		$.get("/content/sizeguide",function(data){
			$("body").append(data);			
			$(".size-guide-div").fadeIn("normal");
		});
	}	else	{
		$('.size-guide-div').fadeIn("normal");
	}
}

function show_buying_guide(){
	if ($(".buying-guide-div").length == 0){
		$.get("/content/buying",function(data){
			$("body").append(data);			
			$(".buying-guide-div").fadeIn("normal");
			$(".buying-guide-div").center();
		});
	}	else	{
		
		$(".buying-guide-div").fadeIn("normal");
		$(".buying-guide-div").center();
	}
}

function signup(){
	$("#signup_email").removeClass("ui-state-error");
	var email = $("#signup_email").val();
	if (email == ''){
		return ;
	}
	if (!is_valid_email(email)){
		$("#signup_email").addClass("ui-state-error");
		show_message('not valid email');
	}
	$.ajax({
		type:"GET",
		url:"/signup/do",
		data:"email="+email,
		dataType:'json',
		success:function(json){			
			show_message(json.message);
			if (json.error == 0){
				$("#signup_email").val('');
			}
	}});
}

function show_message(msg){
	alert(msg);
}

function select_currency(id){
	window.location = '/currency?id='+id;
}
