//=========================================================================
// Mangle.js
//=========================================================================
function mangle() {
    if (!document.getElementsByTagName && !document.createElement &&
        !document.createTextNode) return;
    var nodes = document.getElementsByTagName("span");
    for(var i=nodes.length-1;i>=0;i--) {
        if (nodes[i].className=="change") {
            var at = / at /;
            var dot = / dot /g;
            var node = document.createElement("a");
            var address = nodes[i].firstChild.nodeValue;

            address = address.replace(at, "@");
            address = address.replace(dot, ".");

            node.setAttribute("href", "mailto:"+address);
            node.appendChild(document.createTextNode(address));
			
            var prnt = nodes[i].parentNode;
            for(var j=0;j<prnt.childNodes.length;j++)
	            if (prnt.childNodes[j] == nodes[i]) {
		            if (!prnt.replaceChild) return;
		            prnt.replaceChild(node, prnt.childNodes[j]);
		            break;
	            }
        }
    }
}

//=========================================================================
// Cookie.js
//=========================================================================
var cookiePath = '/';

if (getCookie('refer') == null){
	var curdate = new Date();
	var expDate = new Date();
	//valid one year
	expDate.setTime( expDate.getTime() + ( 365 * 24 * 60 * 60 * 1000 ) );
	setCookie( "refer", curdate.toDateString() + ' - ' + document.referrer, expDate, cookiePath, "buildium.com" );
} 

if (getCookie('affiliate') == null && location.search.indexOf("SSAID") != -1){
   var expDate = new Date();
	//valid one year
	expDate.setTime( expDate.getTime() + ( 365 * 24 * 60 * 60 * 1000 ) );
	setCookie( "affiliate", location.search.substring(1, location.search.length), expDate, cookiePath, "buildium.com" );
}

if (getCookie('promocode') == null && location.search.toLowerCase().indexOf("promocode=") != -1) {
    var expDate = new Date();
    //valid one year
    expDate.setTime(expDate.getTime() + (365 * 24 * 60 * 60 * 1000));
    setCookie("promocode", extractQueryStringParameter('promocode'), expDate, cookiePath, "buildium.com");
}

if (getCookie('utmsource') == null && location.search.toLowerCase().indexOf("utm_source=") != -1) {
    var expDate = new Date();
    //valid one year
    expDate.setTime(expDate.getTime() + (365 * 24 * 60 * 60 * 1000));
    setCookie("utmsource", extractQueryStringParameter('utm_source'), expDate, cookiePath, "buildium.com");
}

if (getCookie('landingUrl') == null && location.search.toLowerCase().indexOf("utm_source=") != -1) {
    var expDate = new Date();
    //valid for six months
    expDate.setTime(expDate.getTime() + (183 * 24 * 60 * 60 * 1000));
    setCookie("landingUrl", location, expDate, cookiePath, "buildium.com");
}

function extractQueryStringParameter(paramName) {
    var queryString = location.search;
    var normQueryString = queryString.toLowerCase(); // normalized
    var searchString = "?" + paramName + "=";
    var position = normQueryString.indexOf(searchString);
    if (position == -1) {
        searchString = "&" + paramName + "=";
        position = normQueryString.indexOf(searchString);
    }
    if (position == -1) {
        return ""; 
    }
    var nextFieldPosition = normQueryString.indexOf('&', position + 1);
    var endPosition = nextFieldPosition != -1 ? nextFieldPosition : normQueryString.length;
    return unescape(queryString.substring(position + searchString.length, endPosition));
}

function getCookieVal( offset )
{
  var endstr = document.cookie.indexOf ( ";", offset );

  if ( endstr == -1 )
  {
    endstr = document.cookie.length;
  }
  return unescape( document.cookie.substring( offset, endstr ) );
}

//---------------------------------------------------------------------------
// Returns the value of a cookie corresponding to the name
// Returns null if the cookie can't be found

function getCookie( name )
{
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;

  while ( i < clen )
  {
    var j = i + alen;

    if ( document.cookie.substring( i, j ) == arg )
    {
      return getCookieVal ( j );
    }

    i = document.cookie.indexOf( " ", i ) + 1;

    if ( i == 0 )
    {
      break;
    }
  }
  return null;
}

//---------------------------------------------------------------------------
// Sets a cookie according to the name and value.
// Optional parameters:
// - expires (Date object)
// - path (use cookiePath for the whole domain)
// - domain
// - secure
// For more information about those parameter, check www.cookiecentral.com

function setCookie( name, value )
{
  var argv = setCookie.arguments;
  var argc = setCookie.arguments.length;
  var expires = (argc > 2) ? argv[2] : null;
  var path = (argc > 3) ? argv[3] : null;
  var domain = (argc > 4) ? argv[4] : null;
  var secure = (argc > 5) ? argv[5] : false;

  document.cookie = name + "=" + escape (value) +
    ( ( expires == null ) ? "" : ( "; expires=" + expires.toGMTString() ) ) +
    ( ( path == null ) ? "" : ( "; path=" + path ) ) +
    ( ( domain == null ) ? "" : ( "; domain=" + domain ) ) +
    ( ( secure == true ) ? "; secure" : "" );
}

function deleteCookie ( name )
{
  var exp = new Date();
  exp.setTime ( exp.getTime() - 1 );  // This cookie is history
  setCookie( name, "", exp, cookiePath );
}

function ValidateForHTML(txtbox){
    
    if (txtbox.value.indexOf('<',0) >= 0)
    {
        
        txtbox.value = txtbox.value.replace(/\</g,'');
    }
}

// time zone calculation
// original script by Josh Fraser (http://www.onlineaspect.com)
var timeZones = [
    { key: '-12:00,0', text: '(-12:00) International Date Line West' }, 
    { key: '-11:00,0', text: '(-11:00) Midway Island, Samoa' }, 
    { key: '-10:00,0', text: '(-10:00) Hawaii' }, 
    { key: '-09:00,1', text: '(-09:00) Alaska' }, 
    { key: '-08:00,1', text: '(-08:00) Pacific Time (US & Canada)' }, 
    { key: '-07:00,0', text: '(-07:00) Arizona' }, 
    { key: '-07:00,1', text: '(-07:00) Mountain Time (US & Canada)' }, 
    { key: '-06:00,0', text: '(-06:00) Central America, Saskatchewan' }, 
    { key: '-06:00,1', text: '(-06:00) Central Time (US & Canada), Guadalajara, Mexico city' }, 
    { key: '-05:00,0', text: '(-05:00) Indiana, Bogota, Lima, Quito, Rio Branco' }, 
    { key: '-05:00,1', text: '(-05:00) Eastern time (US & Canada)' }, 
    { key: '-04:00,1', text: '(-04:00) Atlantic time (Canada), Manaus, Santiago' }, 
    { key: '-04:00,0', text: '(-04:00) Caracas, La Paz' }, 
    { key: '-03:30,1', text: '(-03:30) Newfoundland' }, 
    { key: '-03:00,1', text: '(-03:00) Greenland, Brasilia, Montevideo' }, 
    { key: '-03:00,0', text: '(-03:00) Buenos Aires, Georgetown' }, 
    { key: '-02:00,1', text: '(-02:00) Mid-Atlantic' }, 
    { key: '-01:00,1', text: '(-01:00) Azores' }, 
    { key: '-01:00,0', text: '(-01:00) Cape Verde Is.' }, 
    { key: '00:00,0', text: '(00:00) Casablanca, Monrovia, Reykjavik' }, 
    { key: '00:00,1', text: '(00:00) GMT: Dublin, Edinburgh, Lisbon, London' }, 
    { key: '+01:00,1', text: '(+01:00) Amsterdam, Berlin, Rome, Vienna, Prague, Brussels' }, 
    { key: '+01:00,0', text: '(+01:00) West Central Africa' }, 
    { key: '+02:00,1', text: '(+02:00) Amman, Athens, Istanbul, Beirut, Cairo, Jerusalem' }, 
    { key: '+02:00,0', text: '(+02:00) Harare, Pretoria' }, 
    { key: '+03:00,1', text: '(+03:00) Baghdad, Moscow, St. Petersburg, Volgograd' }, 
    { key: '+03:00,0', text: '(+03:00) Kuwait, Riyadh, Nairobi, Tbilisi' }, 
    { key: '+03:30,0', text: '(+03:30) Tehran' }, 
    { key: '+04:00,0', text: '(+04:00) Abu Dhadi, Muscat' }, 
    { key: '+04:00,1', text: '(+04:00) Baku, Yerevan' }, 
    { key: '+04:30,0', text: '(+04:30) Kabul' }, 
    { key: '+05:00,1', text: '(+05:00) Ekaterinburg' }, 
    { key: '+05:00,0', text: '(+05:00) Islamabad, Karachi, Tashkent' }, 
    { key: '+05:30,0', text: '(+05:30) Chennai, Kolkata, Mumbai, New Delhi, Sri Jayawardenepura' }, 
    { key: '+05:45,0', text: '(+05:45) Kathmandu' }, 
    { key: '+06:00,0', text: '(+06:00) Astana, Dhaka' }, 
    { key: '+06:00,1', text: '(+06:00) Almaty, Nonosibirsk' }, 
    { key: '+06:30,0', text: '(+06:30) Yangon (Rangoon)' }, 
    { key: '+07:00,1', text: '(+07:00) Krasnoyarsk' }, 
    { key: '+07:00,0', text: '(+07:00) Bangkok, Hanoi, Jakarta' }, 
    { key: '+08:00,0', text: '(+08:00) Beijing, Hong Kong, Singapore, Taipei' }, 
    { key: '+08:00,1', text: '(+08:00) Irkutsk, Ulaan Bataar, Perth' }, 
    { key: '+09:00,1', text: '(+09:00) Yakutsk' }, 
    { key: '+09:00,0', text: '(+09:00) Seoul, Osaka, Sapporo, Tokyo' }, 
    { key: '+09:30,0', text: '(+09:30) Darwin' }, 
    { key: '+09:30,1', text: '(+09:30) Adelaide' }, 
    { key: '+10:00,0', text: '(+10:00) Brisbane, Guam, Port Moresby' }, 
    { key: '+10:00,1', text: '(+10:00) Canberra, Melbourne, Sydney, Hobart, Vladivostok' }, 
    { key: '+11:00,0', text: '(+11:00) Magadan, Solomon Is., New Caledonia' }, 
    { key: '+12:00,1', text: '(+12:00) Auckland, Wellington' }, 
    { key: '+12:00,0', text: '(+12:00) Fiji, Kamchatka, Marshall Is.' }, 
    { key: '+13:00,0', text: '(+13:00) Nuku\'alofa' }
];

function calculateTimeZone() {
    var rightNow = new Date();
    var jan1 = new Date(rightNow.getFullYear(), 0, 1, 0, 0, 0, 0);  // jan 1st
    var june1 = new Date(rightNow.getFullYear(), 6, 1, 0, 0, 0, 0); // june 1st
    var temp = jan1.toGMTString();
    var jan2 = new Date(temp.substring(0, temp.lastIndexOf(" ") - 1));
    temp = june1.toGMTString();
    var june2 = new Date(temp.substring(0, temp.lastIndexOf(" ") - 1));
    var std_time_offset = (jan1 - jan2) / (1000 * 60 * 60);
    var daylight_time_offset = (june1 - june2) / (1000 * 60 * 60);
    var dst;
    if (std_time_offset == daylight_time_offset) {
        dst = "0"; // daylight savings time is NOT observed
    } else {
        // positive is southern, negative is northern hemisphere
        var hemisphere = std_time_offset - daylight_time_offset;
        if (hemisphere >= 0)
            std_time_offset = daylight_time_offset;
        dst = "1"; // daylight savings time is observed
    }

    // check just to avoid error messages
    for (var i = 0; i < timeZones.length; i++) {
        if (timeZones[i].key == convert(std_time_offset) + "," + dst) {
            return timeZones[i].text;
        }
    }

    return 'Unknown';
}

function convert(value) {
    var hours = parseInt(value);
    value -= parseInt(value);
    value *= 60;
    var mins = parseInt(value);
    value -= parseInt(value);
    value *= 60;
    var secs = parseInt(value);
    var display_hours = hours;
    // handle GMT case (00:00)
    if (hours == 0) {
        display_hours = "00";
    } else if (hours > 0) {
        // add a plus sign and perhaps an extra 0
        display_hours = (hours < 10) ? "+0" + hours : "+" + hours;
    } else {
        // add an extra 0 if needed 
        display_hours = (hours > -10) ? "-0" + Math.abs(hours) : hours;
    }

    mins = (mins < 10) ? "0" + mins : mins;
    return display_hours + ":" + mins;
}
// end time zone
