var xmlHttp;
var filepath = "http://rajasthan.ibongo.com/ajax.php?"
//var filepath = "http://downtimedetect.com/webmonitoring/ajax.php?"

//XMLhttp variable will hold the XMLHttpRequest object
function GetXmlHttpObject(handler)
{ 
		var objXMLHttp=null
		if (window.XMLHttpRequest)
		{
		objXMLHttp=new XMLHttpRequest()
		}
		else if (window.ActiveXObject)
		{
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
		}
		return objXMLHttp
}
/* Function for stateChanged functionality */
function stateChanged() 
{		
		
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {	
		//alert(xmlHttp.responseText)
		document.getElementById("4").innerHTML=xmlHttp.responseText 
		}
		else if (xmlHttp.readyState==1 || xmlHttp.readyState=="loading")
		{
		document.getElementById("4").innerHTML='&nbsp;<table width="100%"><tr height="100px"><td align="center" class="loading"><img src="http://ibongo.com/images/ajax_loader.gif" border="0">&nbsp;Please wait while the page is loading ....</td></tr></table>';
		}
} 

/* Function to check  valid email or not */
function isNotValidEmail(email) {
			// a very simple email validation checking. 
			// you can add more complex email checking if it helps 
				if(email.length <= 0)
				{
				  return true;
				}
				var splitted = email.match(/^[a-zA-Z0-9_\.]+@[a-zA-Z0-9_]+\.([a-zA-Z]{3}|([a-zA-Z]{2}\.[a-zA-Z]{2}))$/);
				if(splitted == null) return true;
				return false;
}


/* Function to display the businesses in home page */
function displaybusinesses(type,flag) {
		xmlHttp=GetXmlHttpObject()
		if (xmlHttp==null)
		{
		alert ("Browser does not support HTTP Request")
		return
		} 

		if(flag)
		var url	=	filepath +"type="+type+"&flag="+flag;		
		else
		var url	=	filepath +"type="+type+"&flag=getbusiness";
		
		//alert(url);
		
		xmlHttp.onreadystatechange=stateChangedbusiness
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)

}

function stateChangedbusiness() {		
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {	
		//alert(xmlHttp.responseText)
		document.getElementById("countrydivcontainer").innerHTML=xmlHttp.responseText 
		}
		else if (xmlHttp.readyState==1 || xmlHttp.readyState=="loading")
		{
		document.getElementById("countrydivcontainer").innerHTML='&nbsp;<img src="http://192.168.1.26/ibongo/images/ajax_loader.gif" border="0">&nbsp;Loading..';
		}
	}


/* Function to display the businesses in home page */
function displayads(type) {
		xmlHttp=GetXmlHttpObject()
		if (xmlHttp==null)
		{
		alert ("Browser does not support HTTP Request")
		return
		} 

		var url	=	filepath +"type="+type+"&flag=getads";
		//alert(url);
		
		xmlHttp.onreadystatechange=stateChangedads
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)

}

function stateChangedads() {		
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {	
		//alert(xmlHttp.responseText)
		document.getElementById("adsdiv").innerHTML=xmlHttp.responseText 
		}
		else if (xmlHttp.readyState==1 || xmlHttp.readyState=="loading")
		{
		document.getElementById("adsdiv").innerHTML='&nbsp;<img src="http://192.168.1.26/ibongo/images/ajax_loader.gif" border="0">&nbsp;Loading..';
		}
	}


/* Function to display user aded ads i.e., my ads */
function displaymyads(type,slimit,elimit,currentpage) {
		xmlHttp=GetXmlHttpObject()
		if (xmlHttp==null)
		{
		alert ("Browser does not support HTTP Request")
		return
		} 

		var url	=	filepath +"ad_type="+type+"&flag=getmyads"+"&slimit="+slimit+"&elimit="+elimit+"&currentpage="+currentpage;
		//alert(url);
		
		xmlHttp.onreadystatechange=stateChangedmyads
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)

}

function stateChangedmyads() {		
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {	
		//alert(xmlHttp.responseText)
		document.getElementById("myadsdiv").innerHTML=xmlHttp.responseText 
		}
		else if (xmlHttp.readyState==1 || xmlHttp.readyState=="loading")
		{
		document.getElementById("myadsdiv").innerHTML='&nbsp;<img src="http://192.168.1.26/ibongo/images/ajax_loader.gif" border="0">&nbsp;Loading..';
		}
	}


/* Function to display the businesses in home page */
function displayrightads() {
		xmlHttp=GetXmlHttpObject()
		if (xmlHttp==null)
		{
		alert ("Browser does not support HTTP Request")
		return
		} 

		var url	=	filepath +"flag=getrightads";
		//alert(url);
		
		xmlHttp.onreadystatechange=stateChangedrightads
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)

}

function stateChangedrightads() {		
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {	
		//alert(xmlHttp.responseText)
		document.getElementById("ad").innerHTML=xmlHttp.responseText 
		}
}



function stateChangedAvailability() 
{ 		
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{ 
			//alert(xmlHttp.responseText)
			document.getElementById("checkavail").innerHTML=xmlHttp.responseText
		} 
} 

/* Function to get the availability status of a domain name */
function checkdomainajax(domain) {
		xmlHttp=GetXmlHttpObject()
		if (xmlHttp==null)
		{
		alert ("Browser does not support HTTP Request")
		return
		} 

		var url	=	filepath +"domain="+domain+"&flag=getdomain";
		//alert(url)
		
		xmlHttp.onreadystatechange=stateChangeddomain
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)

}


function stateChangeddomain() {		
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {	
		//alert(xmlHttp.responseText)
		document.getElementById("domainname1").innerHTML=xmlHttp.responseText 
		}
		else if (xmlHttp.readyState==1 || xmlHttp.readyState=="loading")
		{
		document.getElementById("domainname1").innerHTML='&nbsp;<img src="http://192.168.1.26/ibongo/images/ajax_loader.gif" border="0">&nbsp;Loading..';
		}
	}

/* Function to get the availability status of a rep user name */
function checkuserajax(username) {
		xmlHttp=GetXmlHttpObject()
		if (xmlHttp==null)
		{
		alert ("Browser does not support HTTP Request")
		return
		} 

		var url	=	filepath +"user="+username+"&flag=getuser";
		//alert(url)
		
		xmlHttp.onreadystatechange=stateChangeduser
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)

}


function stateChangeduser() {		
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {	
		//alert(xmlHttp.responseText)
		document.getElementById("domainname2").innerHTML=xmlHttp.responseText 
		}
		else if (xmlHttp.readyState==1 || xmlHttp.readyState=="loading")
		{
		document.getElementById("domainname2").innerHTML='&nbsp;<img src="http://192.168.1.26/ibongo/images/ajax_loader.gif" border="0">&nbsp;Loading..';
		}
	}

/* Function to get the availability status of a RepID*/
function checkrepidajax(repid) {
		xmlHttp=GetXmlHttpObject()
		if (xmlHttp==null)
		{
		alert ("Browser does not support HTTP Request")
		return
		} 

		var url	=	filepath +"repid="+repid+"&flag=getrepid";
		
		xmlHttp.onreadystatechange=stateChangeddomain
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)

}		

/* Function to get the availability status of a email id */
function checkemailajax(email) {
		xmlHttp=GetXmlHttpObject()
		if (xmlHttp==null)
		{
		alert ("Browser does not support HTTP Request")
		return
		} 

		var url	=	filepath +"email="+email+"&flag=getemail";
		
		xmlHttp.onreadystatechange=stateChangedemail
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)

}


function stateChangedemail() {		
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {	
		//alert(xmlHttp.responseText)
		document.getElementById("emaildiv").innerHTML=xmlHttp.responseText 
		}
		else if (xmlHttp.readyState==1 || xmlHttp.readyState=="loading")
		{
		document.getElementById("emaildiv").innerHTML='&nbsp;<img src="http://192.168.1.26/ibongo/images/ajax_loader.gif" border="0">&nbsp;Loading..';
		}
	}
		


function updateHiddenState(){
	document.getElementById("hiddenstateid").value = document.getElementById("id_selstate").value;
}		
		
/* Function for records manipulation like edit, update, delete etc.*/
function recordsmanipulation(id,type1,slimit,elimit,path,currentpage)
{
		xmlHttp=GetXmlHttpObject()
		if (xmlHttp==null)
		{
			alert ("Browser does not support HTTP Request")
			return
		} 
		
		if(type1=="update"){
			var result=true				
		}
		else if(type1=="edit"){
			var result=true
		}
		else if(type1=="sort"){
			var result=true
		}
		else if(type1=="deactivate"){
			var result = confirm("Are your sure you want to Deactivate this record?")
		}
		else if(type1=="activate"){
			var result = confirm("Are you sure you want to Activate this record?")		
		}
		else if(type1=="delete"){
			var result = confirm("Are you sure you really want to delete this Record?")
		}
		else if(type1=="viewttid"){
			var result = true;
		}
		else if(type1=="insert"){
			var result=true
		}
		else if(type1=="services"){
			var result=true
		}
		else if(type1=="edit_service"){
			var result=true
		}
		else if(type1=="loc_graphs" || type1=="View_more"){
			var result=true
		}else if(type1=="replay_select"){
			var result=true
		}

		if(result==true)
		{
			if(path==" "){
				path = " "
			}
			else {
				if(type1=="replay_select"){
					document.getElementById("id_hid_txdiv").value = "showselservice";
					var sellocation = document.getElementById("sellocation").value;	
					path = "&u_ser_location="+sellocation+"&currentpage="+currentpage
				}else if(type1=="View_more"){
					document.getElementById("id_hid_txdiv").value = "showallgraphs";
					var sellocation = document.getElementById("sellocation").value;
					var selservice  = document.getElementById("selservice").value;
					path = "&u_ser_location="+sellocation+"&u_ser_service="+selservice+"&currentpage="+currentpage
				}
				else if(type1=="loc_graphs" ){ /* for all locations registered*/
					path = "&u_ser_location="+path+"&currentpage="+currentpage
				}else{
					path = "&path="+path+"&currentpage="+currentpage
				}
			}

			var url	= filepath+"id="+id+"&flag="+type1+"&slimit="+slimit+"&elimit="+elimit+path
			//alert(url);
			xmlHttp.onreadystatechange=stateChanged 
			xmlHttp.open("GET",url,true)
			xmlHttp.send(null)
		}
		else
		{
			return false
		}
}

/*This function is ment for paging
1.filepath is-destination url where the action should perform
2.slimit-it indicates the startlimit
3.elimit-it indicates the endlimit that is how many records has to be displayed
*/
function checkpaging(slimit, elimit, currentpage,pageno)
	{
		var objdiv = 0;
		var searchobj = filepath.search('###');
		filepath = filepath;
		if (searchobj != -1)
		{
			objdiv = searchobj;
		}
		xmlHttp=GetXmlHttpObject()
		if (xmlHttp==null)
		{
			alert ("Browser does not support HTTP Request")
			return
		} 
		var url			=	filepath+"slimit="+slimit+"&elimit="+elimit+"&paging=yes&currentpage="+currentpage+"&pg="+pageno
		//alert(url)

		if (objdiv == 0)
		{
			xmlHttp.onreadystatechange=stateChanged 
		}
		else {
			xmlHttp.onreadystatechange=stateChange 
		}
			
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)
}

/**
*   to redirect the page to the path
*   argument-1 location to which redirected
*/
function redirectPage(path){
	window.location = path;
	return false;
}

/**
*   to redirect the page to the path
*   argument-1 reply id to which redirected
*/
function viewUserTroubleTicket(){	
	document.getElementById("linkdiv").style.visibility = 'visible';	
	document.getElementById("linkdiv").style.display = 'block';	
}
/* for services add screen */
function displaycustomport(value){
	/* This is statically fixed as the Custom Port no in database is "7" and if the values is changed this number has to be rechanged */
	if (value==7){						
		document.getElementById("40").style.display = "block";
	}
	else {
		document.getElementById("40").style.display = "none";
	}
 }

/*
* fumction for AJAX for timely check of the performance
* of the hosts
* argument-1 path to be redirected to 
* argument-2 type of page screen
*/
function ajaxTimerCheck(path,type){
	xmlHttp=GetXmlHttpObject()
	var filepath = "timer_check.php?";
	if(type!="timer_check"){
		var result=true
	}
	if(result==true){
		var url	= filepath+path;	
		//alert(url);

		xmlHttp.onreadystatechange=stateChanged 
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)
	}else{
		return false;
	}

}

/*
*  triggers the performance of the hosts
*  by triggering ajax for interval of 1second
*/
function triggerinTime()
{
	var loopcount  = 0;
	var totalrec = document.getElementById("totalrec").value;
	var hidname   = "";
	var id=path= "";
	loopcount  = 0

	for(loopcount=1;loopcount<=totalrec ;loopcount++ ){

		hidname		= "hidtimevalue" + loopcount;
		hidcount	= "hidcount" + loopcount;
		hidval		= eval(document.getElementById(hidcount).value);

		if (document.getElementById(hidname).value == hidval) {	
			id = document.getElementById("hidu_ser_id"+loopcount).value ;
			document.getElementById(hidcount).value = 0;
			setdivid(("div"+loopcount));

			/* triggering AJAX*/
			path = "hidu_ser_id="+id+"&disprec="+loopcount;
			ajaxTimerCheck(path, "insert");
		}else{
			/* increment variables */
			hidval++;
			document.getElementById(hidcount).value = eval(hidval);
		}
	}//End of for loop

	var Digital=new Date()
	var seconds=Digital.getSeconds()
	if (seconds<=9)	seconds="0"+seconds

	/*calling the parent function for every sec */
	setTimeout("triggerinTime()",1000)
}

/*
* to set the div tag to hidden text box in timer check
*/
function setdivid(id) {
	document.getElementById("id_hid_txdiv").value	=	id;
}
function openWnd(path){
	window.open(path,"WebMonitoring","width=450,height=300")	
}