﻿String.prototype.trim = function() {
    return this.replace(/(^\s*)|(\s*$)/g, "");
}

function onUpdating(divToShow) {
    if (divToShow == null) return;
    var updateProgressDiv = document.getElementById(divToShow);
    if (updateProgressDiv == null) throw "The div of update progress is null.";

    Sys.UI.DomElement.setVisible(updateProgressDiv, true);

    var x = parseInt(document.body.offsetWidth / 2) - 80;
    var y = Math.max(document.body.scrollTop, document.documentElement.scrollTop) + parseInt(document.body.offsetHeight / 2) + 50;

    Sys.UI.DomElement.setLocation(updateProgressDiv, x, y);
}

function onUpdated(divToShow) {
    var updateProgressDiv = document.getElementById(divToShow);
    if (updateProgressDiv == null) throw "The div of update progress is null.";
    Sys.UI.DomElement.setVisible(updateProgressDiv, false);
}

function onSilverlightError(sender, args) {
    var appSource = "";
    if (sender != null && sender != 0) {
        appSource = sender.getHost().Source;
    }

    var errorType = args.ErrorType;
    var iErrorCode = args.ErrorCode;

    if (errorType == "ImageError" || errorType == "MediaError") {
        return;
    }
    var errMsg = "Unhandled Error in Silverlight Application " + appSource + "\n";

    errMsg += "Code: " + iErrorCode + "    \n";
    errMsg += "Category: " + errorType + "       \n";
    errMsg += "Message: " + args.ErrorMessage + "     \n";

    if (errorType == "ParserError") {
        errMsg += "File: " + args.xamlFile + "     \n";
        errMsg += "Line: " + args.lineNumber + "     \n";
        errMsg += "Position: " + args.charPosition + "     \n";
    }
    else if (errorType == "RuntimeError") {
        if (args.lineNumber != 0) {
            errMsg += "Line: " + args.lineNumber + "     \n";
            errMsg += "Position: " + args.charPosition + "     \n";
        }
        errMsg += "MethodName: " + args.methodName + "     \n";
    }
    throw new Error(errMsg);
}

function viewHistoryRecord(link, cusip, count) {
    if (undefined != count && count == 0) {
        alert('There is no RWS history on this CUSIP at this time.');
        return true;
    }
    
    var height = 600;
    var width = 850;
    var id = 'popupRwsHist';
    var pageAttribute = 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=' + width + ',height=' + height + ',left=100,top=10';

    var url = '/RWS/History.aspx?cusip='+cusip;
    if (null != link)
        url = link.href;
    
    var win = window.open(url, id, pageAttribute);
    if (win) {
        win.focus();
        return true;
    }
    return false;
}

function viewLiveOffering(link, cusip, count) {
    if (undefined != count && count == 0) {
        alert('There is no Live Offering on this CUSIP at this time.');
        return true;
    }
        
    var height = 600;
    var width = 850;
    var id = 'popupLiveOffering';
    var pageAttribute = 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=' + width + ',height=' + height + ',left=100,top=10';

    var win = window.open(link.href, id, pageAttribute);
	if (win) {
		win.focus();
		return true;
	}
	else
		return false;
}

function viewLiveBw(link, cusip, count) {
    if (undefined != count && count == 0) {
        alert('There is no Live BidWanted on this CUSIP at this time.');
        return true;
    }
        
    var height = 600;
    var width = 850;
    var id = 'popupLiveBw';
    var pageAttribute = 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=' + width + ',height=' + height + ',left=100,top=10';

    var win = window.open(link.href, id, pageAttribute);
	if (win) {
		win.focus();
		return true;
	}
	else
		return false;
}

function showLoading(visible, divId) {
    if (visible) {
        $.openDOMWindow({
            windowSourceID: '#' + divId,
            modal: 1,
            overlayOpacity: 60,
            overlayColor: '#fff',
            windowBGColor: 'Transparent',
            borderSize: 0,
            height: 100,
            width: 160
        });
        $('#DOMWindowOverlay').css('cursor', 'wait');
        $('#DOMWindow').css('cursor', 'wait');
    } else {
        $.closeDOMWindow();
        $('#DOMWindowOverlay').css('cursor', 'auto');
        $('#DOMWindow').css('cursor', 'auto');
    }
}



function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}


function isDate(odtStr) {
    var dtStr = odtStr.replace(/-/g, "/");
    var dtCh= "/";
    var minYear=1800;
    var maxYear=2300;

	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (strYear.length == 2){
		strYear = '20'+strYear;
		year += 2000;
	}
	if (pos1==-1 || pos2==-1){
		//alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		//alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		//alert("Please enter a valid day")
		return false
	}
	if ((strYear.length != 4 && strYear.length != 2) || year==0 || year<minYear || year>maxYear){
		//alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		//alert("Please enter a valid date")
		return false
	}
return true
}

function formatDate(date) {
    var year = '' + date.getFullYear();
    year = year.substr(2,2);
    var month = date.getMonth()+1; // original month will be 0..11
    if (parseInt(month) < 10)
        month = '0' + month;
    var day = date.getDate();
    if (parseInt(day) < 10)
        day = '0' + day;
    
    return month + '/' + day + '/' + year;
}

function get_url_parameter(name){
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec( window.location.href );
    if( results == null )
        return "";
    else
        return results[1];
}
