﻿// JScript File
//this function is called to make sure the start date is always greater than today.
function showModalPopupViaClient(objModelPopUpNames) {
    
    var modalPopupBehavior = $find(objModelPopUpName);
    modalPopupBehavior.show();
}

function hideModalPopupViaClient(objModelPopUpName) {
    
    var modalPopupBehavior = $find(objModelPopUpName);
    modalPopupBehavior.hide();
    return false;
}

function checkDate(sender,args){
    if (sender._selectedDate <= new Date()) 
    {
        alert("Tommorow is the earliest allowable Start Date.");
        sender._textbox.set_Value(sender._textbox.value);
        
        var myDate = new Date();
        myDate.setDate(myDate.getDate()+ 1);
        
        sender._selectedDate = myDate;
        
        // set the date back to the current date + 1
        sender._textbox.set_Value(sender._selectedDate.format(sender._format))
    }
}

//this function is currently called from scheduled requests and request_to schedule web forms for expanding and collapsing form questions
function expandcollapse(obj,row)
{
    var div = document.getElementById(obj);
    var img = document.getElementById('img' + obj);
    
    if (div.style.display == "none")
    {
        div.style.display = "block";
        if (row == 'alt')
        {
            img.src = "Images/minus.gif";
        }
        else
        {
            img.src = "Images/minus.gif";
        }
        img.alt = "Collapse";
    }
    else
    {
        div.style.display = "none";
        if (row == 'alt')
        {
            img.src = "Images/plus.gif";
        }
        else
        {
            img.src = "Images/plus.gif";
        }
        img.alt = "Expand to show Cart Items";
    }
}

function SetCheckBoxState(objControl, state)
{
    var chkBoxCount= objControl.getElementsByTagName("INPUT");
    
    for(var i=0;i<chkBoxCount.length;i++) 
    {
        chkBoxCount[i].checked = state.checked;
    }
}

function SetCheckBoxAllState(objControl, AllCheckBoxId, numberOfCount)
{
    var chkBoxCount= objControl.getElementsByTagName("INPUT");
    
    var intSelectedCount = 0;
    
    for(var i=0;i<chkBoxCount.length;i++) 
    {
        if(chkBoxCount[i].checked)
             intSelectedCount++;
    }
    
    if(numberOfCount == intSelectedCount)
        AllCheckBoxId.checked = true;
    else
        AllCheckBoxId.checked = false;
}

// to allow only numerics to be entered
function AllowOnlyNumeric()
{
    // Get the ASCII value of the key that the user entered
    var key = window.event.keyCode;

    // Verify if the key entered was a numeric character (0-9) or a decimal (.)
    if ( (key > 47 && key < 58))
        // If it was, then allow the entry to continue
        return;
    else
        // If it was not, then dispose the key and continue with entry
        window.event.returnValue = null; 
}

function extractNumber(obj, decimalPlaces, allowNegative)
{
	var temp = obj.value;
	
	// avoid changing things if already formatted correctly
	var reg0Str = '[0-9]*';
	if (decimalPlaces > 0) {
		reg0Str += '\\.?[0-9]{0,' + decimalPlaces + '}';
	} else if (decimalPlaces < 0) {
		reg0Str += '\\.?[0-9]*';
	}
	reg0Str = allowNegative ? '^-?' + reg0Str : '^' + reg0Str;
	reg0Str = reg0Str + '$';
	var reg0 = new RegExp(reg0Str);
	if (reg0.test(temp)) return true;

	// first replace all non numbers
	var reg1Str = '[^0-9' + (decimalPlaces != 0 ? '.' : '') + (allowNegative ? '-' : '') + ']';
	var reg1 = new RegExp(reg1Str, 'g');
	temp = temp.replace(reg1, '');

	if (allowNegative) {
		// replace extra negative
		var hasNegative = temp.length > 0 && temp.charAt(0) == '-';
		var reg2 = /-/g;
		temp = temp.replace(reg2, '');
		if (hasNegative) temp = '-' + temp;
	}
	
	if (decimalPlaces != 0) {
		var reg3 = /\./g;
		var reg3Array = reg3.exec(temp);
		if (reg3Array != null) {
			// keep only first occurrence of .
			//  and the number of places specified by decimalPlaces or the entire string if decimalPlaces < 0
			var reg3Right = temp.substring(reg3Array.index + reg3Array[0].length);
			reg3Right = reg3Right.replace(reg3, '');
			reg3Right = decimalPlaces > 0 ? reg3Right.substring(0, decimalPlaces) : reg3Right;
			temp = temp.substring(0,reg3Array.index) + '.' + reg3Right;
		}
	}
	
	obj.value = temp;
}
function numbersonly(obj, e, allowDecimal, allowNegative)
{
	var key;
	var isCtrl = false;
	var keychar;
	var reg;
		
	if(window.event) {
		key = e.keyCode;
		isCtrl = window.event.ctrlKey
	}
	else if(e.which) {
		key = e.which;
		isCtrl = e.ctrlKey;
	}
	
	if (isNaN(key)) return true;
	
	keychar = String.fromCharCode(key);
	
	// check for backspace or delete, or if Ctrl was pressed
	if (key == 8 || isCtrl)
	{
		return true;
	}

	reg = /\d/;
	var isFirstN = allowNegative ? keychar == '-' && obj.value.indexOf('-') == -1 : false;
	var isFirstD = allowDecimal ? keychar == '.' && obj.value.indexOf('.') == -1 : false;
	
	return isFirstN || isFirstD || reg.test(keychar);
}

function formatCurrency(num)
{
    //for now just return as it is. later you should format the currency
    //return strValue;
    
    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);
}