var shopmyaccountApp = null;

function shopmyaccountAppLoader(options){
	shopmyaccountApp = new shopmyaccountAppSystem(options);
	shopmyaccountApp.init();
}

function shopmyaccountAppSystem(options){
	
	this.options 			= options;
	this.animationActive	= false;
	
	this.init = function () {
		if(this.options.view){
			switch(this.options.view){
				
				case 'login':
					if(this.options.loginFailed){	
						$("#shopMyAccountPassword").focus();
					}else{
						$("#shopMyAccountUsername").focus();
					}
				break;
				
				case 'customerMyAccount':
					$("#shopMyAccount-Tabs-Container").tabs();
				break;
				
				case 'quickLoginApp':
					$(".quickLoginAppDIV").corner("5px");
				break;
				
				default:
					alert('Unknown view name ...');
				break;
			}
		}else{
			alert('No view name given ...');
		}
	}
	
	this.quickAppLogin= function (){
		if(this.checkLoginForm()){
			$("#shopMyAccountLoginForm").submit();
		}
	}
	
	this.checkLoginForm = function (){
		this.resetOutlineFormField(".loginField");
		this.resetOutlineFormField(".loginFieldQuickApp");
		if($("#shopMyAccountUsername").val() == ''){
			this.outlineFormField("#shopMyAccountUsername");
			$("#shopMyAccountUsername").focus();
			return false;
		}else if($("#shopMyAccountPassword").val() == ''){
			this.outlineFormField("#shopMyAccountPassword");
			$("#shopMyAccountPassword").focus();
			return false;
		}else{
			return true;
		}
	}
	
	this.outlineFormField = function (selector){
		$(selector).addClass('myAccountField-mandatory');
	}
	
	this.resetOutlineFormField = function (selector){
		$(selector).removeClass('myAccountField-mandatory');
	}
	
	this.toggleCustomerType = function(type, selector) {
		if(type == 'company') {
			$(selector).css('display', 'inline');
		} else if(type == 'private') {
			$(selector).css('display', 'none');
		}
	}
	
	this.toggleBillingAddress = function (show){
		if(show == 'yes'){
			$(".myAccountBillingField").attr("disabled", true);
		}else{
			$(".myAccountBillingField").attr("disabled", false);
		}
	}
	
	this.showCustomerModalForgetPw = function(){
		
		$("#modalCustomerForgetPasswordDiv").modal({
				
			onOpen : function(dialog) {
				dialog.overlay.fadeIn('slow', function () {
					dialog.container.slideDown('slow', function () {
						dialog.data.fadeIn('slow');
						/** Browser Test */
						if(systemBrowserName != 'ie') {
							$("#simplemodal-container").corner("12px");
							$("#forgetPasswordCancel").corner("4px");
							$("#forgetPasswordSubmit").corner("4px");
						}
					});
				});
			},
			
			onShow : function(dialog) {
	
				$("#forgetPasswordSubmit").click(function(){
					var email = $("#forgetpwemail").val();
					
					if(shopmyaccountApp.isValidEmailAddress(email)){
						
						$.get(sysHttpProtocol + "://" + sysFullWebPath + "apps/shopmyaccount/ajax.php?task=sendForgetPW",{email:email}, function(data){ 
							
							$("#forgetpwemail").val('');
							$(".buttonCancelDiv").html('');
							$(".buttonSubmitDiv").html('');
							$(".buttonCloseDiv").css('display','inline');
							
							/** Browser Test */
							if(systemBrowserName != 'ie') {
								$("#forgetPasswordClose").corner("4px");
							}	
							
							$("#forgetpwtext").html(data);
								
						});    	
						
					}else{
						shopmyaccountApp.resetFieldsForgetPassword();
		                $("#forgetpwemail").css('border', '1px solid #1b6d37');
		                $("#forgetpwemail").focus();
					}
	
				});
				
				$("#forgetPasswordClose").click(function(){
					$.modal.close();
				});	
				
				
			},
			
			onClose : function (dialog) {
				dialog.data.fadeOut('fast', function () {
					dialog.container.slideUp('fast', function () {
						dialog.overlay.fadeOut('fast', function () {
							$.modal.close(); // must call this!
						});
					});
				});
			},
			
			overlayClose:true,
			closeClass: "cancel"
	
		});
	
	}

	this.resetFieldsForgetPassword = function() {
		$("#forgetpwemail").css('border', '1px solid #98ba64');
	}
	
	this.viewOrderDetails = function(orderID){
		$("#shopMyAccountOrdersPreviewDIV").fadeOut(500, function (){
			$.get(sysHttpProtocol + "://" + sysFullWebPath + "apps/shopmyaccount/ajax.php?task=getOrderDetails", {orderID: orderID}, function (res){
				$("#shopMyAccountOrdersPreviewDIV").html(res).fadeIn(850);
			});
		});
		
	}
	
	this.reOrder = function(orderID){
		$.get(sysHttpProtocol + "://" + sysFullWebPath + "apps/shopmyaccount/ajax.php?task=reOrder", {orderID: orderID}, function (res){
			if(res == 'ok'){
				 document.location.href = shopUrl;
			}
		});
	}
	
	this.showOrderConfirmation = function(orderID){
		var popupUrl = sysHttpProtocol + '://' + printOutUrl + '?orderID=' + orderID;
		window.open(popupUrl, '', '');
	}
	
	this.checkValidEmail = function(){
		var email = $("#email").val();
		if(this.isValidEmailAddress(email)){
			return $.get(sysHttpProtocol + "://" + sysFullWebPath + "apps/shopmyaccount/ajax.php?task=checkValidEmail", {email: email}, function(res){
				if(res == 1){
					$("#valid-email1-container").addClass('emailOK');
					$("#valid-email1-container").removeClass('emailNOK');
				} else {
					$("#valid-email1-container").addClass('emailNOK');
					$("#valid-email1-container").removeClass('emailOK');
				}
				return true;
			});
		}else{
			$("#valid-email1-container").addClass('emailNOK');
			$("#valid-email1-container").removeClass('emailOK');
			return false;
		}
	}
	
	this.resetMandatoryFields = function () {
		$(".myAccountField").removeClass('myAccountField-mandatory');
		$(".myAccountSelectField").removeClass('myAccountSelectField-mandatory');
	}
	
	this.saveAccountData = function(){
		
		shopmyaccountApp.resetMandatoryFields();
		
		if(!this.checkValidEmail()){
			$("#email").focus();
			return false;
		}
		
		if($('input[name=customertype]:checked').val() == 'company') {	
			if($("#companyname").val() == '') {
				$("#companyname").addClass('myAccountField-mandatory');
				$("#companyname").focus();
				return false;
			}	
		}
		
		if($("#familyname").val() == '') {
			$("#familyname").addClass('myAccountField-mandatory');
			$("#familyname").focus();
			return false;
		}
		
		if($("#name").val() == '') {
			$("#name").addClass('myAccountField-mandatory');
			$("#name").focus();
			return false;
		}
		
		if($("#address1").val() == '') {
			$("#address1").addClass('myAccountField-mandatory');
			$("#address1").focus();
			return false;
		}
		
		if($("#zipcode").val() == '') {
			$("#zipcode").addClass('myAccountField-mandatory');
			$("#zipcode").focus();
			return false;
		}
		
		if($("#city").val() == '') {
			$("#city").addClass('myAccountField-mandatory');
			$("#city").focus();
			return false;
		}
				
		if($("#phone").val() == '') {
			$("#phone").addClass('myAccountField-mandatory');
			$("#phone").focus();
			return false;
		}
		
		
		if($('input[name=sameBillingAddress]:checked').val() == 'no') {
			
			if($('input[name=billingcustomertype]:checked').val() == 'company') {
			
				if($("#billingcompanyname").val() == '') {
					$("#billingcompanyname").addClass('myAccountField-mandatory');
					$("#billingcompanyname").focus();
					return false;
				}	
			}
			
			if($("#billingfamilyname").val() == '') {
				$("#billingfamilyname").addClass('myAccountField-mandatory');
				$("#billingfamilyname").focus();
				return false;
			}
			
			if($("#billingname").val() == '') {
				$("#billingname").addClass('myAccountField-mandatory');
				$("#billingname").focus();
				return false;
			}
			
			if($("#billingaddress1").val() == '') {
				$("#billingaddress1").addClass('myAccountField-mandatory');
				$("#billingaddress1").focus();
				return false;
			}
			
			if($("#billingzipcode").val() == '') {
				$("#billingzipcode").addClass('myAccountField-mandatory');
				$("#billingzipcode").focus();
				return false;
			}
			
			if($("#billingcity").val() == '') {
				$("#billingcity").addClass('myAccountField-mandatory');
				$("#billingcity").focus();
				return false;
			}

			if($("#billingphone").val() == '') {
				$("#billingphone").addClass('myAccountField-mandatory');
				$("#billingphone").focus();
				return false;
			}
			
		}
		
		$.post(sysHttpProtocol + "://" + sysFullWebPath + "apps/shopmyaccount/ajax.php?task=saveAccountData", {formData: $("#myProfilDataForm").serialize()}, function(res){
			if(res == 1){
				//success
				alert("OK");
			}
		});
	}
	
	this.changeUserPassword = function () {
		$("#modalCustomerChangePasswordDiv").modal({
		
			onOpen : function(dialog) {
				dialog.overlay.fadeIn('slow', function () {
					dialog.container.slideDown('slow', function () {
						dialog.data.fadeIn('slow');
						/** Browser Test */
						if(systemBrowserName != 'ie') {
							$("#simplemodal-container").corner("12px");
							$("#changePasswordCancel").corner("4px");
							$("#changePasswordSubmit").corner("4px");
							$("#oldpassword").focus();
						}
					});
				});
			},
			
			onShow : function(dialog) {
				
				$("#changePasswordSubmit").click(function(){
					
					$("#oldpassword").removeClass('myAccountField-mandatory');
					$("#password1").removeClass('myAccountField-mandatory');
					$("#password2").removeClass('myAccountField-mandatory');
					
					var oldpassword 	= $("#oldpassword").val();
					var newpassword1 	= $("#password1").val();
					var newpassword2 	= $("#password2").val();
					
					if(oldpassword == ''){
						$("#oldpassword").addClass('myAccountField-mandatory');
						$("#oldpassword").focus();
					}else if( (newpassword1 != newpassword2) || (newpassword1 == '') ){
						$("#password1").val("");
						$("#password2").val("");
						$("#password1").addClass('myAccountField-mandatory');
						$("#password2").addClass('myAccountField-mandatory');
						$("#password1").focus();
					}else{
						$.get(sysHttpProtocol + "://" + sysFullWebPath + "apps/shopmyaccount/ajax.php", {task: 'changePassword', oldPassword: oldpassword, newPassword: newpassword1 }, function (data){
							if(data == 'error'){
								$("#oldpassword").val("");
								$("#oldpassword").addClass('myAccountField-mandatory');
								$("#oldpassword").focus();
							}else{
								$(".buttonCancelDiv").html('');
								$(".buttonSubmitDiv").html('');
								$(".buttonCloseDiv").css('display','inline');
								$(".hideWhenDone").hide();
								$(".showWhenDone").show();
								$("#changePasswordClose").corner("4px");
							}							
						});
					}
					
				});
				
				$("#changePasswordClose").click(function(){
					$.modal.close();
				});	
				
				
			},
			
			onClose : function (dialog) {
				dialog.data.fadeOut('fast', function () {
					dialog.container.slideUp('fast', function () {
						dialog.overlay.fadeOut('fast', function () {
							$.modal.close(); // must call this!
						});
					});
				});
			},
			
			overlayClose:true,
			closeClass: "cancel"
	
		});
	}
	
	this.isValidEmailAddress = function(emailAddress) {
	
		var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	
		return pattern.test(emailAddress);
	}
	
	this.logout = function (){
		$("#shopMyAccountFormTask").val("logout");
		$("#shopMyAccountForm").submit();
	}

}


