// JavaScript Document( Created By Ammar )
var MW_NAME = 'Name' 
var MW_NICKNAME = 'Nickname'
var MW_STREET = 'Street'
var MW_TOWNCITY = 'Town City'
var MW_POSTCODE = 'Postcode'
var MW_COUNTY = 'County'
var MW_COUNTRY = 'Country'		
var MW_MOBILENUM = 'Mobile Number'	
var MW_NEWEMAIL = 'New email'
var MW_CUR_PASS = 'Current password'
var MW_NEW_PASS = 'New password'
var MW_CONF_PASS = 'Confirm new password'
var MW_CONF_NEWEMAIL = 'Confirm new email'	
var VEP_EMAIL = 'Email'
var VEP_PASSWORD = 'Password'
var VEP_CONF_PASS = 'Confirm password'	
var VEP_FIRST_NAME = 'First Name' 	
var VEP_LAST_NAME = 'Last Name' 	
var VEP_PAYPAL_EMAIL = 'Your PayPal Email Address'	
var VEP_TOWNCITY = 'Town / City'
	
// **************************** Validation Scripts **************************************************//

function myWozzonEditDetails() {
	
	var frmName = _$id('editUserDetail');
	var nickName = frmName.nickName.value;	
	var postCode = frmName.postcode.value;
	var phone = frmName.phone.value;
	var password = frmName.newPassword.value;
	var oldPassword = frmName.oldPassword.value;
	var rePassword = frmName.reNewPassword.value;
	var newEmail = frmName.newEmail.value;		
	var reNewEmail = frmName.reNewEmail.value;
	var country = frmName.countryName.value;
	var county = frmName.countyName.value;
	var townCity = frmName.townCity.value;
	var street = frmName.street.value;
	var username = frmName.userName.value;
	
	postCode = trim(postCode);
	if(postCode == MW_POSTCODE) postCode = '';
	
	phone = trim(phone);
	if(phone == MW_MOBILENUM)  phone= '';
	
	nickName = trim(nickName);
	if( nickName == MW_NICKNAME) nickName = '';
	
	newEmail = trim(newEmail);
	if(newEmail == MW_NEWEMAIL) newEmail = '';	
	
	reNewEmail = trim(reNewEmail);
	if(reNewEmail == MW_CONF_NEWEMAIL) reNewEmail = '';
	
	country = trim(country);
	if(country == MW_COUNTRY ) country = '';
	
	county= trim(county);
	if(county == MW_COUNTY) county = '';
	
	townCity= trim(townCity);
	if(townCity == MW_TOWNCITY) townCity = '';
	
	street = trim(street);
	if( street == MW_STREET ) street = '';
	
	username = trim(username);
	if( username == MW_NAME ) username = '';
	
	
	
	if(nickName.length == 0) {
		SNG_displayNickNameError($("#text_nickName"),"Please provide nick name.");		
		return false;
	}else if(SNG_checkNickname(nickName)){
		SNG_displayNickNameError($("#text_nickName"), NICK_NAME_INVALID_CHAR);		
		return false;
	}
	else if($('#nickNameAlreadyExist').val() == "1") {
		SNG_displayNickNameError($("#text_nickName"),"There is already an account associated with this nickname");
		return false;
	}
	else if(phone.length > 0 && !validateNumber(phone)){
		SNG_displayLoginError($("#phone"),"Characters in the phone number field need to be numeric.");		
		return false;
	}
	else if($('#emailAlreadyExist').val() == "1") {
		SNG_displayLoginError($("#newEmail"), "There is already an account associated with this email");
		return false;
	}
	else if(newEmail.length > 0 ) {
		if(checkEmail(newEmail)) {
			if( reNewEmail == newEmail) {
				frmName.email.value = newEmail;
			} else if( reNewEmail.length > 0)  {
				SNG_displayLoginError($("#reNewEmail"), "New email address doesn't matches with the confirm new email address");
				return false;
			} else {
				SNG_displayLoginError($("#reNewEmail"), "Please provide confirm new email address");
				return false;
			}
		} else {
			SNG_displayLoginError($("#newEmail"), "Please provide a valid new email address");
			return false;
		}
	}	
	
	 if(password.length > 0) {
		if(password.length  < 5) {
			SNG_displayLoginError($("#newPassword"), "Hang on, you need a minimum of 5 characters");
			return false;
		}
		else if(oldPassword.length > 0) {
		   if(rePassword.length > 0) {
			   if(password == rePassword) {
				   frmName.password.value = password;
			   } else {
				   SNG_displayLoginError($("#reNewPassword"),"New password doesn't matches with the confirm new password");
			   		return false;
			   }
		   } else {
			   SNG_displayLoginError($("#reNewPassword"),"Please provide confirm new password");
			   return false;
		   }
		} else {
			 SNG_displayLoginError($("#oldPassword"),"Please provide value for current password.");
			 return false;
		}
	}
	
	if($('input[name=receiveEmailCheckBox]').is(':checked')){		
		$('#receiveEmail').val("Y");
	}else {
		$('#receiveEmail').val("N");				
	}
	
	
	frmName.postcode.value = postCode;
	frmName.phone.value = phone;			
	frmName.countryName.value = country ;
	frmName.countyName.value = county;
	frmName.townCity.value = townCity;
	frmName.street.value = street;
	frmName.userName.value = username;
	
	frmName.submit();
}

function clearErrorMsg() {
	displayEntityErrorMessage(div, "");	
}
// **************************** End Validation Scripts **************************************************//




//**************************************** Ajax Scripts *************************************************//

function checkNickName(str, oldNickName) {
	var nickName = trim(str);
	if(MW_NICKNAME != nickName && nickName.length > 0 && nickName != oldNickName )
		if(SNG_checkNickname(nickName)){
			SNG_displayNickNameError($('#text_nickName'), NICK_NAME_INVALID_CHAR);
		}else{
			UserManager.checkNickName(nickName, checkNickNameHandler);
		}
	else if ( nickName == oldNickName ) {
		$('#nickNameAlreadyExist').val("0");
		SNG_hideClearAllErrors();
	}
}

function checkNickNameHandler(source) {

	if(source == "error")
		SNG_displayNickNameError($("#text_nickName"),"Error processing request for checking availability of nick name.");
	else if(source == "Exists") {
		$('#nickNameAlreadyExist').val("1");
		SNG_displayNickNameError($("#text_nickName"),"There is already an account associated with this nickname");					
	} else {
		$('#nickNameAlreadyExist').val("0");
		SNG_hideClearAllErrors();
	}
	
}

function checkEmailAvailability(str, oldEmail) {
	var reNewEmail = trim(str)
	var newEmail = trim($("#newEmail").val());	
	
	if(newEmail == MW_NEWEMAIL) newEmail = '';
	
	if(reNewEmail == MW_CONF_NEWEMAIL) reNewEmail = '';
		
	if( newEmail == oldEmail ||  newEmail == '') {
		$('#emailAlreadyExist').val("0");
		SNG_hideClearAllErrors();
	} else if(newEmail.length > 0 && checkEmail(newEmail) &&  newEmail != oldEmail && reNewEmail == newEmail) {		
		UserManager.checkEmailAvailability(newEmail, checkEmailAvailabilityHandler);		
	} else if( !checkEmail(newEmail) ) {
		SNG_displayLoginError($("#newEmail"), "Please provide a valid new email address");
	} else if( reNewEmail.length > 0)  {
		SNG_displayLoginError($("#reNewEmail"), "New email address doesn't matches with the confirm new email address");		
	} else {
		SNG_displayLoginError($("#reNewEmail"), "Please provide confirm new email address");		
	}
}

function checkEmailAvailabilityHandler(source) {

	if(source == "error")
		SNG_displayLoginError($("#newEmail"), "Error processing request for checking availability of email.");
	else if(source == "Exists") {
		$('#emailAlreadyExist').val("1");
		SNG_displayLoginError($("#newEmail"), "There is already an account associated with this email");	
	} else {
		$('#emailAlreadyExist').val("0");
		SNG_hideClearAllErrors();
		
	}
	
}

function updateUserInfo(email) {
    var token= 6;
	var a= readCookie("wozzonuser");
	if(a && email) {
	    UserManager.getUserToken(email, function(source) {
        if(source != "error" && source != "invalid" )
            if(source.length > token) {
                remember(email,source.substr(token));
            }
        });	   
	}  
}

//**************************************** End Ajax Scripts *********************************************//
	
$(document).ready(function() {
	$('#editUserDetail input').blur(function(){
		
		var name = $(this).attr('name');
	    var val = trim($(this).val()); 
	    if(name == 'userName' && val == '') {
	    	$(this).val(MW_NAME);
	    	$(this).removeClass('popupTextField').addClass('popupTextFieldBefore');
	    } else if(name == 'nickName' && val == '' ) {
	    	$(this).val(MW_NICKNAME);
	    	$(this).removeClass('popupTextField').addClass('popupTextFieldBefore');
	    } else if(name == 'street' && val == '' ) {
	    	$(this).val(MW_STREET);
	    	$(this).removeClass('popupTextField').addClass('popupTextFieldBefore');
	    } else if(name == 'postcode' && val == '' ) {
	    	$(this).val(MW_POSTCODE);
	    	$(this).removeClass('popupTextField').addClass('popupTextFieldBefore');
	    } else if(name == 'countyName' && val == '' ) {
	    	$(this).val(MW_COUNTY);
	    	$(this).removeClass('popupTextField').addClass('popupTextFieldBefore');
	    }else if(name == 'countryName' && val == '' ) {
	    	$(this).val(MW_COUNTRY);
	    	$(this).removeClass('popupTextField').addClass('popupTextFieldBefore');
	    }else if(name == 'phone' && val == '' ) {
	    	$(this).val(MW_MOBILENUM);
	    	$(this).removeClass('popupTextField').addClass('popupTextFieldBefore');
	    }else if(name == 'newEmail' && val == '' ) {
	    	$(this).val(MW_NEWEMAIL);
	    	$(this).removeClass('popupTextField').addClass('popupTextFieldBefore');
	    }else if(name == 'reNewEmail' && val == '') {
	    	$(this).val(MW_CONF_NEWEMAIL);	
	    	$(this).removeClass('popupTextField').addClass('popupTextFieldBefore');
	    }else if(name == 'oldPassword' && val == '' ) {
	    	$('input[name="password_text"]').removeClass().addClass('popupTextFieldBefore');
	    	$('input[name="password_text"]').parent().show();	    	
	    	$(this).parent().hide();
	    }else if(name == 'newPassword' && val == '' ) {
	    	$('input[name="new_password_text"]').removeClass().addClass('popupTextFieldBefore');
	    	$('input[name="new_password_text"]').parent().show();	    	
	    	$(this).parent().hide();
	    }else if(name == 'reNewPassword' && val == '' ) {
	    	$('input[name="renew_password_text"]').removeClass().addClass('popupTextFieldBefore');
	    	$('input[name="renew_password_text"]').parent().show();
	    	$(this).parent().hide();
	    }else if(name == 'townCity' && val == '' ) {
	    	$(this).val(MW_TOWNCITY);
	    	$(this).removeClass('popupTextField').addClass('popupTextFieldBefore');
	    }
	    
		
	});
	
	$('#editUserDetail input').focus(function(){
		
		var name = $(this).attr('name');
	    var val = trim($(this).val()); 
	    if(name == 'userName' && val == MW_NAME ) {
	    	$(this).val('');
	    	$(this).removeClass('popupTextFieldBefore').addClass('popupTextField');
	    } else if(name == 'nickName') {
	    	$(this).removeClass('SNG_SPTextFieldActive');
	    	if( val == MW_NICKNAME ) {
		    	$(this).val('');		    	
		    	$(this).removeClass('popupTextFieldBefore').addClass('popupTextField');    	
	    	}
	    } else if(name == 'street' && val == MW_STREET ) {
	    	$(this).val('');
	    	$(this).removeClass('popupTextFieldBefore').addClass('popupTextField');
	    } else if(name == 'postcode' && val == MW_POSTCODE ) {
	    	$(this).val('');
	    	$(this).removeClass('popupTextFieldBefore').addClass('popupTextField');
	    } else if(name == 'countyName' && val == MW_COUNTY ) {
	    	$(this).val('');
	    	$(this).removeClass('popupTextFieldBefore').addClass('popupTextField');
	    }else if(name == 'countryName' && val == MW_COUNTRY ) {
	    	$(this).val('');
	    	$(this).removeClass('popupTextFieldBefore').addClass('popupTextField');
	    }else if(name == 'phone' && val == MW_MOBILENUM ) {
	    	$(this).val('');
	    	$(this).removeClass('popupTextFieldBefore').addClass('popupTextField');
	    }else if(name == 'newEmail' && val == MW_NEWEMAIL ) {
	    	$(this).val('');
	    	$(this).removeClass('popupTextFieldBefore').addClass('popupTextField');
	    }else if(name == 'reNewEmail' && val == MW_CONF_NEWEMAIL ) {
	    	$(this).val('');	
	    	$(this).removeClass('popupTextFieldBefore').addClass('popupTextField');
	    }else if(name == 'password_text' && val == MW_CUR_PASS ) {
	    	$("#oldPassword").parent().show();
	    	$("#oldPassword").focus();
	    	$(this).parent().hide();
	    	$(this).removeClass().addClass('popupTextField');
	    }else if(name == 'new_password_text' && val == MW_NEW_PASS ) {
	    	$("#newPassword").parent().show();
	    	$("#newPassword").focus();
	    	$(this).parent().hide();
	    	$(this).removeClass().addClass('popupTextField');
	    }else if(name == 'renew_password_text' && val == MW_CONF_PASS ) {
	    	$("#reNewPassword").parent().show();
	    	$("#reNewPassword").focus();
	    	$(this).parent().hide();
	    	$(this).removeClass().addClass('popupTextField');
	    }else if(name == 'townCity' && val == MW_TOWNCITY ) {
	    	$(this).val('');
	    	$(this).removeClass('popupTextFieldBefore').addClass('popupTextField');
	    }
		
	});
	
});	