/************************************************************************************
		Author	: Dhaval Dave
		Update	: 15 June 2006.
		Purpose	: This code is used for javascript general function.
************************************************************************************/

// Below function used for checked|unchecked all checkboxes;.
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
	{
		return true;
	}
}

// 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)
{
	if (input_field.value == "")
	{	
		alert(mesg);			
		input_field.focus();
		return false;
	}
	else
	{	return true;			}
}

// Below function will check length of input field.
function check_length (input_field, mini, maxi, mesg)
{
	if (input_field.value.length < mini || input_field.value.length > maxi)
	{	
		alert(mesg);
		input_field.focus();
		return false;
	}
	else
	{	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)
{
	if (input_field.value != input_field2.value)
	{	
		alert(mesg);
		input_field2.focus();
		return false;
	}
	else
	{	return true;			}
}

// Below function will check combobox of input field.
function check_combo(input_field, val, mesg)
{
	if (input_field.value == val)
	{
		alert(mesg);
		input_field.focus();
		return false;
	}
	else
	{	return true;			}
}

// Below function used for checking only numbers.
function check_number(input_field, mesg)
{
	var re = /^[0-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 only float numbers.
function check_float(input_field, mesg)
{
	if (input_field.length != 0)
	{
		if (isNaN(input_field.value) )
		{
			alert(mesg);
			input_field.focus();
			return false;
		}
	}
	return true;
}

// Below function used for checking only alphabets.
function check_alphabets(input_field, mesg)
{
	var re = /^[A-Za-z]*$/;

	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 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;
}

// Below function used for checking email address.
function check_email(input_field, mesg)
{
	var re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;

	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. (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 email address. (Regular Exp by : Tejas Trivedi).
function check_url(input_field, mesg)
{
	var re = /^((http|https):\/\/)?((w){3}\.)?([a-z0-9\-]+)?\.((.*))$/;

	if (input_field.length != 0)
	{
//		if (!re.test(input_field.value))
		if (!input_field.value.match(/^((http|https):\/\/)?((w){3}$\.)?([a-z0-9\-]+)?\.((.*))$/))
		{
			alert(mesg);
			input_field.focus();
			return false;
		}
	}
	return true;
}

// 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 email address.
function check_allowchar(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 email address.
function check_pws_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.value.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 + ', location=no, menubar=no, directories=no, status=' + status + ', scrollbars=' + sb + ', width=' + width + ', height=' + height + '"');
//	mywindow.moveTo(0, 0);
}

// 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 trailing space. (Written by : Jagdish Patel).
function trim(input_field)
{
	if (input_field < 1)
	{
		return "";
	}

	trim_value = rtrim(input_field);
	trim_value = ltrim(input_field);

	if (input_field == "")
	{
		return "";
	}
	else
	{
		return input_field;
	}
}

// Below function used for right trailing space. (Written by : Jagdish Patel).
function rtrim(input_field)
{
	var trimed = "";

	if (input_field.value.length < 0)
	{
		return "";
	}

	var i = input_field.value.length -1;
	while (i > -1)
	{
		if (input_field.value.charAt(i) != " ")
		{
			input_field.value = input_field.value.substring(0, i + 1);
			break;
		}
		i--;
	}
	return trimed;
} 

// 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;
}

