// JavaScript Document
// Developped by avichal jangir //

//--------------------------------------------------------------------------------
//No valid any special character
//--------------------------------------------------------------------------------
function nonspecialchar(varStr, errormessage)
{
	var iChars = "`~!@#$%^&*()-=+\|,./?'\"[] {}";
	for (var i = 0; i < varStr.length; i++) 
	{
		if (iChars.indexOf(varStr.charAt(i)) != -1)
		{
			if (errormessage != "")
			{
				alert (errormessage);
			}
            return false;
        }
	}
}
//--------------------------------------------------------------------------------


//--------------------------------------------------------------------------------
//No valid email address
//--------------------------------------------------------------------------------
function emailCheck(emailStr, errormessage)
{
	var checkTLD=1;
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray=emailStr.match(emailPat);

	if (matchArray==null) 
	{
		alert(errormessage);
		return false;
	}
	
	var user=matchArray[1];
	var domain=matchArray[2];

	for (i=0; i<user.length; i++) 
	{
		if (user.charCodeAt(i)>127) 
		{
			alert(errormessage);
			return false;
		}
	}
	for (i=0; i<domain.length; i++) 
	{
		if (domain.charCodeAt(i)>127) 
		{
			alert(errormessage);
			return false;
   		}
	}

	if (user.match(userPat)==null) 
	{
		alert(errormessage);
		return false;
	}

	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) 
	{
		for (var i=1;i<=4;i++) 
		{
			if (IPArray[i]>255) 
			{
				alert(errormessage);
				return false;
   			}
		}
		return true;
	}

	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) 
	{
		if (domArr[i].search(atomPat)==-1) 
		{
			alert(errormessage);
			return false;
	   	}
	}

	if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1) 
	{
		alert(errormessage);
		return false;
	}

	if (len<2) 
	{
		alert(errormessage);
		return false;
	}

	return true;
}
//--------------------------------------------------------------------------------


function Chkradio(objname)
{
	chk = false;
	for(i = 0; i < document.myform.length; i++)
	{
		if(document.myform.elements[i].type == "radio" && document.myform.elements[i].name == objname)
		{
			if(document.myform.elements[i].checked == true)
			{
				chk = true;
				break;
			}
		}
	}

	if(!chk)
	{
		alert("Please Select A Record");
	}
	return chk;
}

//--------------------------------------------------------------------------------
//Check number - allow decimal
//--------------------------------------------------------------------------------
function checkcurr(object)
{
	var x=object;
	var anum=/(^\d+$)|(^\d+\.\d+$)/;
	if (anum.test(x))
	{
		return true;
	}
	else
	{
		return false;
	}
}
//--------------------------------------------------------------------------------

//--------------------------------------------------------------------------------
//Trim
//--------------------------------------------------------------------------------
function trim (s)
{
	return rtrim(ltrim(s));
}

function ltrim (s)
{
	return s.replace( /^\s*/, "" );
}

function rtrim (s)
{
	return s.replace( /\s*$/, "" );
}
//--------------------------------------------------------------------------------

//--------------------------------------------------------------------------------
//for mouse over and click color change of table row.
    /*
	Example to use the code
	-----------------------
	<tr onmouseover="set_hover(this, '#DEE3E7', 'orange');" onmouseout="remove_hover(this, '#FFFFFF', 'orange')">
      <td><input type="checkbox" name="checkbox4" value="checkbox" onclick="highlight_row(this, 'orange', 'red');"></td>
      <td>Superman</td>
      <td>Clark</td>
      <td>Kent</td>
    </tr>
	*/
//--------------------------------------------------------------------------------
function highlight_row(the_element, checkedcolor, uncheckedcolor) {
	if(the_element.parentNode.parentNode.style.backgroundColor != checkedcolor) {
		the_element.parentNode.parentNode.style.backgroundColor = checkedcolor;
	} else {
		the_element.parentNode.parentNode.style.backgroundColor = uncheckedcolor;
	}
}

function set_hover(the_row, color, checkedcolor) {
	if(the_row.style.backgroundColor != checkedcolor) {
		the_row.style.backgroundColor = color;
	}
}

function remove_hover(the_row, color, checkedcolor) {
	if(the_row.style.backgroundColor != checkedcolor) {
		the_row.style.backgroundColor = color;
	}
}
//--------------------------------------------------------------------------------

/*
   written by avichal.
*/
function howManyChecked(whichForm,whichCheckBoxArray,myMax,myMin,whichQuestion)
/*
  This function takes 5 paramaters:
  whichForm -- the NAME of the form to be validated, a string
  whichCheckBoxArray -- the NAME of the checkbox to be checked, a string
  myMax -- the most you want the user to be able to check, an integer
  myMin -- the least you want the user to be able to check, an integer
  whichQuestion -- a short description of the question, a string
  
  example use:
  howManyChecked('myform','cb_industry',6,1,'Industry');
*/
{
	var _countChecked = 0;
	var err = 0;
	/* iterate through all the elements in the checkbox array */
	for(i=0;i<document[whichForm][whichCheckBoxArray].length;i++)
	{
		/* and check to see if each is checked */
		if(document[whichForm][whichCheckBoxArray][i].checked==true)
		/* if it is, increment a counter */
		{ 
		_countChecked++; 
		}
	}
				      /* is the count too high? */
	if(_countChecked > myMax)
	{ 
		alert('Limit '+myMax+' checks for the '+whichQuestion+' question.');
		err = 1;
	}
	/* of is the count too low */
	else if(_countChecked < myMin)
	{ 
		alert('You must fill out at least '+myMin+' entry(s) for the '+whichQuestion+' question.');
		err = 1;
	}
	if (err == 1) 
	{ 
		return false; 
	}
}


function chkbox(frmnm)
{
	var frm = document.frmnm.length;
	var _countChecked = 0;
	for(var i =0;i<frm;i++)
	{
		if(document.frmnm.elements[i].type="checkbox")
		{
			if(document.frmnm.elements[i].checked==true)
			{
				_countChecked++; 
			}
		}
	}
	if(_countChecked ==0){alert("Please select at least one record");return false;}
	document.frmnm.submit();
}

