/************************************************************************************
		Author	: Dhvanit Upadhyay
		Update	: 26 July 2007.
************************************************************************************/

// Below function used for checked|unchecked all checkboxes;.
//alert('validation function');
function check_delbox(field, mesg)
{  
	var checked;
	checked = 0;
	count = document.formlist.elements.length;

	for (i=0; i < count; i++)
	{
		ele = document.formlist.elements[i];

		if ( (ele.type == "checkbox") && (ele.name != 'check_box') )
		{
			if (document.formlist.elements[i].checked)
			{	
				checked++;	
			}
		}
	}
	if (checked == 0)
	{
		alert("Select Atleast One " + mesg + "." );
		return false;
	}
	else
	{
		var cnf = confirm("Are You Sure to Delete?");
		if(cnf)
		{
		   return true;
		}
		else
		{
		   return false;
		}
	}
 }

 


// Below function used for checked that wheather user had checked atleast one checkbox or not.
function get_check(chk_delete, field)
{ 

	count = document.formlist.elements.length;

	if (chk_delete.checked)
	{
		for (i=0; i < count; i++)
		{
			ele = document.formlist.elements[i];

			if ( (ele.type == "checkbox") && (ele.name != 'check_box') )
			{
				document.formlist.elements[i].checked = 1;
			}
		}
	}
	else
	{
		for (j=0; j < count; j++) 
		{
			ele = document.formlist.elements[j];

			if ( (ele.type == "checkbox") && (ele.name != 'check_box') )
			{
				document.formlist.elements[j].checked = 0;
			}
		}
	}
	return true;
}



// Below function will check that whather input field is empty or contain any value.
function check_empty(input_field,mesg,name,frm)
{		
	//alert(input_field.value);
	if (input_field.value == "")
	{	
		hideAllDiv('Tr',frm);
		document.getElementById('Tr'+name).style.display='';
		document.getElementById('Dv'+name).innerHTML=mesg;
		input_field.focus();
		
		return false;
	}
	else
	{

			hideAllDiv('Tr',frm);
		return true;	
	}
}
function hideAllDiv(Tr, theForm)
{
//  alert(eval(theForm));

var len= eval(theForm).elements.length;
//alert("len--"+len);
  for(var i=0; i<len ; i++)
  {
//	  alert(Tr+(eval(theForm).elements[i].name));
	 if(document.getElementById(Tr+(eval(theForm).elements[i].name)))
	 {
		 
	   document.getElementById(Tr+(eval(theForm).elements[i].name)).style.display='none';
	 }

  }
}

/*function hideAllDiv(Tr)
{
 
  if(document.forms.length > 1)
  {
  var len= document.forms[2].elements.length;

  for(var i=0; i<len ; i++)
  {
	 if(document.getElementById(Tr+(document.forms[2].elements[i].name)))
	 {
	   document.getElementById(Tr+(document.forms[2].elements[i].name)).style.display='none';
	 }
	 
  }
  
  }
  else
  {
	var len= document.forms[0].elements.length;
		for(var i=0; i<len ; i++)
		{
			if(document.getElementById(Tr+(document.forms[0].elements[i].name)))
			{
			   document.getElementById(Tr+(document.forms[0].elements[i].name)).style.display='none';
			}
				 
		}  
	}

}
*/// Below function will check length of input field.
function check_length(input_field, mini, maxi, mesg,name,frm)
{
	if (input_field.value.length < mini || input_field.value.length > maxi)
	{	
		hideAllDiv('Tr',frm);
		document.getElementById('Tr'+name).style.display='';
		document.getElementById('Dv'+name).innerHTML=mesg;
		input_field.focus();
		return false;
	}
	else
	{hideAllDiv('Tr',frm);	return true;			}
}

// Below function will check range of input field.
function check_range(input_field, mini, maxi, mesg)
{
	if (input_field.value < mini || input_field.value > maxi)
	{	
		alert(mesg);
		input_field.focus();
		return false;
	}
	else
	{	return true;			}
}

// Below function will compare two field value.
function check_compare(input_field, input_field2, mesg,name,frm)
{
	if (input_field.value != input_field2.value)
	{
		hideAllDiv('Tr',frm);
		document.getElementById('Tr'+name).style.display='';
		document.getElementById('Dv'+name).innerHTML=mesg;
		input_field2.focus();
		return false;
	}
	else
	{hideAllDiv('Tr',frm);	return true;}
}

// Below function will check combobox of input field.
function check_combo(input_field, val, mesg,name,frm)
{
	if (input_field.value == val)
	{	
		hideAllDiv('Tr',frm);
		document.getElementById('Tr'+name).style.display='';
		document.getElementById('Dv'+name).innerHTML=mesg;
		input_field.focus();
		return false;
	}
	else
	{hideAllDiv('Tr',frm);	return true;			}
}

// Below function used for checking only numbers.
function check_number(input_field, mesg,name,frm)
{
	var re = /^[0-9]*$/;

	if (input_field.length != 0)
	{
		if (!re.test(input_field.value) )
		{
			hideAllDiv('Tr',frm);
			document.getElementById('Tr'+name).style.display='';
		    document.getElementById('Dv'+name).innerHTML=mesg;

		input_field.focus();
		return false;
		}
		else
		{hideAllDiv('Tr',frm);	return true;			}	
	}
	else
	{hideAllDiv('Tr',frm);	return true;			}
}

// Below function used for checking only alphabets.
function check_alphabets(input_field, mesg,name,frm)
{
	var re = /^[.A-Za-z ]*$/;

	if (input_field.length != 0)
	{
		if (!re.test(input_field.value) )
		{
			hideAllDiv('Tr',frm);
			document.getElementById('Tr'+name).style.display='';
		    document.getElementById('Dv'+name).innerHTML=mesg;

			//alert(mesg);
			input_field.focus();
			return false;
		}
		else
		{hideAllDiv('Tr',frm);	return true;}	
	}
	else
	{
		hideAllDiv('Tr',frm);	
		return true;
	}
}

// Below function used for checking only alpha-numeric.
function check_alphanumeric(input_field, mesg)
{
	var re = /^[A-Za-z0-9]*$'/;

	if (input_field.length != 0)
	{
		if (!re.test(input_field.value) )
		{
			alert(mesg);
			input_field.focus();
			return false;
		}
	}
	return true;
}

function check_alphanumeric_card(input_field, mesg)
{
	var re = /^[A-Za-z0-9 ]*$/;

	if (input_field.length != 0)
	{
		if (!re.test(input_field.value) )
		{
			alert(mesg);
			input_field.focus();
			return false;
		}
	}
	return true;
}


// Below function used for checking email address.
function check_email(input_field, mesg,name,frm)
{
	var re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;

	//alert('here test');
	if (input_field.length != 0)
	{
		if (!re.test(input_field.value) )
		{
			hideAllDiv('Tr',frm);
			document.getElementById('Tr'+name).style.display='';
		    document.getElementById('Dv'+name).innerHTML=mesg;

		input_field.focus();
		return false;
		}
		else
		{hideAllDiv('Tr',frm);	return true;			}	
	}
	
	//return true;
}

// Below function used for checking email address. (Regular Exp by : Tejas Trivedi).
function check_ip(input_field, mesg)
{
	var re = /^(25[0-5]|2[0-4]\d|[01]?\d\d|\d)\.((25[0-5]|2[0-4]\d|[01]?\d\d|\d))\.((25[0-5]|2[0-4]\d|[01]?\d\d|\d))\.((25[0-5]|2[0-4]\d|[01]?\d\d|\d))$/;

	if (input_field.length != 0)
	{
		if (!re.test(input_field.value))
		{
			alert(mesg);
//			input_field.focus();
			return false;
		}
	}
	return true;
}

// Below function used for checking url address. (Regular Exp by : Tejas Trivedi).
function check_url(input_field, mesg)
{
  if(input_field.value.match(/^(http|ftp)\:\/\/\w+([\.\-]\w+)*\.\w{2,4}(\:\d+)*([\/\.\-\?\&\%\#]\w+)*\/?$/i) ||
     input_field.value.match(/^mailto\:\w+([\.\-]\w+)*\@\w+([\.\-]\w+)*\.\w{2,4}$/i))
  {
  	  return true;
  } 
  else
  {
    alert(mesg);
    input_field.select();
    input_field.focus();
    return false;
  }
}	
// Below function used for checking US zip code (Written by : Tejas Trivedi).
function check_zip(input_field, mesg)
{
	var re = /^[0-9]{5}$/;

	if (input_field.length != 0)
	{
		if (!re.test(input_field.value))
		{
			alert(mesg);
			input_field.focus();
			return false;
		}
	}
	return true;
}

// Below function used for checking email address.
function check_specialchar(input_field, mesg)
{
	var count;
	var special = "~`!@#$%^&*()'?><>.,+=|\*-+/{}[]|";
	var chars	= input_field.value.split("");								// create array

	if (input_field.length != 0)
	{
		count = 0;
		for (i = 0; i < chars.length; i++)
    	{
			if (special.indexOf(chars[i]) != -1)
            {	count++;		}
		}

		if (count > 0)
		{
			alert(mesg);
			input_field.focus();
			return false;
		}
	}
	return true;
}

// Below function used for checking valid date with us format. (Written by : Tejas Trivedi).
function check_date(input_field, mesg)
{
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;
	var matchArray = input_field.match(datePat);

	if (matchArray == null)
	{
		alert(mesg)
		return false;
	}

	month	= matchArray[1]; 							// parse date into variables
	day		= matchArray[3];
	year	= matchArray[4];

	if (month < 1 || month > 12) 
	{	// check month range
		alert("Month must be between 1 and 12.");
		return false;
	}

	if (day < 1 || day > 31)
	{
		alert("Day must be between 1 and 31.");
		return false;
	}

	if ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31)
	{
		alert("Month " + month + " doesn't have 31 days!")
		return false
	}

	if (month == 2)
	{	// check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));

		if (day > 29 || (day == 29 && !isleap))
		{
			alert("February " + year + " doesn't have " + day + " days!");
			return false;
		}
	}

	return true;  // date is valid
}

// Dhaval Dave : 22 June 2006, function used for popup window.
// eg.	mypopup('http://www.google.com', 'Google', 'no', 'no', 0, 0, 500, 400)
function mypopup(file, title, menubar, toolbar, status, sb, width, height)
{
/*	status  	: The status bar at the bottom of the window. 
	toolbar  	: The standard browser toolbar, with buttons such as Back and Forward. 
	location  	: The Location entry field where you enter the URL. 
	menubar  	: The menu bar of the window 
	directories	: The standard browser directory buttons, such as What's New and What's Cool 
	resizable 	: Allow/Disallow the user to resize the window. 
	scrollbars  : Enable the scrollbars if the document is bigger than the window 
	height 		: Specifies the height of the window in pixels. (example: height='350') 
	width  		: Specifies the width of the window in pixels. 	*/

//	mywindow = window.open (file, title, '"menubar=' + menubar + ', toolbar=' + toolbar + ', status=' + status + ', scrollbars=' + sb + ', width=' + width + ', height=' + height + '"');
//	mywindow.moveTo(0, 0);
	mywindow = window.open (file, title, '"menubar=' + menubar + ', toolbar=' + toolbar + ', status=' + status + ', scrollbars=' + sb + ', width=' + width + ', height=' + height + '"');
}

// Dhaval Dave : 21 June 2006, function used for print current open window page.
function printPage()
{
	if (window.print)
	{
		agree = confirm('This page contains a form.\n\nClick OK to print this form');
		if (agree) window.print();
	}
}



// Below function used for right trailing space. (Written by : Jagdish Patel).
/*function ValidateComboBox(Element, FieldAttrib)
{
	alert(Element);
	if(Element.selectedIndex < 0)
	{
		//ErrMsg+="- Please select a value for "+FieldAttrib["Alias"]+"\n";
		ErrMsg="Please select a value for "+FieldAttrib["Alias"];
		eName=Element.name;
		document.getElementById(Tr+eName).style.display='';
		document.getElementById(Dv+eName).innerHTML=ImgTag+ErrMsg;
		document.getElementById(eName).focus();
		if(Flg==0)
		 { 
		  Element.focus();
		  Flg=1;
		 }
		ok=false;
		return false;
	}
}*/

// Below function used for left trailing space. (Written by : Jagdish Patel).
/*function ltrim (input_field)
{
	var trimed 	= "";
	var i	 	= 0;

	if (input_field.value.length < 1)
	{
		return "";
	}

	while (i < input_field.value.length)
	{
		if (input_field.value.charAt(i) != " ")
		{
			input_field.value = input_field.value.substring(i, input_field.value.length);
			break;
		}
		i++;
	}
	return trimed;
}*/
// Below function will check that whather input date is < today date.
function compare_date (input_field, mesg)
{
	var m = new Array(12);
	m[1] = "01";
	m[2] = "02";
	m[3] = "03";
	m[4] = "04";
	m[5] = "05";
	m[6] = "06";
	m[7] = "07";
	m[8] = "08";
	m[9] = "09";
	m[10] = "10";
	m[11] = "11";
	m[12] = "12";
			
	var t = new Date();
	var month = t.getMonth()+1;
	var date  = t.getDate() ;
	if(date >= 1 && date <= 9)
	{
	  date = m[date];
	}	
	var year  = t.getFullYear() ;
	
	var matchArray	= input_field.value.split("-");
	if (matchArray == null)
	{
		alert(mesg)
		return false;
	}

	x_month	= matchArray[0]; 							// parse date into variables
	x_day	= matchArray[1];
	x_year	= matchArray[2];
	
	if(x_year < year)
	{
		return true;
	}
	else if(x_year == year)
	{
		if(x_month < m[month])
		{
			return true;
		}
		else if(x_month == m[month])
		{
			if(x_day > date)
			{
				alert(mesg);
				input_field.focus();
				return false;
			}
			else
			{
				return true;	
			}
		}
		else
		{
			alert(mesg);
			input_field.focus();
			return false;
		}
	}
	else
	{
		alert(mesg);
		input_field.focus();
		return false;
	}	
}

// Below function will check that whather From date is < To date.
function compare_twodate (input_field, input_field2, mesg)
{
	var from = input_field.value.split("-");
	f_month	= from[0]; 							
	f_day	= from[1];
	f_year	= from[2];
	
	var to = input_field2.value.split("-");
	t_month	= to[0]; 							
	t_day	= to[1];
	t_year	= to[2];
	
	if(f_year < t_year)
	{
		return true;
	}
	else if(f_year == t_year)
	{
		if(f_month < t_month)
		{
			return true;
		}
		else if(f_month == t_month)
		{
			if(f_day > t_day)
			{
				alert(mesg);
				input_field.focus();
				return false;
			}
			else
			{
				return true;	
			}
		}
		else
		{
			alert(mesg);
			input_field.focus();
			return false;
		}
	}
	else
	{
		alert(mesg);
		input_field.focus();
		return false;
	}	
}
function ltrim(s)
{
	var l=0;
	while(l < s.length && s[l] == ' ')
	{	l++; }
	return s.substring(l, s.length);
}

function rtrim(s)
{
	var r=s.length -1;
	while(r > 0 && s[r] == ' ')
	{	r-=1;	}
	return s.substring(0, r+1);
}
function rightTrim(strValue) {
var objRegExp = /^([\w\W]*)(\b\s*)$/;
      if(objRegExp.test(strValue)) {
       strValue = strValue.replace(objRegExp, '$1');
    }
  return strValue;
}

function leftTrim(strValue) {
var objRegExp = /^(\s*)(\b[\w\W]*)$/;
 
      if(objRegExp.test(strValue)) {
       strValue = strValue.replace(objRegExp, '$2');
    }
  return strValue;
}

function trim(strValue) {

var objRegExp = /^(\s*)$/;
    if(objRegExp.test(strValue)) {
       strValue = strValue.replace(objRegExp, '');
       if( strValue.length == 0)
          return strValue;
    }
   objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
   if(objRegExp.test(strValue)) {
       strValue = strValue.replace(objRegExp, '$2');
    }
  return strValue;
}