﻿// JScript File
function trim(str)
{
    return str.replace(/^[\s]+/,'').replace(/[\s]+$/,'').replace(/[\s]{2,}/,' ');
}
function highlightRow(obj, newColor)
{
    obj.style.cursor = "hand";
    obj.style.backgroundColor = "#B6CFE7";
}
//  
function dehighlightRow(obj, originalColor)
{
    obj.style.backgroundColor = originalColor;
}
//
function isEmail(email){
    var exp=/^(.)+@{1,1}((.)+\.(.)+)+$/;
    return exp.test(email);
}
//
function isNumeric(num){
    var exp=/^-?[0-9]+(\.[0-9]{1,2})?$/;
    return exp.test(num);
}
//
function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(d,m,y){
   dtStr = m + "/" + d + "/" + y;
	var daysInMonth = DaysArray(12);
	var pos1=dtStr.indexOf("/");
	var pos2=dtStr.indexOf("/",pos1+1);
	var strMonth=dtStr.substring(0,pos1);
	var strDay=dtStr.substring(pos1+1,pos2);
	var strYear=dtStr.substring(pos2+1);
	strYr=strYear;
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		return false
	}
	if (dtStr.indexOf("/",pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, "/"))==false){
		return false
	}
    return true
}
//
function clearerror(id)
{
    document.getElementById(id).style.display="none";
}
//
function seterror(id, msg)
{
    document.getElementById(id).innerText=msg;
    document.getElementById(id).style.display="";
}
//
function copyvalue(fromid, toid)
{
   document.getElementById(toid).value = document.getElementById(fromid).value;
}
function ValidateAmount(id,errid)
{
    if (!isNumeric(trim(document.getElementById(id).value)))
    {
        seterror(errid,"please enter a valid amount");
        document.getElementById(id).focus();
        return false;
    }
    return true;
}
function ValidateBatch(fromid, toid, errid)
{
    if ( parseInt(document.getElementById(fromid).value) >= parseInt(document.getElementById(toid).value) )
    {
        seterror(errid,"please select a valid batch");
        document.getElementById(fromid).focus();
        return false;
    }
    return true;
}
//
function ddlMandatory(id, errid)
{
    if (document.getElementById(id).selectedIndex == 0)
    {
        document.getElementById(errid).style.display="";
        document.getElementById(id).focus();
        return false;
    }
    return true;
}
//
function txtMandatory(id, errid )
{
    if (trim(document.getElementById(id).value) == "")
    {
        document.getElementById(errid).style.display="";
        document.getElementById(id).focus();
        return false;
    }
    return true;
}
//
function emailMandatory(id, errid, isoptional)
{
    var email = trim(document.getElementById(id).value);        
    if (email == "")
    {
        if (isoptional) return true;
        //
        document.getElementById(errid).style.display="";
        document.getElementById(id).focus();
        return false;
    }
    if (!isEmail(email))
    {
        seterror(errid,"please enter a valid email id");
        document.getElementById(id).focus();
        return false;
    } 
    return true;
}
//
function dateMandatory(dayid, monthid, yearid, errid, isoptional)
{
    var day = trim(document.getElementById(dayid).value);
    var month = trim(document.getElementById(monthid).value);
    var year = trim(document.getElementById(yearid).value);
    //
    if (day != "" || month != "" || year != "")
    {
        if (day == "" || month == "" || year == "")
        {
            document.getElementById(errid).style.display="";           
            if (day == ""){ document.getElementById(dayid).focus(); return false;}
            if (month == "") {document.getElementById(monthid).focus(); return false;}
            if (year == "") { document.getElementById(yearid).focus(); return false;}
        }
        if (!isDate(day,month,year ))
        {
            document.getElementById(errid).innerText="please select a valid date";
            document.getElementById(errid).style.display="";           
            document.getElementById(dayid).focus();
            return false;
        }
    }
    else
    {
        if (isoptional==false)
        {
            document.getElementById(errid).style.display="";     
            document.getElementById(dayid).focus();
            return false;
        }
    }
    return true;        
}   
