
/* Javascript functions for Challenger Connect */


/* 	Name:			formatMyDate
	Description:	formats date parameter and writes it out to the document
	Parameters:		dateSource=date to format
					dateFormat=format to return date as
	Example:
					dateFormat(1) = dd/mm/yyyy   = 21/04/2004
					dateFormat(2) = dd MMMM yyyy = 21 March 2004
	
	Created By:		Ian Fairbairn
	Created Date:	21 April 2004
*/
function formatMyDate(dateSource, dateFormat)
{
    if (dateSource) {
    
        if (dateFormat==1) {     
            var d  = dateSource.getDate();
            var day = (d < 10) ? '0' + d : d;
            var m = dateSource.getMonth() + 1;
            var month = (m < 10) ? '0' + m : m;
            var yy = dateSource.getYear();
            var year = (yy < 1000) ? yy + 1900 : yy;
            return (day + "/" + month + "/" + year);
        }else{
            var d  = dateSource.getDate();
            var day = (d < 10) ? '0' + d : d;
            var month = getMonthName(dateSource.getMonth() + 1);
            var yy = dateSource.getYear();
            var year = (yy < 1000) ? yy + 1900 : yy;
            return (day + " " + month + " " + year);
        }

    }else{
    
        return "empty";
    }    
}

/* 	Name:			getMonthName
	Description:	formats a month given as a integer as its long name
	Parameters:		monthNumber = month number to format
	Example:
					getMonthName(1) = "January"
					getMonthName(11) = "November"
					getMonthName(0) = "Unknown"
					
	Created By:		Ian Fairbairn
	Created Date:	21 April 2004
*/
function getMonthName(monthNumber) {

    switch(monthNumber) {
        case 1 :
            return "January";
        case 2 :
            return "February";
        case 3 :
            return "March";
        case 4 :
            return "April";
        case 5 :
            return "May";
        case 6 :
            return "June";
        case 7 :
            return "July";
        case 8 :
            return "August";
        case 9 :
            return "September";
        case 10 :
            return "October";
        case 11 :
            return "November";
        case 12 :
            return "December";
        default :
            return "Unknown";
    }
}


/* 	Name:			popupWindow
	Description:	opens a popup window
	Parameters:		url = url of page to show in new window
					windowHeight = height of popup window
					windowWidth = width of popup window
	Example:
					popupWindow("/intranet/test.html", 500, 500)
					
	Created By:		Ian Fairbairn
	Created Date:	21 April 2004			
*/
function popupWindow(url, windowHeight, windowWidth)
{
    window.open(url,"","height=" + windowHeight + ",width=" + windowWidth + ",titlebar=no,status=no,toolbar=no,menubar=no,location=no");
}


/* 	Name:			formatTable
	Description:	formats and tables within the document held within a div element with id="table"
	Parameters:		divTable = HTML div element
	Example:
					var div = document.getElementById("table")
					if (div) {
						formatTable(div);
					}
	Created By:		Ian Fairbairn
	Created Date:	21 April 2004
	Updated Date:	02 May 2008 : Altered to work in FireFox					
*/
function formatTable(divTable)
{

	if (divTable)
		{
			//var tablesToFormat = divTable.children.tags("table");
			var rowsToFormat;
			for (var i=0; i < divTable.childNodes.length; i++) {
				if(divTable.childNodes[i].tagName=='TABLE') {
				
					divTable.childNodes[i].className = "publicTable"
				
					rowsToFormat = divTable.childNodes[i].rows;
				
					
					for (var j=0; j < rowsToFormat.length; j++) {
						var colMax;
						colMax = rowsToFormat[j].cells.length;
						if (j==0) {
							for (var k=0; k<colMax;k++)
							{
								if (k==colMax-1)
								{
								//	if (j == rowsToFormat.length -1)
								//		rowsToFormat[j].cells[k].className = "publicLastRowTableHeadEndCol";
								//	else
										rowsToFormat[j].cells[k].className = "publicRowTableHeadCellWithNoRightBorder";
									
								}
								else
								{
								//	if (j == rowsToFormat.length -1)
								//		rowsToFormat[j].cells[k].className = "publicLastRowTableHead";
								//	else
										rowsToFormat[j].cells[k].className = "publicRowTableHeadCellWithNoLeftBorder";
								}
							}
								
						}
						else
						if (j % 2) {
							for (var k=0; k<rowsToFormat[j].cells.length;k++)
							{
								if (k==colMax-1)
								{
									if (j == rowsToFormat.length -1)
										rowsToFormat[j].cells[k].className = "publicRowTableFooterCellWithNoLeftBorder";
									else
										rowsToFormat[j].cells[k].className = "publicRowShadedCellWithNoRightBorder";
								}
								else
								{
									if (j == rowsToFormat.length -1)
										rowsToFormat[j].cells[k].className = "publicRowTableFooterCellWithNoRightBorder";
									else
										rowsToFormat[j].cells[k].className = "publicRowShadedCellWithNoLeftBorder";
								}
					
							}
						} else {
							for (var k=0; k<rowsToFormat[j].cells.length;k++)
							{
								if (k==colMax-1)
								{
									if (j == rowsToFormat.length -1)
										rowsToFormat[j].cells[k].className = "publicRowTableFooterCellWithNoLeftBorder";
									else
										rowsToFormat[j].cells[k].className = "publicRowBlankCellWithNoRightBorder";
								}
								else
								{
									if (j == rowsToFormat.length -1)
										rowsToFormat[j].cells[k].className = "publicRowTableFooterCellWithNoRightBorder";
									else
										rowsToFormat[j].cells[k].className = "publicRowBlankCellWithNoLeftBorder";
								}
							}
						}
						
					}	
					
				}
			}
		}
}		


/* 	Name:			getRandomImage
	Description:	returns a random URL of an image from any number of image URL's passed into the function
	Parameters:		imageURL[n]
	Example:
					document.write "<img src='" + getRandomImage("images/image1.gif","images/image2.gif","images/image3.gif") + "'/>"

	Created By:		Ian Fairbairn
	Created Date:	22 April 2004					
*/
function getRandomImage()
{

	return arguments[Math.floor(Math.random()*arguments.length)];

}


/* 	Name:			formatFileSize
	Description:	removes decimal places from a filesize
	Parameters:		strFileSize = number to format
	Example:
					document.write(formatFileSize('<%attributeDocumentSize%>'));

	Created By:		Bernard
	Created Date:	28 Oct 2005					
*/
function formatFileSize(strFileSize) 
{
	var formattedFileSize = parseInt(strFileSize,10);
	if (formattedFileSize < 1024)
		return formattedFileSize + 'B'
	else if (formattedFileSize >= 1024 &&  formattedFileSize <= (1024 * 1024))
		return parseInt(formattedFileSize / 1024, 10) + 'KB'
	else
		return parseInt(formattedFileSize / (1024 * 1024), 10) +'MB'
}

function InStr(strSearch, charSearchFor)
/*
InStr(strSearch, charSearchFor) : Returns the first location a substring (SearchForStr)
                           was found in the string str.  (If the character is <b>not</b>
                           found, -1 is returned.)
                           
Requires use of:
	<a href="http://www.4guysfromrolla.com/webtech/code/Mid.shtml">Mid function</a>
	<a href="http://www.4guysfromrolla.com/webtech/code/Len.shtml">Len function</a>
*/
{
	for (i=0; i < strSearch.length; i++)
	{
		if (charSearchFor == strSearch.substring(i, i+charSearchFor.length))
	    {
			return i;
	    }
	}
	return -1;
}

function GotoOrderLiterature(ctl)
{
	var currentURL = new String(window.location);
	var posInvestorPublic = InStr(currentURL.toLowerCase(), "/publicinvestorservices/");
	var posInvestorSecure = InStr(currentURL.toLowerCase(), "/investorservices/");
	var posAdviserPublic = InStr(currentURL.toLowerCase(), "/publicadviserservices/");
	var posAdviserSecure = InStr(currentURL.toLowerCase(), "/adviserservices/");
	
	var minpos = currentURL.length
	var type = "public"
	
	if (posInvestorPublic < minpos && posInvestorPublic >=0)
	{
		minpos = posInvestorPublic;
		type = "investor";
	}
	
	if (posInvestorSecure < minpos && posInvestorSecure >=0)
	{
		minpos = posInvestorSecure;
		type = "investor";
	}
	
	if (posAdviserPublic < minpos && posAdviserPublic >=0)
	{
		minpos = posAdviserPublic;
		type = "adviser";
	}
	
	if (posAdviserSecure < minpos && posAdviserSecure >=0)
	{
		minpos = posAdviserSecure;
		type = "adviser";
	}
	
	if (type == "investor")
	{
		var httproot = currentURL.substring(0, minpos);
		ctl.href = httproot + '/ChallengerOnline/Content/InvestorServices/PublicOrderLiterature.asp';
		ctl.target = 'ChallengerInvestorOrderLiterature';
		window.open(ctl.href,'ChallengerInvestorOrderLiterature','height=550, width=800, titlebar=no, status=yes, toolbar=no, menubar=no, location=no, resizable=yes, scrollbars=yes');
		return(false);
	}
	else if (type == "adviser")
	{
		var httproot = currentURL.substring(0, minpos);
		ctl.href = httproot + '/Content/AdviserServices/OrderLiterature.asp';
		ctl.target = 'ChallengerAdviserOrderLiterature';
		window.open(ctl.href,'ChallengerAdviserOrderLiterature','height=550, width=800, titlebar=no, status=yes, toolbar=no, menubar=no, location=no, resizable=yes, scrollbars=yes');
		return(false);
	}
	else
	{
		ctl.href = 'PublicOrderLiterature.asp';
		ctl.target = '';
		window.location = ctl.href;
	}
}

function GotoEnquiryForm(ctl)
{
	
	var currentURL = new String(window.location);
	var posInvestorPublic = InStr(currentURL.toLowerCase(), "/publicinvestorservices/");
	var posInvestorSecure = InStr(currentURL.toLowerCase(), "/investorservices/");
	var posAdviserPublic = InStr(currentURL.toLowerCase(), "/publicadviserservices/");
	var posAdviserSecure = InStr(currentURL.toLowerCase(), "/adviserservices/");
	
	var minpos = currentURL.length
	var type = "public"
	
	if (posInvestorPublic < minpos && posInvestorPublic >=0)
	{
		minpos = posInvestorPublic;
		type = "investor";
	}
	
	if (posInvestorSecure < minpos && posInvestorSecure >=0)
	{
		minpos = posInvestorSecure;
		type = "investor";
	}
	
	if (posAdviserPublic < minpos && posAdviserPublic >=0)
	{
		minpos = posAdviserPublic;
		type = "adviser";
	}
	
	if (posAdviserSecure < minpos && posAdviserSecure >=0)
	{
		minpos = posAdviserSecure;
		type = "adviser";
	}
	if (type == "investor")
	{
		var httproot = currentURL.substring(0, minpos);
		ctl.href = httproot + '/Content/InvestorServices/EnquiryForm.asp';
		ctl.target = 'ChallengerInvestorEnquiryForm';
		window.open(ctl.href,'ChallengerInvestorEnquiryForm','height=550, width=800, titlebar=no, status=yes, toolbar=no, menubar=no, location=no, resizable=yes, scrollbars=yes');
		return(false);
	}
	else if (type == "adviser")
	{
		var httproot = currentURL.substring(0, minpos);
		ctl.href = httproot + '/Content/AdviserServices/AdviserEnquiryForm.asp';
		ctl.target = 'ChallengerAdviserEnquiryForm';
		window.open(ctl.href,'ChallengerAdviserEnquiryForm','height=550, width=800, titlebar=no, status=yes, toolbar=no, menubar=no, location=no, resizable=yes, scrollbars=yes');
		return(false);
	}
	else
	{
		ctl.href = 'EnquiryForm.asp';
		ctl.target = '';
		window.location = ctl.href;
	}
}

function GotoComplaintsForm(ctl)
{
	var currentURL = new String(window.location);
	var posInvestorPublic = InStr(currentURL.toLowerCase(), "/publicinvestorservices/");
	var posInvestorSecure = InStr(currentURL.toLowerCase(), "/investorservices/");
	var posAdviserPublic = InStr(currentURL.toLowerCase(), "/publicadviserservices/");
	var posAdviserSecure = InStr(currentURL.toLowerCase(), "/adviserservices/");
	
	var minpos = currentURL.length
	var type = "public"
	
	if (posInvestorPublic < minpos && posInvestorPublic >=0)
	{
		minpos = posInvestorPublic;
		type = "investor";
	}
	
	if (posInvestorSecure < minpos && posInvestorSecure >=0)
	{
		minpos = posInvestorSecure;
		type = "investor";
	}
	
	if (posAdviserPublic < minpos && posAdviserPublic >=0)
	{
		minpos = posAdviserPublic;
		type = "adviser";
	}
	
	if (posAdviserSecure < minpos && posAdviserSecure >=0)
	{
		minpos = posAdviserSecure;
		type = "adviser";
	}
	
	if (type == "investor")
	{
		
		var httproot = currentURL.substring(0, minpos);
		ctl.href = httproot + '/Content/InvestorServices/ComplaintsForm.asp';
		ctl.target = 'ChallengerInvestorComplaintsForm';
		window.open(ctl.href,'ChallengerInvestorComplaintsForm','height=550, width=800, titlebar=no, status=yes, toolbar=no, menubar=no, location=no, resizable=yes, scrollbars=yes');
		return(false);
	}
	else if (type == "adviser")
	{
		
		var httproot = currentURL.substring(0, minpos);
		ctl.href = httproot + '/Content/AdviserServices/ComplaintsForm.asp';
		ctl.target = 'ChallengerAdviserComplaintsForm';
		window.open(ctl.href,'ChallengerAdviserComplaintsForm','height=550, width=800, titlebar=no, status=yes, toolbar=no, menubar=no, location=no, resizable=yes, scrollbars=yes');
		return(false);
	}
	else
	{
		ctl.href = 'ComplaintsForm.asp';
		ctl.target = '';
		window.location = ctl.href;
	}
}

