// JScript source code ************** by G.Srividya ****************
function IsValid(opt,obj,msg,name)
//this script is must for calling all the other functions
//for example 	if (IsValid('S',txtEmail,'Email must be specified',"Email")==false)
//where 'S' is to check for blank fields  txtEmail is the field name or object msg is the msg to be displayed 
//and name is the name of the Field for display
{
	var pat
	if (opt == "N")
		pat = /^[a-z .]*$/i
	if ((opt == "P") || (opt == "F")) // Phone and Fax
	{
		pat = /^([0-9]{3})[0-9]{3}-[0-9]{4}$/
		if (pat.test(obj.value))
		{
			return true
		}
		else
		{
		alert("Enter valid "+name+", "+name+" should be in the format (999)999-9999")
		obj.focus()
		obj.select()
		return false
		}
	}	
		
	if (opt == "C")
		pat = /^[a-zA-Z0-9 ]*$/i  //pattern for alphanumeric
	if (opt == "Z") // Zip Code
	{
		pat = /^[0-9]{5}$/
		pat1 = /^[0-9]{5}-[0-9]{4}$/
		if (pat.test(obj.value) || pat1.test(obj.value))
		{
			return true
		}
	else
		{
		alert("Enter A Valid "+name+", "+name+" should be in format 99999 or 99999-9999")
		obj.focus()
		obj.select()
		return false
		}
	}
	
	if (opt == "NM") // Numeric values are only allowed
	{
		if (isNaN(obj.value))
		{
			alert("Enter Valid " +name+ ", " +name+ " should be numeric")
			obj.focus()
			obj.select()
			return false
		}
		else
			return true	
	}		
	if (opt == "NNM") // Non numeric make is valid ==true for this
	{
		if (isNaN(obj.value))
		{
			alert("Enter Valid " +name+ ", " +name+ " should not contain numeric")
			obj.focus()
			obj.select()
			return true
		}
		else
			return false	
	}			
	if (opt == "S") // Check for Blank fields
	{
		var pat = new RegExp("^\[ \t]*$");
		if (pat.test(obj.value))
		{
			alert(name +" Cannot be blank")
			obj.focus()
			obj.select()
			return false
		}
	else
	{
		return true
	}
	}	
	

	if (opt == "CH") // Check for Alphabets With space inbetween words
	{
		var pat = new RegExp("^[a-zA-Z \b]+$");
		if (pat.test(obj.value))
		{
			return true
		}
		else
		{
			alert("Enter a valid "+name+", "+name+" should be alphabets")
			obj.focus()
			obj.select()
			return false
		}
	}
	if (opt == "SH") // Check for Alphabets With space inbetween words
	{
		var pat = new RegExp("^[a-zA-Z \b*&.!@#$%^&*()]+$");
		if (pat.test(obj.value))
		{
			return true
		}
		else
		{
			alert("Enter a valid "+name+", "+name+" should be alphabets")
			obj.focus()
			obj.select()
			return false
		}
	}
	function OpenPreview(userId)//preview the selected image in anothere iexplore
		{
			var dFeatures;
			dFeatures="width=450,height=250,left=200,top=200";
						
       		var ImagePreview=window.open("ImagePreview.aspx?userId="+userId,"ImagePreview",dFeatures);
			ImagePreview.focus();
			return false;
		}
		function CheckIt(X,ABC)  // removes the leading blank space if any (for example) 
		//onchange="CheckIt(this,this.value)" 
		//calling it in a onchange event of a textbox will remove leading white spaces if any
		{ 
				a=0; 
				FL=ABC.slice(0,1); 
				LL=ABC.slice(ABC.length-1,ABC.length); 
				if(FL==" "){ABC=ABC.slice(1,ABC.length);a=1;} 
				if(LL==" "){ABC=ABC.slice(0,ABC.length-1);a=1;} 
				X.value=ABC; 
				if(a!=0){CheckIt(X,ABC)} 
		} 

		function comparedate(firstdate,seconddate)//comparing the dates by spliting the days month and year
		{
			
			var farray=firstdate.split("/");
			var sarray=seconddate.split("/");
			
			
			var day1=farray[1];
			var month1=farray[0];
			var year1=farray[2].substr(0,4);
			
			var day2=sarray[1];
			var month2=sarray[0];
			var year2=sarray[2].substr(0,4);
			
			
			var date1=new Date(year1,month1,day1);
			var date2=new Date(year2,month2,day2);
						
			var datediff = date1 - date2;
			
			return datediff;
					
		}
		
		

	if (opt == "CS") // Check for Alphabets without blank inbetween
	{
		var pat = new RegExp("^[a-zA-Z]+$");
		if (pat.test(obj.value))
		{
			return true
		}
		else
		{
			alert("Enter a valid "+name+", "+name+" should be alphabets")
			obj.focus()
			obj.select()
			return false
		}
	}
	
	if (opt == "EM") // Email Validation
	{
		//pat = /^[a-z][a-z_0-9\.]+@[a-z_0-9\.]+\.[a-z]*$/i	
		pat = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	}
	if (pat.test(obj.value))
		return true
	else
	{
		alert(msg)
		obj.focus()
		obj.select()
		return false
	}							
}	


/*function checkemail(str)
{
var testresults
var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
if (filter.test(str))
testresults=true
else{
alert("Enter a valid email address!")
testresults=false
}
return (testresults)
}



function checkbae(str)//call this function to check email id does not allow 2 Continuous dots(.)
{
if (document.layers||document.getElementById||document.all)
return checkemail(str)
else
return true
}*/



  function round(obj) //round(obj) rounding a decimal number
  {
	 var num
	 var index,len
	 if(trim(obj.value) !="")
	 {
	 if (IsValid("NM",obj,"Enter numbers only",""))
	 {	
	 
		num = (Math.round(parseFloat(obj.value) * 100 ))/100
		num = num.toString()
		index = num.indexOf(".")
		len = num.length
		if (index == -1)
			num = num + ".00"
		else
		{
			index++
			if (len - index == 1)
				num = num + "0"
		}			
		obj.value = num
		return true			
	}
	else	
		return false
	}
  }	

// Round with Decimal Values
function round_decimals(original_number, decimals)
{
    var result1 = original_number * Math.pow(10, decimals)
    var result2 = Math.round(result1)
    var result3 = result2 / Math.pow(10, decimals)
    return pad_with_zeros(result3, decimals)
}

function pad_with_zeros(rounded_value, decimal_places) {

    var value_string = rounded_value.toString()
    var decimal_location = value_string.indexOf(".")
    if (decimal_location == -1) {
        decimal_part_length = 0
        value_string += decimal_places > 0 ? "." : ""
    }
    else {
        decimal_part_length = value_string.length - decimal_location - 1
    }
    var pad_total = decimal_places - decimal_part_length
    
    if (pad_total > 0) {
        for (var counter = 1; counter <= pad_total; counter++) 
            value_string += "0"
        }
    return value_string
}
// End Round with Decimal Values
  
  function isDate(obj) // Date Validation
  {
	var pat
	var mon,yr,days
	pat = /^[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4}$/
	val = obj.value
	indmon = val.indexOf('/')
	mon = val.substring(0,indmon)
	indyr = val.lastIndexOf('/')
	day = val.substring(indmon+1,indyr)
	yr = val.substring(indyr+1,val.length )
	if (pat.test(obj.value))
	{
		if ((mon > 12) || (mon < 1))
		{
			alert("Month should be between 1 and 12")
			obj.focus()
			obj.select()
			return false
		}
		else if ((day > 31) || (day < 1)) 
		{
			alert("Day should be between 1 and 31")
			obj.focus()
			obj.select()
			return false
		}	
		else if (((mon == 2) || (mon == 4) || (mon == 6) || (mon == 9) || (mon == 11)) && (day > 30)) 
		{
				alert("Enter the correct date!!")
				obj.focus()
				obj.select()
				return false
		}
		else if (mon == 2)
		{
			if ((yr%4==0) && (yr%100 !=0 || yr%400 == 0))
				maxdate = 29
			else
				maxdate = 28
			if (day > maxdate)
			{		 
				alert("Enter the correct date!!")
				obj.focus()
				obj.select()
				return false
			}
		}	
//		else
//		{
			if (mon < 10)
			{
				mon = mon.toString()
				if (mon.length < 2)
				{
					mon = "0" + mon
					obj.value = mon+"/"+day+"/"+yr
				}	
				
			}
			if (day < 10)
			{
				day = day.toString()
				if (day.length < 2)
				{
					day = "0" + day
					obj.value = mon+"/"+day+"/"+yr
				}	
			}
			
			return true	
//		}				
	}
	else
	{
		alert("Enter date in mm/dd/yyyy format")
		obj.focus()
		obj.select()
		return false
	}		
  }
  	/*if (opt == "A")
	{
		var pat = new RegExp("^[\(\)0-9\- ]+$");
		if (pat.test(obj.value))
		{
			return true
		}
		else
		{
			alert("Invalid data. "+name+" should be xxx-xxx-xxxx")
			obj.focus()
			obj.select()
			return false
		}
	}*/


/*if (obj.value != "")
		{
			return true
		}
		else
		{
			alert("Invalid data. "+name+" should not be empty")
			obj.focus()
			obj.select()
			return false
		}*/
		
function checkAll(intValue,formName)   ///check all check boxes
{	 form = eval("document." + formName)	 
	with(form)
	{
		if(chkDelete.type=="checkbox")
		{
			chkDelete.checked=intValue
		}
		else
		{
			var intLength;
			intLength = chkDelete.length;
			for(intCount=0;intCount<intLength;intCount++)
			{
				chkDelete[intCount].checked=intValue
			}
		}
	}
}

function FormatNumber_IBI(num,decimalNum,bolLeadingZero,bolParens,bolCommas)

{ 
        if (isNaN(parseInt(num))) return "0";

	var tmpNum = num;
	var iSign = num < 0 ? -1 : 1;		// Get sign of number
	
	// Adjust number so only the specified number of numbers after
	// the decimal point are shown.
	tmpNum *= Math.pow(10,decimalNum);
	tmpNum = Math.round(Math.abs(tmpNum))
	tmpNum /= Math.pow(10,decimalNum);
	tmpNum *= iSign;					// Readjust for sign
	
	
	// Create a string object to do our formatting on
	var tmpNumStr = new String(tmpNum);

	// See if we need to strip out the leading zero or not.
	if (!bolLeadingZero && num < 1 && num > -1 && num != 0)
		if (num > 0)
			tmpNumStr = tmpNumStr.substring(1,tmpNumStr.length);
		else
			tmpNumStr = "-" + tmpNumStr.substring(2,tmpNumStr.length);
		
	// See if we need to put in the commas
	if (bolCommas && (num >= 1000 || num <= -1000)) {
		var iStart = tmpNumStr.indexOf(".");
		if (iStart < 0)
			iStart = tmpNumStr.length;

		iStart -= 3;
		while (iStart >= 1) {
			tmpNumStr = tmpNumStr.substring(0,iStart) + "," + tmpNumStr.substring(iStart,tmpNumStr.length)
			iStart -= 3;
		}		
	}

	// See if we need to use parenthesis
	if (bolParens && num < 0)
		tmpNumStr = "(" + tmpNumStr.substring(1,tmpNumStr.length) + ")";

	return tmpNumStr;		// Return our formatted string!
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

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 MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

