// JavaScript Document
$(document).ready(function() {
	// insert subscribe img						   
	$("tr:has(img[name=home])").after('<TR><TD COLSPAN="7"><IMG NAME="cm-subscribe" SRC="/images/subscribe.gif" WIDTH="694" HEIGHT="45" id="cm-subscribe"></TD></TR>');
	
	$("body").append('<div id="cm-form-subscribe" title="Subscribe to ROI-NLS [New Listing Service]" style="overflow:hidden;"></div>');
	$("body").append('<div id="cm-confirm-unsubscribe" title="Confirmation Required">Are you sure that you would like to unsubscribe from the ROI New Listing Service. By clicking "Yes" you will no longer be automatically notified when new listings are posted.</div>');
	$("body").append('<div id="cm-unsubscribe-success" title="Unsubscribe Successful">You have successfully unsubscribed from the ROI New Listing Service.</div>');
	$.get("cm-subscribe.asp", function(data){
		$("#cm-form-subscribe").html(data);
 	});

	
	$("#cm-subscribe").click(function(){
		//show subscribe modal window
		$("#cm-form-subscribe").dialog({resizable:false, draggable: false, width: 694,height:560,show:'slide'});
									  
	});

	$('#cm-form-subscribe').ajaxError(function(e, xhr, settings, exception) {
		alert('error in: ' + settings.url + ' \n'+'error:\n' + xhr.responseText );
	});
		
	if (getParameterByName("message") == "cm-verified") {
		alert("Thank you for activating your subscription.\n\rYou will now recieve automatic updates any time there are new listings available.");
	}
	if (getParameterByName("subscribe") !="") {
		$("#cm-subscribe").click();
	}
	if (getParameterByName("unsubscribe") !="") {
		$("#cm-confirm-unsubscribe").dialog({modal:true,resizable:false, draggable: false, width: 694,height:250,show:'slide',
			buttons: {
				"No, continue my subscription": function() {
					$( this ).dialog( "close" );
				},
				"Yes, unsubscribe": function() {
					$.post("cm-doUnsubscribe.asp",{"unsubscribe": getParameterByName("unsubscribe")},function() {
								$("#cm-confirm-unsubscribe").dialog("close");
								$("#cm-unsubscribe-success").dialog({resizable:false, draggable: false, width: 694,height:180,show:'slide',buttons: {
				"Okay": function() {
					$( this ).dialog( "close" );
					}
				}
								});
					} );
				}
			}
		});
	}

	// check if this is the practices page
	if ($("img[src*=practices-over.gif]").length !=0) {
		if (getCookie("cm-subscribe") == "") {
			$("#cm-subscribe").click();
		}
	}
});
$(document).ready(function() {
	$("#cm-subscribe").mouseover(function() { 
            var src = $(this).attr("src").replace(".gif","-hover.gif");
            $(this).attr("src", src);
        })
        .mouseout(function() {
            var src = $(this).attr("src").replace("-hover", "");
            $(this).attr("src", src);
        });						   
						   
});
function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}
function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "";
}
function getParameterByName( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}

function cm_reset_form_error() {
	$("input[name*=cm-subscribe-]").val("");
}
function cm_show_form_error(elem) {
		
}
function cm_validate_subscribe() {
	var valid = true;
	// check first name
	if ($("#cm-subscribe-fname").val() == "") {
		cm_show_form_error("fname");
		valid=false;
	}
	if ($("#cm-subscribe-lname").val() == "") {
		cm_show_form_error("lname");
		valid=false;
	}
	if (valid) {
		$.post('cm-doSubscribe.asp?',$("#cm-subscribe-form").serialize(),function(data) {
			$("#cm-form-subscribe").prepend(data);																		 
		});
	}
	
}
