//
//	This function changes the state of all the checkboxes in the form
//	Arguments:	FormName		: Name of the form
//				CheckboxName	: Name of checkboxes. All must have the same name.
//				CheckUncheck	: The value 0 or 1. ( To be checked or not).
//
//Trim spaces 

function checkid(str)
{ 
 lenstr = str.length
 for (i=0;i<lenstr;i++)
 {
	c = str.charAt(i);	
	if(c == ' ' || c == '\n'  || c == '\r')
	{
		alert("Enter Valid Member ID");
		return false;
		break   
	}	
 }
}

function Trim(str)
{ 
 lenstr = str.length
 substr = str
 for (i=0;i<lenstr;i++)
 {
	c = str.charAt(i);	
	if(c == ' ' || c == '\n'  || c == '\r')
		substr = str.substring(i+1,str.length)
	else
		break   
 }

 lenstr = substr.length
 str = substr 
 for(i=(lenstr)-1;i>=0;i--)
 {
	c = str.charAt(i);
	if (c == ' ' || c == '\n' || c == '\r')
	{
		substr = str.substring(0,i);		
	}
	else
	{
		break;
	}
 } 
 return substr	
}

//Numeric Validation
function isNumeric(str)
{
  var checkOK = "0123456789.-";
  var checkStr = str;
  var allValid = true;
  var decPoints = 0;
  var allNum = "";
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
    allNum += ch;
  }
  if (!allValid)
  {    
    return (false);
  }
	return (true) ;
}

//Phone Validation
function isPhoneFax(str)
{  
  var checkOK = "0123456789-()";
  var checkStr = str;
  var allValid = true;
  var decPoints = 0;
  var allNum = "";
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
    allNum += ch;
  }
  if (!allValid)
  {    
    return (false);
  }
	return (true) ;
}

function isEMail(str)
{
	lenstr = Trim(str).length;
	nAtr = str.indexOf("@");
	nDot = str.indexOf(".",nAtr);
	nAtr2 = str.indexOf("@",nAtr+1);
	nfDot = str.indexOf(".");
	nlDot = str.indexOf(".",lenstr-1);
	nDDot = str.indexOf("..");

	if (lenstr>4 &&  nAtr>0 && nDot>(nAtr+1) && nAtr2==-1 && nDot<lenstr-1 && nfDot>0 && nlDot==-1 && nDDot==-1)
		return true;
	else
		return false;
return true ;
}


function isURL(str)
{  
return true ;
}

//Decimal Validation
function isDecimal(str)
{  
  var checkOK = "0123456789.";
  var checkStr = str;
  var allValid = true;
  var decPoints = 0;
  var allNum = "";
  var deccnt = 0
  for (iCount = 0;  iCount < checkStr.length;  iCount++)
  {
    ch = checkStr.charAt(iCount);    
    for (jCount = 0;  jCount < checkOK.length;  jCount++)
      if (ch == checkOK.charAt(jCount))
        {	  
		  if (ch=='.')	
		    deccnt=deccnt+1		    
          break;
        } 
    if (jCount == checkOK.length)
    {
      allValid = false;
      break;
    }
    allNum += ch;
  }  
   if (!allValid || deccnt>1)
  {    
    return false;
  }
	return true ;
}


//Alpha Numeric Validation
function isAlphaNumeric(str)
{
  var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_";
  var checkStr = str;
  var allValid = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
  }
  if (!allValid)
  {    
    return false;
  }
	return true ;
}

//Date Validation
function isDate(fd,fm,fy)
{
	fd = parseInt(fd);
	fm = parseInt(fm);
	fy = parseInt(fy);
	
	lipyr=false;	
	div = (fy%4);
	days=28;
	
	if(div==0)
	{
		lipyr=true;
		days = 29;
	}
	if(fm==2 && fd>days)
	{
		return false;
	}
	if((fm==4 || fm==6 || fm==9 || fm==11) && fd==31)
	{
		return false;
	}
	return true;
}

//Compare dates
function CompareDate(fd,fm,fy,td,tm,ty)
{
	fd = parseInt(fd);
    fm = parseInt(fm);
    fy = parseInt(fy);
   
    td = parseInt(td);
    tm = parseInt(tm);
    ty = parseInt(ty);
	
	if(fy == ty)
	{
		if(fm == tm && fd>td)
		{
			alert("Please Enter Valid Date Range.");				
			return false;
		}					
		else
		{
			if(parseInt(fm) > parseInt(tm))
			{
				alert("Please Enter Valid Date Range.");				
				return false;
			}
		}	
	}
	else
	{
		if(parseInt(fy) > parseInt(ty))
		{
			alert("Please Enter Valid Date Range.");						
			return false;
		}
	}
	return true;
}

function fCheckAll(oCheckBox,bChecked)
{
	if(oCheckBox)
	{
		if (oCheckBox.length >= 0) 
			for(i=0;i<oCheckBox.length;i++)
				oCheckBox[i].checked = bChecked ;
		else
			oCheckBox.checked = bChecked ;
	}
}

function CheckAll(oCheckBox,bChecked)
{
	if (oCheckBox.length >= 0) 
	{
		for(i=0;i<oCheckBox.length;i++)
		{
		oCheckBox[i].checked = bChecked ;
		}
	}
	else
	{
		oCheckBox.checked = bChecked ;
	}
}

//check wheather checkbox is selected or not
//frmchkname : formname.checkboxname
function IsCheckboxSelected(frmchkname)
{
	
	if(frmchkname){
		if(frmchkname.length >= 0)
		{	
			for(i=0;i<frmchkname.length;i++)
			{
				if(frmchkname[i].checked)			
					return true;		
			}		
		}
		else
		{
			if(frmchkname.checked)
				return true;
		}			
		alert("Please Check atleast one value !");
	}
	return false;
}

function IsCheckboxSelected11(FormName, CheckboxName)
{
	formContainer = eval("document." + FormName);
	len = formContainer.elements.length;
	for(i = 0 ; i < len; i++)
	{
		ele = formContainer.elements[i];
		var elename = ele.name
		elename = elename.substring(0,CheckboxName.length)
		if(elename == CheckboxName)
		{
			if(ele.checked == true)
				return true;
		}
	}
	return false;
}

function createRadioButton(bIsColumn, strControl, strSlot, strAddress)
{
//	var strCBName = "cbGroup" + strGroup + strText;

/*	if(bIsColumn)
		if(iRCount > 0)
			return;
		else
			iRCount ++;
	else if(iCCount > 0)
		return;
	else
		iCCount ++;
*/
		
	var strCBName = "cbGroupSelect";
	var strOut = "";
	strOut += "<INPUT TYPE=\'CheckBox\' VALUE=\'ON\' ";
	strOut += " name=\'" + strCBName + "\' ";
	if(bIsColumn)
		strOut += "onclick = 'selectColumn(";
	else
		strOut += "onclick = 'selectRow(";
	strOut += " this.form,\"" + strControl + "\"";
	strOut += ", \"" + strSlot + "\",\"" + strAddress + "\"";
	strOut += ",this.checked ";
	strOut += ");\'";
	strOut += " >";

	document.write(strOut);

}

function selectValue(formName, fieldName, value)
{
	comboBox = eval("document."+formName+"."+fieldName);

	if(comboBox != null)
	{
		len = comboBox.options.length;
		for(i=0;i<len;i++)
		{
			val = comboBox.options[i].value;
			if(val == value)
			{
				comboBox.options[i].selected = true;
				return;
			}
		}
	}
}

function selectRadio(formName, radioName, value)
{
	containerForm = eval(formName);
	for(i=0;i<containerForm.elements.length;i++)
	{
		ele = containerForm.elements[i];
		if(ele.name == radioName && ele.value == value)
		{
			ele.checked = true;
			return;
		}
	}
}



//Set Selected value in appropriate Combo
function fselected(sColumn,sId,sName)
{
	nLength = eval("document.forms[0]."+sColumn+".length") ;
	eval("document.forms[0]."+sColumn+".options[nLength] = new Option(sName,sId)") ;
	eval("document.forms[0]."+sColumn+".value = \""+sId+"\"") ;
}

function ftoupper(objId,e)
{
	if (event.keyCode >= 65 && event.keyCode <= 90)
	{
		sId = objId.value;
		objId.value = sId.toUpperCase() ;
	}
}


//displays current date as Sunday, January 01, 2001.
function getDate()
{
	var day = "";
	var month = "";

	today = new Date();
	iDate = today.getDate();		//0 - 31
	iDay = today.getDay();			//index of weekday
	iMonth = today.getMonth();		//month no.
	iYear = today.getYear();		//year

	switch(iDay)
	{
		case 0:		day = "Sunday";			break;
		case 1:		day = "Monday";			break;
		case 2:		day = "Tuesday";		break;
		case 3:		day = "Wednesday";		break;
		case 4:		day = "Thursday";		break;
		case 5:		day = "Friday";			break;
		case 6:		day = "Satureday";		break;
	}
	switch(iMonth)
	{
		case 0:		month = "January";		break;
		case 1:		month = "February";		break;
		case 2:		month = "March";		break;
		case 3:		month = "April";		break;
		case 4:		month = "May";			break;
		case 5:		month = "June";			break;
		case 6:		month = "July";			break;
		case 7:		month = "August";		break;
		case 8:		month = "September";	break;
		case 9:		month = "October";		break;
		case 10:	month = "November";		break;
		case 11:	month = "December";		break;
	}
	return(day + ', ' + month + ' ' + iDate + ', ' + iYear);
}

//displays current time as 10.25 pm
function getTime()
{

	var ampm="";
	var ampmhour="";
	var myminutes="";


	iHours = today.getHours();
	ampm = (iHours >= 12) ? 'pm' : 'am';
	iHours = (iHours > 12) ? iHours - 12 : iHours;

	iMinutes = today.getMinutes();
	iMinutes = ((iMinutes < 10) ? ':0' : ':') + iMinutes;
	
	return(iHours + iMinutes + ' ' + ampm);
}

/*-----------------------------------------------------------
Creator : Biraj(27-March-2002)
purpose : function checks maximum no. of characters u can enter in a textarea
Arguments : 
	oFrmName : form name (eg.this)
	nLength : No. of characters u want to restrict
return :	true/false
*/
function fTACheckLength(oFrmName,nLength)
{	
	for (i=0;i<oFrmName.elements.length;i++)
	{	
		if(oFrmName.elements[i].type=="textarea")
		{
			nLen=oFrmName.elements[i].value.length;			
			if(parseInt(nLen) > parseInt(nLength))
			{	
				alert("Enter maximum " + nLength + " characters only");
				oFrmName.elements[i].focus();
				return false;
			}			
		}
	}
	return true;
}

/*-----------------------------------------------------------
Creator : Biraj(2-April-2002)
purpose : function adds an item as first item from left to right
Arguments : 
	oListBoxOnRight : name of listbox on right
	sNewText : text of a string which u want to add from left to right
	sNewValue : value of a string which u want to add from left to right
return :	nothing
*/
function fAddFirst(oListBoxOnRight,sNewText,sNewValue)
{
	iLen = oListBoxOnRight.length;

	if(iLen>0)
	{
		sText = oListBoxOnRight[iLen-1].text;
		sVal = oListBoxOnRight[iLen-1].value;
		oListBoxOnRight.options[iLen] = new Option(sText,sVal);
		
		for(iLST=iLen-1;iLST>=0;iLST--)
		{
			if(iLST==0)
			{
				oListBoxOnRight[iLST].text = sNewText; 
				oListBoxOnRight[iLST].value = sNewValue; 
				break;
			}
			
			oListBoxOnRight[iLST].text = oListBoxOnRight[iLST-1].text ; 
			oListBoxOnRight[iLST].value = oListBoxOnRight[iLST-1].value ; 
		}
	}
	else	
		oListBoxOnRight.options[iLen] = new Option(sNewText,sNewValue);
		
}

/*-----------------------------------------------------------
Creator : Biraj(10-April-2002)
purpose : function shows message to admin 
Arguments : None
return :	nothing
*/
function fMessage(len)
{
	if(len==1)
		return true;
		
	var msg = "Only first selected value will be saved.\n";
	msg += "Do you want to continue?"
	if(confirm(msg))
		return true;
	else
		return false;	
}

/*-----------------------------------------------------------
Creator   : Umesh(19-April-2002)
purpose   : function Set the focus on first control
Arguments : name of object
return    :	nothing
*/
function SetFocus(element)
{
	if(element)
	{
		element.focus();
	}	
}
//************* check for valid numeric strings	*************************
function chkNumeric(strString)
{
	var strValidChars = "0123456789.-";
  var strChar;
  var blnResult = true;

  //if (strString.length == 0) return false;
	//  test strString consists of valid characters listed above
  for (i = 0; i < strString.length && blnResult == true; i++)
  {
		strChar = strString.charAt(i);
    if (strValidChars.indexOf(strChar) == -1)
    {
			blnResult = false;
    }
  }
  return blnResult;
}
//************* Conver Date in JAVA SCRIPT Format	*************************
// Date Format might be in mdy,dmy OR ymd it will return Date in JAVA SCRIPT FORMAT

function DateComponents(dateStr, format)
	{
		var results = new Array();
		var datePat;
		
		if(format.charAt(0)!="y"){
			datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;
		}
		else {
			datePat = /^(\d{1,4})(\/|-)(\d{1,2})(\/|-)(\d{1,2})$/;
		}
		
		var matchArray = dateStr.match(datePat);
		var rDate = new Date();

		if (matchArray == null) return null; 
		// parse date into variables
		if (format.charAt(0)=="d")
		{ //format=dd/mm 
			results[0] = matchArray[1];
			results[1] = matchArray[3];
			results[2] = matchArray[4];
		} 
		else if(format.charAt(0)=="y")
		{
				results[0] = matchArray[5];
				results[1] = matchArray[3]; 
				results[2] = matchArray[1];
		}
		else { 
				results[1] = matchArray[1];
				results[0] = matchArray[3]; 
				results[2] = matchArray[4];
				
		}
		
		rDate.setFullYear(results[2]);
		
		if(results[1]-1 == 1)
		{
			rDate.setDate(results[0]);
			rDate.setMonth(results[1]-1);
		}
		else
		{
			rDate.setMonth(results[1]-1);
			rDate.setDate(results[0]);
		}	
	
		return rDate;
}
