﻿function dalert(s) {
    //alert( '*' + s);
}

function startsWith(p_str, p_startsWith) {
    if (p_startsWith.length > p_str.length)
        return false;
    return p_str.substr(0, p_startsWith.length) == p_startsWith;
}

function endsWith(p_str, p_endsWith) {
    if (p_endsWith.length > p_str.length)
        return false;
    return p_str.substr(p_str.length - p_endsWith.length, p_endsWith.length) == p_endsWith;
}

function matchesRegularExpression(p_str, p_regex) {
    dalert('matchesRegularExpression( "' + p_str + '", "' + p_regex + '")');
    var re = new RegExp(p_regex);
    var result = re.exec(p_str);
    if (result != null) {
        dalert('matchesRegularExpression:2: passed');
        return true;
    }

    dalert('matchesRegularExpression:3');
    return false;
}

function checkErrorArray(l_Array) {
    if (l_Array.length > 0)
        alert('There were some mistakes in your form:\r\n\r\n'
	         + '    - ' + l_Array.join('\r\n    - ')
	         + '\r\n\r\nPlease correct, and re-submit.')
    return l_Array.length == 0;
}

function getFormElement(p_Button, p_Name) {
    var l_forms = document.forms[0];
    var l_ID = p_Button.id.substr(0, p_Button.id.lastIndexOf('_'));
    var l_element = document.getElementById(l_ID + '_' + p_Name);
    return l_element;
}

function SelectAllInGrid(p_checkbox) {
    //alert('This is the TestFunction');
    var l_bSelected = p_checkbox.checked;
    var i = 0;
    for (i = 0; i < p_checkbox.form.elements.length; ++i)
        if(    (p_checkbox.form.elements[i].type == 'checkbox')
		    && (startsWith( p_checkbox.form.elements[i].name, 'id_')))
            p_checkbox.form.elements[i].checked = l_bSelected;
}

//=============================================================================
// function popupCalendar( p_dateField)
//=============================================================================
function popupCalendar( p_dateField)
{
	window.dateField = p_dateField;
	cal = window.open( '../../Pages/Popups/calendar.html','cal','WIDTH=400px,HEIGHT=400px,directories=no,status=no,scrollbars=yes,resizable=yes,menubar=no');
}

function openLargeWindow(p_fileName, p_windowName, p_height, p_width)
{
    //alert( 'CP1');
    var l_options = "toolbar=no,width="
                  + (p_width == null ? 700 : p_width)
                  + "px,height="
                  + (p_height == null ? 600 : p_height)
                  + "px,directories=no,status=no,scrollbars=yes,resizable=yes,menubar=no";

    //alert( 'CP1a:\r\n\r\n' 
    //     + 'options: ' + l_options + '\r\n'
    //	 + 'p_windowName: ' + p_windowName + '\r\n'
    //     );
    var msgWindow;
    msgWindow = window.open(p_fileName, p_windowName, l_options);
    //alert( 'CP1');
    try {
        msgWindow.focus();
    }
    catch (l_error) {
    }
    //alert( 'CP1');
}

function AllOrNothing( a_Text1, a_Text2, a_Text3)
{
	if(    (a_Text1 != '')
	    || (a_Text2 != '')
	    || (a_Text3 != ''))
	{
		if(    (a_Text1 == '')
		    || (a_Text2 == '')
		    || (a_Text3 == ''))
			return false;
	}
	return true;
}

function ShowErrors( a_msg, l_Errors)
{
  if (l_Errors.length > 0)
  {
    alert( a_msg + '\r\n\r\n'
         + l_Errors.join('\r\n')
         + '\r\n\r\nPlease correct and re-submit');
	return true;
  }
  return false;
}

function Multiply( a_Currency, a_NumberOfPayments)
{
	var l_cents = 0;
	if( a_Currency.indexOf(".") != -1)
	{
		var l_parts = a_Currency.split( '.');
		l_cents = parseInt( l_parts[0]) * 100;
		l_cents += parseInt( l_parts[1]);	
	}
	else
		l_cents = parseInt( a_Currency) * 100;

	return parseInt( a_NumberOfPayments * l_cents);
}

function CentsToDollarsString( a_cents)
{
	if( a_cents == 0)
		return "0.00";
	if( a_cents < 0)
	{
		var l_str = '' + (a_cents - (2 * a_cents));
		var l_part1 = l_str.substr(0, l_str.length - 2);
		var l_part2 = l_str.substr(l_str.length - 2, 2);
		return '-' + l_part1 + '.' + l_part2;
	}
	var l_str = '' + a_cents;
	var l_part1 = l_str.substr(0, l_str.length - 2);
	var l_part2 = l_str.substr(l_str.length - 2, 2);
	return l_part1 + '.' + l_part2;
}

function boolAnd( a_a, a_b)
{ return a_a && a_b; }

function boolGt(a_a, a_b)
{ return a_a > a_b; }

function boolLt(a_a, a_b)
{ return a_a < a_b; }

//function trim( s)
//{
//    if( s == null)
//        return "";
//
//    var l_idx = 0;
//    var l_str = "";
//    for( l_idx = 0; l_idx < s.length; l_idx++)
//        if(    (s.charAt( l_idx) != ' ')
//            && (s.charAt( l_idx) != '\t'))
//            l_str += s.charAt( l_idx);
//
//    return l_str;
//}

String.prototype.trim = function()
{
	return this.replace(/^\s+|\s+$/g,"");
}
