function getVirtualDirectory()
{
	var strURLPrefix	=	window.document.URL;
	var iStartIndex		=	strURLPrefix.indexOf('//');
		iStartIndex		=	strURLPrefix.indexOf('/', iStartIndex + 2);
	var iEndIndex		=	strURLPrefix.indexOf('/', iStartIndex + 1);
	var strVirtualDirectory		=	strURLPrefix.substr(iStartIndex, (iEndIndex - iStartIndex));
	
	var strToReplace = "/Manager";
	var regExpRemove = new RegExp(strToReplace, '');

	strVirtualDirectory = strVirtualDirectory.replace(regExpRemove, '');
	
	return strVirtualDirectory;
}

function getSiteURLPrefix()
{
	var strURLPrefix	=	window.document.URL;
	var iStartIndex		=	strURLPrefix.indexOf('//');
		iStartIndex		=	strURLPrefix.indexOf('/', iStartIndex + 2);
	var iEndIndex		=	strURLPrefix.indexOf('/', iStartIndex + 1);
	strURLPrefix		=	strURLPrefix.substr(0, iEndIndex);
	
	var strToReplace = "/Manager";
	var regExpRemove = new RegExp(strToReplace, '');

	strURLPrefix = strURLPrefix.replace(regExpRemove, '');

	return strURLPrefix + "/";
}

function getSessionID()
{
	return window.document.getElementsByName("__Session_ID")[0].value;
}

function getCurrentMonth()
{
	return window.document.getElementsByName("__Current_Month")[0].value;
}

function getCurrentYear()
{
	return window.document.getElementsByName("__Current_Year")[0].value;
}

function getCurrentDate()
{	
	return window.document.getElementsByName("__Current_Date")[0].value;
}

function getCurrentTime()
{
	return window.document.getElementsByName("__Current_Time")[0].value;
}

function getCurrentDateTime()
{
	return window.document.getElementsByName("__Current_Date_Time")[0].value;
}

function getUserName()
{
	return window.document.getElementsByName("__USER_NAME")[0].value;	
}

function getUserRole()
{
	return window.document.getElementsByName("__USER_ROLE")[0].value;	
}

function getUserID()
{
	return window.document.getElementsByName("__USER_ID")[0].value;
}

function IsCurrentDateLessThanGivenDate(givenDate)
{
	var	strMonth	=	0;
	var	strDay		=	0;
	var	strYear		=	0;
	
	//Given date is assumed as MM/DD/YYYY format. I.e. soemthing like 05/18/2008
	givenDate	=	getFormattedDate(givenDate, "MM/DD/YYYY");
	strMonth	=	givenDate.substr(0,2);
	strDay		=	givenDate.substr(3,2);
	strYear		=	givenDate.substr(6,4);

	var currentDate	= new Date(getFormattedDate(getCurrentDate(), "MM/DD/YYYY"));
	if (currentDate.getFullYear() < Number(strYear))
	{
//		alert("Date selected can not be greater than current date.");
		return true;
	}
		
	if ((currentDate.getFullYear() == Number(strYear)) && (currentDate.getMonth()+1 < Number(strMonth)))
	{
//		alert("Date selected can not be greater than current date.");
		return true;
	}

	if ((currentDate.getFullYear() == Number(strYear)) && (currentDate.getMonth()+1 == Number(strMonth)) && (currentDate.getDate() < Number(strDay)))
	{
//		alert("Date selected can not be greater than current date.");
		return true;
	}
	
	return false;
}

function IsCurrentDateLessThanOrEqualToGivenDate(givenDate)
{
	var	strMonth	=	0;
	var	strDay		=	0;
	var	strYear		=	0;
	
	//Given date is assumed as MM/DD/YYYY format. I.e. soemthing like 05/18/2008
	givenDate	=	getFormattedDate(givenDate, "MM/DD/YYYY");
	strMonth	=	givenDate.substr(0,2);
	strDay		=	givenDate.substr(3,2);
	strYear		=	givenDate.substr(6,4);

	var currentDate	= new Date(getFormattedDate(getCurrentDate(), "MM/DD/YYYY"));
	if (currentDate.getFullYear() < Number(strYear))
	{
//		alert("Date selected can not be greater than current date.");
		return true;
	}
		
	if ((currentDate.getFullYear() == Number(strYear)) && (currentDate.getMonth()+1 < Number(strMonth)))
	{
//		alert("Date selected can not be greater than current date.");
		return true;
	}

	if ((currentDate.getFullYear() == Number(strYear)) && (currentDate.getMonth()+1 == Number(strMonth)) && (currentDate.getDate() <= Number(strDay)))
	{
//		alert("Date selected can not be greater than current date.");
		return true;
	}
	
	return false;
}

function IsCurrentDateGreaterThanOrEqualToGivenDate(givenDate)
{
	var	strMonth	=	0;
	var	strDay		=	0;
	var	strYear		=	0;
	
	//Given date is assumed as MM/DD/YYYY format. I.e. soemthing like 05/18/2008
	givenDate	=	getFormattedDate(givenDate, "MM/DD/YYYY");
	strMonth	=	givenDate.substr(0,2);
	strDay		=	givenDate.substr(3,2);
	strYear		=	givenDate.substr(6,4);

	var currentDate	= new Date(getFormattedDate(getCurrentDate(), "MM/DD/YYYY"));
	if (currentDate.getFullYear() > Number(strYear))
	{
//		alert("Date selected can not be greater than current date.");
		return true;
	}
		
	if ((currentDate.getFullYear() == Number(strYear)) && (currentDate.getMonth()+1 > Number(strMonth)))
	{
//		alert("Date selected can not be greater than current date.");
		return true;
	}

	if ((currentDate.getFullYear() == Number(strYear)) && (currentDate.getMonth()+1 == Number(strMonth)) && (currentDate.getDate() >= Number(strDay)))
	{
//		alert("Date selected can not be greater than current date.");
		return true;
	}
	
	return false;
}


function IsCurrentDateGreaterThanGivenDate(givenDate)
{
	var	strMonth	=	0;
	var	strDay		=	0;
	var	strYear		=	0;
	
	//Given date is assumed as MM/DD/YYYY format. I.e. soemthing like 05/18/2008
	givenDate	=	getFormattedDate(givenDate, "MM/DD/YYYY");
	strMonth	=	givenDate.substr(0,2);
	strDay		=	givenDate.substr(3,2);
	strYear		=	givenDate.substr(6,4);

	var currentDate	= new Date(getFormattedDate(getCurrentDate(), "MM/DD/YYYY"));
	if (currentDate.getFullYear() > Number(strYear))
	{
//		alert("Date selected can not be greater than current date.");
		return true;
	}
		
	if ((currentDate.getFullYear() == Number(strYear)) && (currentDate.getMonth()+1 > Number(strMonth)))
	{
//		alert("Date selected can not be greater than current date.");
		return true;
	}

	if ((currentDate.getFullYear() == Number(strYear)) && (currentDate.getMonth()+1 == Number(strMonth)) && (currentDate.getDate() > Number(strDay)))
	{
//		alert("Date selected can not be greater than current date.");
		return true;
	}
	
	return false;
}


function trimSpaces(strCtlId)
{
	var ctlField	=	window.document.getElementById(strCtlId);
	if (ctlField == null || ctlField == undefined)
		return "";
	
	var strText		=	ctlField.value;
	if (strText == null || strText == "")
		return "";

	var strTrimmed	=	"";
	for (var index=0; index<strText.length; index++)
	{
		var ch	=	strText.substr(index, 1);	
		if (ch != ' ')
		{
			strTrimmed = strText.substr(index, (strText.length - index));
			break;
		}
	}
	for (var index=strTrimmed.length; index>0; index--)
	{
		var ch	=	strTrimmed.substr(index-1, 1);
		if (ch != ' ')
		{
			strTrimmed = strTrimmed.substr(0, index);
			break;
		}
	}

	ctlField.value	=	strTrimmed;
	
	return strTrimmed;
}

function popUpCalender(strCtrlID)
{	
	popUpCalender(strCtrlID, "", "");
}

function popUpCalender(strCtrlID, strStartYear, strEndYear)
{	
	window.document.getElementById("__ACTION_FILED").value = strCtrlID;

	var strMonth = null;
	var strDay = null;
	var strYear = null;
	var dtSelected	=	window.document.getElementById(strCtrlID).value
	if (dtSelected != undefined && dtSelected != "")
	{
		var arrElements	=	dtSelected.split("/");
		strMonth	=	arrElements[0];	
		strDay		=	arrElements[1];	
		strYear		=	arrElements[2];	
	}

	var strURLPrefix	=	getSiteURLPrefix();

	if(strMonth == null && strYear == null)
		var status = window.open(strURLPrefix + "BusinessPages/Common/Calendar.aspx?strStartYear="+strStartYear+"&strEndYear="+strEndYear,"DMVCalendar","location=0,height=245,width=270,top=350,left=400,toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no")
	else
		var status = window.open(strURLPrefix + "BusinessPages/Common/Calendar.aspx?strMonth="+strMonth+"&strDay="+strDay+"&strYear="+strYear+"&strStartYear="+strStartYear+"&strEndYear="+strEndYear,"DMVCalendar","location=0,height=245,width=270,top=350,left=400,toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no")

	status.focus();
}

function getDatefromCalender(strDate)
{		
	var strCtrlID = window.opener.document.getElementById("__ACTION_FILED").value;	
	if(window.opener.callbackforCalender(strDate))
	{	
		window.opener.document.getElementById(strCtrlID).value = getFormattedDate(strDate, "MM/DD/YYYY");
		window.close();	
	}
	else
	{
		window.focus();
	}
}

function getFormattedDate(strDate, strFormat)
{
	if (strFormat == null || strFormat == undefined)
		return strDate;
	
	var arrElements	=	strDate.split("/");
	var iMonth		=	Number(arrElements[0]);
	var iDay		=	Number(arrElements[1]);
	var iYear		=	Number(arrElements[2]);
	
	if (strFormat == "MM/DD/YYYY")
	{
		strDate	=	"";
		if (iMonth < 10)
			strDate	+=	"0" + iMonth;
		else
			strDate	+=	iMonth;

		if (iDay < 10)
			strDate	+=	"/" + "0" + iDay;
		else
			strDate	+=	"/" + iDay;

		strDate	+=	"/" + iYear;
	}
	
	return strDate;	
}


function restrictToInt(ctl)
{	
    if(event.keyCode<48 || event.keyCode>57)
    {                       
        event.cancelBubble = true;
        event.returnValue = false;
    } 
}

function callBackFromNavigatorPrevious(strURL)
{
	window.document.location.href=getVirtualDirectory() + "/" + strURL;
}

function allValidChars(email) {
	var parsed = true;
	var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
	for (var i=0; i < email.length; i++)
	{
		var letter = email.charAt(i).toLowerCase();
		if (validchars.indexOf(letter) != -1)
			continue;
		parsed = false;
		break;
	}
	return parsed;
}

function validEmail1(emailaddr)
{
    if(!allValidChars(emailaddr))
	{
   		alert('Please enter a valid email id.'); 
// 		alert('You must enter Alphanumeric characters for the E-Mail Address - @ or periods allowed.'); 
   		return false; 
 	} 

	if (emailaddr.indexOf("@") < 0)
	{
   		alert('Please enter a valid email id.'); 
//		alert('You must enter at least one @ in the E-Mail Address.');
		return false; 	
	}

	if (emailaddr.indexOf("@") != emailaddr.lastIndexOf("@"))
	{
   		alert('Please enter a valid email id.'); 
//		alert('You must enter only one @ in the E-Mail Address.');
		return false; 	
	}
	
	if (emailaddr.indexOf("@") < 1)
	{
   		alert('Please enter a valid email id.'); 
//		alert('You must enter at least one character before @ in the E-Mail Address.');
		return false; 	
	}

	if (emailaddr.indexOf(".") < 1)
	{
   		alert('Please enter a valid email id.'); 
//		alert('You must enter at least one character before period in the E-Mail Address.');
		return false; 	
	}

	var iAtIndex	=	emailaddr.indexOf("@");
	if (emailaddr.substr((iAtIndex-1),1) == ".")
	{
   		alert('Please enter a valid email id.'); 
//		alert('Period just before @ in the E-Mail Address is not allowed.');
		return false; 	
	}

	if (emailaddr.lastIndexOf(".") < emailaddr.indexOf("@"))
	{
   		alert('Please enter a valid email id.'); 
//		alert('You must enter at least one period after @ in the E-Mail Address.');
		return false; 	
	}

	if (emailaddr.substr((iAtIndex+1),1) == ".")
	{
   		alert('Please enter a valid email id.'); 
//		alert('Period next after @ in the E-Mail Address is not allowed.');
		return false; 	
	}

	if (emailaddr.lastIndexOf(".") == emailaddr.length-1)
	{
   		alert('Please enter a valid email id.'); 
//		alert('Email can not end with period.');
		return false; 	
	}

	if (emailaddr.lastIndexOf(".") > (emailaddr.length-3))
	{
   		alert('Please enter a valid email id.'); 
//		alert('You must enter at least two characters after period.');
		return false; 	
	}

	return true;
}

function getSelectedDropdownListText(ctrl)
{	
	for(var index=0; index<ctrl.options.length; index++)
	{
		if(ctrl.options[index].value == ctrl.value)
		{			
			return ctrl.options[index].text;
		}
	}
	
	return "";
}

function selectDropdownListValue(ctrl, valueToBeSelected)
{	
	for(var index=0; index<ctrl.options.length; index++)
	{
		if(ctrl.options[index].value == valueToBeSelected)
		{			
			ctrl.options[index].selected = true;			
		}
	}
}

function callBackProgressTab()
{
/*	var userResponse	=	confirm("All your changes done in this transaction will be cancelled. Would you like to continue?");
	if (userResponse)
	{
		//Go ahead and clear the session then submit the page
		window.document.getElementsByName("__ACTION_PERFORMED")[0].value	=	"TabChange";
		document.forms[0].submit();	
	}
	else
	{
		event.cancelBubble = true;
		event.returnValue = false;
	}*/
}

// List Grid
function __doSorting(eventTarget, eventArgument) {
var theForm = document.forms[0];
    if (!theForm.onsubmit || (document.form1.onsubmit() != false)) {
        theForm.__ACTION_PERFORMED.value = eventArgument;
        theForm.submit();
    }
}

function __viewRecord(eventArgument)
{
	var theForm = document.forms[0];
    if (!theForm.onsubmit || (document.form1.onsubmit() != false)) {
        theForm.__ACTION_PERFORMED.value = 'View$'+eventArgument;
        theForm.submit();        
    } 
}

function __sortRecord(eventTarget,eventArgument)
{	
	 var strPreValue = window.document.getElementById("__SORT_COLUMN").value;
	 window.document.getElementById("__SORT_COLUMN").value = eventArgument;
     displayGrid("Sorting",strPreValue);
}

function __deleteRecord(eventArgument)
{
    var theForm = document.forms[0];
    if (!theForm.onsubmit || (document.form1.onsubmit() != false)) {
        theForm.__ACTION_PERFORMED.value = 'Delete$'+eventArgument;
        theForm.submit();
    }
} 

function __unlockRecord()
{
    var theForm = document.forms[0]; 
    if (!theForm.onsubmit || (document.form1.onsubmit() != false)) {
        theForm.__ACTION_UNLOCKRECORD.value = 'Assign';
        theForm.submit();
    }
} 

function replaceSpeciaclColumnValues(strColumnName, iRowCount)
{
	for (var index=0; index<=parseInt(iRowCount); index++)
	{
		var ctlSpecialColumn	=	document.getElementById(strColumnName + "_" + index);
		if (ctlSpecialColumn != null && ctlSpecialColumn != undefined)
		{
			var strManupulatedValue	=	getColumnValueForGivenSpecialColumn(ctlSpecialColumn, strColumnName, index);
			ctlSpecialColumn.innerHTML	=	strManupulatedValue;
		}
	}
}

function getColumnValueForGivenSpecialColumn(ctlSpecialColumn, strColumnName, rowID)
{
	var strSpecialColumnValue	=	ctlSpecialColumn.innerText;
	var ctlInstrumentID	=	document.getElementById("Instrument_ID" + "_" + rowID);
	if (ctlInstrumentID == null || ctlInstrumentID == undefined)
		return "";

	var strInstrumentID	=	ctlInstrumentID.value;	

	if (strColumnName == "Report_Type")
	{
		if (strSpecialColumnValue == "BUY")
		{
			strSpecialColumnValue	=	"<a href=\"javascript:openTalkingPointsBuyReport('" + strInstrumentID + "')\">Buy Report</a>";
		}
		else if (strSpecialColumnValue == "SELL")
		{
			strSpecialColumnValue	=	"<a href=\"javascript:openTalkingPointsSellReport('" + strInstrumentID + "')\">Sell Report</a>";
		}
	}
	else if (strColumnName == "Draft")
	{
		if (strSpecialColumnValue == "1")
		{
			strSpecialColumnValue	=	"<INPUT type=checkbox checked disabled></INPUT>";
		}
		else if (strSpecialColumnValue == "0")
		{
			strSpecialColumnValue	=	"<INPUT type=checkbox disabled></INPUT>";
		}
	}


	return strSpecialColumnValue;
}

function closeWindow()
{	
	window.close();
}

function disableControls()
{
	var theForm = window.document.forms[0];
	
	var ctlToBeDisabled	=	theForm.document.getElementById("btnNewThesis");
	if (ctlToBeDisabled != null && ctlToBeDisabled != undefined)
		ctlToBeDisabled.disabled	=	true;			
}

function listCallback(strResponse)
{
	var theForm = window.document.forms[0];
	var ctlToBeDisabled	=	theForm.document.getElementById("divListGrid");
	if(strResponse.value == null)
	{		
		ctlToBeDisabled.innerHTML = "";		
		theForm.document.getElementById("tdError").innerText = "No Records Found";		
	}
	else
	{	
		theForm.document.getElementById("tdError").innerText = "";	
		ctlToBeDisabled.innerHTML	=	strResponse.value;		
	}
}

function displayGrid(eventTarget, Command)
{	
	var intTotalPages = null;
	var intRecordsPerPage = null;
	var strListFilter = null;
	var intPageNumber = null;
	var strSortColumn = null;
	var strSortOrder = null;
	var ctrl = window.document.getElementById("TotalPages");
	
	if(ctrl != undefined && ctrl != null)
	{		
		intTotalPages = window.document.getElementById("TotalPages").innerText;
	}
	
	ctrl = window.document.getElementById("__PAGE_NUMBER");
	if(ctrl != undefined && ctrl != null)
	{
		intPageNumber = window.document.getElementById("__PAGE_NUMBER").value;
	}
	
	ctrl = window.document.getElementById("__SORT_COLUMN");
	if(ctrl != undefined && ctrl != null)
	{
		strSortColumn = window.document.getElementById("__SORT_COLUMN").value;
	}
	
	ctrl = window.document.getElementById("__SORT_ORDER");
	if(ctrl != undefined && ctrl != null)
	{
		strSortOrder = window.document.getElementById("__SORT_ORDER").value;
	}	
	
	ctrl = window.document.getElementById("ddlPageNo");
	if(ctrl != undefined && ctrl != null)
	{
		intRecordsPerPage = window.document.getElementById("ddlPageNo").value;
	}		

	ctrl = window.document.getElementById("ddlStatus");
	if(ctrl != undefined && ctrl != null)
	{
		strListFilter = window.document.getElementById("ddlStatus").value;
	}	
		
	displayGridPage(eventTarget, Command, intPageNumber, intTotalPages, strSortColumn, strSortOrder, intRecordsPerPage, strListFilter);
}

function getScreenWidth(strTableName)
{
	var iWidth	=	window.screen.width - 65;
	window.document.getElementById(strTableName).style.width = iWidth;
}

function setToUpperCase(ctl)
{
	ctl.value = ctl.value.toUpperCase();
}

function setToLowerCase(ctl)
{
	ctl.value = ctl.value.toLowerCase();
}

function printReceipt() 
{
    var Detail = open(getSiteURLPrefix() + "BusinessPages/Common/GeneralPrint.aspx","name","width=1024,height=700,status=no,resize=no,scrollbars=yes,toolbar=no,menubar=no");
    if (!document.all && window.focus) Detail.focus();
}


function y2k(number) { return (number < 1000) ? number + 1900 : number; }

function daysElapsed(date1,date2) 
{	
	date1	=	new Date(date1);
	date2	=	new Date(date2);

    var difference =	Date.UTC(y2k(date1.getYear()),date1.getMonth(),date1.getDate(),0,0,0) - Date.UTC(y2k(date2.getYear()),date2.getMonth(),date2.getDate(),0,0,0);
    return difference/1000/60/60/24;
}

