var dtCh= "/";
var minYear=1900;
var maxYear=2100;

//var maxYear = Date();
//maxYear = maxYear.substring(maxYear.length,maxYear.length-4);

//var maxYear=d

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function showPopupWindow(strPageName, strPageTitle, intWindowHeight, intWindowWidth)
{
	var lintWindowTopPosition = eval(screen.height - intWindowHeight) / 2;
	var lintWindowLeftPosition = eval(screen.width - intWindowWidth) / 2;
	strPageTitle = replaceAll(strPageTitle,' ','');
	return  window.open(strPageName,strPageTitle,"top=" + lintWindowTopPosition + ",left=" + lintWindowLeftPosition + ",height=" + intWindowHeight + ",width=" + intWindowWidth + ",toolbar=none,scrollbars=1");
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function dateKeyPress(objInput)
{ 
	if((event.keyCode >= 48 && event.keyCode <=59) || (event.keyCode==45) || (event.keyCode==13))
		return true;
	else 
		return false;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)	
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : MM/DD/YYYY")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
return true
}



function isalphanum(obj)
{	
	if(obj.value.search(/[^0-9^a-z^A-Z]/) != -1)  
	{
		//alert('Value should be alphanumeric.');
		//obj.value="";
		obj.focus();
		return false;			
	}
	return true;
}


function istext(obj)
{	
	if(obj.value.search(/[^0-9^a-z^A-Z]/) != -1)  
	{
		alert('Value should be in chararacters.');
		//obj.value="";
		obj.focus();
		return false;			
	}
	return true;
}


function trim(strComp)
{
	ltrim = /^\s+/
	rtrim = /\s+$/
	strComp = strComp.replace(ltrim,'');
	strComp = strComp.replace(rtrim,'');
	return strComp;
}


function validateZIP(field)
{
var valid = "0123456789-";
var hyphencount = 0;

if (field.length!=5 && field.length!=10) {
alert("Please enter your 5 digit or 5 digit+4 zip code.");
return false;
}
for (var i=0; i < field.length; i++) {
temp = "" + field.substring(i, i+1);
if (temp == "-") hyphencount++;
if (valid.indexOf(temp) == "-1") {
alert("Invalid characters in your zip code.  Please try again.");
return false;
}
if ((hyphencount > 1) || ((field.length==10) && ""+field.charAt(5)!="-")) {
alert("The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'.   Please try again.");
return false;
   }
}
return true;
}



function isZip(field)
{
var valid = "0123456789-";
var hyphencount = 0;

if (field.length!=5 && field.length!=10) {
alert("Please enter your 5 digit or 5 digit+4 zip code.");
return false;
}
for (var i=0; i < field.length; i++) {
temp = "" + field.substring(i, i+1);
if (temp == "-") hyphencount++;
if (valid.indexOf(temp) == "-1") {
alert("Invalid characters in your zip code.  Please try again.");
return false;
}
if ((hyphencount > 1) || ((field.length==10) && ""+field.charAt(5)!="-")) {
alert("The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'.   Please try again.");
return false;
   }
}
return true;
}



		function isNumber(obj)
			{
				if(obj.value.search(/[^0-9^.^ ]/) != -1 || obj.value.indexOf(".")!=obj.value.lastIndexOf(".") )
				{
					
					//obj.value="";
					obj.focus();
					return false;			
				}
				 
				
				return true;
				
			}
		
		function isEmail(obj)
		{
			var flg = true;
			var index = obj.value.indexOf("@");
			var spc = obj.value.indexOf(" ");
			if(spc == -1)
			{
				if (index > 0)
				{
						var pindex = obj.value.indexOf(".",index);
						if (!(pindex > index+1) && (obj.value.length > pindex+1))
			    			{
								alert("Please Enter valid e-mail i.e 'xyz@aol.com'");
							    flg=false;
							}	
			  	}
			 	else
			  	{	
					   alert("E-mail must have '@'. Please Enter valid e-mail.");
					   flg=false;
				}
			}
			else
			{
		     	 alert("Space is not allowed in E-mail Address");
			     flg=false;
			}	
			if(flg)
			{
				SingleQuote = obj.value.indexOf("'");
				if(SingleQuote!= -1)
				{
					alert("Single Quote is not allowed in E-mail Address");
					flg=false;
				}
			}
			if(!flg)
			{
			//	obj.value='';
				obj.focus();
				return false;
			}
		     return true;
		}
		
		
		function isUsername(obj)
		{	
				
			if(obj.value.search(/[^0-9^a-z^A-Z^?^.^'^"^ ]/) != -1)  
			{
					//obj.value="";
					obj.focus();
					return false;			
			}
			 
				   return true;
			
		}
		
		function SSNValidation(ssn)
		{
			var matchArr = ssn.match(/^(\d{3})-?\d{2}-?\d{4}$/);
			var numDashes = ssn.split('-').length - 1;
			if (matchArr == null || numDashes == 1)
				{
				alert('Invalid SSN. Must be 9 digits or in the form NNN-NN-NNNN.');
				//msg = "does not appear to be valid";
				return false;
				}
			else 
			if (parseInt(matchArr[1],10)==0) 
			{
				alert("Invalid SSN: SSN's can't start with 000.");
				//msg = "does not appear to be valid";
				return false;
			}
			else 
			{
				//msg = "appears to be valid";
				//alert(ssn + "\r\n\r\n" + msg + " Social Security Number.");
				return true;
			}
		}
		/**********************************************************
 * This function is used for debugging purpose
 **********************************************************/
function displayProperties(obj) {
	var str = ""; 	var arrProp = new Array();
	var prop;
	var arrIndex = 0;

	if(typeof(obj) == "undefined"){
		alert('displayProperties: Parameter is not an object!');
		return false; 	
	}
	
	if(typeof(obj[0]) != "undefined") {
		for(prop = 0;prop < obj.length;prop++) {
			str += "\n" + prop + "=" + obj[prop];
			arrProp[arrIndex++] = "\n" + prop + "=" + obj[prop];
		}
	}else{
		for(prop in obj){
			str += "\n" + prop + "=" + eval("obj." + prop);
			arrProp[arrIndex++] = "\n" + prop + "=" + obj[prop];
		}
	}
	alert(arrProp.sort()); 
}


function displayProperties(obj) {
	var str = ""; 	var arrProp = new Array();
	var prop;
	var arrIndex = 0;

	if(typeof(obj) == "undefined"){
		alert('displayProperties: Parameter is not an object!');
		return false; 	
	}
	
	if(typeof(obj[0]) != "undefined") {
		for(prop = 0;prop < obj.length;prop++) {
			str += "\n" + prop + "=" + obj[prop];
			arrProp[arrIndex++] = "\n" + prop + "=" + obj[prop];
		}
	}else{
		for(prop in obj){
			str += "\n" + prop + "=" + eval("obj." + prop);
			arrProp[arrIndex++] = "\n" + prop + "=" + obj[prop];
		}
	}
	alert(arrProp.sort()); 
}

function CheckFirstChar(obj)
{
	if(typeof(obj) != "undefined" && obj.value!="")
	{
		var Value = obj.value
		Value = Value.substring(0,1);
		var AlphaNumericFilter =/^[a-zA-Z0-9]*$/
		if (!AlphaNumericFilter.test(Value))
			return false;
		return true;	
	}
	else
		return false;
}

