// JavaScript Document for email popup

/////////////////////////////////// Email form validation. ////////////////////////////
$(document).ready(function(){
		$('.error').hide();
})
function mailCheck(f) {
	$('.error').hide();
	if (f.find('#fullname').val()=="") {
		f.find("label#fullname_error").show()
		f.find('input#fullname').focus()
		return false
	}
	if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(f.find('#emailaddr').val()))){
		f.find("label#emailaddr_error").show();  
		f.find('#emailaddr').focus()
		return false
	} 
	if(f.find('#phonenum').val().match(/^((\([\d]{2}\))|([\d]{2}))([ ]{0,1}[\d]{3,4}[ ]{0,1}[\d]{4})$/) == null  &&  f.find('#phonenummob').val().match(/(04)([\d]{2}[ ]{0,1})([\d]{3}[ ]{0,1})([\d]{3})/) == null ) {
		if(f.find('#phonenum').val().match(/^((\([\d]{2}\))|([\d]{2}))([ ]{0,1}[\d]{3,4}[ ]{0,1}[\d]{4})$/) == null  ||  f.find('#phonenummob').val().match(/(04)([\d]{2}[ ]{0,1})([\d]{3}[ ]{0,1})([\d]{3})/) == null ) {
			f.find("label#phonenummob_error").show()
			f.find('#phonenum').focus()  
			return false
		}
	}
	if(f.find('#phonenum').val().match(/([a-z]|[A-Z]|[!@#\$%\^&\*_\+=<>\/\?\.,'"])/) != null  ||  f.find('#phonenummob').val().match(/([a-z]|[A-Z]|[!@#\$%\^&\*_\+=<>\/\?\.,'"])/) != null ) {
		f.find("label#phonenummob_error").show()
		f.find('#phonenum').focus()  
		return false
	}
	if(f.find('#textfield').val().length==0) {
		f.find("label#textfield_error").show()  
		f.find('#textfield').focus()
		return false
	}
	return true
}

////////////////////////////////// Popup box and link setup ///////////////////////////
function setupMessageContent(index){
	$('#dialog'+index).dialog({
		autoOpen: 	false,
		width: 		700,
		minHeight: 	550, 
		minWidth: 	600, 
		zIndex:		5003,
		buttons: 	{
				"Send": function() { 
					if(mailCheck($('#dialog'+index).children('form'))){
						$.post(urlPath+"/agents/contactAction.cfm"
							,$('#dialog'+index).children('form').serialize()
							,function(data){
								 $('#dialog'+index).html(data)
							})
						// Remove the "send" and "cancel" buttons and add "close"	
						$('#dialog'+index).dialog( "option", "buttons", { "Close": function() { $(this).dialog("close"); } } )
					}
				}
				,"Cancel": function() { 
					$(this).dialog("close") 
				} 
		}
	})
}

$(document).ready(function(){
	// FOR MULTIPLE EMAIL POPUPS PER PAGE: iterate through all popup links and popup containers on the page and assign matching consecutive numbers to them.
	$("div[name='dialog']").each(function(index,myDialog){
		$(myDialog).attr('id','dialog'+index)
	})
	$("a[name='dialog_link']").each(function (index,myDialogLink) {
		$(myDialogLink).attr('id','dialog_link'+index)

		//save the message content to refesh if click a second time.
		var savedMessageContent 	=	new Array(1)
		savedMessageContent[index]	=	$('#dialog'+index).html()

		//make the popup link function.
		$('#dialog_link'+index).click(function(){
			//refresh the message box before opening. 
			$('#dialog'+index).html(savedMessageContent[index])
			setupMessageContent(index)
			$('#dialog'+index).dialog('open')
		})
	})
})
