//Number.prototype.withCommas = function () { return String(this).replace(/\B(?=(?:\d{3})+(?!\d))/g, ',') }
function SecondDateIsSmaller(firstDate, secondDate) {
	var d1 = new Date(firstDate);
	var d2 = new Date(secondDate);
	return (d2 <= d1) ;
}

//prototype adds the method to all date vars
Date.prototype.ToStr = function () {
	return GetDateString(this);
	//	var e = this.toISOString().split("T")[0].split("-");
	//	return e[1] + "/" + e[2] + "/" + e[0];
}

function GetDateString(d) {
	var curr_date = d.getDate();
	curr_date = curr_date < 10 ? '0' + curr_date : curr_date;
	var curr_month = d.getMonth() + 1; //months start at 0 value, thus, January is represented by 0, February as 1
	curr_month = curr_month < 10 ? '0' + curr_month : curr_month;
	var curr_year = d.getFullYear();
	return curr_month + "/" + curr_date + "/" + curr_year;

	//isoString chokes on cmopatibility mode in ie9
	//	var e = d.toISOString().split("T")[0].split("-");
	//	return e[1] + "/" + e[2] + "/" + e[0];
	//	var a = new Date();
	//	var b = a.toISOString();
	//	var c = b.substring(8, 10) + "/" + b.substring(5, 7) + "/" + b.substring(0, 4); // c is in dd/mm/yyyy format
}

function FlushOutDateString(d) {
	var curr_date = d.getDate();
	curr_date = curr_date < 10 ? '0' + curr_date : curr_date;
	var curr_month = d.getMonth() + 1; //months start at 0 value, thus, January is represented by 0, February as 1
	curr_month = (curr_month < 10) ? '0' + curr_month : curr_month;
	var curr_year = d.getFullYear();
	return curr_month + "/" + curr_date + "/" + curr_year;
}

function IsValidDate(input) {
	var validformat = /^\d{2}\/\d{2}\/\d{4}$/; //Basic check for format validity

	if (!validformat.test(input))
		return false;
	else {
		var monthfield = input.split("/")[0];
		var dayfield = input.split("/")[1];
		var yearfield = input.split("/")[2];
		var dayobj = new Date(yearfield, monthfield - 1, dayfield);

		if ((dayobj.getMonth() + 1 != monthfield) || (dayobj.getDate() != dayfield) || (dayobj.getFullYear() != yearfield))
			return false;
		else
			return true;
	}
}

function addCommas(num) {
	num = num.toString().replace(/\$|\,/g, '');
	num = num.toLocaleString().replace(/\B(?=(?:\d{3})+(?!\d))/g, ',');
	return num;
}
function stripDollarAndCommas(num) {
	num = num.toString().replace(/\$|\,/g, '');
	return num;
}

function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g, '');
	num = '$' + num.toLocaleString().replace(/\B(?=(?:\d{3})+(?!\d))/g, ',');
	return num;
}

function _stripDollarAndCommas(curValue) {
    var str = String(curValue);
    str = str.replace('$', '');
    str = str.replace(/\,/g, '');
    return str;
   }

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);
}

function setfocus()
{
	var myElement;
	
	//we changed this to form[1] instead of form[0]
	//the cusip search 'form' is form[0] because it is the first form on the page
	//all other data entry forms are form[1]
	var myForm = document.forms[1];
	
	// loop through all elements on form
	for (var i = 0; i < myForm.length;i++)
	{  
		myElement = myForm[i];

		if (myElement.tabIndex == "1")
		{
			myElement.focus();
			i=100;
		}
	}
}
function mobilesetfocus() {
    var myElement;

    //these pages do not have the cusip lookup form, so the form is form[0]
    var myForm = document.forms[0];

    // loop through all elements on form
    for (var i = 0; i < myForm.length; i++) {
        myElement = myForm[i];

        if (myElement.tabIndex == "1") {
            myElement.focus();
            i = 100;
        }
    }
}

//see this site for explanation
//http://www.sitepoint.com/article/standards-compliant-world/1
function externalLinks()
{ 
 if (!document.getElementsByTagName)
	return; 

 var anchors = document.getElementsByTagName("a"); 
 
 for (var i=0; i<anchors.length; i++)
 { 
   var anchor = anchors[i]; 
   
   if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") 
     anchor.target = "_blank"; 
 } 
} 
window.onload = externalLinks;


