// JavaScript Document
//------------global variables start here-------
var w=window,d=document;
//this class is used to test given object,string,array
function Test(str){
	this.str=str || '';
	
	this.isEmpty=function (){
		if(this.str=='' || this.str==0 || this.str==null) 
		return true;
		else return false;
	}
	
	
	this.isLength=function(){
		if(this.isEmpty()) return !this.isEmpty();
		if(this.str.length>=this.mini && this.str.length<=this.maxi)
		return true;
		else return false;
		}
		
		
	this.setLength=function(mini,maxi){
	this.mini=mini;
	this.maxi=maxi;
	return true;
	}
	
	
	this.isEmail=function(){
	var emailFilter=/^.+@.+\..{2,3}$/;
	return (emailFilter.test(this.str)) && !this.isIllegals();
	}
	
	
	this.isIllegals=function(){
	var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/;
	if(this.str.match(illegalChars)) return true;
	else return false;
	}
	
	
	this.isAlNum=function(){
	var illegalChars = /[\W_]/;
	return !illegalChars.test(this.str)
	}
	
	
	this.isNumber=function(){
	var stripped = this.str.replace(/[\(\)\.\-\ ]/g, '');
	return  !isNaN(parseInt(stripped));
	}
	
	
	this.length=function(length){
		if(this.str.length==length)return true;
		else return false;
	}
	
	
	 this.isNull =function(theVar) {
	  if (typeof(theVar) == 'undefined') return true;
	  if (theVar == null) return true;
	  return false;
	}
	
	
	 this.isObject= function(theVar) {
		  ret = false;
		  if (typeof(theVar) == 'object') {
			ret = !this.isNull(theVar);
		  }
		return ret;
	}
	
	
	this.typeOf=function (theVar) {
  	ret = 'undefined';
 	 switch (typeof(theVar)) {
		case 'boolean':  ret = 'boolean';  break;
		case 'number':   ret = 'number';   break;
		case 'string':   ret = 'string';   break;
		case 'function': ret = 'function'; break;
		case 'object':
			  if (isNull(theVar)) {
				ret = 'null';
				break;
			  }
			  if (theVar.concat && theVar.join && theVar.sort && theVar.pop) { 
				ret = 'array';
				break;
			  }
			 break;
		case 'undefined':
		default:   
		  ret = 'undefined';
	  }
 	 return ret;
	}
	

}
//end of class.................
//start of validate class......................
Test.prototype.Validate=function(val,name,type){
	this.str=val;
	this.type=type || 'empty';
	this.errormsg='';
	
	
	switch(this.type){
		case 'empty': 
		if(this.isEmpty())
		this.errormsg+=name+" is empty.\n";
		break;
		
			case 'length': 
			this.setLength(3,45);
			if(this.isEmpty())
			this.errormsg+=name+" is empty.\n";
			else
			if(!this.isLength())
			this.errormsg+=name+" must have atleast 3 characters.\n";
			break;
		
		case 'phone':
		if(this.isEmpty())
		this.errormsg+=name+" is empty.\n";
		else if(!this.isNumber())
		this.errormsg+=name+" must be number.\n";
		break;
		
			case 'email':  
			if(this.isEmpty())
			this.errormsg+=name+" is empty.\n";
			else if(!this.isEmail())
			this.errormsg+=name+" is not a valid email.\n";
			break;
		
		case 'pwd': 
		this.setLength(5,45);
		if(this.isEmpty())
		this.errormsg+=name+" is empty.\n";
		else if(!this.isLength())
		this.errormsg+=name+" must have atleast 5 characters.\n";
		else if(this.isIllegals())
		this.errormsg+=name+" has illegal charcters.\n";
		break;
		default:this.errormsg=false;
		}
return this.errormsg;

}//end of validate class...................

/* uses of this class show n below
hi=new Test();
//hi.setLength(5,7);
alert(hi.Validate("rrrgg@g.co","name","email"));
below functions to test given date...
*/

Test.prototype.isDate=function(year, month, day){
	
if (year >= 1970) {
		
		var tDate = new Date(year, month -1, day);
		if (day   != tDate.getDate())               return false;
		if (month != (tDate.getMonth() +1))         return false;
		if (year  != this.fixYear(tDate.getYear())) return false;
	} else {
		//doing some manual checks
		if ((day   < 1)    || (day   > 31))   return false;
		if ((month < 1)    || (month > 12))   return false;
		if ((year  < 1000) || (year  > 3000)) return false;
	}
	//somehow even the js date object does not detect 2003-02-31 as wrong. so we do that 
	//manually here.
	if (day > 28) {
		if (this.getNumOfDays(year, month) < day) return false;
	}
	return true;	
}
//-------------------this method return no of days--
Test.prototype.getNumOfDays=function(year, month){
	switch (month) {
		case 2:
			if (this.isLeapYear(year)) return 29;
			return 28;
			break;
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			return 31;
			break;
		default:
			return 30;
	}
}

//----------------test wheather a given year is leap year or not------
Test.prototype.isLeapYear=function(year){
	return (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0));
	
}
//----------------test given year valid in 19/20 centur----------
Test.prototype.fixYear=function(year) {
	if (year < 100) {
		year = parseInt('19' + year, 10);
	} else if ((year >= 100) && (year < 110)) {
		year = parseInt(200 + '' + year.toString().substr(2, 1), 10);
	}
	return year;
}
//----------------------end of date utility functions added-----------
//--------------------alert message class-------------start here
function Msg(content){
	this.doc= content ||'&nbsp;';
	this.bg=document.createElement("div");
	this.box=document.createElement("div");
	
		this.init=function(){
			this.bg.innerHTML=this.doc;
			this.bg.style.position='absolute';
			this.bg.style.height='auto';
			this.bg.style.left='200px';
			this.bg.style.top='300px';
			this.bg.style.zIndex='1000';
			this.bg.style.fontWeight='bold';
			this.bg.style.width='400px';
			this.bg.style.fontFamily='arial';
			this.bg.style.border='1px solid #A5CFE9';
			this.bg.style.padding='50px';
			this.bg.style.fontSize='11px';
			this.bg.style.color='#4B7A98';
			this.bg.style.background='#D5EBF9';
			this.bg.style.filter='alpha(opacity=85)'; // IE
			this.bg.style.opacity='0.85'; // FF
			document.body.appendChild(this.bg);
		//document.body.appendChild(this.bg);
			
		}
		
	
}
//--------------------------------------ajax general------------
function Ajax(url,callback){
	var resp;
if (document.getElementById) { 
var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest(); 
}
		if (x) {
			x.onreadystatechange = function() {
				if (x.readyState == 4 && x.status == 200) {
					callback+"()";
					//document.getElementById(displayid).innerHTML = x.responseText;
					//document.getElementById(id).innerHTML = '';
				}
			}
			
			url=programa;
			x.open('POST', url, true);
			//x.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			//x.setRequestHeader("Content-length", parameters.length);
			x.setRequestHeader("Connection", "close");
			x.send(null);
		}	
}
//-------------------------------------------------------
function $(id){return document.getElementById(id);}
//-------------------------------------------------------
function $v(id){return document.getElementById(id).value;}
//---------------------------------------------------------
function $h(id){return document.getElementById(id).innerHTML;}
//------------------------------------------------------------
