
// Checks if user has logged in by checking for cookie named
// 'NiraUserName'. If cookie not present, redirect to login page.
function checkValidUser()
{
if (getCookie("NiraUserName") == null) {
	
	window.location = "/swadhyay/login.html";
}
}

// Gets cookie with given name
function getCookie(NameOfCookie)
{

// First we check to see if there is a cookie stored.
// Otherwise the length of document.cookie would be zero.

if (document.cookie.length > 0)
{

// Second we check to see if the cookie's name is stored in the
// "document.cookie" object for the page.

// Since more than one cookie can be set on a
// single page it is possible that our cookie
// is not present, even though the "document.cookie" object
// is not just an empty text.
// If our cookie name is not present the value -1 is stored
// in the variable called "begin".

begin = document.cookie.indexOf(NameOfCookie+"=");
if (begin != -1) // Note: != means "is not equal to"
{

// Our cookie was set.
// The value stored in the cookie is returned from the function.

begin += NameOfCookie.length+1;
end = document.cookie.indexOf(";", begin);
if (end == -1) end = document.cookie.length;
return unescape(document.cookie.substring(begin, end)); }
}
return null;

// Our cookie was not set.
// The value "null" is returned from the function.

} // End of getCookie()

// Trims spaces from the given value
function Trim(TRIM_VALUE){
	if(TRIM_VALUE.length < 1){
		return"";
	}
	TRIM_VALUE = RTrim(TRIM_VALUE);
	TRIM_VALUE = LTrim(TRIM_VALUE);
	if(TRIM_VALUE==""){
		return "";
	}
	else{
		return TRIM_VALUE;
	}
} //End Function

// Right trims value.
function RTrim(VALUE){
	var w_space = String.fromCharCode(32);
	var v_length = VALUE.length;
	var strTemp = "";
	if(v_length < 0){
		return"";
	}
	var iTemp = v_length -1;

	while(iTemp > -1){
		if(VALUE.charAt(iTemp) == w_space){
		}
		else{
			strTemp = VALUE.substring(0,iTemp +1);
			break;
		}
	iTemp = iTemp-1;
	} //End While
	
	return strTemp;
} //End Function

// 
function LTrim(VALUE){
	var w_space = String.fromCharCode(32);
	if(v_length < 1){
		return"";
	}
	var v_length = VALUE.length;
	var strTemp = "";

	var iTemp = 0;

	while(iTemp < v_length){
		if(VALUE.charAt(iTemp) == w_space){
		}
		else{
			strTemp = VALUE.substring(iTemp,v_length);
			break;
		}
		iTemp = iTemp + 1;
	} //End While
	return strTemp;
} //End Function

function makeArray()
{
        for (i = 0; i<makeArray.arguments.length; i++)
        	this[i + 1] = makeArray.arguments[i];
}

function nths(day)
{
        if (day == 1 || day == 21 || day == 31) return 'st';
        if (day == 2 || day == 22) return 'nd';
        if (day == 3 || day == 23) return 'rd';
        return 'th';
}

function y2k(number)
{
     return (number < 1000) ? number + 1900 : number;
}

function getDate()
{

    var myDays= ["Sun","Mon","Tues","Wed","Thu","Fri","Sat","Sun"];
    var months = new makeArray('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');

    var date = new Date();
    thisDay=date.getDay();
    thisDay=myDays[thisDay];
    var day  = date.getDate();
    var month = date.getMonth() + 1;

    return thisDay+" "+ months[month] +" " +day +nths(day)+ ", " + y2k(date.getYear());
}

function getDateTime()
{

    var myDays= ["Sun","Mon","Tues","Wed","Thu","Fri","Sat","Sun"];
    var months = new makeArray('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');

    var date = new Date();
    thisDay=date.getDay();
    thisDay=myDays[thisDay];
    var day  = date.getDate();
    var month = date.getMonth() + 1;
    var Hours=date.getHours();
    if(Hours>12){Hours = Hours-12; var zone="PM";}
    else{var zone="AM"}
    var Minutes = date.getMinutes();
    if (Minutes<10){Minutes="0"+Minutes;}


    return thisDay+" "+ months[month] +" " +day +nths(day)+ ", " + y2k(date.getYear())+" "+Hours+":"+Minutes+" "+zone;
}

function numChecked()
{
    var frm=document.deleteForm;
    j=0;
    for(i=0;i<frm.length; i++)
    {
         e=frm.elements[i];
         if(e.type=='checkbox' && e.checked && e.name !='RstrctFlag')
            j++;
    }
    return j;
}

function validateDate(elementID)
{
    var value = document.getElementById(elementID).value;

    if (value == "")
    {
        return true;
    }

    var firstSlash = value.indexOf('/');
    var lastSlash = value.lastIndexOf('/');
    var month = value.substring(0,firstSlash);
    var day = value.substring((firstSlash+1), lastSlash);
    var year = value.substr((lastSlash+1),4);

    if (isNaN(month))
    {
        return false;
    }
    else
    {
        if (month < 1 || month > 12)
        {
            return false;
        }
    }

    if (isNaN(day))
    {
        return false;
    }
    else
    {
        if (day < 1 || day > 31)
        {
            return false;
        }
    }

    if (isNaN(year))
    {
        return false;
    }
    else
    {
        if (year < 1900 || year > 2100)
        {
            return false;
        }
    }

    return true;
}

function formatCurrency(amount)
{
    var i = parseFloat(amount);
    if (isNaN(i))
    {
        i = 0.00;
    }

    var minus = '';

    if (i < 0)
    {
        minus = '-';
    }

    i = Math.abs(i);
    i = parseInt((i + .005) * 100);
    i = i / 100;
    s = new String(i);

    if (s.indexOf('.') < 0)
    {
        s += '.00';
    }

    if (s.indexOf('.') == (s.length - 2))
    {
        s += '0';
    }
    s = minus + s;
    return s;
}
// end of function CurrencyFormatted()

// Checks if given inputValue is of decimal format
// of exactly given precision.
function checkDecimalPrecision(inputVal, precision) {

	// Check if number has period
	var index = inputVal.indexOf(".");
	if(index == -1) {
		return false;
	}
	
	// check decimal precision
	var decPrecision = inputVal.substring(index, inputVal.length-1).length;
	if(decPrecision != precision) {
		return false;
	}

	return true;
}

// Checks if given inputValue contains number of digits before
// decimal (if any) of atleast given precision.
function checkIntPrecision(inputVal, precision) {

	// Eliminate +, - and $ signs
	while (inputVal.indexOf("$") >= 0)
    {
        inputVal = inputVal.replace("$", "");
    }
    while (inputVal.indexOf("+") >= 0)
    {
        inputVal = inputVal.replace("+", "");
    }
    while (inputVal.indexOf("-") >= 0)
    {
        inputVal = inputVal.replace("-", "");
    }
    
	// Check if number has period
	var index = inputVal.indexOf(".");
	if(index == -1 && inputVal.length > 0) {
		return true;
	}
	
	// check decimal precision
	var decPrecision = inputVal.substring(0, index).length;
	if(decPrecision < precision) {
		return false;
	}

	return true;
}

// Confirms cancellation and takes to url.
function confirmCancel(url) {
	if(confirm("Do you wish to cancel? You will be taken to NIRA home page.")) {
		window.location = url;
	}
}

