$(function() {
	$("#bttnGoToLogin").click(function(e){
		location = '/login';
	});
	
	$("#bttnRequestPassword").click(function(e){
		$ctl = null;
		
		//
		// all fields are required...
		$collection = $("#frmCreateManager").find('input,textarea');
		$collection.each(function(){
			if($(this).val().length < 2) {
				$(this).addClass('invalid');
				if($ctl == null)
					$ctl = $(this);
			} else {
				$(this).removeClass('invalid');
			}
		});
		
		if($ctl) {
			$ctl.focus();
			$(".OptionHeader > p").html('<strong style="color:maroon;text-align:center;padding:20px;">All fields are required!</strong>');
			return false;
		}

		$.ajax({
			type:'POST',
			cache:false,
			url:'/forgot-password',
			data:'email='+$("#email").val(),
			dataType:'json',
			success: function(data, textStatus, jqXHR) {
				if(data.success)
					location.href='/login';
				else
					fancyAlert(data.message);
			},
			error: function(xhr,status,error) {
				var notice = 'Status['+xhr.status+'] '+xhr.statusText+'<br/><br/>'+
							 'Response: '+xhr.responseText;
				fancyAlert(notice);
			}
		});
		
		return false;
	});
	
	/* -- login buttons --*/
	$("#bttnLogin").click(function(e) {
		var uid = $("#uid").val();
		var pwd = $("#pwd").val();
		
		if(!uid.length || !pwd.length) {
			fancyAlert('Email &amp; Password are required fields.!');
			return false;
		}
		
		$.ajax({
			type:'POST',
			cache:false,
			url:'/authorizations',
			data:'uid='+uid+'&pwd='+pwd,
			dataType:'json',
			success: function(data, textStatus, jqXHR) {
				if(data.success)
					location.href='/checkregistration';
				else
					fancyAlert(data.message);
			},
			error: function(xhr,status,error) {
				var notice = 'Status['+xhr.status+'] '+xhr.statusText+'<br/><br/>'+
							 'Response: '+xhr.responseText;
				fancyAlert(notice);
			}
		});
		
		return false;
	});
	
	/* -- manager / artist account form */
	$("#bttnCreateManager").unbind('click').click(function(e){
		$ctl = null;
		
		//
		// all fields are required...
		$collection = $("#frmCreateManager").find('input,textarea');
		$collection.each(function(){
			if($(this).val().length < 2) {
				$(this).addClass('invalid');
				if($ctl == null)
					$ctl = $(this);
			} else {
				$(this).removeClass('invalid');
			}
		});
		
		if($ctl) {
			$ctl.focus();
			$(".OptionHeader > p").html('<strong style="color:maroon;text-align:center;padding:20px;">All fields are required!</strong>');
			return false;
		}
		
		//
		// verify passwords...
		var o_pwd = $('#opwd').val();
		var v_pwd = $('#vpwd').val();
		if(o_pwd.value != v_pwd.value) {
			$("#opwd").focus();
			$(".OptionHeader > p").html('<strong style="color:maroon;text-align:center;padding:20px;">Passwords do not match!</strong>');
			return false;
		}
		
		$(".OptionHeader > p").html('');
		
		$.ajax({
			type:'POST',
			url:'/managers',
			data:$("#frmCreateManager").serialize(),
			dataType:'json',
			success: function(data, textStatus, jqXHR) {
				if(data.success) {
					location.href='/checkregistration';
				} else
					fancyAlert(data.message);
			},
			error: function(xhr,status,error) {
				var notice = 'Status['+xhr.status+'] '+xhr.statusText+'<br/><br/>'+
							 'Response: '+xhr.responseText;
				fancyAlert(notice);
			}
		});
		
		return false;
	});
	
	$("#bttnSubmitArtist").unbind('click').click(function(e){
		$ctl = null;
		
		//
		// all fields are required...
		$collection = $("#createLogin").find('input,textarea');
		$collection.each(function(){
			if($(this).val().length < 2 && 
				$(this).attr('id') != 'photo_file' &&
				$(this).attr('id') != 'photo_file_uri' &&
				$(this).attr('id') != 'website' &&
				$(this).attr('id') != 'artist_id' &&
				$(this).attr('id') != 'contact_company' &&
				$(this).attr('id') != 'contact_name' &&
				$(this).attr('id') != 'contact_address1' &&
				$(this).attr('id') != 'contact_address2' &&
				$(this).attr('id') != 'contact_city' &&
				$(this).attr('id') != 'contact_fax' &&
				$(this).attr('id') != 'contact_zip' &&
				$(this).attr('id') != 'contact_phone2' &&
				$(this).attr('id') != 'contact_email2' &&
				$(this).attr('id') != 'narrative' &&
				$(this).attr('id') != 'countdown' &&
				$(this).attr('type') != 'file'

				) {
				$(this).addClass('invalid');
				if($ctl == null)
					$ctl = $(this);
			} else {
				$(this).removeClass('invalid');
			}
		});
		
		if($ctl) {
			$ctl.focus();
			$(".OptionHeader > p").html('<strong style="color:maroon;text-align:center;padding:20px;">All fields are required!</strong>');
			return false;
		}
		
		$(".OptionHeader > p").html('');
		
		$.ajax({
			type:'POST',
			url:'/artists',
			data:$("#createLogin").serialize(),
			dataType:'json',
			success: function(data, textStatus, jqXHR) {
				if(data.success) {
					// if($("#skip_fee").length > 0)
						location.href='/artists/'+data.artistid;
					// else
					// 	location.href='/checkregistration';
				} else
					fancyAlert(data.message);
			},
			error: function(xhr,status,error) {
				var notice = 'Status['+xhr.status+'] '+xhr.statusText+'<br/><br/>'+
							 'Response: '+xhr.responseText;
				
				if(notice.lastIndexOf('UNIQUE') > 0)
					notice = 'It appears that the email address provided already has an account. Please try another email or use our <b><a href="/forgot-password">Forgot Password</a></b> feature.';
				else
					notice = 'It appears that we are not able to create your account right now, please try again later. If this continues to occur, please contact our help desk.';
				
				fancyAlert(notice);
			}
		});
		
		return false;
	});
	
	$("#bttnUpdateArtist").unbind('click').click(function(e){
		$ctl = null;
		
		//
		// all fields are required...
		$collection = $("#updateLogin").find('input,textarea');
		$collection.each(function(){
			if($(this).val().length < 2 && 
				$(this).attr('id') != 'photo_file' &&
				$(this).attr('id') != 'photo_file_uri' &&
				$(this).attr('id') != 'website' &&
				$(this).attr('id') != 'artist_id' &&
				$(this).attr('id') != 'contact_company' &&
				$(this).attr('id') != 'contact_name' &&
				$(this).attr('id') != 'contact_address1' &&
				$(this).attr('id') != 'contact_address2' &&
				$(this).attr('id') != 'contact_city' &&
				$(this).attr('id') != 'contact_fax' &&
				$(this).attr('id') != 'contact_zip' &&
				$(this).attr('id') != 'contact_phone2' &&
				$(this).attr('id') != 'contact_email2' &&
				$(this).attr('id') != 'narrative' &&
				$(this).attr('id') != 'countdown' &&
				$(this).attr('id') != 'password' &&
				$(this).attr('type') != 'file'
				) {
				$(this).addClass('invalid');
				if($ctl == null)
					$ctl = $(this);
			} else {
				$(this).removeClass('invalid');
			}
		});
		
		if($ctl) {
			$ctl.focus();
			$(".OptionHeader > p").html('<strong style="color:maroon;text-align:center;padding:20px;">All fields are required!</strong>');
			return false;
		}
		
		$(".OptionHeader > p").html('');
		
		$.ajax({
			type:'PUT',
			url:'/artists/'+$("#artist_id").val(),
			data:$("#updateLogin").serialize(),
			dataType:'json',
			success: function(data, textStatus, jqXHR) {
				if(data.success) {
					if($("#usebypass").val() == 'true')
						location.href='/managers/accounts';
					else
						location.href='/artists/'+$("#artist_id").val()+'/summary';
				} else
					fancyAlert(data.message);
			},
			error: function(xhr,status,error) {
				var notice = 'Status['+xhr.status+'] '+xhr.statusText+'<br/><br/>'+
							 'Response: '+xhr.responseText;
				fancyAlert(notice);
			}
		});
		
		return false;
	});
	
	/* artist registration payment form */
	$("#bttnRenewRegistration").unbind('click').click(function(e){
		$ctl = null;
		
		//
		// all fields are required...
		$collection = $("#frmArtistRegistration").find('input,textarea');
		$collection.each(function(){
			if($(this).val().length < 2) {
				$(this).addClass('invalid');
				if($ctl == null)
					$ctl = $(this);
			} else {
				$(this).removeClass('invalid');
			}
		});
		
		if($ctl) {
			$ctl.focus();
			$(".OptionHeader > p").html('<strong style="color:maroon;text-align:center;padding:20px;">All fields are required!</strong>');
			return false;
		}
		
		$(".OptionHeader > p").html('');
		
		$.ajax({
			type:'POST',
			url:'/artists/'+$("#artist_id").val()+'/renew',
			data:$("#frmArtistRegistration").serialize(),
			dataType:'json',
			success: function(data, textStatus, jqXHR) {
				if(data.success) {
					location.href='/artists/'+$("#artist_id").val()+'/receipts';
				} else
					fancyAlert(data.message);
			},
			error: function(xhr,status,error) {
				var notice = 'Status['+xhr.status+'] '+xhr.statusText+'<br/><br/>'+
							 'Response: '+xhr.responseText;
				fancyAlert(notice);
			}
		});
		
		return false;
	});
	
	$("body").ajaxStart(function() { $.fancybox.showActivity();	});
	$("body").ajaxStop (function() { $.fancybox.hideActivity(); });
	
	/* == utility to format ALL phone numbers ==*/
	$("input[name^=phone],input[name^=mobile],input[name^=fax],input[id^=phone],input[id^=mobile],input[id^=fax],input[id^=contact_phone],input[id^=contact_fax]").bind('blur', function(e) {
		$(this).val( formatPhone($(this).val()) );
	});
});

function fancyAlert( msg ) {
	$.fancybox('<div><img src="/slir/w50-h50/images/maaa-logo.jpg"/></div>' +
			   '<div style="width:300px;min-height:200px;padding:10px;">'+msg+'</div>');
}

function formatPhone( rawPhoneFieldValue ) {
	var pattern = /[^A-Za-z0-9]/ig;
	var formatedPhoneNum = "";
	var strippedPhoneNum = rawPhoneFieldValue.replace( pattern, "" );
	
	if(strippedPhoneNum.length >= 3)
		formatedPhoneNum = strippedPhoneNum.substr(0,3);
	
	if(strippedPhoneNum.length >= 7)
		formatedPhoneNum += "-" + strippedPhoneNum.substr(3,3);
	
	if(strippedPhoneNum.length >= 10)
		formatedPhoneNum += "-" + strippedPhoneNum.substr(6,4);
	
	return formatedPhoneNum;
}

