$(document).ready(function() {	
			
    setupPopups();
    
});

function setupPopups() {
	$('.popupLink').click(function() {
    	
    	if( jQuery.browser.msie && parseInt(jQuery.browser.version) == 6 ) {
    		$('select').hide();
    	}
    
    	if( $(this).attr("href") ) {
    		//we have an image in href so show this in popup
    		$('div#popupMap').html('<img src="' + $(this).attr("href") + '" />');
    	}
    			
    	return showDialog( $(this).attr("rel") );
    });
	
    if( $('.cleanPopup').length != 0 ) { // no element found    	
		$(".cleanPopup").dialog({ autoOpen: false,
								   height: 10, 
								   width: 500,
								   resizable: false,
								   modal: true });
    }
	
}

function showDialog(popup){
	
	//if they are try to sent the quote by email, validate the request then show popup after email sent
	if( popup == 'popupSentEmail' ) {
		if( SpecialProcessingForEmailQuoteRequest() ) {	
			$("#" + popup).dialog('open');
			return false;
		}
	}
	else
	{
		$("#" + popup).dialog('open');
		return false;
	}
	
}

function SpecialProcessingForEmailQuoteRequest() {
	/* special processing for send email quote */
		
	var domain = 'http://' + document.domain;
	var path = window.location.pathname;
	var url = '';
	var theForm = document.getElementById('emailThisPageForm1');
	
	var pageName = $('input[name*=pageName]').val();
	var recipient = $('input[name*=recipient]').val();
	
	if( pageName != 'holidayOptions' ) {
		return true;
	}
	else
	{		
		var message = '';
	
		if (!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(theForm.recipient.value))
			  message+= "Please enter a valid email address.\n";
	
		if (message != '') {
			//error in validation
			alert("Please check the following:\n\n" + message);	
			return false;
		}
		else
		{
			//send email
			if( path.indexOf('/website-dev/') >= 0 ) {
				//we are on dev or test?
				url = domain + '/website-dev/';
			}
			else {
				url = domain + '/';
			}
			src = url + "emailThisPage.vm?recipient=" + recipient + "&pageName=holidayOptions";
					
			$('#hiddenIframe').attr('src',src);
			return true;
		}
	} 
	/* END OF - special processing for send email quote */
}

