/*
	
	1)  string trim(string)
	2)  boolean checkempty(object,errmsg)
	3)	boolean checkemail(object,errmsg)
	4)	boolean chemnumeric(object,errmsg)	
	5)	boolean checktelno(object,errmsg)
	6)  boolean checkmaxlength(object,length,errmsg)
	7)	boolean checkminlength(object,length,errmsg)
	8)  boolean checkinteger(object,errmsg)
*/	

//Function trim similiar to Visual Basic Trim()
//Removes Leading and trailing spaces and tabs from the argument passed
//returns a string
//trim all the required fields using this function
function trim(str)
{
	//alert('str ' +str);
	var x;
	var ch;
	
	for(x=0;x<str.length;x++)
	{
		ch=str.substr(x,1);
		if(ch==' ' || ch=='\t')
		{
			str=str.substr(x+1,str.length-1);
		}
		else
			break;
	}
	
	for(x=str.length-1;x>=0;x=x-1)
	{
		ch=str.substr(x,1);
		if(ch==' ' || ch=='\t')
		{
			str=str.substr(0,str.length-1);
		}
		else
			break;
	}
	
	return str;
}

	//this function accepts the object as a parameter and display the appropriate error messages if the text box is empty
	function checkempty(objname,errmsg)
	{
		if (trim(objname.value)=="")
		{
			alert(errmsg);
			objname.focus();
			return false
		}
		return true;
	}

	function checkString(objname,errmsg)
	{
		var str = trim(objname.value);
		for(x=0;x<str.length;x++)
		{
			ch=str.substr(x,1);
			if ((ch > '0' && ch <'9'))
			{
				alert(errmsg);
				objname.focus();
				return false;
			}
		}
		return true;
	}

	function checkempty_disabled(objname,errmsg)
	{
		if (trim(objname.value)=="")
		{
			alert(errmsg);
			
			return false
		}
		return true;
	}
	//this function accepts the object as a parameter and display the appropriate error messages if the text box is empty
	//similar to the above function only that it checks for combo box
	function checkcomboempty(objname,errmsg)
	{
		if ((trim(objname[objname.selectedIndex].value)=="")||(trim(objname[objname.selectedIndex].value)=="-1") )
		{
			alert(errmsg);
			objname.focus();
			return false
		}
		return true;
	}

	function checknumeric(objname,errmsg)
	{
		if (isNaN(trim(objname.value)))
		{
			alert(errmsg);
			objname.focus();
			return false;
		}
		return true;
	}
//Email validation starts from here
	function checkemail(objname,errmsg)
	{
		vvalue=trim(objname.value);
		atPos = vvalue.indexOf('@');
		sppos = vvalue.indexOf(" ");
		dopos = vvalue.indexOf(".");
		dopos1 = vvalue.indexOf(".")+1;

		comma = vvalue.indexOf(",")
		if (atPos < 1 || atPos == (vvalue.length - 1) || (sppos != -1)|| (dopos == -1) || (comma==-1))
		{
			alert(errmsg);
			objname.focus();
			return false;
		}
		return true;
	}
	function checkemail_new1(objname,errmsg)
	{
	    vvalue=trim(objname.value);
	 	var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/; 
		var val = emailPattern.test(vvalue);

		if(!val){
  		        alert(errmsg);
				objname.focus();
				return false;
		}
		return true;
	}

//Email validation ends from here

	function checkspecialchar(objname,errmsg)
	{
	  var spChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";
	    for (var i = 0; i < objname.value.length; i++) {
			if (spChars.indexOf(objname.value.charAt(i)) != -1) {
				alert(errmsg);
				objname.focus();
				return false;
			}
		}
		return true;
  }

//telno validation starts from here 
//it checks that the entered value does not contain anything except numeric characters and hyphen
	function checktelno(objname,errmsg)
	{
		var str = trim(objname.value);
		for(x=0;x<str.length;x++)
		{
			ch=str.substr(x,1);
			if ((ch < '0' || ch >'9')&&(ch!='-'))
			{
				alert(errmsg);
				objname.focus();
				return false;
			}
		}
		return true;
	}
//telno validation ends over here

//maxlength validation starts over here
	function checkmaxlength(objname,maxlength,errmsg)
	{
		var str = objname.value;
		if (str.length>maxlength)
		{
			alert(errmsg);
			objname.focus();
			return false;
		}
		return true;
	}
//maxlength validation ends over here

//minlength validation starts over here
	function checkminlength(objname,minlength,errmsg)
	{
		var str = objname.value;
		if (str.length<minlength)
		{
			alert(errmsg);
			objname.focus();
			return false;
		}
		return true;
	}
//minlength validation ends over here
	function checkinteger(objname,errmsg)
	{
		var str = trim(objname.value);
		for(x=0;x<str.length;x++)
		{
			ch=str.substr(x,1);
			if ((ch < '0' || ch >'9'))
			{
				alert(errmsg);
				objname.focus();
				return false;
			}
		}
		return true;
	}
//

//rupees validation starts from here 
//it checks that the entered value does not contain anything except numeric characters and hyphen
	function checkrupees(objname,errmsg)
	{
		var str = trim(objname.value);
		var dotctr=0;
		for(x=0;x<str.length;x++)
		{
			ch=str.substr(x,1);
			
			if(ch=='.') {
				dotctr = dotctr +1 ;
			}
			if ((ch < '0' || ch >'9')&&(ch!=',')&&(ch!='.'))
			{
				alert(errmsg);
				objname.focus();
				return false;
			}
		}
		if(dotctr==2) {
		alert(errmsg);
		objname.focus();
		return false;
		}
		return true;
	}
//rupees validation ends over here

//function mid start from here
//for Mid("Hello",1,1), you will get "e", not "H".  To get "H", you would
// simply type in Mid("Hello",0,1)

function midstring(str, start, len)
        /***
                IN: str - the string we are LEFTing
                    start - our string's starting position (0 based!!)
                    len - how many characters from start we want to get

                RETVAL: The substring from start to start+len
        ***/
        {
                // Make sure start and len are within proper bounds
                if (start < 0 || len < 0) return "";

                var iEnd, iLen = String(str).length;
                if (start + len > iLen)
                        iEnd = iLen;
                else
                        iEnd = start + len;

                return String(str).substring(start,iEnd);
        }

function InStr(strSearch, charSearchFor)
/*
InStr(strSearch, charSearchFor) : Returns the first location a substring (SearchForStr)
                           was found in the string str.  (If the character is not
                           found, -1 is returned.)
                           
Requires use of:
	Mid function
	Len function
*/
{
	for (i=0; i < Len(strSearch); i++)
	{
	    if (charSearchFor == Mid(strSearch, i, 1))
	    {
			return i;
	    }
	}
	return -1;
}


function Left(str, n)
        /***
                IN: str - the string we are LEFTing
                    n - the number of characters we want to return

                RETVAL: n characters from the left side of the string
        ***/
        {
                if (n <= 0)     // Invalid bound, return blank string
                        return "";
                else if (n > String(str).length)   // Invalid bound, return
                        return str;                // entire string
                else // Valid bound, return appropriate substring
                        return String(str).substring(0,n);
        }


function Right(str, n)
        /***
                IN: str - the string we are RIGHTing
                    n - the number of characters we want to return

                RETVAL: n characters from the right side of the string
        ***/
        {
                if (n <= 0)     // Invalid bound, return blank string
                   return "";
                else if (n > String(str).length)   // Invalid bound, return
                   return str;                     // entire string
                else { // Valid bound, return appropriate substring
                   var iLen = String(str).length;
                   return String(str).substring(iLen, iLen - n);
                }
        }
function Len(str)
        /***
                IN: str - the string whose length we are interested in

                RETVAL: The number of characters in the string
        ***/
        {  return String(str).length;  }

function upperstring(obj){
		a = new String(obj.value);
		obj.value = a.toUpperCase()
	}

function BumpUp_Box(box)  {
	for(var i=0; i<box.options.length; i++) {
		if(box.options[i].value == "")  {
			for(var j=i; j<box.options.length-1; j++)  {
				box.options[j].value = box.options[j+1].value;
				box.options[j].text = box.options[j+1].text;
			}
			var ln = i;
			break;
	   }
	}	
	if(ln < box.options.length)  {
		box.options.length -= 1;
		BumpUp_Box(box);
	}
}

function Sort_SelectListValue(box)  {
	var temp_opts = new Array();
	var temp = new Object();
	for(var i=0; i<box.options.length; i++)  {
		temp_opts[i] = box.options[i];
	}
	for(var x=0; x<temp_opts.length-1; x++)  {
		for(var y=(x+1); y<temp_opts.length; y++)  {
			if(temp_opts[x].text > temp_opts[y].text)  {
				temp = temp_opts[x].text;
				temp_opts[x].text = temp_opts[y].text;
				temp_opts[y].text = temp;
				temp = temp_opts[x].value;
				temp_opts[x].value = temp_opts[y].value;
				temp_opts[y].value = temp;
	    	}
		}
	}
	for(var i=0; i<box.options.length; i++){
		box.options[i].value = temp_opts[i].value;
		box.options[i].text = temp_opts[i].text;
   }
}

	function checknumeric1(objname,errmsg)
	{
		if (isNaN(trim(objname.value)))
		{
			alert(errmsg);
			return false;
		}
		return true;
	}

		//this function accepts the object as a parameter and display the appropriate error messages if the text box is empty
	function checkempty1(objname,errmsg)
	{
		if (trim(objname.value)=="")
		{
			alert(errmsg);
			return false
		}
		return true;
	}

//following functino is dateadd function which accetp three parameter as first is date, second is
//d, h, m OR s intervals  by how is to add and the third parameter is number parameter 

/*function demoIt(frm) {
    // grab the values from the form.
    var d = new Date(frm.theDate.value) ;
    var i = frm.interval.options[frm.interval.selectedIndex].value ;
    var j = frm.units.value ;
	
    // call the dateAdd function.
    temp = dateAdd( d, i, j ) ;
	
    // update the theDate field with our new value.
    if ( temp != null ) frm.theDate.value = temp.toString() ;
}
*/

function DateAddition( start, interval, number ) {
	
    // Create 3 error messages, 1 for each argument. 
    var startMsg = "Sorry the start parameter of the dateAdd function\n"
        startMsg += "must be a valid date format.\n\n"
        startMsg += "Please try again." ;
		
    var intervalMsg = "Sorry the dateAdd function only accepts\n"
        intervalMsg += "d, h, m OR s intervals.\n\n"
        intervalMsg += "Please try again." ;

    var numberMsg = "Sorry the number parameter of the dateAdd function\n"
        numberMsg += "must be numeric.\n\n"
        numberMsg += "Please try again." ;
		
    // get the milliseconds for this Date object. 
    var buffer = Date.parse( start ) ;
	
    // check that the start parameter is a valid Date. 
    if ( isNaN (buffer) ) {
        alert( startMsg ) ;
        return null ;
    }
	
    // check that an interval parameter was not numeric. 
    if ( interval.charAt == 'undefined' ) {
        // the user specified an incorrect interval, handle the error. 
        alert( intervalMsg ) ;
        return null ;
    }

    // check that the number parameter is numeric. 
    if ( isNaN ( number ) )	{
        alert( numberMsg ) ;
        return null ;
    }

    // so far, so good...
    //
    // what kind of add to do? 
    switch (interval.charAt(0))
    {
        case 'd': case 'D': 
            number *= 24 ; // days to hours
            // fall through! 
        case 'h': case 'H':
            number *= 60 ; // hours to minutes
            // fall through! 
        case 'm': case 'M':
            number *= 60 ; // minutes to seconds
            // fall through! 
        case 's': case 'S':
            number *= 1000 ; // seconds to milliseconds
            break ;
        default:
        // If we get to here then the interval parameter
        // didn't meet the d,h,m,s criteria.  Handle
        // the error. 		
        alert(intervalMsg) ;
        return null ;
    }
    return new Date( buffer + number ) ;
}

//upto here dataadd function

//alternatively addtion of date function for eg.
//DayDateAdd(new Date(),2) it will add 2 days to current date

function DayDateAdd(objDate, intDays)
{
  var iSecond=1000;	 // Dates are represented in milliseconds
  var iMinute=60*iSecond;
  var iHour=60*iMinute;
  var iDay=24*iHour;
  var objReturnDate=new Date();
  objReturnDate.setTime(objDate.getTime()+(intDays*iDay));
  return objReturnDate;
}
function Replace(string,text,by) {
// Replaces text with by in string
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;
    var newstr = string.substring(0,i) + by;
    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}
function upperFirst(string,text) {
    return Replace(string,text,text.charAt(0).toUpperCase() + text.substring(1,text.length));
}
function upperInitial(string,text) {
    return replace(string,' '+text,' '+text.charAt(0).toUpperCase() + text.substring(1,text.length));
}

function isSpaceInString(objid){
    var str = trim(objid.value) ;
	 for(x=0;x<str.length;x++){
			ch=str.substr(x,1);
			if(ch==' ') {
			    return true;
			}
		}
	 return false;	
}

 