<!--
// -------------------------------------- Make XMLHttpRequest Request Object --------------------------------------
// Build a new Object and check to see what browser it is
var request = null;

try {
 request = new XMLHttpRequest();
} catch (trymicrosoft) {
 try {
   request = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (othermicrosoft) {
   try {
     request = new ActiveXObject("Microsoft.XMLHTTP");
   } catch (failed) {
     request = null;
   }
 }
}

if (request == null)
 alert("Error creating request object!");

var request2 = null;

try {
 request2 = new XMLHttpRequest();
} catch (trymicrosoft) {
 try {
   request2 = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (othermicrosoft) {
   try {
     request2 = new ActiveXObject("Microsoft.XMLHTTP");
   } catch (failed) {
     request2 = null;
   }
 }
}

if (request2 == null)
 alert("Error creating request object!");

function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e){
 // Internet Explorer
 try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }
 }
return xmlHttp;
}

//includes js files for the application
function include(file){
  var script  = document.createElement('script');
  script.type = 'text/javascript';
  script.src  = file;
  script.defer = false;
  document.getElementsByTagName('head').item(0).appendChild(script);
}
// Include other files
include('/templates/common/js/livesearch.js');  // live search bar
//include('/templates/common/js/ticker.js');      // ticker
include('/templates/common/js/newsticker.js'); // news and weather ticker
include('/templates/common/js/trim.js'); // news and weather ticker
include('/templates/common/js/level2-newsevents.js'); // level news and calendar piece
//include('/templates/common/js/lightwindow-vcsu.js');  // Light window library for popup windows

function trim(str){
	var newstr = '';
	for(var i = 0;i<str.length;i++) { newstr = (str.charAt(i)!=' ' && str.charAt(i)!='')? newstr + str.charAt(i):newstr; }
	return newstr;
}

// ScriptAculoUs Effects 
function myEffects(effect,id,val1,val2){
	//alert(val1);
	//val1 = parseFloat(val1)*1000;
	//val2 = parseFloat(val2)*1000;
	
	switch(effect){
		case 'blindUp':
		case 'slideUp':
			$('#' + id).slideUp(val1);
			//new Effect.SlideUp(document.getElementById(id), {duration:val1}); return false;
		break;
		case 'slideDown':
		case 'blindDown':
		    $('#' + id).slideDown(val1);
			//new Effect.SlideDown(document.getElementById(id), {duration:val1}); return false;
		break;
		case 'fade':
			$('#' + id).fadeOut(val1);
			//new Effect.Fade(document.getElementById(id), {duration:val1}); return false;
		break;
		case 'appear':
			$('#' + id).show(val1);
			//new Effect.Appear(document.getElementById(id), {duration:val1}); return false;
		break;
	}	
}
// inherited from dojo
function wipes(effect,id,duration,other)
{
	//var duration = duration/1000;
	//alert(duration);
	switch(effect) {
		case 'wipeIn':  $('#' + id).slideDown(duration); //new Effect.SlideDown(document.getElementById(id), {duration:duration}); return false; break;
		case 'wipeOut': $('#' + id).slideUp(duration);   //new Effect.SlideUp(document.getElementById(id), {duration:duration}); return false; break;
		case 'fadeIn':  $('#' + id).fadeIn(duration);    //new Effect.Appear(document.getElementById(id), {duration:duration}); return false; break;
		case 'fadeOut': $('#' + id).fadeOut(duration);   //new Effect.Fade(document.getElementById(id), {duration:duration}); return false; break;
	}
}

//Opacity
/* ------------------------------------ This function sets opacity on an object using JQuery------------------------------------ */
function setJOpacity(opacity, theID) {
	$("#"+theID).fadeTo(200,(opacity*(.01)));
}

function setOpacity(opacity, theID) {
 var object = document.getElementById(theID).style;
 // If it's 100, set it to 99 for Firefox.
 if (navigator.userAgent.indexOf("Firefox") != -1) {
  if (opacity == 100) { opacity = 99.9999; } // This is majorly awkward
 }
try {object.filter  = "alpha(opacity=" + opacity + ")"; } catch(err) { object.opacity = (opacity / 100); }
 
}

// Email Validation
function emailValidate(field){
	var emailID=document.getElementById(field);
	
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(emailID.value)){
	return (true)
	}	
	return (false)
 }

// Create random string
function randomString(mylength,withnumbers) {
	if(withnumbers) {
		var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	} else {
		var chars = "ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	}
	var randomstring = '';
	for (var i = 0; i < mylength; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		randomstring += chars.substring(rnum,rnum+1);
	}
	return randomstring;
}

// Create dynamic XMLHttpRequest object
function fetchObj(){
	var myobj   = randomString(6,0);
	try { myobj = new XMLHttpRequest(); } catch (trymicrosoft) { try { myobj = new ActiveXObject("Msxml2.XMLHTTP"); } catch (othermicrosoft) { try { myobj = new ActiveXObject("Microsoft.XMLHTTP"); } catch (failed) { myobj = null; } } }
	if (myobj   == null) alert("Error creating request object!");
	return myobj;
}

// Currency format
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);
	return (((sign)?'':'-') + num + '.' + cents);
}

// Rounding off a number to specific decimal places
function round_number(number,dec_places) {
	//(c) Copyright 2008, Russell Walker, Netshine Software Limited. www.netshinesoftware.com   
    //Version 2.0. Change log:
    //18/12/08 Fixed bug where digits after decimal point greater than 995
    //29/01/09 Added support for negative numbers (symmetrical rounding) and strip white space
    //12/03/09 Fixed bug where first digit is a 9 and needs to be rounded up
    var new_number = '';
    var i = 0; //Just used in loops
    var sign = ""; //If negative, a minus sign will be prefixed to the result
    number = number.toString(); //We need to operate on and return a string, not a number
    number = number.replace(/^\s+|\s+$/g, ''); //Remove any excess white space

    //Do we have a negative number?
    if (number.charCodeAt(0) == 45) //minus sign
    {
        sign = '-';
        number = number.substr(1).replace(/^\s+|\s+$/g, '');
    }

    dec_places = dec_places * 1; //We need an integer
    dec_point_pos = number.lastIndexOf(".");

    //If there is nothing before the decimal point, prefix with a zero
    if (dec_point_pos == 0) {
        number = "0" + number;
        dec_point_pos = 1;
    }

    //Has an integer been passed in?
    if (dec_point_pos == -1 || dec_point_pos == number.length - 1) {
        if (dec_places > 0) {
            new_number = number + ".";
            for(i=0; i<dec_places; i++) {
                new_number += "0";
            }
            if (new_number == 0) {
                sign = "";
            }
            return sign + new_number;
        }
        else {
            return sign + number;
        }
    }

    //Do we already have the right number of decimal places?
    var existing_places = (number.length - 1) - dec_point_pos;
    if (existing_places == dec_places) {
        return sign + number; //If so, just return the input value
    }

    //Do we already have less than the number of decimal places we want?
    if (existing_places < dec_places) {
        //If so, pad out with zeros
        new_number = number;
        for(i=existing_places; i<dec_places; i++) {
            new_number += "0";
        }
        if (new_number == 0) {
            sign = "";
        }
        return sign + new_number;
    }

    //Work out whether to round up or not
    var end_pos = (dec_point_pos * 1) + dec_places;
    var round_up = false; //Whether or not to round up (add 1 to) the next digit along
    if ((number.charAt(end_pos + 1) * 1) > 4) {
        round_up = true;
    }

    //Record each digit in an array for easier manipulation
    var digit_array = new Array();
    for(i=0; i<=end_pos; i++) {
        digit_array[i] = number.charAt(i);
    }

    //Round up the last digit if required, and continue until no more 9's are found
    for(i=digit_array.length - 1; i>=0; i--) {
        if (digit_array[i] == ".") {
            continue;
        }
        if (round_up) {
            digit_array[i]++;
            if (digit_array[i] < 10) {
                break;
            }
        }
        else {
            break;
        }
    }

    //Reconstruct the string, converting any 10's to 0's (except for first digit which can stay as a 10)
    for (i=0; i<=end_pos; i++) {
        if (digit_array[i] == "." || digit_array[i] < 10 || i == 0) {
            new_number += digit_array[i];
        }
        else {
            new_number += "0";
        }
    }

    //If there are no decimal places, we don't need a decimal point
    if (dec_places == 0) {
        new_number = new_number.replace(".", "");
    }

    if (new_number == 0) {
        sign = "";
    }

    //That should do it!
    return sign + new_number;
}

// Equivalent of php's in_array function
function inArray(needle, haystack) {
    var length = haystack.length;
    for(var i = 0; i < length; i++) {
        if(haystack[i] == needle) return true;
    }
    return false;
}
