function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function



function MM_validateForm() { //v4.0

  var i,p,q,noFill,err_msg,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  
  for (i=0; i<(args.length-2); i+=3) { 
  	err_msg=args[i+1];test=args[i+2]; 
	val=MM_findObj(args[i]);
	val1=MM_findObj(args[i]);
	    if (val) { 
			makeSafe(val); 
			nm=val.name; 
			if ((val=val.value)!="") {
				if (test.indexOf('inLength') != -1) { 
					p=test.indexOf(':');
         				min=test.substring(9,p); 
						max=test.substring(p+1);
         				if (val.length<min || max<val.length){
						errors+='- '+err_msg+'.\n';
						val1.style.background='#ffff99';
						}
   				} 
				else if (test.indexOf('isChar')!=-1) { 
					CharRet=isLetter(val1);
					if(CharRet != true){ 
						errors+='.\n';
						val1.style.background='#ffff99';
					}
				}
				else if (test.indexOf('chkBlank')!=-1) { 
					CharRet=trim(val1.value);
					if(CharRet.length == 0){ 
						errors+='- '+err_msg+'.\n';
						val1.style.background='#ffff99';
				 	}
					//CharVal=val.indexOf("-");
					//if(CharVal != -1){ 
						//errors+='- '+err_msg+'.\n';
						//val1.style.background='#ffff99';
					//}
					CharVal1=val.indexOf(">");
					if(CharVal1 != -1){ 
						errors+='- '+err_msg+'.\n';
						val1.style.background='#ffff99';
					}
					CharVal2=val.indexOf("<");
					if(CharVal2 != -1){ 
						errors+='- '+err_msg+'.\n';
						val1.style.background='#ffff99';
					}
				}
				else if (test.indexOf('isSpl')!=-1) { 
					CharVal1=val.indexOf(">");
					if(CharVal1 != -1){ 
						errors+='- '+err_msg+'.\n';
						val1.style.background='#ffff99';
					}
					CharVal=val.indexOf("-");
					if(CharVal != -1){ 
						errors+='- '+err_msg+'.\n';
						val1.style.background='#ffff99';
					}
				}
				else if (test.indexOf('isDate')!=-1) { 
					DateRet=validate(val1); 
					if(DateRet != "OK"){ 
						errors+='- '+DateRet+'.\n';
						val1.style.background='#ffff99';
					}
				}else if (test.indexOf('isEmail')!=-1) { 
					p=val.indexOf('@');
					p1=val.indexOf('.');
	        		if (p<1 || p==(val.length-1)||p1<1 || p1==(val.length-1)){
						errors+='- '+err_msg+'.\n';
						val1.style.background='#ffff99';
					}
	      		}else if (test.indexOf('isNum')!=-1 || test.indexOf('inRange') != -1) { 
						num = parseFloat(val);
	        			if (isNaN(val)){
							errors+='- '+err_msg+'.\n';
							val1.style.background='#ffff99';
						}
	        			if (test.indexOf('inRange') != -1) { 
							p=test.indexOf(':');
	          				min=test.substring(8,p); 
							max=test.substring(p+1);
	          				if (num<min || max<num){
								errors+='- '+err_msg+' must contain a number between '+min+' and '+max+'.\n';
								val1.style.background='#ffff99';
							}
	    				} 
			} 
		}else if (test.charAt(0) == 'R') {	
			errors += '- '+err_msg+'.\n';val1.style.background='#ffff99';
		}
	}
 } 
  if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
} 


function validate(item)
{
dtflag="OK"
temp = new String(item.value);
var mm = temp.substring(0,2);
var dd = temp.substring(3,5);
var yy = temp.substring(6,temp.length);

var itemname="";
var strMonthArray = new Array("31","28","31","30","31","30","31","31","30","31","30","31");
var strleapArray = new Array("31","29","31","30","31","30","31","31","30","31","30","31");
intmonth = parseInt(mm, 10);
intdays = parseInt(dd, 10);
intyear = parseInt(yy, 10);
	
var test1=isNaN(dd);
var test2=isNaN(mm);
var test3=isNaN(yy);

if(temp.length==10)
{
	if ((intyear % 4) == 0 )
	 {
	 var intcheck  =parseInt((strleapArray[intmonth-1]),10)
	 }
	  
	else 
	 {
	  var intcheck  =parseInt((strMonthArray[intmonth-1]),10)
	 }
		
	if (intdays >intcheck )
	  {
	  
	  dtflag= "Sorry wrong date -- "+item.value;
	  }	
	if(test1==1)
	{	dateok=0;
		blankdate=1;
		dtflag= ("Sorry wrong date -- "+item.value);
	}
	else
	{
		if( (dd>31)||(dd<1))  
		{
		dateok=0;
		blankdate=1;
		dtflag=(dd+" is Invalid  Date -- "+item.value);
		}
	}
	if(test2==1)
	{
		dateok=0;
		blankdate=1;
		dtflag= ("Sorry wrong Month -- "+item.value);
	}
	else
	{
		if( (mm>12)||(mm<1))  
		{
			dateok=0;
			blankdate=1;
		dtflag=(mm+" -- is Invalid Month -- "+item.value);
		} 
	}
	if(test3==1)
	{
		dateok=0;
		blankdate=1;
		dtflag=("Sorry wrong Year -- "+item.value);
	}
	else
	{
   		if( (yy>2100)||(yy<1900))  
		{
			dateok=0;
			blankdate=1;
			dtflag= (yy+" is Invalid Year -- "+item.value);
		}
		else
		{
			dateok=1;
			blankdate=0;
		
		} 
	}
}
else
{
	if(temp.length>0)
	{
		dateok=0;
		dtflag= ("Date is not in proper format -- "+item.value);
		blankdate=1;
	
	}
	else
	{
		blankdate=0;
		dateok=1;

	}
}
// If evrything is clear
return dtflag;  
}

function dateComp()
{
	if (arguments[0] == null)
		return false;
		var date1=arguments[0];
	
	if (arguments[1] == null)
		tmpdate2=new Date();
	else
	{
		var date2 =arguments[1];		
		var dd2 = date2.substring(0,2);	
		var mm2 = date2.substring(3,5);
		var yy2 = date2.substring(6,date2.length);
		tmpdate2 = new Date(yy2,mm2-1,dd2);
	}
			 
	var dd1 = date1.substring(0,2);	
	var mm1 = date1.substring(3,5);
	var yy1 = date1.substring(6,date1.length);
	tmpdate1 = new Date(yy1,mm1-1,dd1);

	if (tmpdate1>tmpdate2)
	{
		return 1;
	}
	else if (tmpdate1<tmpdate2)
	{
		return -1;
	}
	else
	{
		return 0
	}
}

function isLetter(c)
{ 
	return (((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z"))|| ( c==" "))
}

function isDigit(c)
{ 
	return ((c >= "0") && (c <= "9"))
}

function startsWithWS(obj)
{
	contains = false;
	quotesRegExp = /^\s/;

	if( quotesRegExp.test(obj.value) )
	{
	contains = true;
	}
	return contains;
}

var msg=new Array("Please select unselected Field","Please input numbers without decimals ","Please enter characters only.","Please Do Not Leave Required Field Blank.","Please Donot Leave Required Field unselected.","Please Enter valid date Entries","Please Enter valid field values.The permissible value for this field is Number(0-9),Alphabets(a-z),comma(,),dot(.),hyphen(-)","Please Enter valid field values.The permissible value for this field is Number(0-9),Alphabets(a-z),comma(,),dot(.),hyphen(-),forward(/)","Please Input Numbers only.","Please Enter valid field values.The permissible value for this field is Number(0-9),Alphabets(a-z),hyphen(-),small Brackets()","The Form will not be Submitted without Consumer Category.","The Form will not be Submitted without Sub Consumer Category.","The Form will not be Submitted without Sub Sub Consumer Category.","The Form will not be Submitted without Tariff Category.","The Form will not be Submitted without Sub Tariff Category.","The Form will not be Submitted without Sub Sub Tariff Category.","Please Select Division Name.","Please Select Sub Station Name.","Please Select Feeder Name.","Please Select Consumer Name.","Please Select Bulk Consumer State. ","Please Select Bulk Consumer Name.","Please Enter valid field values.The permissible value for this field is Number(0-9),Alphabets(a-z),comma(,),dot(.),hyphen(-),forward(/) and Small Brackets()","Please Enter valid field values.The permissible value for this field is Alphabets(a-z) and hyphen(-)","Please Enter valid field values.The permissible value for this field is Numbers(0-9) and plus(+),comma(,) and dot(.)","Please Select Link Line Name.","Please Select Capacitor ID.","Please Select Rural Electrification Scheme 27","Please Select Repair And Renovation Scheme 28","Please Select Work Plan Financier Name.","Please Input valid field value.The permissible value for this field is Alphabets(a-z),hyphen(-)and forward(/).","Please Select Equipment Id (Equipment Type).","Please Enter The value Alpha Numeric.The permisible values are alphabet(a-z) and numbers(0-9)","Please Select Village Name.","Please Select Hamlet/Toka Name.","Please Select Capacitor Detail ID","Please Select Recorded Month and Financier Name For Data. ","Please Select District Name.","Please Select Recoreded Month and Year With Description of work.");

function sqlSafe(s)
{
	var new_s = s;
	new_s = replaceAll (new_s, "'", "`");
	new_s = replaceAll (new_s, "|", "`");
	new_s = replaceAll (new_s, "\"", "`");
	new_s = replaceAll (new_s, "|", "`");
	return new_s;
}

function makeSafe(i)
{
  i.style.background='#ffffff';
  i.value = sqlSafe(i.value);
}

function replaceAll(s,fromStr,toStr)
{
	var new_s = s;
	for (i = 0; i < 100 && new_s.indexOf (fromStr) != -1; i++)
	{
		new_s = new_s.replace (fromStr, toStr);
	}
	return new_s;
}

function textCounter(field, countfield, maxlimit) {
	if (field.value.length > maxlimit)
	{ 
		field.value = field.value.substring(0, maxlimit);
	}
	else 
	{
		countfield.value = maxlimit - field.value.length;
	}
}

