// JavaScript Document

// register
var _click = 0;
var _register_click = 0;
function checkout_hide(sel){
	
	var selects = '#popup_login,#popup_forgot,#popup_create,#popup_confirm';
	if(_click) selects += ',#checkout_step1 .checkout_list,#checkout_step1 .checkout_form';
	
	$(selects).hide();
	$(sel).show();
}
function register(from_click,is_profile){
	if(from_click){
		_click = from_click;
		_register_click = 1;
	}
	if(is_profile){
		checkout_hide('#popup_profile',from_click);
		var obj = $('#popup_profile');
		
		
		//page click
		$(obj).find('.orderhistory .paging a').live('click',function(){
			$(obj).find('.orderhistory .paging a').removeClass('active');
			$(this).addClass('active');
			if(this.href.indexOf('order_page.php')==-1){
				this.href = this.href.replace('#','')+'order_page.php?page=0';
			}
			
			$.post(this.href,{},function(data){
				$('.order_list_profile').html(data);
			});
			return false;
		});
		
		//$(obj).find('a.findorder').click();
	}else{
		checkout_hide('#popup_create',from_click);
		var obj = $('#popup_create');
	
	}
	$(obj).find('input[name="create_email"]').blur(function(){
		if(!isEmail($(this).val())){
			return false;
		}				
		$.post(domain+'register.php?email='+$(this).val(),{'submit': 1},function(data){
			$(obj).find('span.check_email').html(data=='1'?'Available':'Already exists! <a href="#" onclick="login(1);return false;">Sign in</a>');
			$(obj).find('input[name="check_email"]').val(data);
		});
	});
	
	$(obj).find('select[name="country"]').change(function(){
		$.post(domain+'state_data.php?code='+$(this).val(),{},function(data){
			$(obj).find('select[name="state"]').html(data);
		});
	});
	
	var action = function(){
		var email = $(obj).find('input[name="create_email"]');
		var check_email = $(obj).find('input[name="check_email"]');
		if(check_email.val()=='0'){ alert('Please enter a valid email'); email.focus(); return false;}

		var password = $(obj).find('input[name="create_password"]');
		
		
		var repassword = $(obj).find('input[name="create_confirm"]');
		if(!is_profile){
			if(password.val() == ''){ alert('Please enter a password'); password.focus(); return false;}
			if(repassword.val() == ''){ alert('Please enter a retype password'); repassword.focus(); return false;}
		}
		if(repassword.val() != password.val()){ alert('Retype password and Password must same'); repassword.focus(); return false;}
		
		var firstname = $(obj).find('input[name="create_bill_firstname"]');
		if(firstname.val()==''){ alert('Please enter a  Firstname'); firstname.focus(); return false;}

		var lastname = $(obj).find('input[name="create_bill_lastname"]');
		if(lastname.val()==''){ alert('Please enter a  Lastname'); lastname.focus(); return false;}

		var mm = $(obj).find('select[name="mm"]');
		if(mm.val()=='0'){ alert('Please choose a  Month\s birthday'); mm.focus(); return false;}

		var dd = $(obj).find('select[name="dd"]');
		if(dd.val()=='0'){ alert('Please choose a  Day\s birthday'); dd.focus(); return false;}

		var yy = $(obj).find('input[name="yy"]');
		if(yy.val()==''){ alert('Please enter a Year\s birthday'); yy.focus(); return false;}
		
		if(!/[0-9]{4}/.test(yy.val())) { alert('Year\s birthday just a 4-digits'); yy.focus(); return false;}
		
		var addr_num = $(obj).find('input[name="create_bill_str_number"]');
		if(addr_num.val()==''){ alert('Please enter a Address Number'); addr_num.focus(); return false;}
		
		var addr_street = $(obj).find('input[name="create_bill_str_name"]');
		if(addr_street.val()==''){ alert('Please enter a Address Street'); addr_street.focus(); return false;}
		
		var country = $(obj).find('select[name="create_bill_country"]');
		if(country.val()=='0'){ alert('Please choose a Country'); country.focus(); return false;}
		
		var state = $(obj).find('select[name="create_bill_state"]');
		if(state.val()=='0'){ alert('Please choose a State'); state.focus(); return false;}
		
		var postal = $(obj).find('input[name="create_bill_postal"]');
		if(postal.val()==''){ alert('Please enter a Postal Code'); postal.focus(); return false;}
		if(!isPostCode(postal.val(),country.val())){ alert('Please enter a valid Code' );return false;}
		
		
		var phone_code = $(obj).find('input[name="create_bill_phone_code"]'); 
		var phone_number = $(obj).find('input[name="create_bill_phone_number"]'); 
		var phone_number2 = $(obj).find('input[name="create_bill_phone_number2"]'); 
		var addr_unit = $(obj).find('input[name="create_bill_unit"]'); 
		var city = $(obj).find('input[name="create_bill_city"]'); 
		
// shipping
		var ship_firstname = $(obj).find('input[name="create_ship_firstname"]');
		if(ship_firstname.val()==''){ alert('Please enter a  Firstname'); ship_firstname.focus(); return false;}

		var ship_lastname = $(obj).find('input[name="create_ship_lastname"]');
		if(ship_lastname.val()==''){ alert('Please enter a  Lastname'); ship_lastname.focus(); return false;}

		var ship_addr_num = $(obj).find('input[name="create_ship_str_number"]');
		if(ship_addr_num.val()==''){ alert('Please enter a Address Number'); ship_addr_num.focus(); return false;}
		
		var ship_addr_street = $(obj).find('input[name="create_ship_str_name"]');
		if(ship_addr_street.val()==''){ alert('Please enter a Address Street'); ship_addr_street.focus(); return false;}
		
		var ship_country = $(obj).find('select[name="create_ship_country"]');
		if(ship_country.val()=='0'){ alert('Please choose a Country'); ship_country.focus(); return false;}
		
		var ship_state = $(obj).find('select[name="create_ship_state"]');
		if(ship_state.val()=='0'){ alert('Please choose a State'); ship_state.focus(); return false;}
		
		var ship_postal = $(obj).find('input[name="create_ship_postal"]');
		if(ship_postal.val()==''){ alert('Please enter a Postal Code'); ship_postal.focus(); return false;}
		if(!isPostCode(ship_postal.val(),ship_country.val())){ alert('Please enter a valid Code');return false;}
		
		var security = $(obj).find('input[name="security"]');
		if(security.val()==''){ alert('Please enter a Security Code'); security.focus(); return false;}

		var ship_phone_code = $(obj).find('input[name="create_ship_phone_code"]'); 
		var ship_phone_number = $(obj).find('input[name="create_ship_phone_number"]'); 
		var ship_phone_number2 = $(obj).find('input[name="create_ship_phone_number2"]'); 
		var ship_addr_unit = $(obj).find('input[name="create_ship_unit"]'); 
		var ship_city = $(obj).find('input[name="create_ship_city"]'); 
		var newsletter = $(obj).find('input[name="newsletter"]'); 
		var security = $(obj).find('input[name="security"]'); 
		var hiden_security = $(obj).find('input[name="hiden_security"]'); 
		
		var args = {
			'email': email.val(),
			'password': password.val(),
			
			'firstname': firstname.val(),
			'lastname': lastname.val(),
			'mm': mm.val(),
			'dd': dd.val(),
			'yy': yy.val(),
			'addr_num': addr_num.val(),
			'addr_street': addr_street.val(),
			'addr_unit': addr_unit.val(),
			'country': country.val(),
			'state': state.val(),
			'postal': postal.val(),
			'city': city.val(),
			'phone_code': phone_code.val(),
			'phone_number': phone_number.val(),
			'phone_number2': phone_number2.val(),
			
			'ship_firstname': ship_firstname.val(),
			'ship_lastname': ship_lastname.val(),
			'ship_addr_num': ship_addr_num.val(),
			'ship_addr_street': ship_addr_street.val(),
			'ship_addr_unit': ship_addr_unit.val(),
			'ship_country': ship_country.val(),
			'ship_state': ship_state.val(),
			'ship_postal': ship_postal.val(),
			'ship_city': ship_city.val(),
			'ship_phone_code': ship_phone_code.val(),
			'ship_phone_number': ship_phone_number.val(),
			'ship_phone_number2': ship_phone_number2.val(),
			
			'newsletter': newsletter.val(),
			'security': security.val(),
			'hiden_security': hiden_security.val(),
			'submit': 1
		}
		if(is_profile) args['is_profile'] = 1;
		$.post(domain+'register.php',args,function(data){
			var dataObj = $.evalJSON(data);
			$(obj).find('.error_msg').html(dataObj['msg']);
			if(dataObj['no']=='0') checkout_hide('#popup_confirm');
			if(is_profile){
				return false;
			}else{
				//var href = domain.replace('http:','https:')+'cart.php';
				var href = domain + 'cart.php';
				var txt = 'Continue with your purchase';
				if(_click!=0){
					var href = domain+'product.php';
					var txt = 'Continue shopping';
				}
				$('#popup_confirm').find('a.link').attr({'href': href}).text(txt);
			}
		});
		
		return false;
	}
	$(obj).find('a.submit').click(action);
	$(obj).find('input').bind('keypress', function(e) {
        if(e.keyCode==13){
           return action();
        }
	});
}

function login(from_click){
	if(from_click) _click = from_click;
	_register_click = 0;
	checkout_hide('#popup_login',from_click);
	var obj = $('#popup_login');
	var action = function(){
		var email = $(obj).find('input[name="login_email"]'); 
		var password = $(obj).find('input[name="login_psw"]'); 
		if(!isEmail(email.val())){ alert('Please enter a valid Email');email.focus();return false;}
		if(password.val()==''){ alert('Please enter a Password');password.focus();return false;}
	
		var args = {
			'email': email.val(),
			'password': password.val(),
			'submit': 1
		}
		$('#popup_login .error_msg').html('<span style="color: green">Processing...</span>');
		$.post(domain+'login.php',args,function(data){
			if(data=='0') $('#popup_login .error_msg').html('<span style="color: red;">Invalid Email/Password, Try again!</span>');
			else{
				$('#toolbar_user').html(data);	
				
				//window.location.reload();
				window.location.href  = http+'profile.php';
				
			}
		});
		
		return false;
	};
	$(obj).find('a.submit').click(action);
	$(obj).find('input').bind('keypress', function(e) {
		if(e.keyCode==13){
			return action();
        }
	});
}

function forgot(from_click){
	if(from_click) _click = from_click;
	checkout_hide('#popup_forgot');
	var obj = $('#popup_forgot');
	var action = function(){
		var email = $(obj).find('input[name="forgot_email"]'); 
		if(!isEmail(email.val())){ alert('Please enter a valid Email');email.focus();return false;}
		var args = {
			'email': email.val(),
			'submit': 1
		}
		$('#form_forgot .error_msg').html('');
		$.post(domain+'forgot.php',args,function(data){
			$('#popup_forgot_msg').show().find('.error_msg').html(data);
		});
		return false;
	}
	$(obj).find('a.submit').click(action);
	$(obj).find('input').bind('keypress', function(e) {
        if(e.keyCode==13){
            return action();
        }
	});
}

function register_cancel(){
	if(_register_click) caobox.hide();
	else{
		$('#popup_create').hide();
		$('#popup_login').show();
	}
}

function order_info(id){
	$('#popup_profile .edit_profile').hide();
	$('#popup_profile .edit_order').html('<p class="order_loading">Loading...</p>').show();
	$.post(domain+'order_info.php?id='+id,{},function(data){
		$('#popup_profile .edit_order').html(data);
	});
}

function back2profile(){
	$('#popup_profile .edit_order').hide();
	$('#popup_profile .edit_profile').show();
	
}

function order_page(p){
	$.post(domain+'order_page.php?page='+p,{},function(data){
		$('.order_list_profile').html(data);
	});
	
}

function search_order(){
	var obj = $('#popup_profile');
//	var num = 
	$.post(domain+'order_page.php?num='+num+'&ship='+shipcode,{},function(data){
		$('.order_list_profile').html(data);
	});
}

function send2friend(){
	var obj = $('#send2friend form');
	
	var action = function(){
		var firstname = $(obj).find('input[name="firstname"]');
		if(firstname.val()==''){alert('Please enter a Your name');firstname.focus();return false;}
		
		var lastname = $(obj).find('input[name="lastname"]');
		if(!isEmail(lastname.val())){alert('Please enter a Your email');lastname.focus();return false;}
		
		var to_name1 = $(obj).find('input[name="to_name1"]');
		var to_email1 = $(obj).find('input[name="to_email1"]');
		if(to_name1.val()!='' && !isEmail(to_email1.val())){ alert('Please enter a valid Email'); to_email1.focus();return false;}
		
		var to_name2 = $(obj).find('input[name="to_name2"]');
		var to_email2 = $(obj).find('input[name="to_email2"]');
		if(to_name2.val()!='' && !isEmail(to_email2.val())){ alert('Please enter a valid Email'); to_email2.focus();return false;}

		var to_name3 = $(obj).find('input[name="to_name3"]');
		var to_email3 = $(obj).find('input[name="to_email3"]');
		if(to_name3.val()!='' && !isEmail(to_email3.val())){ alert('Please input a valid Email'); to_email3.focus();return false;}
		
		
		if(to_name1.val()=='' && to_name2.val()=='' && to_name3.val()==''){alert('Please enter a Your friend\'s name:');to_name1.focus();return false;}
		var radio = $(obj).find('input[name="radio"]:checked').val();

		var content = $(obj).find('textarea[name="content"]');
		var productid = $(obj).find('input[name="productid"]');
		if(radio=='1' && content.val()==''){alert('Plase enter a Message');content.focus();return false;}
		
		var args = {
			'firstname':  firstname.val(),
			'lastname': lastname.val(),
			'sendto':[],
			'radio': radio,
			'content': content.val(),
			'productid': productid.val(),
			'submit': 1
		}
		if(to_name1.val()!='' && isEmail(to_email1.val())){
			args['to_name1'] =to_name1.val();
			args['to_email1'] = to_email1.val();
		}
		
		if(to_name2.val()!='' && isEmail(to_email2.val())){
			args['to_name2'] =to_name2.val();
			args['to_email2'] = to_email2.val();
		}
		
		if(to_name3.val()!='' && isEmail(to_email3.val())){
			args['to_name3'] =to_name3.val();
			args['to_email3'] = to_email3.val();
		}
		$(obj).find('a.submit').after('<span class="msg_sending">Sending...</span>');
		$.post(domain+'send2friend.php',args,function(data){
			$(obj).find('.before_send').hide();
			$(obj).find('.after_send').show();
			window.scrollTo(0, 0);
			
		});
		return false;
	};
	$(obj).submit(action);
	$(obj).find('a.submit').click(action);
}
function send2friend_other(){
	var obj = $('#send2friend form');
	$(obj).find('span.msg_sending').remove();
	$(obj).find('input[type="text"],textarea').val('');
	$(obj).find('.after_send').hide();
	$(obj).find('.before_send').show();
			
}

function findorder(){
	var obj = $('#popup_profile');
	var num = $(obj).find('input[name="ordernumber"]').val();
	var shipcode = $(obj).find('input[name="shipcode"]').val();
	if(!num || !shipcode){alert('Both Order Number and Zip Code are required');return false;}
	$.post(domain+'order_page.php?num='+num+'&shipcode='+shipcode,{},function(data){
		$('.order_list_profile').html(data);
	});
	return false;
}


function update_billing(){
	if($('#bill_firstname').val()==''){alert('Please enter billing information: First Name');$('#bill_firstname').focus();return false;}
	if($('#bill_lastname').val()==''){alert('Please enter billing information: Last name');$('#bill_lastname').focus();return false;}
	if($('#bill_str_number').val()==''){alert('Please enter billing information: Street Number');$('#bill_str_number').focus();return false;}
	if($('#bill_str_name').val()==''){alert('Please enter billing information: Street Name');$('#bill_str_name').focus();return false;}
	if($('#bill_country').val()=='0'){alert('Please enter billing information: Country');$('#bill_country').focus();return false;}
	if($('#bill_state').val()=='0'){alert('Please enter billing information: State/Provine');$('#bill_state').focus();return false;}
	if(!isPostCode($('#bill_postal').val(),$('#bill_country').val())){alert('Please input a valid billing information: Postal code');$('#bill_postal').focus();return false;}
	if($('#bill_phone_code').val()==''){alert('Please enter billing information: Phone Code');$('#bill_phone_code').focus();return false;}
	//if($('#bill_phone_number').val()=='' || $('#bill_phone_number2').val()==''){alert('Please enter billing information: Phone number');$('#bill_phone_number').focus();return false;}
	if(!isEmail($('#bill_email').val())){alert('Please enter billing information: Email Address');$('#bill_email').focus();return false;}

	var $inputs = $('#checkout_step1 input, #checkout_step1 textarea, #checkout_step1 select');
	var k = 0;
	var values = {};
	$inputs.each(function(i, el) {
		values[el.name] = $(el).val();
		k=k+1;
	});

	$.post('update_profile.php',values,function(data){
		$('.update_billing').html('<span style="color: green">Your billing information has been updated!</span>');
	})
return false;
}
/*############# Libraries #################*/
function isEmail(s){if(s=="") return false;
if(s.indexOf(" ")>0) return false;
if(s.indexOf("@")==-1)return false;
var i=1; var sLength=s.length;
if(s.indexOf(".")==-1) return false;
if(s.indexOf("..")!=-1)return false;
if(s.indexOf("@")!=s.lastIndexOf("@")) return false;
if(s.lastIndexOf(".")==s.length-1)return false;
var str="abcdefghikjlmnopqrstuvwxyz-@._0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for(var j=0;j<s.length;j++)
if(str.indexOf(s.charAt(j))==-1)
return false;return true;
}


