﻿    /**************************************************************
	** Function used to open a new window.	
	**
	** Created by:      Gene Ernst
	** Last modified:	1/17/2007										
	***************************************************************/	
function PopUpWindow(strURL, strWidth, strHeight, strLeft, strTop, strStatus, strLoc, strToolbar, strMenu, strScroll, strResize) 
{	
	var DocWindow
	
	if (DocWindow && !DocWindow.closed)
		{
		    DocWindow.close();
		}
	
	DocWindow = window.open(strURL,"DocWindow","width="+strWidth+",height="+strHeight+",left="+strLeft+",top="+strTop+",status="+strStatus+",location="+strLoc+",toolbar="+strToolbar+",menubar="+strMenu+",scrollbars="+strScroll+",resizable="+strResize+"");
	DocWindow.focus; 
}	 


function openWin(url,windowName,options){
	var WindowHandle=window.open(url,windowName,options);
	WindowHandle.focus();
}
function openmyweichert() {
    MWopenPopupStart();
}
function ImgChange(imgName,imgSrc){
    var target = eval(imgSrc);
    document.images[imgName].src = target.src;  

    if( ImgResize(target, 240, 320) )
    {
        document.images[imgName].style.height = target.height;  
        document.images[imgName].style.width = target.width;  
    }
}
function SetSize(imgName, maxHeight, maxWidth)
{
    var img = document.getElementById(imgName);
    var copy = new Image();
    copy.src = img.src;
    if(ImgResize(copy, maxHeight, maxWidth))
    {
        img.style.height = copy.height + 'px';  
        img.style.width = copy.width + 'px';  
    }
}
function ImgResize(img, maxHeight, maxWidth) 
{
    if (img == undefined || img == null) return false;
    if (maxHeight == 0 || maxWidth == 0 ) return false;
    if (img.height == 0 || img.width == 0 ) return false;

    var targetRatio;
    targetRatio = maxWidth / maxHeight;
    var imgRatio;
    imgRatio = img.width / img.height;
    
    if( targetRatio == imgRatio )
    {
         img.height = maxHeight;
         img.width = maxWidth;
    }
    else if (targetRatio > imgRatio) 
    {
        img.width = maxHeight * imgRatio;
        img.height = maxHeight;
    }
    else 
    {
        img.height = maxWidth / imgRatio;
        img.width = maxWidth;
    }
    return true;
}
function NextImage(imgName, fullImages){
    var index;
    for( index = 0; index < fullImages.length-1; index++ )
    {
        if( fullImages[index].src == document.images[imgName].src )
        {
            ImgChange(imgName,fullImages[index + 1].id);
            break;
        }
    }
}
function PreviousImage(imgName, fullImages){
    var index;
    for( index = fullImages.length-1; index > 0; index-- )
    {
        if( fullImages[index].src == document.images[imgName].src )
        {
            ImgChange(imgName,fullImages[index - 1].id);
            break;
        }
    }
}


//TODO: Homestore
function SaveSearch(saveURL){
    window.top.location.href="../../weichertvalet/savedsearches.aspx?Action=Save&search=hs&hq=" + escape(saveURL)
    //alert("../../weichertvalet/savedsearches.aspx?Action=Save&hq=" + escape(saveURL))
}
//TODO: Homestore
function SaveListing(MlsName, MlsNumber, PerPopulatedSaveName){
    window.top.location.href="../../weichertvalet/savedlistings.aspx?ID=0&Action=Save&hs=1&MlsNumber=" + escape(MlsNumber) + "&MlsName=" + escape(MlsName) + "&Name=" + escape(PerPopulatedSaveName)
    //alert("../../weichertvalet/savedlistings.aspx?Action=Save&propertyID=" + escape(propertyID) + "&PerPopulatedSaveName=" + escape(PerPopulatedSaveName))
}

function externalLinks(elem) 
{
    if (!elem.getElementsByTagName){ return; }
    var anchors = elem.getElementsByTagName("a");
    for (var i=0; i<anchors.length; i++) 
    {
        var anchor = anchors[i];
        if (anchor.getAttribute("href"))
        {
         anchor.target = "_blank";
        }
    }
}

function CreateXMLDocumentFromString( xmlText )
{
    var doc = null;
    // code for IE
    if (window.ActiveXObject)
    {
        var doc=new ActiveXObject("Microsoft.XMLDOM");
        doc.async="false";
        doc.loadXML(xmlText);
    }
    // code for Mozilla, Firefox, Opera, etc.
    else
    {
      var parser=new DOMParser();
      var doc=parser.parseFromString(xmlText,"text/xml");
    }
    return doc;
}
function CreateXMLDocument()
{
    var xmlDoc = null;
    // code for IE
    if (window.ActiveXObject)
    {
	    xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
	    xmlDoc.async=false;
    }
    // code for Mozilla, Firefox, Opera, etc.
    else if (document.implementation && document.implementation.createDocument)
    {
	    xmlDoc=document.implementation.createDocument("","",null);
    }
    else
    {
        //error
	    alert('Your browser cannot handle this script');
    }
    return xmlDoc;
}

function ProcessCallBackResult(result, context)
{
    var results = result.split('&');
    for( var i = 0; i < results.length; i++ )
    {
        var item = results[i];
        var key = item.split('=')[0];
        var value = unescape(item.split('=')[1]);
        value = value.replace(/\+/gi, ' ');

        if(key.toLowerCase() == "redirect")
        {
            location.href = value;
            return;
        }
        else
        {
            document.getElementById(key).innerHTML = value;        
        }
    }
}
function ClientCallbackError(result, context)
{
    alert("An error has occured, please try again.");
}

    /**************************************************************
	** Function to test for numeric value.	
	** Returns false if value passed is not a numeric value
	** 
	** Created by:      Gene Ernst
	** Last modified:	1/17/2007										
	***************************************************************/
function IsNumeric(strString)   
{
    var strValidChars = "0123456789.-";
    var strChar;
    var blnResult = true;

    if (strString.length == 0) return false;

    //  test strString consists of valid characters listed above
    for (i = 0; i < strString.length && blnResult == true; i++)
        {
            strChar = strString.charAt(i);
            if (strValidChars.indexOf(strChar) == -1)
            {
                blnResult = false;
            }
        }
   return blnResult;
}

function showPopup(id)
{
    document.getElementById(id).style.display = "";
    return false;
}
function hidePopup(id)
{
    document.getElementById(id).style.display = "none";
    return false;
}

/*
   name - name of the cookie
   value - value of the cookie
   [expires] - expiration date of the cookie
     (defaults to end of current session)
   [path] - path for which the cookie is valid
     (defaults to path of calling document)
   [domain] - domain for which the cookie is valid
     (defaults to domain of calling document)
   [secure] - Boolean value indicating if the cookie transmission requires
     a secure transmission
   * an argument defaults when it is assigned null as a placeholder
   * a null placeholder is not required for trailing omitted arguments
*/

function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}


/*
  name - name of the desired cookie
  return string containing value of specified cookie or null
  if cookie does not exist
*/

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

function addCommas(nStr)
{
    nStr += '';
    var x = nStr.split('.');
    var x1 = x[0];
    var x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
}

function handleEnter(field, event, buttonid) {
    var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
    if (keyCode == 13) {
        document.getElementById(buttonid).onclick();
	    return false;
    } 
    else
    return true;
}     

function addEvent(context, eventName, handler){
    if(context) {
        //strip off 'on' prefix for use with addEventListener
        if(eventName.substring(0, 2) == "on") {
            eventName = eventName.substring(2,eventName.length);
        }
        if (context.addEventListener){
            context.addEventListener(eventName, handler, false);
        } 
        else if (context.attachEvent){
            context.attachEvent("on"+eventName, handler);
        }
    }
}

function makeExternalLinks() { 
    if (!document.getElementsByTagName) return; 

    var anchors = document.getElementsByTagName("a"); 
    for (var i=0; i<anchors.length; i++) { 
        var anchor = anchors[i]; 
        var relvalue = anchor.getAttribute("rel");

        if (anchor.getAttribute("href")) {
            var external = /external/;
            var relvalue = anchor.getAttribute("rel");
            if (external.test(relvalue)) { anchor.target = "_blank"; }
        }
    } 
} 

function getQueryValue( url, name )
{
  var name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( url );
  if( results == null )
    return "";
  else
    return results[1];
}

function switchImage(imgName, imgSrc)

   {
   document.getElementById(imgName).src = imgSrc;
   }
   
function displayFlashVideo(elemId,player, theme, video, height, width){
		var so = new SWFObject(player, 'player', width, height, '7', "#ffffff");
		so.addParam("allowScriptAccess","always");
		so.addParam("allowFullScreen","true");
		so.addParam("wmode", "window");
		so.addParam("scale", "noscale");
		so.addVariable("file", video);
		so.addVariable("width", width);
		so.addVariable("height", height);
		so.addVariable("theme", theme);
		// the movie loop parameter - can be set to 0 (the video will stop after the playback is over) or 1 (playback is looped)
		so.addVariable("repeat", "0");
		// the movie automatic startup parameter - can be set to 0 (the playback will not start automatically once the page with the video is loaded) or 1 (autostart)
		so.addVariable("autostart", "0");
		// the name and path of the image that will be displayed when the page is loaded, provided that the autostart value is set to 0 (i.e. no autostart is enabled)
		so.addVariable("image", "");
		// the volume that will be used to playback your movie
		so.addVariable("volume", "50");
		so.addVariable("scaleonresize", "none");
		// the top and the bottom captions displayed when you roll your mouse cursor over the player window
		so.addVariable("top_caption", " ");
		so.addVariable("bottom_caption", " ");
		so.addVariable("showlogo", "0");
		so.addVariable("v", getQueryParamValue("v"));
		so.write(elemId);
}