var saveElement;

function enableSave() {
    var elem = document.getElementById(saveElement);
    if (elem != null) {
         elem.onclick = returnTrue;
         elem.style.color = '#2A56D6';
    }
}

function returnTrue() {
    return true;
}

function changeColor(element) {
    var elem = document.getElementById(element);
    if (elem != null) {
        alert(elem.style.color); 
        elem.style.color = 'red';
    }
}

if (document.images) {
	var menu_up = new Array();
	var menu_over = new Array();
}

function changepic(imgTagName,imgFileName) {
	if (document.images) {
		if (document.images[imgTagName]) {
			document.images[imgTagName].src = imgFileName;
		}
	}
}

function movepic(img_name,img_src) {
document[img_name].src=img_src;
}

//******************************************************************
//Functions used to hide and show controls inside assetClasses pages
//******************************************************************
function showHideInputs(ctls, show) {
    //show or hide the controls in the given array (ctls)
    validator.clear();
    for (i = 0; i < ctls.length; i++) {
        s = ctls[i][0];
        $('dt' + s).style.display = ((show) ? '' : 'none');
        $('dd' + s).style.display = ((show) ? '' : 'none');
        if (show) {
            //add validators
            if (ctls[i][1].length > 0) {
                v = ctls[i][2];
                for (j = 0; j < v.length; j++) {
                    validator.addField(ctls[i][1], v[j], null, null, 'dt' + ctls[i][0]);
                }
            }
        }
    }
}

function getArrayElement(search, find) {
    //find the element in "search" using "find" where search 2-dim array
    for (i = 0; i < search.length; i++) {
        x = search[i].indexOf(find);
        if (x != -1) { return i; }
    }
}
//******************************************************************

// Resizes Iframe according to content
function resizeIFrame(obj, ifr){
    docHeight = $(ifr).contentWindow.document.body.scrollHeight + 30;
    obj.style.height = docHeight + 'px';
}

//css bubble code
/*************************************************************************
  This code is from Dynamic Web Coding at dyn-web.com
  Copyright 2003-5 by Sharon Paine
  See Terms of Use at http://www.dyn-web.com/bus/terms.html
  regarding conditions under which you may use this code.
  This notice must be retained in the code as is!
*************************************************************************/

function doTooltip(e, title, text) {
  if ( typeof Tooltip == "undefined" || !Tooltip.ready ) return;
  Tooltip.show(e, title, text);
}
function doTooltipBlue(e, title, text) {
  if ( typeof TooltipBlue == "undefined" || !TooltipBlue.ready ) return;
  TooltipBlue.show(e, title, text);
}

function hideTip() {
  if ( typeof Tooltip == "undefined" || !Tooltip.ready ) return;
  Tooltip.hide();
}
function hideTipBlue() {
  if ( typeof TooltipBlue == "undefined" || !TooltipBlue.ready ) return;
  TooltipBlue.hide();
}


var searchState = 'in';
var searchShown = false;

function slideSearch() {
    switch (searchState) {
        case 'out':
            searchSlide.slideOut();
            searchState = 'in';
            break;
            
        case 'in':
            searchState = 'out';
            searchSlide.slideIn();
            break;
    }  
}

var map = null;
var geocoder = null;

function initializeGMaps_Coords(lat, long) {
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map_canvas"));
        var point = new GLatLng(lat, long);
        map.setCenter(point, 14);
        var marker = new GMarker(point);
        map.addOverlay(marker);
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
        log('GMaps: Using lat / long');
    }
}


function initializeGMaps_Address(address, backupAddress) {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map_canvas"));
    geocoder = new GClientGeocoder();
    
     if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (point) {
              map.setCenter(point, 14);
              var marker = new GMarker(point);
              map.addOverlay(marker);
              map.addControl(new GSmallMapControl());
              map.addControl(new GMapTypeControl());
              //map.addControl(new GSmallZoomControl());
              //marker.openInfoWindowHtml(address);
              //log('GMaps: Using main address');
            } else {
               geocoder.getLatLng(
                  backupAddress,
                  function(point) {
                    if (!point) {
                      alert('We were unable to find the Google Map for this address');
                    } else {
                      map.setCenter(point, 14);
                      var marker = new GMarker(point);
                      map.addOverlay(marker);
                      map.addControl(new GSmallMapControl());
                      map.addControl(new GMapTypeControl());
                      //log('GMaps: Using backup address');
                    }
                  }
                );            
            }
          }
        );
      }
  }
}

function log(message) {
    if (window.gecko && typeof(window["console"]) != 'undefined' && debug)
        console.log(message);
    else if (window.ie && debug)
        alert(message);  
}

function registerPopup() {
    var internalWidth = '340px;';
    var height = '322';
    var width = '360'; 
    
    if (window.ie)
        height = '306';
    else if (window.chrome || window.webkit)
        height = '321';
        
    MOOdalBox.open(
        baseUrl+'content/registerWhich.aspx?dt=' + formatDate(new Date(), 'yyyyMMddHHmmss') + '&style='+URLEncode('width:'+internalWidth),
        'Register with Rubii',
        width + ' ' + height,
        true
    );
}

function imageMoodal(path, width, height, text) {
    Lightbox.show(
        baseUrl + path,
        text
    );
    /*h = '<html><body><img src="' + baseUrl + path + '" border="0" /></body></html>';
    MOOdalBox.open(h, text, width + ' ' + height, true);*/
}

function contactAdviser_Main(destUrl) {
    /*log('contact adviser clicked: authenticated = ' + authenticated + '. Redirected to ' + destUrl);
    if (authenticated) {
        setTimeout(function() {
            window.location.href = destUrl;
        }, 0);
    } else {
        if (typeof(pageTracker) != 'undefined') 
            pageTracker._trackPageview("/content/contactAdviser/registerLogin.aspx"); 
            
        doMoodalRegister(destUrl); 
    }*/
    setTimeout(function() {
        window.location.href = destUrl;
    }, 0);
}

// widths and heights for the moodal register / login popups, which need to be different per browser
var registerHeight = '453';
var registerWidth = '620';
var loginHeight = '293';
var loginWidth = '620';

if (window.ie7)
    registerHeight = '460';
else if (window.ie6) {
    registerHeight = '468'; 
    loginHeight = '300'; 
} 
else if (window.chrome) {
    registerHeight = '471';
    loginHeight = '306';
} else if (window.webkit) {
    registerHeight = '469';
    loginHeight = '305'; 
} 

function doMoodalRegister(returnUrl) {
    //alert(registerHeight);
    MOOdalBox.open(
        baseUrl+'content/moodalRegister.aspx?dt=' + formatDate(new Date(), 'yyyyMMddHHmmss') + '&ReturnUrl=' + URLEncode(returnUrl),
        'Register with Rubii',
        registerWidth + ' ' + registerHeight,
        true
    );
}

function tellAFriendPopup_Main(url) {
    var isAdviser = true;
    if (url.indexOf('/blogs/') > -1 || url.indexOf('/articles/') > -1) 
        isAdviser = false;
         
    if (authenticated) {
        width = '620';
        
        //mozilla
        if (isAdviser)
            height = '362';
        else
            height = '326';
        
        if (window.ie7) {
            if (isAdviser)
                height = '342';
            else
                height = '306'; 
        } else if (window.ie6) {
            if (isAdviser)
                height = '356';
            else
                height = '308'; 
        } else if (window.chrome) {
            if (isAdviser)
                height = '365';
            else
                height = '331';
        } else if (window.webkit) {
            if (isAdviser)
                height = '364';
            else
                height = '330'; 
        }
            
    } else {
        width = '620';
        
        //mozilla
        if (isAdviser)
            height = '401';
        else
            height = '365';
        
        if (window.ie7) {
            if (isAdviser)
                height = '384';
            else
                height = '348'; 
        } else if (window.ie6) {
            if (isAdviser)
                height = '386';
            else
                height = '350';
        } else if (window.chrome) {
            if (isAdviser)
                height = '403';
            else
                height = '368';
        } else if (window.webkit) {
            if (isAdviser)
                height = '402';
            else
                height = '367';
        }
    }
    
    //alert(width + '\n' + height);
    MOOdalBox.open(
        url,
        'Tell a friend',
        width + ' ' + height,
        true
    );
}
            
function postData() {
    new Ajax(ajaxProcessorURL, {
        method: 'post',
        postBody: $(mainFormID),
        onComplete: handleResponse,
        //update: $('returnMsg'),
        evalResponse: true
    }).request();
} 

var ajaxGWC_Ctl;
var ajaxGWC_UpdateCtl;
function ajax_GetWebControl(ctl, updateCtl, querystring) {
    ajaxGWC_Ctl = ctl;
    ajaxGWC_UpdateCtl = updateCtl;
    new Ajax(baseUrl + 'AJAX/ajaxWebControlHTML.aspx?control='+ctl+'&'+querystring, {
        method: 'get',
        update: $(updateCtl),
        onComplete: handleGotWebControl
    }).request();
}

function handleGotWebControl() {
    if (typeof(handleGotWebControl_Local) != "undefined")
        handleGotWebControl_Local();
}
          
function addAdvisorToList(advisorId) {
    if (authenticated) {
        new Ajax(ajaxProcessorURL + '?dt=' + formatDate(new Date(), 'yyyyMMddHHmmss') + '&processor=addMyAdvisor&id=' + advisorId, {
            method: 'get',
            onComplete: addedAdvisor,
            evalResponse: true
        }).request();
    } else { 
        alert('You need to be logged in to add Advisers to your list. Please login then try again.');
    }
}

function addedAdvisor() {
    if (response.error == 'none') {
        alert('We have added this Advisor to your \'My Advisors\' list.');
        $('addAdvisorLink').style.display = 'none';
    } else {
        log(response.error);
        alert('We are very sorry, but a problem occurred while adding your adviser.\n\nOur technical staff have been informed and will fix the problem as soon as possible.\n\nPlease try again later.');
   }
}

function removeAdvisorFromList(advisorId) {
    new Ajax(ajaxProcessorURL + '?dt=' + formatDate(new Date(), 'yyyyMMddHHmmss') + '&processor=removeMyAdvisor&id=' + advisorId, {
        method: 'get',
        onComplete: removedAdvisor,
        evalResponse: true
    }).request();
}

function removedAdvisor() {
    if (response.error == 'none') {
        setTimeout(function() {
            window.location.href = window.location.href;
        }, 0);
        //alert('We have removed this Advisor form your \'My Advisors\' list.');
    } else {
        log(response.error);
        alert('We are very sorry, but a problem occurred while removing your adviser.\n\nOur technical staff have been informed and will fix the problem as soon as possible.\n\nPlease try again later.');
   }
}

function resetAdvancedSearch() {
    f = document.forms[0];
    for (i = 0; i < f.elements.length; i++) {
        if (f.elements[i].name.indexOf('SearchPane') > -1 || f.elements[i].name.indexOf('SearchContainer') > -1) {
            switch (f.elements[i].type) {
                case 'checkbox':
                    f.elements[i].checked = false;
                    break;
                case 'select-one':
                    f.elements[i].value = 0;
                    break;
                case 'text':
                    f.elements[i].value = '';
                    break;
                case 'radio':
                    if (f.elements[i].name.indexOf('gender') > -1) {
                        if (f.elements[i].value == 'rdoGenderEither') { f.elements[i].checked = true; }
                    }
                    if (f.elements[i].name.indexOf('payment') > -1) {
                        if (f.elements[i].value == 'rdoPaymentAny') { f.elements[i].checked = true; }
                    }
                    if (f.elements[i].name.indexOf('affiliate') > -1) {
                        if (f.elements[i].value == 'rdoAffiliateAny') { f.elements[i].checked = true; }
                    }
                    break;
            }
        }
    }
}