/*
**
@author       : K.Mahesh Kumar Patro
@version      :
@created date :
*
*/

/*
**
Function to compare two dates .
with seperators as '/' or '.' or '-'
*/
function checkDate(fromdate,todate)
{
		// First checks for token which is used for date .
		var fToken =  fromdate.indexOf("/") != -1 ? "/" : fromdate.indexOf(".")!= -1 ? "." : fromdate.indexOf("-")!=-1 ? "-" : "";
		var tToken =  todate.indexOf("/") != -1 ? "/" : todate.indexOf(".")!= -1 ? "." : todate.indexOf("-")!=-1 ? "-" : "";

		// splits the date for dd, mm, yyyy.
		var fdate = fromdate.split(fToken);
		var tdate = todate.split(tToken);
	
		fday = fdate[0];
		fmonth = fdate[1];
		fyear = fdate[2];

		tday = tdate[0];
		tmonth = tdate[1];
		tyear = tdate[2];

		if(fyear < tyear)
			return false;

		if(fmonth == tmonth && fyear == tyear && fday <= tday)
			 return false;
		
		if(fmonth < tmonth && fyear == tyear)
			 return false;

	return true;
}

/*
**
Function to check text field for valid Integer.
*/
function isValidNumeric(string)
{ 
    // Modify to check for decimal also - by order Vineeth Das
	var isError = false;
	for(i=0;i<string.length;i++)
	{	
		if(!(string.charCodeAt(i) > 45 && string.charCodeAt(i)<58) )
		{
			isError = true;
			break;
		}
	}
	if( !isError||string.length==0||string.charCodeAt(0)==32 || string.charCodeAt(0)==45)
		return true;
	else 
		return false;
}

/*
**
Function to check text field for valid Character data.
*/
function isValidChars(string)
{
	var isError = false;
	for(i=0;i<string.length;i++)
	{	
		if(! ((string.charCodeAt(i)>64&string.charCodeAt(i)<91) || (string.charCodeAt(i)>96&&string.charCodeAt(i)<123) || string.charCodeAt(i)==32||string.charCodeAt(i)==46||string.charCodeAt(i)==44))
		{
			isError = true;
			break;
		}
		
	}
	if(!isError||string.length==0||string.charCodeAt(0)==32)
		return true;
	else 
		return false;
}


/*
**
Function to check text field any pattern as 
specified in parmaeter.
*/
function isValidPattern(source, pattern)
{
	var isError = false;
	for(i=0;i<source.length;i++)
	{	
		if(pattern.indexOf(source.charAt(i))==-1)
		{
			isError = true;
			break;
		}
	}
	return !isError;
}

/*
**
Function to check text field for valid alpha-numeric data.
*/
function isValidAlphaNumeric(string)
{
	var isError = false;
	for(i=0;i<string.length;i++)
	{
	  if(!((string.charCodeAt(i)>43&&string.charCodeAt(i)<60)||(string.charCodeAt(i)>63&&string.charCodeAt(i)<93)||(string.charCodeAt(i)>94&&string.charCodeAt(i)<123)))
		{
			isError = true;
			break;
		}
	}
	if(!isError||string.length==0||string.charCodeAt(0)==32)
		return true;
	else 
		return false;
}	

/**
*	Function to check text field for null data.
*/
function isNull(source)
{
	while(source.indexOf(" ")!= -1)
		source = source.replace(" ","");
	if(source.length == 0)
		return true;
	else
		return false;
}

/**
*  Function to check valid E-mail ID.
*/
function isValidEmail(cvalue)
{
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(cvalue))
		return true
	else 
	{
		return false
	}
}


/**
*  Function to format URL as in web site if 
   entry is incomplete.
*/
function formatURL(source)
{
	if(source.indexOf("http://") != -1)
	 return source
	else

	 return source = "http://"+source;

}


/**
*	Function to check for valid phone number.
*/
function isValidPhoneNO(phoneNo)
{
	var leftPara=0;
	var rightPara=0;
	var isError = false;
	var	isvalid = isValidPattern(phoneNo,"0123456789-()");
	alert(isvalid)
	if(isvalid)
	{
		while( leftPara!=-1 || rightPara != -1)
		{
		alert(leftPara)
		alert(rightPara)
		  if(leftPara>rightPara)
		   {
			isError = true;
			break;
		   }
		  leftPara = phoneNo.indexOf("(",leftPara);
		  rightPara = phoneNo.indexOf(")",rightPara);
		}
	}
	if(isvalid == true && isError == false)
		return true;
	else
		return false

/*	 if (/^\d+[-]?[\(,\d+,\)]*$/.test(phoneNo))
		return true
	else 
	{
		return false
	}
*/
}

/**
*  Function to format Text entered in textarea.
-- To be generalised to include any replacement string.
*/
function formatText(text)
{
    string = text.split("\r"); 
    temp=""		      
   for( i=0;i!=string.length;i++)
	temp += string[i].replace("\n","\\n");
   return temp;
}

/**  This method is for radio button or check box. This method check any one radio button or check 
  *  box is selected or not. 
  *  returns true if any one checked or selected and returns false for not selecting.
  */
function isOneChecked(object1)
	{
	  var valid = false;
	  if(object1.length == null)
	  {
	     return object1.checked == true ? true: false;
	  }
	  else
	  {	  
	     for(var i=0; i < object1.length ;i++)
	     {
			if(object1[i].checked == true)
			{ valid = true;
			  break;
			}
	     }
	  }
	  return valid;
	}

/**  This method is for radio button or check box. This method check any one radio button or check 
  *  box is selected or not. 
  *  returns value of checked or selected control and returns false for not selecting.
  */
function whichIsChecked(object1)
	{
	  var valid = false;
	  if(object1.length == null)
	  {
	     return object1.checked == true ? object1.value : false;
	  }
	  else
	  {	  
	     for(var i=0; i < object1.length ;i++)
	     {
			if(object1[i].checked == true)
			{ valid = object1[i].value;
			  break;
			}
	     }
	  }
	  return valid;
	}

/**
 *  Function to check or uncheck all according to one check box.
 */
function doCheckAll(destObj, sourceObj)
	{
		if(destObj.length != null)
		     for(var i=0; i<destObj.length; i++)
		        destObj[i].checked = sourceObj.checked;
		else
		   destObj.checked = sourceObj.checked;
	}


function getArray(fromdate)
{
		// First checks for token which is used for date .
		var fToken =  fromdate.indexOf("/") != -1 ? "/" : fromdate.indexOf(".")!= -1 ? "." : fromdate.indexOf("-")!=-1 ? "-" : "";
		return fromdate.split(fToken); ;
}

// Assigns one from source list to destination list
	function assign(source,destination)
	{
	   if(source.selectedIndex!= -1 )
	   {
	       var text = source[source.selectedIndex].text;
	       var value = source[source.selectedIndex].value;

	      destination[destination.length] = new Option(text,value,0,0);
	      source[source.selectedIndex] = null;
	   }
	}
// Assigns all from source list to destination list
	function assignAll(source,destination)
	{
	       var text = "";
	       var value = "";
	       for(var i=0; i<source.length;i++)
	       {
		text = source[i].text;
		value = source[i].value;
	        destination[destination.length] = new Option(text,value,0,0);
		}
	     for(var i= source.length; i>=0;i--)
		source.options[i] = null;

	}
// Moves selected option to words top. 
	function moveUp(object)
	{
	  var sourceText, sourceValue;
	  var selIndex = object.selectedIndex;
	  if( selIndex!=-1 && selIndex!=0)
	  {
	     sourceText = object[selIndex].text ;
	     sourceValue = object[selIndex].value ;
	     object[selIndex].text = object[selIndex-1].text ;
	     object[selIndex].value = object[selIndex-1].value;
	     object[selIndex-1].text = sourceText;
	     object[selIndex-1].value = sourceValue;
	     object.selectedIndex =  selIndex-1;0
	  }
	}
// Moves selected option to top. 
	function moveToTop(object)
	{
	  var sourceText, sourceValue;
	  var selIndex = object.selectedIndex;
	  
	  if( selIndex!=-1 && selIndex!=0)
	  {
	   for(var i=selIndex; i>0;i--)
	   { 
	      sourceText = object[i].text ;
	      sourceValue = object[i].value ;
	      object[i].text = object[i-1].text ;
	      object[i].value = object[i-1].value;
	      object[i-1].text = sourceText;
	      object[i-1].value = sourceValue;
	      object.selectedIndex =  i-1;
		}
	  }
	}

// Moves selected option to words down. 
	function moveDown(object)
	{
	 
	  var sourceText, sourceValue;
	  var selIndex = object.selectedIndex;
	  var len  = object.length-1;

	  if( selIndex!=-1 && selIndex!= len )
	  {
  	     sourceText = object[selIndex].text ;
	     sourceValue = object[selIndex].value ;

	     object[selIndex].text = object[selIndex+1].text ;
	     object[selIndex].value = object[selIndex+1].value;

	     object[selIndex+1].text = sourceText;
	     object[selIndex+1].value = sourceValue;
	     object.selectedIndex =  selIndex+1;
	  }
	}
	
// Moves selected option to down. 
	function moveToBottom(object)
	{
	 
	  var sourceText, sourceValue;
	  var selIndex = object.selectedIndex;
	  var len  = object.length-1;

	  if( selIndex!=-1 && selIndex!= len )
	  {
	   for(var i=selIndex; i<len;i++)
	     {
  	      sourceText = object[i].text ;
	      sourceValue = object[i].value ;

	      object[i].text = object[i+1].text ;
	      object[i].value = object[i+1].value;

	      object[i+1].text = sourceText;
	      object[i+1].value = sourceValue;
	      object.selectedIndex =  i+1;
	    }//endfor 
	  }// endif
	}//end of method

function selectAllOfList(source)
{
	for(var i=0 ; i<source.length;i++)
	source[i].selected = true;
}
/*
	 * expects "source" as a comma separated string and "what" as a text wheather 
	 * "what" is in source or not.
	 */
	function isThere(source, what)
	{
		var arry = source.split(",")
		for(var i=0;i<arry.length;i++)
		 if(arry[i] == what)
			 return true;
	 return false;
	}