//Object used in this context
var xmlhttp;
//the id of the element whose html has to be changed
var tabid;
// callback function id
var callbackid;
// progressmeter
var progress=0;
// wait amount
var wait=10;
//tabid should be empty when hitting url
var jobArray = [];
var jobArrayURLS = [];
var jobArrayMethods = [];

function updateProgress()
{
	var cprog;
	cprog = (progress/4);
	document.getElementById(tabid).innerHTML="<table bgcolor=#ffff00 width=\""+cprog+"\"><tr><td bgcolor=#ffff00>Percentage Completed = "+cprog+"%</td></tr></table>";
	
}
	

function onChangeOfStatus()
{
	progress = progress+1;
}


function xmlhttpChange()
{
	if (xmlhttp.readyState==4)
  	{
		
	  	if (xmlhttp.status == 200) 
	  	{
	  		//Clear the callback function
	  		clearInterval(callbackid);
	  		var tabhtm = xmlhttp.responseText;
	  		var tb = document.getElementById(tabid);
	  		tb.innerHTML = tabhtm;
		}
		else
		{
			//Clear the callback function
			var tabhtm = '<table ><tr><td>Error : </td></tr><tr><td>Unable to Load the data</td></tr></table>';
			var tb = document.getElementById(tabid);
			tb.innerHTML = tabhtm;
		
			clearInterval(callbackid);
			alert('Invalid response From Server');
		}
		tabid=''; //clear tabid to get the next job

		if(jobArray.length)
		{
				xmlLoader(jobArrayURLS.pop(),jobArray.pop(),jobArrayMethods.pop());
		}
	}
	else
	{
		onChangeOfStatus();
		updateProgress();
	}
}
/** Url to Load , Elements ID where to load and the method ( POST/GET always use caps )**/
function xmlLoader(url,tbid,method)
{
		
	
	if(tabid!='' && tabid!=null)
	{
		jobArray.push(tbid);
 		jobArrayURLS.push(url);
		jobArrayMethods.push(method);
		return; 
	}	
	

	tabid = tbid;
	var netscap=true;
	progress=0;
	if(callbackid!=undefined)
		clearInterval(callbackid);
	
	callbackid = setInterval("updateProgress()", wait);
		try {
		xmlhttp = new XMLHttpRequest();
		//netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
		xmlhttp.open(method, url,true);
		xmlhttp.onreadystatechange=xmlhttpChange;
		xmlhttp.send(null);
	}
	catch (e)
	{
		netscap=false;
	}
	if(!netscap)
	{
		try {
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
			if (xmlhttp)
		    	{
			    	xmlhttp.onreadystatechange=xmlhttpChange
			    	xmlhttp.open(method,url,true)
				xmlhttp.send();
    			}
		}catch(e)
		{
			netscap=false;
		}
	}
}//en o func

function getValueOfElement(elem,nam)
{
	
	switch(elem.type)
	{
		case 'text':
			return elem.value || "";	
		break;
		case 'hidden':
			return elem.value || "";	
		break;
	
		case 'radio':
			if(elem.checked)
			return elem.value || "on";	
	
		break;
		case 'textarea':
			return elem.value;	
				
		break;
		case 'checkbox':
			if(elem.checked) return elem.value || "on";
		break;
	
		case 'select-one':
			return elem.value || null;
		break;

		case 'select-multiple':
			
		 var i;var cnt = 0;
		var sels =null;		 
		for (i=0; i<elem.options.length; i++) 
		 {
    		if (elem.options[i].selected) {
				if(cnt==0) sels= new Array();    	  		
				sels[cnt] = elem.options[i].value;
   	   			++cnt;
   	 		}
  		 }
		 return sels || null;
		break;

	
		default:
			return elem.value || "";	
		break;
		
		

	}
}

function verify_field(name,value,form,index)
{
	compulsory = name.charAt(name.length-2);
	datatyp = name.charAt(name.length-1);
	iscollection=0;

	if(datatyp==']')
	{
		compulsory = name.charAt(name.length-4);
		datatyp = name.charAt(name.length-3);
		iscollection=1;
	}
	itm = form.elements[index];

	if(iscollection==0)
	{
				if(datatyp!='c')
				{
					if(!verify_comma(itm.value))
					{
						alert("Comma Character is not allowed in this field");	
						//correctString(itm);
						alert(itm.value);
						itm.focus();
						return false;
					}
				}
				//first to check the cumpulsory field
				if(compulsory=='c')
				{
					if(!verify_length(val))
					{
						html= html+"Error: Please fill field Focused ";
						alert(html);
						itm.focus();
						return false;
					}
				}
				//for number and cumpulsory field
				if(datatyp=='n' && compulsory=='c')
				{
					if(!verify_number(val))
					{
						html= html+"Error : Please fill field Focused with Numeric Data";
						alert (html);
						itm.focus();
						return false;
					}
				}
				//For Number and optional field
				if(datatyp=='n' && compulsory=='o')
				{
					if(verify_length(val))
					{
						if(!verify_number(val))
						{
							html= html+"Error : Please fill field Focused With Numeric Data";
							alert (html);
							itm.focus();
							return false;
						}
					}
				}
				//For email and cumpulsory field
				if(datatyp=='e' && compulsory=='c')
				{
					if(!verify_email(val))
					{
						html= html+"Error : Please fill field With Valid Email Data";
						alert (html);
						itm.focus();
						return false;
					}
				}
				//For email and cumpulsory field
				if(datatyp=='e' && compulsory=='o')
				{
					if(verify_length(val))
					{
						if(!verify_email(val))
						{
							html= html+"Error : Please fill field with Valid Email Data";
							alert (html);
							itm.focus();
							return false;
						}
					}
			
				}
		
				//For date and cumpulsory field
				if(datatyp=='d' && compulsory=='c')
				{
				
					if(!verify_date(val))
					{
						html= html+"Error : Please fill field Focused with Valid date in YYYYMMDD format";
						alert (html);
						itm.focus();
						return false;
					}
				
				}
				//For date and cumpulsory field
				if(datatyp=='d' && compulsory=='o')
				{
					if(verify_length(val))
					{
						if(!verify_date(val))
						{
							html= html+"Error : Please fill field Focused with Valid date in YYYYMMDD format";
							alert (html);
							itm.focus();
							return false;
						}
					}
				
				}
				//For time and cumpulsory field
				if(datatyp=='t' && compulsory=='c')
				{
					if(!verify_time(val))
					{
						html= html+"Error : Please fill field Focused with Valid Time Format HHMMSS";
						alert (html);
						itm.focus();
						return false;
					}
					
				}
				//For time and cumpulsory field
				if(datatyp=='t' && compulsory=='o')
				{
					if(verify_length(val))
					{
						if(!verify_time(val))
						{
							html= html+"Error : Please fill field Focused with Valid Time Format HHMMSS";
							alert (html);
							itm.focus();
							return false;
						}
					}
				}
			}//iscollection end
			else
			{
				if(compulsory=='c')			
				{
				 	if(itm.options.length<1)
				 	{
				 		alert('Please Fill this List with atleast one value');
				 		itm.focus();
				 		return false;
					}
				}
			}//else iscollection end
			
		/////////////////////////////////end of checks //////////////////////////
	
		return true;
			

}


function getPostString(form) 
{ 
    

	var index=0;
	var html = "";
	var nam,val;
	for(index=0;index<form.length;++index)
	{
		nam =form.elements[index].name;
		
		val = getValueOfElement(form.elements[index],nam);
		if(val!=null)		 
		{
			if(form.elements[index].type !='select-multiple')		
			html 	= html + nam +"=" + escape(val) + "&";
			else
			{	
				while(val.length > 0)
				html 	= html + escape(nam) +"=" + escape(val.pop()) + "&";
			
			}			 
		}
	}
 	html = html.substr(0,(html.length - 1)); 
	return html;
 }


/** Function to be used to submit all forms **/
function submitForm(url,form,tbid)
{
	var str = getPostString(form);
	tabid = tbid;
	var netscap=true;
	progress=0;
	if(callbackid!=undefined)
		clearInterval(callbackid);
	
	callbackid = setInterval("updateProgress()", wait);
		try {
		xmlhttp = new XMLHttpRequest();
		//netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
		xmlhttp.open("POST", url,true);
		xmlhttp.onreadystatechange=xmlhttpChange;
		xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");					
					
		xmlhttp.send(str);
	}
	catch (e)
	{
		netscap=false;
	}
	if(!netscap)
	{
		try {
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
			if (xmlhttp)
		    	{
			    	xmlhttp.onreadystatechange=xmlhttpChange
			    	xmlhttp.open("POST",url,true)
					xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");					
					xmlhttp.send(str);
    			}
		}catch(e)
		{
			netscap=false;
		}
	}

}

/** Function to set the Heading **/
function setHeading(tbid,heading)
{
		var tb = document.getElementById(tbid);
		tb.innerHTML = heading;
	

}

