// Centralized place for onload function call

var idPrefix = "ctl00_contentBody_";
function getElement(id) {
    if (document.getElementById(idPrefix + id) != null)
        return document.getElementById(idPrefix + id);
    else if (document.getElementById(id) != null)
        return document.getElementById(id);
    else
        return document.getElementsByName(id)[0];
}

function getWebPartElement(webpartelementid, id)
{
    var webpartidprefix = webpartelementid.toString().substring(0,webpartelementid.toString().lastIndexOf("_")+1);
    if (document.getElementById(webpartidprefix + id) != null)
    {
        return document.getElementById(webpartidprefix + id);
    }
}


// Clear Address
function clearAddress() {
	document.getElementById('email_address').value = "";
}

// Write Flash Objects
function writeflash(src, width, height, name, align, bgcolor, version) {
	document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=' + version + '" width="' + width + '" height="' + height + '" id="' + name + '" align="' + align + '">');
	document.write('<param name="allowScriptAccess" value="sameDomain" />');
	document.write('<param name="movie" value="' + src + '" />');
	document.write('<param name="quality" value="high" />');
	document.write('<param name="wmode" value="opaque" />');
	document.write('<param name="bgcolor" value="#' + bgcolor + '" />');
	document.write('<embed src="' + src + '" quality="high" wmode="opaque" bgcolor="#' + bgcolor + '" width="' + width + '" height="' + height + '" name="' + name + '" align="' + align + '" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />');
	document.write('</object>');
}

function writemeetyourteamflash(src, width, height, name, align, bgcolor, version, datafile) {
	document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=' + version + '" width="' + width + '" height="' + height + '" id="' + name + '" align="' + align + '">');
	document.write('<param name="movie" value="' + src + '" />');
	document.write('<param name="quality" value="high" />');
	document.write('<param name="bgcolor" value="#' + bgcolor + '" />');
	document.write('<param name="datafile" value="' + datafile + '" />');
	document.write('<embed src="' + src + '" quality="high" bgcolor="#' + bgcolor + '" width="' + width + '" ' +
	                ' height="' + height + '" name="' + name + '" align="' + align + '" ' +
	                ' datafile="' + datafile + '" ' +
	                ' type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />');
	document.write('</object>');
}


// Generate Anti-SPAM E-Mail
function generate_address(appearance, username, hostname, tld)
{
	var atsign = "&#64;";

	// If SAMEADDRSS is present, then the E-mail address will be shown instead of a custom value
	if (appearance == "SAMEADDRESS")
	{
		appearance = username + atsign + hostname + "." + tld;
	}

	var addr = username + atsign + hostname + "." + tld;
	document.write("<" + "a" + " " + "href=" + "mail" + "to:" + addr + ">" + appearance + "<\/a>");
}

// Pre-Load images for image swapping.
function wm_preloadimages()
{
	if (document.images)
	{
		if (typeof(document.wm) == 'undefined')
		{
			document.wm = new Object();
		}
		document.wm.loadedimages = new Array();
		var arglength = wm_preloadimages.arguments.length;
		for(arg=0;arg<arglength;arg++)
		{
			document.wm.loadedimages[arg] = new Image();
			document.wm.loadedimages[arg].src = wm_preloadimages.arguments[arg];
		}
	}
}

// Swap images function.
function wm_imageswap(dasrc, daimage)
{
	var objStr,obj;

	if(document.images)
	{
		if (typeof(daimage) == 'string')
		{
			objstr = 'document.' + daimage;
			obj = eval(objstr);
			obj.src = dasrc;
		}
		else if ((typeof(daimage) == 'object') && daimage && daimage.src)
		{
			daimage.src = dasrc;
		}
	}
}


function checkEmail(contactform)
{
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(contactform.email.value))
		return true;
	
	alert("I'm sorry, but that E-mail address doesn't appear to be correct.\nPlease check to make sure it is correct.")
	return false;
}

function URLEncode (clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') {
    	output += match[1];
      x += match[1].length;
    } else {
      if (clearString[x] == ' ')
        output += '+';
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}

function URLDecode (encodedString) {
  var output = encodedString;
  var binVal, thisString;
  var myregexp = /(%[^%]{2})/;
  while ((match = myregexp.exec(output)) != null
             && match.length > 1
             && match[1] != '') {
    binVal = parseInt(match[1].substr(1),16);
    thisString = String.fromCharCode(binVal);
    output = output.replace(match[1], thisString);
  }
  return output;
}

function AutoAdvance(e, txtStart, iLength, txtNext)
{
    var key;
    if(window.event)
        key = e.keyCode;     //IE
    else
        key = e.which;     //firefox
    
    // IF IT'S NOT THE DELETE KEY THAT WAS PRESSED, AUTO ADVANCE IF LENGTH IS 3
    if (key != 8 && key != 16 && key != 9)
    {
        if (txtStart.value.length == iLength)
            txtNext.focus();
    }
}

function phoneautotab(e, areacodefield, phonefield)
{
	// IF IT'S NOT THE DELETE KEY THAT WAS PRESSED, AUTO ADVANCE IF LENGTH IS 3
	if (e.keyCode != 8 && e.keyCode != 16 && e.keyCode != 9)
	{
		if (areacodefield.value.length == 3)
			phonefield.focus();
	}
}

function maskit(e, fld)
{
	 if(window.event)
			key = e.keyCode;     //IE
     else
     	key = e.which;     //firefox

	if (key != 45 && key != 109)
	{
	    var tmpStr = "";
		var isnamedFone;
		isnamedFone = true;
	    keyCount = fld.value.length;
	    keyEntered = fld.value.substring(keyCount-1,keyCount);
	    if (keyCount <= 1) isnamedFone = false;
	    if (!isnamedFone) isnamedFone = chkNAN(keyEntered);
	    keyCount++;
	    switch (keyCount)
	    {
			case 1:
				tmpStr += fld.value;
				fld.value = tmpStr;
				break;
			case 4:
				fld.value += "-";
				break;
		}
	}
	else
		return false;
}

function chkNAN(char2chk)
{
	var validNum = "0123456789";
	if (validNum.indexOf(char2chk) == "-1")
		return(confirm("You have entered a non-numeric character.\n Do you want to turn off non-numeric character checking?"));
}

function disableEnterKey(e)
{
     var key;      
     if(window.event)
          key = window.event.keyCode; //IE
     else
          key = e.which; //firefox      

     return (key != 13);
}


function isenterkey(e)
{
     var key;      
     if(window.event)
          key = window.event.keyCode; //IE
     else
          key = e.which; //firefox      

     return (key == 13);
}

function callsearch()
{
    window.location = "/searchresults.aspx?criteria=" + getElement("txtSiteSearch").value;
}
function call1031search()
{
    window.location = "/1031 Exchange/searchresults.aspx?criteria=" + getElement("txtSiteSearch").value;
}

function showhide(divid, linkid, ontext, offtext) {
    if (getElement(divid).style.display == "none") {
        getElement(divid).style.display = "";
        getElement(linkid).innerHTML = ontext;
    }
    else {
        getElement(divid).style.display = "none";
        getElement(linkid).innerHTML = offtext;
    }
}

function formatCurrency(num) {
    num = num.toString().replace(/\$|\,/g, '');
    if (isNaN(num))
        num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num * 100 + 0.50000000001);
    cents = num % 100;
    num = Math.floor(num / 100).toString();
    if (cents < 10)
        cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
        num = num.substring(0, num.length - (4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3));
    return (((sign) ? '' : '-') + '$' + num + '.' + cents);
}