function isdefined(variable)
{
	return (typeof(variable) == "undefined" || variable == null )?  false: true;
}


function loadTotalAgents(frmfilter){
	
	if (isdefined(document.getElementById("modelsearch")) == true){
		var str = document.getElementById("modelsearch").value;
	}else if (isdefined(document.getElementById("modelsearch")) == false){
		var str = "";
	}
	
	
	if (isdefined(document.getElementById("showfav"))  == true){
		var mod_fav = document.getElementById("showfav").value;	
	}else if (isdefined(document.getElementById("showfav")) == false){
		var mod_fav = 0;
	}
	
	if (isdefined(document.getElementById("showstatus"))  == true ){
		var mod_status = document.getElementById("showstatus").value;	
	}else if (isdefined(document.getElementById("showstatus")) == false){
		var mod_status = 0;
	}
	
	
	
	var regionlist = "" ;
	if (isdefined(document.getElementById("region"))  == true ){
		if(document.getElementById("region").checked){
			regionlist = regionlist + " " + document.getElementById("region").value;
		}
		
		if(document.getElementById("region2").checked){
			regionlist = regionlist + " " + document.getElementById("region2").value;
		}
		
		if(document.getElementById("region3").checked){
			regionlist = regionlist + " " +  document.getElementById("region3").value;
		}
		
		if(document.getElementById("region4").checked){
			regionlist = regionlist + " " +  document.getElementById("region4").value;
		}
		
		if(document.getElementById("region5").checked){
			regionlist = regionlist + " " +   document.getElementById("region5").value;
		}
		
		if(document.getElementById("region6").checked){
			regionlist = regionlist + " " +  document.getElementById("region6").value;
		}
	}else{
		regionlist = "";	
	}
	
	hXmlHttp2 = getXMLHttpHandler();
	hXmlHttp2.onreadystatechange = function(){
		if(hXmlHttp2.readyState == 4){	
			if(hXmlHttp2.status == 200){
				
				xmlDocAg = hXmlHttp2.responseXML.documentElement;
				recordCountAg = parseInt(xmlDocAg.getElementsByTagName("recordcount")[0].childNodes[0].nodeValue);
				loggedInCountAg = parseInt(xmlDocAg.getElementsByTagName("recordcount")[0].getAttribute("loggedin"));
				agentListAg = "";
				//window.open(url);
				recordsAg = xmlDocAg.getElementsByTagName("record");

				for(i=0 ; i < recordCountAg ; i++){
					agentListAg += recordsAg[i].childNodes[0].nodeValue;
					//alert(recordsAg[i].childNodes[0].nodeValue)
					
				}
				
				if(recordCountAg == 0){
					document.getElementById("onlinemodel").innerHTML = 0;
					document.getElementById("totalmodels").innerHTML = 0;
					document.getElementById("agents_list").innerHTML = "No" + " " + basename + " " +  "Found";
					//alert("no record");
					return;
				}
				else {
					
					recordsAg = xmlDocAg.getElementsByTagName("record");
					
				}
				
				if(frmfilter == 1 ){
					parent.document.getElementById("onlinemodel").innerHTML = loggedInCountAg;
					parent.document.getElementById("totalmodels").innerHTML = recordCountAg;
					parent.document.getElementById("agents_list").innerHTML = agentListAg;	
				
				}else{
					
					document.getElementById("onlinemodel").innerHTML = loggedInCountAg;
					document.getElementById("totalmodels").innerHTML = recordCountAg;
					document.getElementById("agents_list").innerHTML = agentListAg;	
					
					$(document).ready(function(){
						//Examples of how to assign the ColorBox event to elements
						$(".abcd").colorbox({width:"350px", height:"180", iframe:true , scrolling:false});
						$(".Pmsg").colorbox({width:"600", height:"520", iframe:true , scrolling:false});
						$(".Pgif").colorbox({width:"640", height:"500", iframe:true , scrolling:false});
						$(".myshop").colorbox({width:"800", height:"561", iframe:true , scrolling:false});
						$(".myshow").colorbox({width:"800", height:"531", iframe:true , scrolling:false});
						//Example of preserving a JavaScript event for inline calls.
						$("#click").click(function(){ 
							$('#click').css({"background-color":"#f00", "color":"#fff", "cursor":"inherit"}).text("Open this window again and this message will still be here.");
							return false;
						});
					});
				}
			
			}
		}
	}
	url = baseurl + "/agents.cfm?searchval=" + str + "&model_region=" + regionlist  + "&sortfav=" + mod_fav + "&mod_status=" + mod_status;
	//window.open(url);
	hXmlHttp2.open("POST", url);
	hXmlHttp2.send(null);
	
	
}

/*function showFilterDiv(){
	if(document.getElementById('onoff').value == 1){
		document.getElementById('filter_div').style.display = '';
		document.getElementById('onoff').value = 0;
	} else {
		document.getElementById('filter_div').style.display = 'none';
		document.getElementById('onoff').value = 1;	
	}
}*/

function showNotifyDiv(id){
	if(document.getElementById('onoff_not_' + id).value == 1){
		document.getElementById('notify_div_' + id).style.display = '';
		document.getElementById('wrapper_div_' + id).style.display = '';
		document.getElementById('onoff_not_' + id).value = 0;
		document.getElementById('wrapper_div_' + id).style.width=screen.width - 22;
		document.getElementById('wrapper_div_' + id).style.height=screen.height;
		//var msgbox = document.getElementById('notify_div_' + id);
//		var x = (document.innerWidth / 2) - (msgbox.offsetWidth / 2);
//  		var y = (document.offsetHeight / 2) - (msgbox.offsetHeight / 2);
//		msgbox.style.top = y;
//  		msgbox.style.left = x;
		//x = event.clientX + document.body.scrollLeft;
//		y = event.clientY + document.body.scrollTop + 35;
//		document.getElementById('wrapper_div_' + id).style.display="block";
//		document.getElementById('wrapper_div_' + id).style.left = x;
//		document.getElementById('wrapper_div_' + id).style.top = y;
	} else {
		document.getElementById('notify_div_' + id).style.display = 'none';
		document.getElementById('wrapper_div_' + id).style.display = 'none';
		document.getElementById('onoff_not_' + id).value = 1;	
	}
}

function addFav(hmaid,hma_name){
	if(confirm("Do you want to add this" + " " + basename + " " +  "to favourites?")){
		hXmlHttp3 = getXMLHttpHandler();		
		hXmlHttp3.onreadystatechange = function(){
			if(hXmlHttp3.readyState == 4){				
				if(hXmlHttp3.status == 200){
					resText = hXmlHttp3.responseText;
					if(resText == 0){
						alert('Successfully added to favourites.');
						loadTotalAgents();
					} else {
						alert('Already in favourites.');
					}
					
				}
			}
		}
		url = baseurl + "/common_script.cfm?key=1&hmaid=" + hmaid + "&hma_name=" + hma_name;
		//window.open(url);
		hXmlHttp3.open("GET", url);
		hXmlHttp3.send(null);
	}
}

function addNotify(hmaid,hma_name){
	if(document.getElementById("notify_name_" + hmaid).checked){
		notify_id = 1;
	} else {
		alert('Please check notify babe to add notification.');
		return false;
	}
	
	
		hXmlHttp4 = getXMLHttpHandler();		
		hXmlHttp4.onreadystatechange = function(){
			if(hXmlHttp4.readyState == 4){				
				if(hXmlHttp4.status == 200){
					resText = hXmlHttp4.responseText;
					alert('Successfully added notification.');
					parent.$(".Pmsg").colorbox.close();
				}
			}
		}
		url = baseurl + "/common_script.cfm?key=3&hmaid=" + hmaid + "&hma_name=" + hma_name + "&notify_id=" + notify_id;
		//window.open(url);
		hXmlHttp4.open("GET", url);
		hXmlHttp4.send(null);
	
}


function showGifts(cmb,direction,srv_id,tokens,grp){
	
		var cat_id = document.getElementById(cmb).value;
		hXmlHttp5 = getXMLHttpHandler();		
		hXmlHttp5.onreadystatechange = function(){
			if(hXmlHttp5.readyState == 4){				
				if(hXmlHttp5.status == 200){
					resText = hXmlHttp5.responseText;
					document.getElementById('main_cat_content').innerHTML = resText;
				}
			}
		}
		if(direction == 1){
			document.getElementById('gift_id').value = 0;
			document.getElementById('tokens_req').value = 0;
			url = baseurl + "/common_script.cfm?key=5&cat_id=" + cat_id;
			document.getElementById('choose_another').style.display = "none";
		} else if(direction == 2){
			document.getElementById('gift_id').value = srv_id;
			document.getElementById('tokens_req').value = tokens;
			url = baseurl + "/common_script.cfm?key=6&cat_id=" + cat_id + "&srv_id=" + srv_id;
			document.getElementById('choose_another').style.display = "";
		} else if(direction == 3){
			document.getElementById('gift_id').value = 0;
			document.getElementById('tokens_req').value = 0;
			
			if (isdefined(document.getElementById("txt_search")) == true){
				var str_search = document.getElementById("txt_search").value;
			}else if (isdefined(document.getElementById("txt_search")) == false){
				var str_search = "";
			}
			
			url = baseurl + "/common_script.cfm?key=5&cat_id=" + cat_id + "&txt_search=" + str_search;
			document.getElementById('choose_another').style.display = "none";
		}  else if(direction == 4){
				document.getElementById('gift_id').value = 0;
				document.getElementById('tokens_req').value = 0;
				
				if (isdefined(document.getElementById("txt_search")) == true){
				var str_search = document.getElementById("txt_search").value;
				}else if (isdefined(document.getElementById("txt_search")) == false){
					var str_search = "";
				}
				
				url = baseurl + "/common_script.cfm?key=5&cat_id=" + cat_id + "&grp=" + grp + "&txt_search=" + str_search;
				document.getElementById('choose_another').style.display = "none";
		}
		
		//window.open(url);
		hXmlHttp5.open("GET", url);
		hXmlHttp5.send(null);
	
}

var fieldcount = 0;

function limitText(limitField, limitNum) {
	var count = document.getElementById('remLen_txt');
	
	if ((limitField.value.length - fieldcount) > limitNum){
		limitField.value = limitField.value.substring(0, (limitNum+fieldcount));
		}
	else{
		fieldcount = 0;
		
	// calculate the remaining characters 
	count.value = limitNum - limitField.value.length + fieldcount;
	}
	
	//if (limitField.value.length > limitNum) {
//		limitField.value = limitField.value.substring(0, limitNum);
//	} 
}

function BuyGift(ex_token,hmaid){
	if(document.getElementById('gift_id').value == 0){
		alert('Please select any virtual gift to send.');
		return false;
	}
	
	if(document.getElementById('txt_comments').value == ""){
		alert('Please type some text in personal message area.');
		return false;
	}
	
	var tokens_req = document.getElementById('tokens_req').value;
	var srv_id = document.getElementById('gift_id').value;
	var comments = document.getElementById('txt_comments').value;
	
	if(ex_token < tokens_req){
		alert('You are not allowed to send this virtual gift as you have not enough tokens.');
		return false;
	} else {
		if(confirm('Do you want to send this virtual gift?')){
			
			url = baseurl + "/common_script.cfm?key=7&xml=yes&srv_id=" + srv_id + "&hmaid=" + hmaid + "&comments=" + comments;
			//window.open(url);
			var xmlemail = new XT.ParseXML(url);
			var docemail = xmlemail.parse();			
			if(docemail)
				{			
				if(docemail.result){
					reccntval = docemail.result.usercnt;
					reccntmsg = reccntval.msg;
					reccnt1 = reccntval.reccount;
					if(reccntmsg == 1){
						alert('Your virtual gift is successfully sent.');
						parent.document.getElementById('current_status').innerHTML = reccnt1;
						parent.$(".Pgif").colorbox.close();
					}
					if(reccntmsg == 0)
						{
						alert('You are not allowed to send this virtual gift as you have not enough tokens.');	
						}
				}//end if result
			}// end if doc					
			return false;
		}
	}
	return true;
}


