var C21AgentRESTServiceUrl = "/DesktopModules/C21Agents/AgentService.aspx";
var C21SearchRESTServiceUrl = "/DesktopModules/C21PropertySearch/SearchService.aspx";


// Document Ready Actions
jQuery(function($)
{
	if($('img .Photo') != null)
	{
		$('img .Photo').photoborder();
	}
	
    //If an href tags destination is off the current domain then have it pop up in a new window
    //that has a century 21 bar that always stays on it. /Offsite-Redirect/redirect only accepts one parameter
    //The parameter is URL and must include the http:// , this will get displayed in a bottom frame						
    if($("a") != null)
	{
		$("a").filter(function(index) {
		        var URL=this.href;        
		        if((URL.substring(0,15)=='http://64.90.16')||(URL.substring(0,15)=='http://www.c21l')|| (URL.substring(0,15)=='http://c21looki') || (URL.substring(0,15)=='http://visualto') || (URL.substring(0,21)=='http://www.visualtour') ||(URL.substring(0,1)=='/') || (URL.substring(0,16)=='http://localhost') || (URL.indexOf(location.hostname)>0))
		            return false;
		        else if (URL.substring(0,3)=='htt')
		            return true;
		        else
		            return false;
		     }).click( function(){
				//alert(escape(this.href));
		        window.open("/Offsite-Redirect/redirect.aspx?URL="+escape(this.href),null,"height=600,width=780,status=yes,toolbar=yes,menubar=yes,location=yes,resizable=yes");return false;
		       });
	}

	var url = window.location.href;
    if(url.indexOf("/Buy/")>0)
    {
        $("#navMenuItem53").attr("id","navMenuCurrentItem");
        $("#navMenuLink53").attr("id","navMenuCurrentLink");
    }
    else if(url.indexOf("/Sell/")>0)
    {
        $("#navMenuItem54").attr("id","navMenuCurrentItem");
        $("#navMenuLink54").attr("id","navMenuCurrentLink");
    }
    else if(url.indexOf("/Learn/")>0)
    {
        $("#navMenuItem55").attr("id","navMenuCurrentItem");
        $("#navMenuLink55").attr("id","navMenuCurrentLink");
    }
    else if(url.indexOf("/Contact/")>0)
    {
        $("#navMenuItem56").attr("id","navMenuCurrentItem");
        $("#navMenuLink56").attr("id","navMenuCurrentLink");
    }
	else if(url.indexOf("/Home/")>0)
	{
        $("#navMenuItemHome").attr("id","navMenuCurrentItem");
        $("#navMenuLinkHome").attr("id","navMenuCurrentLink");		
	}
	else
	{
        $("#navMenuItemHome").attr("id","navMenuCurrentItem");
        $("#navMenuLinkHome").attr("id","navMenuCurrentLink");		
	}	
	
	// hide the dnn "Visiblity" radio button areas in the Member Profile page
	jQuery("span[@id*=__vis]").hide();
});

function C21ClearSearchForm()
{
    // clear all drop-downs
    $('.FormRow select').each(function(i){
       this.selectedIndex = 0;
     });
    // clear all textboxes
    $('.FormRow input[@type=text]').each(function(i){
       this.value = '';
     });
    // clear all checkboxes
    $('.FormRow input[@type=checkbox]').each(function(i){
       this.checked = false;
     });     
}

function C21DelayClosePopup()
{
    $(function()
    {
        window.top.setTimeout("TB_remove()",2000); // 2 second delay before closing thickbox
    });        
}

function C21GetHomepageNewlyListed(userId)
{
    $.post(C21SearchRESTServiceUrl,
        { a: "NewlyListed", u: userId },
        function(data){           
            var response = ParseRESTRespone(data);
            if(response[0] == "true")
            {                                
			    if($('#slideshow').length > 0)
			    {
                    $('#slideshow').html(response[1]);
    				$('#slideshow img .Photo2').photoborder2();
    				
				    $('#slideshow').slideshow({timeout: 4000});
			    }          
            }
        }
    );    
}

function C21SetFormMasterSendTo()
{
    // populate the Form Master 'hSendTo' field
    $formMasterTable = $('.FormMaster');
    if($formMasterTable.length > 0)
    {
	    var qs = jqs.parse(); // parse querystring params
	    var s = String(qs['sendTo']);
	    if(s != "undefined")
	    {
	        $('.hSendTo').val(s.rot13());
	    }
	}
}

function C21SetFormMasterFieldFromParam(elmCssClass,qryParamName,isUrlEncoded)
{
    $formMasterTable = $('.FormMaster');
    if($formMasterTable.length > 0)
    {
	    var qs = jqs.parse(); // parse querystring params
	    var s = String(qs[qryParamName]);	    
	    if(s != "undefined")
	    {	    
	        $('.' + elmCssClass).val(s.unescape());
	    }
	} 
}
function C21SetFormMasterFieldFromParam(elmCssClass,qryParamName)
{
    $formMasterTable = $('.FormMaster');
    if($formMasterTable.length > 0)
    {
	    var qs = jqs.parse(); // parse querystring params
	    var s = String(qs[qryParamName]);
	    if(s != "undefined")
	    {
	        $('.' + elmCssClass).val(s);
	    }
	}    
}


// Parse a REST webservice response and return just the stuff between the <body> tags
function ParseRESTRespone(data)
{
    // grab stuff between the <body> tags
    var startTag = "<body>";
    var endTag = "</body>";
    var start = data.indexOf(startTag);
    var end = data.lastIndexOf(endTag);
    if(start > -1 && end > start)
    {
        var text = data.substr( start + startTag.length, end - (start + startTag.length) )
        return Trim(text).split("|");
    }
    else
    {
        return data;
    }
}


/*------------------------------------------------
	String Functions
-------------------------------------------------*/	
String.prototype.rot13 = function(){ //v1.0
	return this.replace(/[a-zA-Z]/g, function(c){
		return String.fromCharCode((c <= "Z" ? 90 : 122) >= (c = c.charCodeAt(0) + 13) ? c : c - 26);
	});
};

// Removes leading whitespaces
function LTrim( value ) {
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");	
}
// Removes ending whitespaces
function RTrim( value ) {
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
}
// Removes leading and ending whitespaces
function Trim( value ) {
	return LTrim(RTrim(value));	
}