// Globals
var whPopup = ''; // this allows us to 'find' an existing instance

function Left(strString, intLen)
{
	var strNew = new String(strString);
	return strNew.substr(0,intLen);
}

function Right(strString, intLen)
{
	var strNew = new String(strString);
	return strNew.substr(strNew.length-intLen)
}

function Mid(strString,start, intLen)
{ 
	//Caution: start is a zero based index!
	var strNew = new String(strString);
	return strNew.substr(start, intLen)
}

function fncTrim(strString) 
{

	var strValue = new String(strString);
	while (strValue.substring(0,1) == ' ') strValue = strValue.substring(1);
	while (strValue.substring(strValue.length-1,strValue.length) == ' ') strValue = strValue.substring(0,strValue.length-1);
	return strValue;
}

function fncGo(lngCurrentPage, cboCombo)
{
	var lngPageChoisie = cboCombo.selectedIndex + 1;
	var strPage = cboCombo.options[lngPageChoisie-1].value + '?P=' + lngPageChoisie ;
        if (lngCurrentPage != lngPageChoisie)
	        {window.location.href = strPage;}
}

function fncGotoPage(strPage)
{
	var frmSelectView = document.getElementById('frmSelectView')
	frmSelectView.reset();
	if (isNaN(strPage))
	{
		strPage = 1;
	}
	frmSelectView.txtGoto.value = parseFloat(strPage);
	frmSelectView.JSSubmit.value = "Y";
	frmSelectView.submit();
}

function fncRedirect(strPage){
	window.location.href = strPage;
}

function fncConfirmUpdate()
{
	if (window.confirm('This procedure will:\n1. Send email to all unfunded members. \n2. Update all Pending requests to Unfunded.\n3. Copy each fund\'s current balance to its Opening Balance field.\n\nProceed?'))
	{
		return true;
	}
	else
	{
		return false;
	}
}

function fncSubmitPledge()
{
	var lngOption;
	lngOption = document.forms[0].cboPledge.options[document.forms[0].cboPledge.selectedIndex].value;

	// Set hidden fileds then submit
	switch (lngOption) 
	{ 
	   case '2' : 
	      document.forms[0].item_name.value = 'Modest Needs - $2 Monthly Pledge';
	      document.forms[0].a3.value = '2.00';
	      break; 
	   case '5' : 
	      document.forms[0].item_name.value = 'Modest Needs - $5 Monthly Pledge';
	      document.forms[0].a3.value = '5.00';
	      break; 
	   case '10' : 
	      document.forms[0].item_name.value = 'Modest Needs - $10 Monthly Pledge';
	      document.forms[0].a3.value = '10.00';
	      break; 
	   case '25' : 
	      document.forms[0].item_name.value = 'Modest Needs - $25 Monthly Pledge';
	      document.forms[0].a3.value = '25.00';
	      break; 
	   case '50' : 
	      document.forms[0].item_name.value = 'Modest Needs - $50 Monthly Pledge';
	      document.forms[0].a3.value = '50.00';
	      break; 
	   case '100' : 
	      document.forms[0].item_name.value = 'Modest Needs - $100 Monthly Pledge';
	      document.forms[0].a3.value = '100.00';
	      break; 
	   case '250' : 
	      document.forms[0].item_name.value = 'Modest Needs - $250 Monthly Pledge';
	      document.forms[0].a3.value = '250.00';
	      break; 
	   case '500' : 
	      document.forms[0].item_name.value = 'Modest Needs - $500 Monthly Pledge';
	      document.forms[0].a3.value = '500.00';
	      break; 
	   default : 
	      break; 
	} 
	document.forms[0].submit();
}


function fncShowComment(lngId, strURL)
{
	var strOptions = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=400,height=300,top=100,left=100";
	strURL = strURL + '?id=' + lngId;
	whPopup = window.open(strURL,null,strOptions);
	whPopup.focus(); // In case existing window was reused 
}


function fncShowReview(lngRequestId)
{

	var strOptions = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=400,height=300,top=100,left=100";
	var strURL = 'showreview.asp?id=' + lngRequestId;
	whPopup = window.open(strURL,null,strOptions);
	whPopup.focus(); // In case existing window was reused 
}

function fncShowPointsHistory(lngRequestId)
{
	var strOptions = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=400,height=300,top=100,left=100";
	var strURL = '/features/ledger/showpointshistory.asp?id=' + lngRequestId;
	whPopup = window.open(strURL,null,strOptions);
	whPopup.focus(); // In case existing window was reused 
}

function fncShowImage(strImageURL)
{

	var strOptions = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=400,height=300,top=100,left=100";
	var strURL = '/features/testimonials/showimage.asp?image=' + strImageURL;
	whPopup = window.open(strURL,null,strOptions);
	whPopup.focus(); // In case existing window was reused 
}


function fncShowUploadedDoc(strImageURL)
{

	var strOptions = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=400,height=300,top=100,left=100";
	whPopup = window.open(strImageURL,null,strOptions);
	whPopup.focus(); // In case existing window was reused 
}

// -----------------------------------------------------------------
// Affiche une image dans un popup aux dimensions exactes de l'image
// 
// strURL: adresse de l'image
// -----------------------------------------------------------------

function fncWinPop(strURL,intWidth,intHeight)
{
	var strOptions = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=" + intWidth +",height=" + intHeight;
	whPopup = window.open(strURL,null,strOptions);
	whPopup.focus(); // In case existing window was reused 
}

function fncWinPopWithScroll(strURL,intWidth,intHeight)
{
	var strOptions = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=" + intWidth +",height=" + intHeight;
	whPopup = window.open(strURL,null,strOptions);
	whPopup.focus(); // In case existing window was reused 
}

function fncResetLedgerForm(strMode)
{
	var dtmNow = new Date();
	var frmForm = document.getElementById("frmSelectView")

	// Here, Month number and index correspond 
	frmForm.cboMonths.selectedIndex = dtmNow.getMonth();
	
	// Here we substract 2002 (first year ever) to generate the correct index (ex: 2002 is index 0)
	frmForm.cboYears.selectedIndex = (dtmNow.getFullYear()-2002);			
	
	if (strMode == "C")
	{
		frmForm.cboMode.selectedIndex = 0;
		frmForm.cboFrequency.selectedIndex = 0;		
		if (frmForm.cboUserId)
			{frmForm.cboUserId.selectedIndex = 0;}

	}

	if (strMode == "R")
	{
		if (frmForm.cboStatus)
			frmForm.cboStatus.selectedIndex = 0;
	
		if (frmForm.chkOnlyMine)
			{frmForm.chkOnlyMine.checked = false;}
			
		if (frmForm.txtSpecificReqNo)
			frmForm.txtSpecificReqNo.value = "";			
			
		if (frmForm.txtKeywords)
			frmForm.txtKeywords.value = "";
			
		if (frmForm.txtRangeStart)
			frmForm.txtRangeStart.value = "0";
			
		if (frmForm.txtRangeEnd)
			frmForm.txtRangeEnd.value = "999";
			
		if (frmForm.txtUserId)
			{frmForm.txtUserId.value = "";}
			
		if (frmForm.chkSortRecomm)
			{frmForm.chkSortRecomm.checked = false;}
			
		if (frmForm.cboUserId)
			{frmForm.cboUserId.selectedIndex = 0;}
		
	}
}



function fncNow()
{
	var strMonth
	var strYear
	var strDay
	var strHour
	var strMinute
	var strSecond
	
	dtmDate = new Date();
	strDay = new String("00" + dtmDate.getDate());
	strMonth = new String("00" + (dtmDate.getMonth()+1));
	strYear = dtmDate.getFullYear();
	strHour =  new String("00" + dtmDate.getHours());
	strMinute = new String("00" + dtmDate.getMinutes());
	strSecond = new String("00" + dtmDate.getSeconds());

	strMonth = fncRight(strMonth,2)
	strDay = fncRight(strDay,2)
	strHour = fncRight(strHour,2)
	strMinute = fncRight(strMinute,2)
	
	return  strYear + "-" + strMonth + "-" + strDay 
	
}


function fncRight(strString, lngLen)
{
	return strString.substring(strString.length-lngLen)
}

function fncUserName()
{
	var strMonth
	var strYear
	var strDay
	var strHour
	var strMinute
	var strSecond
	var today = new Date();
	
	dtmDate = new Date();
	strDay = new String("00" + dtmDate.getDate());
	strMonth = new String("00" + (dtmDate.getMonth()+1));
	strYear = dtmDate.getFullYear();
	strHour =  new String("00" + dtmDate.getHours());
	strMinute = new String("00" + dtmDate.getMinutes());
	strSecond = new String("00" + dtmDate.getSeconds());

	strMonth = fncRight(strMonth,2)
	strDay = fncRight(strDay,2)
	strHour = fncRight(strHour,2)
	strMinute = fncRight(strMinute,2)
	strSecond = fncRight(strSecond,2)

	document.getElementById('frmRegistration').txtAlias.value = "Guest" + getJulian(today.getDate(),today.getMonth()+1,today.getYear) + strHour + strMinute + strSecond
}


function makeArray()
{
    this[0] = makeArray.arguments.length;
    for (i = 0; i<makeArray.arguments.length; i++)
        this[i+1] = makeArray.arguments[i];
}

var accumulate    = new makeArray(  0, 31, 59, 90,120,151,181,212,243,273,304,334);
var accumulateLY  = new makeArray(  0, 31, 60, 91,121,152,182,213,244,274,305,335);

function LeapYear(year) {
    if ((year/4)   != Math.floor(year/4))   return false;
    if ((year/100) != Math.floor(year/100)) return true;
    if ((year/400) != Math.floor(year/400)) return false;
    return true;
}

function getJulian(day,month,year) {
    if (LeapYear(year))
        return (day + accumulateLY[month]);
    else
        return (day + accumulate[month]);
}

function fncShowLinkCode(objImage, strSize)
{
	var strImgSource = objImage.src;
	var strSizeString;
	var strCommon1;
	var strCommon2;
	
	strCommon1 = '<a href="http://modestneeds.org/"><img src="';
	strCommon2 = ' alt="Visit Modest Needs"></a>'


	switch (strSize) 
	{ 
	   case 'B' : 
		strSizeString = '" width="468" height="60"';
		break; 
	   case 'S' : 
		strSizeString = '" width="234" height="60"';
	      break; 
	   case 'V' : 
		strSizeString = '" width="120" height="240"';
	      break; 
	   default : 
	      break; 
	} 
	document.getElementById	("txtCode").value = strCommon1 + strImgSource + strSizeString + strCommon2
	return false;

}



function fncShowDefinition(lngStatusID) {
	
	var strText;

	switch (lngStatusID) 
	{ 

		case 1 :
			strText = "Pending: the request currently is under review; it may have been read by one or more readers, but no action has been taken on that request as yet."
			break;
		
		case 2 :
			strText = "Funded: Modest Needs donors have funded this application, and we currently are processing payment."
			break;
		
		case 3 :
			strText = "Closed: the request was reviewed and closed because it did not fall within our funding guidelines. See \'How does Modest Needs decide who gets the funds that are available?\' in the Requesting FAQ for details."
			break;
		
		case 4 :
			strText = "Unfunded: the request could not be funded due to lack of funds. After 30 days, a Pending request will automatically be converted to Unfunded."
			break;
			
		case 5 :
			strText = ""
			break;
			
		case 6 :
			strText = "Prequalified: The authenticity of this request has been verified by Modest Needs."
			break;
			
		case 7 :
			strText = "Withdrawn: the person did not respond to Modest Needs' offer of assistance."
			break;
			
		case 8 :
			strText = "None: Not presecrened yet."
			break;

		case 9 :
			strText = "Adopted: A Modest Needs member has decided to personally fund this request."
			break;
		
		case 10 :
			strText = "Not Processed: The request has not been processed."
			break;

		case 11 :
			strText = "Paid: Modest Needs has remitted payment for the cost of this application."
			break;

		default : 
		break; 
	} 
	
	alert(strText);

}


function fncDeleteContribution(objForm,lngID) 
{
	objForm.js_submit.value = "fncDeleteContribution";
	objForm.txt_INDCB_ID.value = lngID;
	objForm.txtMode.value = "del";
	objForm.submit();
	return true;
}


function fncDeletePoint(objForm,lngID) 
{
	objForm.js_submit.value = "fncDeletePoint";
	objForm.txt_POINT_ID.value = lngID;
	objForm.txtMode.value = "del";
	objForm.submit();
	return true;
}


function fncDeletePointAllocationFromRequest(objForm,lngID) 
{
	objForm.js_submit.value = "fncDeletePointAllocationFromRequest";
	objForm.txt_REQUEST_ID.value = lngID;
	objForm.txtMode.value = "del";
	objForm.submit();
	return true;
}


function fncMyAlert(strString)
{

	var strOptions = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=300,height=200,top=100,left=100";
	var strURL = '/includes/myalert.asp?msg=' + strString;
	var whPopup = window.open(strURL,null,strOptions);
	whPopup.focus(); // In case existing window was reused 
}


function fncSendEmail(strUser,strDomain,strSubject)
{
	var strOptions = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=1,height=1,top=10,left=10";
	var whPopup = window.open('',null,strOptions);
	var whMain = whPopup.opener;
	whMain.focus();

	if (strSubject != '')
	{
		strSubject = '?subject=' + strSubject
	}
	
	whPopup.location.href = 'mail' + 'to:' + strUser + '@' + strDomain + strSubject;
	whPopup.close();
}


function fncUpdateExpTotal(objForm)
{
	var dbl_EXP_AMT_PROGR = parseFloat(objForm.txt_EXP_AMT_PROGR.value);
	var dbl_EXP_AMT_MANGM = parseFloat(objForm.txt_EXP_AMT_MANGM.value);
	var dbl_EXP_AMT_FUNDR = parseFloat(objForm.txt_EXP_AMT_FUNDR.value);

	if (isNaN(dbl_EXP_AMT_PROGR))
		dbl_EXP_AMT_PROGR = 0;

	if (isNaN(dbl_EXP_AMT_MANGM))
		dbl_EXP_AMT_MANGM = 0;

	if (isNaN(dbl_EXP_AMT_FUNDR))
		dbl_EXP_AMT_FUNDR = 0;

	var dblTotal = parseFloat(dbl_EXP_AMT_PROGR + dbl_EXP_AMT_MANGM + dbl_EXP_AMT_FUNDR);
	objForm.txt_TotalExpAmount.value = dblTotal

}


function subPostSwitch(intVal)
{
	var frmSelectView = document.getElementById('frmSelectView');
	frmSelectView.action = "index.asp?Staff=" + intVal
	frmSelectView.submit();
}



function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name)
{
	createCookie(name,"",-1);
}

function fncManageDonationSection(intVal)
{
	var objCreditCardSection = document.getElementById('sectionCreditCard');
	var objEcheckSection = document.getElementById('sectionECheck');

	switch (intVal) 
	{ 
		case 0 :
			objCreditCardSection.style.display = 'none';
			objEcheckSection.style.display = 'none';			
			break;
		
		case 1 :
			objCreditCardSection.style.display = '';
			objEcheckSection.style.display = 'none';			
			break;
		
		case 2 :
			objCreditCardSection.style.display = 'none';
			objEcheckSection.style.display = '';			
			break;		

		default : 
		break; 
	} 

	return null;
}


function fncManageDonationSection_byMode(strMode)
{
	var objCreditCardSection = document.getElementById('sectionCreditCard');
	var objEcheckSection = document.getElementById('sectionECheck');

	switch (strMode) 
	{ 
		case "-1" :
			objCreditCardSection.style.display = 'none';
			objEcheckSection.style.display = 'none';			
			break;
		
		case "CC" :
			objCreditCardSection.style.display = '';
			objEcheckSection.style.display = 'none';			
			break;
		
		case "ECHECK" :
			objCreditCardSection.style.display = 'none';
			objEcheckSection.style.display = '';			
			break;		

		default : 
		break; 
	} 

	return null;
}

	
	

function fncOpenSecWin(strTopic, strDisplayMode)
{
	return fncOpenSecWin2(strTopic,strDisplayMode,"")
		
}


function fncOpenSecWin2(strTopic, strDisplayMode, strParam)
{
	var intWinLeft = (screen.width/2) - 200;
	var intWinTop =  (screen.height/2) - 200;

	var strOptions = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=400,height=400,top=" + intWinTop + ",left=" + intWinLeft;
	var strURL = "/includes/secondaryWindowManager.asp?d=" + strDisplayMode + strParam + "&t=" + strTopic;	
	

	if (!whPopup.closed && whPopup != '')
	{
		whPopup.location.href = strURL;
		whPopup.focus();
	}
	else
	{
		whPopup = window.open(strURL,null,strOptions);
	}
	return false;
}


function fncNavigateClose(strURL)
{
	window.opener.location.href = strURL;
	self.close();
}


function fncSubmitPostBack(actionType){
	var oWebform = document.getElementById("webform")
	var oActionType = document.getElementById("ActionType")
		
	if (oWebform && oActionType){
		oActionType.value = actionType
		oWebform.submit();
	}//end if
}//end function

function fncSubmitPostBack2(formID, actionTypeID, actionType){
	var oWebform = document.getElementById(formID)
	var oActionType = document.getElementById(actionTypeID)
		
	if (oWebform && oActionType){
		oActionType.value = actionType
		oWebform.submit();
	}//end if
}//end function

function fncShowProgressAlert(p_msg,p_options){
        var p_width = p_options.width || "400";
        var p_height = p_options.height || "100";
        
        var p_divMNAlert_Msg = document.getElementById("divMNAlert_Msg");
        if(p_divMNAlert_Msg){
            p_divMNAlert_Msg.innerHTML = "<center>" + p_msg + "</center>";
			tb_show('','#TB_inline?height=' + p_height + '&width=' + p_width + '&inlineId=divMNAlert&modal=true',null);
		}//end if
}//end show




function fncformatCurrency(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 fncRoundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}

function fncToNumeric(num){
    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num)){
        num = "0";
    }
    return num;
}//end fncToNumeric