				/*/																\*\
									Tinno Trade Mall System
					©Developer: Chen Song Jie, Chen@Sjolzy.Cn, http://sjolzy.cn/
				\*\																/*/

$(document).ready(function(){
	$('#minicart').mouseover(function(){
		$('#minicart-dropdown').css('height','auto').slideDown("1000");
	});
	$('body').click(function(){
		$('#minicart-dropdown').css('height','0px').slideUp("1000");
	});
	$('#search').click(function(){
		if( sj.empty('q') ){
			sj.msg( 'Please input a search value' );return false;
		}
	});
});

var sys={
	init:function(){}
}

var sj={
	tips:function(Obj,filedName,info){
		var tipObj = $("#"+filedName);
		if(tipObj.length>0) tipObj.remove();
		Obj.focus().after("<span style='color:#F06' id='"+filedName+"'> "+info+"</span>");
	},
	msg:function(m){
		sj.show('msg');
		sj.h('msg',m);
		sj.t("sj.hide('msg')",2);
	},
	ok:function(Obj,filedName){
		var tipObj = $("#"+filedName);
		if(tipObj.length>0) tipObj.remove();
		Obj.after("<span style='color:#0C0' id='"+filedName+"'> √correct</span>");
	},
	check:function(Obj,type){
		var reg	= {
			"email" : /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/,
			"eng" : /^[A-Za-z]+$/,
			"chn" :/^[\u0391-\uFFE5]+$/,
			"number" : /^\d+$/,
			"password" : /^(\w){6,20}$/,
			"qq" : /[1-9][0-9]{4,}/,
			"phone" : /^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/,
			"zipcode" :  /^[A-Za-z0-9]+$/,///^[1-9]\d{5}$/,
			"ip" : /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/
		};
		return reg[type].test(Obj);
	},
	empty:function(v){
		var _v	= this.v(v);
		if(_v==''||'undefined'==_v||null==_v){
			return true;
		}
		return false;
	},
	go:function(i){
		if(i==null){
			return false;
		}
		history.go(i);
	},
	href:function(url){
		if(url==null){
			url = '';
		}
		window.location.href=URL+url
	},
	g:function(o){
		return $('#'+o);
	},
	v:function(o){
		return this.g(o).val();
	},
	f:function(a,b){
		return this.g(a).val(this.v(b));
	},
	h:function(o,h){
		return sj.g(o).html(h);
	},
	t:function(f,t){
		window.setTimeout(f,t*1000);
	},
	disable:function(o){
		return this.g(o).attr('disabled','disabled');
	},
	able:function(o){
		return this.g(o).attr('disabled','');
	},
	hide:function(o){
		return this.g(o).hide();
	},
	show:function(o){
		return this.g(o).show();
	},
	reflush:function(){
		window.location.href=location.href;
	},
	ajaxDo:function(url,data){
		$.ajax({
			url		: url,
			data	: data,
			type	: 'post',
			success	: function( rs ){
				if(!rs||''==rs||null==rs){
					return false;
				}
				eval("var u="+rs+";");
				sj.msg(u['info']);
			},
			error	: function(){
				return false;
			}
		});
	}
}
var currency={
	change:function(va){
		jQuery.ajax({
			type:'post',url:'/Index/setCurrency/',
			data:{'curr':va},
			beforeSend:function(){
				sj.msg("Please wait...");
			},
			success:function(rs){
				if(rs==1){
					sj.msg("Currency rate change success!");
					sj.t("sj.reflush();",2);
				}else{
					return false;
				}
			},
			error:function(){
				return false;
			}
		});
	}
}

var cart = {
	qty:0,
	err:['Please enter a valid number','Number must be greater than 0'],
	add:function(){
		var _qty 		= 'qty';
		var _qty_tips	= 'emailTips';
		var qty		= sj.v(_qty);
		if(sj.empty(_qty)){
			sj.tips(sj.g(_qty),_qty_tips,this.err[0]);return false;
		}
		if( !sj.check(qty,'number') ){
			sj.tips(sj.g(_qty),_qty_tips,this.err[0]);return false;
		}else if( qty<1 ){
			sj.tips(sj.g(_qty),_qty_tips,this.err[1]);return false;
		}else{
			return true;
		}
	},
	remove:function(id){
		if(id==null){
			return false;
		}
		sj.ajaxDo('/Cart/removeItem','id='+id);
		sj.t('location.href=location.href;',3);
	},
	update:function(id,qty){
		sj.ajaxDo('/Cart/updateItem','id='+id+'&qty='+qty);
		sj.t('location.href=location.href;',3);
	}
}

var user = {
	checkField:['first_name','last_name','email','password','re_password'],
	errorMsg:['require','require','require','require','require'],
	checkMsg:'',
	tipId:'registerTips',
	loginSuccessUrl:'',
	rs:false,
	exists:false,
	checkNull:function(){
		this.rs = false;
		$.each(this.checkField,function(i,j){
			if( sj.v(j)=='' ){
				user.checkMsg = user.errorMsg[i];
				sj.g(j).focus();
				sj.tips(sj.g(j),user.tipId,user.checkMsg);
				return false;
			}else{
				user.checkMsg = '';
			}
		})
		this.rs = ''==this.checkMsg?true:false;
	},
	register:function(){
		this.checkNull();
		if(this.rs){
			if(sj.v('password')!=sj.v('re_password')){
				sj.tips(sj.g('password'),user.tipId,'error');return false;
			}
			if(!sj.check(sj.v('email'),'email')){
				sj.tips(sj.g('email'),user.tipId,'invalid');return false;
			}
			return true;
		}
		return false;
	},
	login:function(){
		var _email	= 'email';
		var _pwd	= 'password';
		var email	= sj.v(_email);
		var pwd		= sj.v(_pwd);
		if(sj.empty(_email)){
			sj.tips(sj.g(_email),user.tipId,'require');return false;
		}else if(sj.empty(_pwd)){
			sj.tips(sj.g(_pwd),user.tipId,'require');return false;
		}
		$.ajax({
			url:'/User/login',
			data:{'email':email,'password':pwd,'loginBt':1},
			type:'post',
			beforeSend:function(){
				sj.disable('loginBt');
			},
			success:function(rs){
				if(!rs||''==rs||null==rs){
					return false;
				}
				eval("var u="+rs+";");
				sj.tips(sj.g('loginBt'),'loginTips',u['info']);
				if( u['status']==1 ){
					sj.href(user.loginSuccessUrl);
				}
			},
			error:function(){
				alert('error');
			},
			complete:function(){
				sj.able('loginBt');
			}
		});
	},
	checkExists:function( email ){
		user.exists = false;
		if( null == email ){
			return false;
		}
		var emailId	= 'billing_email';
		if(!sj.check(email,'email')){
			sj.tips(sj.g(''),user.tipId,'invalid');
			return false;
		}
//		else{
//			sj.tips(sj.g(emailId),user.tipId,'');
//		}
		$.ajax({
			url:'/User/checkExists',
			data:{'email':email},
			type:'post',
			beforeSend:function(){ },
			success:function(rs){
				if(!rs||''==rs||null==rs){
					return false;
				}
				eval("var u="+rs+";");
				if( 0==u['status'] ){
					user.exists = true;
				}
				sj.msg( u['info'] );
			},
			error:function(rs){
				return false;
			},
			complete:function(){
				if( true === user.exists&&true!==logined ){
					sj.g(emailId).focus();
				}
			}
		});

		return true;
	}
}

var checkout = {
	liC:[ 'chk_login','chk_billing','chk_shipping','chk_shipping_method','chk_payment','chk_review' ],
	method:'',
	back:function(){
		$('#checkout_account').slideDown();
		$('#checkout_panel').slideUp();
		$('#back_account').hide();
	},
	setMethod:function(){
		var method	= 'register';
		this.method = method;
		('register'==method)?$('.registerOter').show():$('.registerOter').hide();
		$('#checkout_account').slideUp();
		$('#checkout_panel').slideDown();
		$('#back_account').show("slow");
	},
	init:function(){
		this.chooese(( false ==logined )?'login':'billing');
	},
	login:function(){
		user.loginSuccessUrl = 'Cart/checkOut';
		user.login();
	},
	save:function(){
		if( !billing.ok ){
			billing.save();
			sj.msg( ' Please check your billing address information. ' );
			this.href('a_billing_address');
			return false;
		}else if( !shipping.ok ){
			shipping.save();
			sj.msg( ' Please check your shipping address information. ' );
			this.href('a_shipping_address');
			return false;
		}else if( !shipping_method.ok ){
			shipping_method.save();
			sj.msg( ' Please select shipping method. ' );
			this.href('a_shipping_method');
			return false;
		}else if( !payment.ok ){
			payment.save();
			sj.msg( ' Please select payment method. ' );
			this.href('a_payment_method');
			return false;
		}
		return true;
	},
	chooese:function(o){
		sj.msg( ' choose -> '+o );
	},
	href:function(u){
		sj.href('Cart/checkOut#'+u);
	}
}
var billing={
	field:['first_name','last_name','address','city','state','zip','country','phone','email'],
	tipId:'billingTipId',
	ok:false,
	save:function(){
		$.each(this.field,function(i,j){
			var a = 'billing_'+j
			var b = 'shipping_'+j;
			if(sj.empty(a)){
				billing.ok	= false;
				sj.tips(sj.g(a),billing.tipId,'require');
				return false;
			}else{
				if(j=='email'){
					if(!sj.check(sj.v(a),'email')){
						sj.tips(sj.g(a),billing.tipId,'invalid');
						billing.ok	= false;
						return false;
					}else{
						billing.ok	= true;
					}
				}else if(j=='country'&&logined==false){
					sj.f(b,a);
				}else{
					billing.ok	= true;
				}
			}
			if(sj.empty(b)){
				sj.f(b,a);
			}
		})
		if( !billing.ok ){
			billing.uncheck();
			return false;
		}
		if( checkout.method == 'register' ){	// register, check password
			var a	= 'billing_password';
			var b	= 'billing_re_password';
			if( sj.empty(a) ){
				sj.tips(sj.g(a),billing.tipId,'require');billing.uncheck();return false;
			}else if( sj.empty(b) ){
				sj.tips(sj.g(b),billing.tipId,'require');billing.uncheck();return false;
			}
			if( sj.v(a)!=sj.v(b) ){
				sj.tips(sj.g(b),billing.tipId,'error');billing.uncheck();return false;
			}
		}
		var use_to_billing	= $("input[name='billing[use_to_billing]']:checked").val();
		if(billing.ok){
			( use_to_billing == 1 )?sj.hide('shipping_address_panel'):sj.show('shipping_address_panel');
			if( use_to_billing == 1 ){
				shipping.save();
			}
		}
		return true;
	},
	uncheck:function(){
		$("input[name='billing[use_to_billing]']:checked").attr("checked",false);
	}
}
var shipping={
	field:['first_name','last_name','email','address','city','state','zip','country','phone'],
	tipId:'shippingTipId',
	ok:false,
	save:function(){
		$.each(this.field,function(i,j){
			var a = 'shipping_'+j
			var b = 'billing_'+j;
			if(sj.empty(a)){
				shipping.ok	= false;
				sj.tips(sj.g(a),shipping.tipId,'require');return false;
			}else{
				shipping.ok	= true;
			}
			if(sj.empty(a)){
				sj.f(a,b);
			}
		})
		if( !billing.ok ){
			billing.save();
			return false;
		}
		if(shipping.ok){
			shipping_method.save();
		}
	}
}
var shipping_method={
	ok:false,
	save:function(){
		var sm	= $("input[name='order[shipping_method]']:checked").val();
		if(null==sm){
			this.ok = false;
			sj.msg('Please Select Shipping Method !');
			return false;
		}
		if( !shipping.ok ){
			shipping_method.uncheck();
			shipping.save();
			return false;
		}

		// get shipping price by choose method
		$.ajax({
			url		: '/Shipping/getShippingPrice',
			data	: {'shipping_method':sm},
			type	: 'post',
			success	: function( rs ){
				if(!rs||''==rs||null==rs){
					sj.msg('Shipping fee get error, please retry!');
					return false;
				}
				eval("var u="+rs+";");
				var deny_country	= u['data']['deny_country'];
				var if_deny			= false;
				if(typeof(deny_country)== "object"){
					var shippingCountry	= $('#shipping_country').val();
					$.each(deny_country,function(i,k){
						if( i == shippingCountry ){
							alert( 'Now do not support the '+sm+' to the '+i );
							if_deny	= true;
						}
					});
				}
				if( if_deny ){
					return false;
				}
				$('#chk_review_total_price').html(u['data']['formatTotalPrice']);
				$('#chk_review_shipping_price').html(u['data']['formatShippingPrice']);
				$('#chk_review_sub_total_price').html(u['data']['formatSubTotalPrice']);
				$('#order_'+sm+'_shipping_amount').val(u['data']['shippingPrice']);
				shipping_method.ok = true;
			//	payment.save();
			},
			error	: function(){
				return false;
			}
		});
		if(shipping_method.ok){
			return true;
		}
		return false;
	},
	uncheck:function(){
		$("input[name='order[shipping_method]']:checked").attr("checked",false);
	}
}
var payment={
	ok:false,
	save:function(){
		var pm	= $("input[name='order[payment_method]']:checked").val();
		if(null==pm){
			this.ok = false;
			sj.msg('Please Select Payment Method !');
			return false;
		}
		if( !shipping_method.ok ){
			shipping_method.save();
			return false;
		}
		this.ok = true;
	}
}

var product={
	bigImg:function(s){
		var _self = $('#proBigPic');
		var _panel	= $('#proBigPicDiv');
		_panel.addClass('layer').append('<div id="loading" style="text-align:center;margin-top:-150px;" ><p align=center><img src="'+TPL+'images/grey.gif" /></p></div>');
		var img = new Image();
		$(img).load(function(){
			_self.attr("src", s);
			_panel.fadeIn("slow").removeClass('layer');
			$('#loading').remove();
		}).attr("src", s); 
		return false;
	}
}
sys.init();
