var emailform;
var popupWidth=700;
if(window.location.href.indexOf("cuidadodesalud.gov") == -1){
	var PopupHeight=400;
}
else{
	var PopupHeight=420;
}
var popup_head="";

$(document).ready(function () {
	 emailform = $('div#emailform').html();
	 $('div#emailform').detach();
	 
	 var posX = -9999;
	 var posY = -9999;
	 
	 //var dposX = $(window).width();
	 var dposY = $(document).height();
	 
	 
	 
	
	 if( ! $("popup_container").length){
			$("body").append("<div class='popup_bg' style='display:none;height:" + dposY + "px'></div><div class='popup_container' style='width: " + popupWidth + "px;height:" + PopupHeight + "px;top:" + posY + "px;left:" + posX + "px;'>" +
									 "<div class='popup_window'><div class='popup_header'><div class='control'><a href='javascript:closePopup()'><img src='" + rxs_navbase + "/images/sys_images/modal-ltblue-x.gif' alt='close' title='close' /></a></div>" + 
									 popup_head + "<div class='popup_body'><div class='pop_emailform'></div></div></div></div></div>")
			
			$("div.popup_body div.pop_emailform").html(emailform);
									 
			var sendmail_options = { 
 				url: "/healthcare_ugc/emails.do",
				dataType: 'text',
        beforeSubmit:  mailRequest, 
        success:       mailResponse
	    }; 
			
			// bind to the form's submit event 
	    $('#sendemail_form').submit(function() {         
	        $(this).ajaxSubmit(sendmail_options); 
	        return false; 
	    }); 
		}
									 
});

function createPopup(){
	
	var posX = ( $(window).width() - popupWidth) / 2;
	var posY = ( $(window).height() - PopupHeight ) / 2 + $(window).scrollTop();
	
	
	if( ! $("popup_container").length){
		$("body div.popup_bg").show();
		$("body div.popup_container").css("top", "" + posY + "px");
		$("body div.popup_container").css("left", "" + posX + "px");
		$("div.popup_body input:first").focus();
		
		
		
		
	}
	
	
}

function closePopup(){	
	//$("body div.popup_container").detach();
	$("div.popup_body div.pop_emailform").show();
	$("body div.popup_container").css("top", "-999px");
	$("body div.popup_container").css("left", "-999px");
	$("body div.popup_bg").hide();	
	$("div.emailresponse").detach();
}




function mailRequest(formData, jqForm, options) {     	
    var queryString = $.param(formData); 
    var formElement = jqForm[0];
    
    if(formElement.sendto.value == ""){
    	alert("Send to e-mail address is required to submit.")
    	formElement.sendto.focus();
    	return false;
    }
    else if(formElement.confirmemail.value == ""){
    	alert("E-mail confirmation is required to submit.")
    	formElement.confirmemail.focus();
    	return false;
    }
    else if(formElement.confirmemail.value != formElement.sendto.value){
    	alert("Send to E-mail address need match up with confirmation e-mail address.")
    	formElement.confirmemail.focus();
    	return false;
    }
    else if(formElement.fromemail.value == ""){
    	alert("From e-mail address is required to submit.")
    	formElement.fromemail.focus();
    	return false;
    } 
    
    return true; 
} 
 

function mailResponse(responseText, statusText, xhr, $form)  {    
		//alert( "responseText: " + responseText + "\nstatusText: " + statusText);
		if(statusText == "success"){	
			$("div.popup_body div.pop_emailform").hide();		
			$("div.popup_body").append("<div class='emailresponse'><p><b>The page link and comments were sent to the specified email(s).</p><p>Thank you.</p></b></p><p><input type='button' value='Close the Window' onclick='javascript:closePopup()')></div>");
		}  
} 

function emailupdate(){
	window.open('http://service.govdelivery.com/service/multi_subscribe.html?code=USHHSHC','Popup','width=780,height=440,toolbar=no,scrollbars=yes,resizable=yes'); void(''); 	
}

