var ajax2int;
var maxinstance=0;
var chatreq8 = new Array();
var targets = new Array();
var inprogress = false;
var alarmreq;

function calculate_time_zone() {
	var rightNow = new Date();
	var jan1 = new Date(rightNow.getFullYear(), 0, 1, 0, 0, 0, 0);  // jan 1st
	var june1 = new Date(rightNow.getFullYear(), 6, 1, 0, 0, 0, 0); // june 1st
	var temp = jan1.toGMTString();
	var jan2 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));
	temp = june1.toGMTString();
	var june2 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));
	var std_time_offset = (jan1 - jan2) / (1000 * 60 * 60);
	var daylight_time_offset = (june1 - june2) / (1000 * 60 * 60);
	var dst;
	if (std_time_offset == daylight_time_offset) {
		dst = "0"; // daylight savings time is NOT observed
	} else {
		// positive is southern, negative is northern hemisphere
		var hemisphere = std_time_offset - daylight_time_offset;
		if (hemisphere >= 0)
			std_time_offset = daylight_time_offset;
		dst = "1"; // daylight savings time is observed
	}
	var i;

	return convert(std_time_offset)+","+dst;
}

function convert(value) {
	var hours = parseInt(value);
   	value -= parseInt(value);
	value *= 60;
	var mins = parseInt(value);
   	value -= parseInt(value);
	value *= 60;
	var secs = parseInt(value);
	var display_hours = hours;
	// handle GMT case (00:00)
	if (hours == 0) {
		display_hours = "00";
	} else if (hours > 0) {
		// add a plus sign and perhaps an extra 0
		display_hours = (hours < 10) ? "+0"+hours : "+"+hours;
	} else {
		// add an extra 0 if needed 
		display_hours = (hours > -10) ? "-0"+Math.abs(hours) : hours;
	}
	
	mins = (mins < 10) ? "0"+mins : mins;
	return display_hours+":"+mins;
}

function stripspeech(str) {
	return str.replace(new RegExp("\"","g"),"");	
}

function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function ajaxchatdone(chattarget) {
// only if req is "loaded"
  if (chatreq1.readyState == 4) {
    // only if "OK"
    if (chatreq1.status == 200 || chatreq1.status == 304) {
		if (chattarget!="null") {
      results = chatreq1.responseText;
      if(document.getElementById(chattarget)) {
	  var oldresults = document.getElementById(chattarget).innerHTML;
	  if (stripspeech(trim(results.toLowerCase())) != stripspeech(trim(oldresults.toLowerCase()))) {
	  }
      document.getElementById(chattarget).innerHTML = results;
	  inprogress = false;
	
	  var objDiv = document.getElementById('chatholder');
	  objDiv.scrollTop = objDiv.scrollHeight;
		
		}
		}
    }
  }
}



function ajaxchatdelayeddone(chattarget, instance) {
	chattarget = targets[instance];
// only if req is "loaded"
  if (chatreq8[instance].readyState == 4) {
    // only if "OK"
    if (chatreq8[instance].status == 200 || chatreq8[instance].status == 304) {
		if (chattarget!="null") {
      results = chatreq8[instance].responseText;
	  var oldresults = document.getElementById(chattarget).innerHTML;
      document.getElementById(chattarget).innerHTML = results;
	  var objDiv = document.getElementById('chatholder');
	  objDiv.scrollTop = objDiv.scrollHeight;

		}
    }
  }
}


function ajaxaddchatdone(chattarget) {
  // only if req is "loaded"
  if (req5.readyState == 4) {
    // only if "OK"
    if (req5.status == 200 || req5.status == 304) {
		if (chattarget!="null") {
      results = req5.responseText;
      document.getElementById(chattarget).innerHTML = results;
	  var objDiv = document.getElementById('chatholder');
	  objDiv.scrollTop = objDiv.scrollHeight;
		}
    }
  }
}

function ajaxchat(chaturl,chattarget) {
	if (inprogress == false) {
	inprogress = true;
  // native XMLHttpRequest object 
  var now = new Date();
  var hour        = now.getHours() + '';
  var minute      = now.getMinutes() + '';
  var second      = now.getSeconds() + '';
  var timenow = hour + minute + second;
  //url = url + "&time=" + timenow;
  if (window.XMLHttpRequest) {
    chatreq1 = new XMLHttpRequest();
    chatreq1.onreadystatechange = function() {ajaxchatdone(chattarget);};
    chatreq1.open("GET", chaturl + "&time=" + timenow, true);
    chatreq1.send(null);
    // IE/Windows ActiveX version
  } else if (window.ActiveXObject) {
    chatreq1 = new ActiveXObject("Microsoft.XMLHttp");
    if (chatreq1) {
      chatreq1.onreadystatechange = function() {ajaxchatdone(chattarget);};
      chatreq1.open("GET", chaturl + "&time=" + timenow, true);
      chatreq1.send(null);
    }
  }
  //setTimeout("ajaxchat('" + chaturl + "','" + chattarget + "')", 1000);
	}
}

function ajaxchatdelayed(chaturl,chattarget, instance) {
	targets[instance]=chattarget;
	instance = instance/1;
  // native XMLHttpRequest object 
  var now = new Date();
  var hour        = now.getHours() + '';
  var minute      = now.getMinutes() + '';
  var second      = now.getSeconds() + '';
  var timenow = hour + minute + second;
  //url = url + "&time=" + timenow;
  if (window.XMLHttpRequest) {
	chatreq8[instance] = new XMLHttpRequest();
    chatreq8[instance].onreadystatechange = function() {ajaxchatdelayeddone(chattarget, instance);};
    chatreq8[instance].open("GET", chaturl + "&time=" + timenow, true);
    chatreq8[instance].send(null);
    // IE/Windows ActiveX version
  } else if (window.ActiveXObject) {
    chatreq8[instance] = new ActiveXObject("Microsoft.XMLHttp");
    if (chatreq8[instance]) {
      chatreq8[instance].onreadystatechange = function() {ajaxchatdelayeddone(chattarget, instance);};
      chatreq8[instance].open("GET", chaturl + "&time=" + timenow, true);
      chatreq8[instance].send(null);
    }
  }
  if (instance>maxinstance) maxinstance=instance;
  nextinstance=maxinstance+1;
  //setTimeout("ajaxchatdelayed('" + chaturl + "','" + chattarget + "','"+nextinstance+"')", 7000);
}

function ajaxblocklist(url,target) {
  // native XMLHttpRequest object 
  var now = new Date();
  var hour        = now.getHours() + '';
  var minute      = now.getMinutes() + '';
  var second      = now.getSeconds() + '';
  var timenow = hour + minute + second;
  //url = url + "&time=" + timenow;
  if (window.XMLHttpRequest) {
    req2 = new XMLHttpRequest();
    req2.onreadystatechange = function() {ajaxblocklistdone(target);};
    req2.open("GET", url + "&time=" + timenow, true);
    req2.send(null);
    // IE/Windows ActiveX version
  } else if (window.ActiveXObject) {
    req2 = new ActiveXObject("Microsoft.XMLHttp");
    if (req2) {
      req2.onreadystatechange = function() {ajaxblocklistdone(target);};
      req2.open("GET", url + "&time=" + timenow, true);
      req2.send(null);
    }
  }
  setTimeout("ajaxblocklist('" + url + "','" + target + "')", 5000);
}

function ajaxblocklistdone(target) {
  // only if req is "loaded"
  if (req2.readyState == 4) {
    // only if "OK"
    if (req2.status == 200 || req2.status == 304) {
		if (target!="null") {
      results = req2.responseText;
      document.getElementById(target).innerHTML = results;
		}
    }
  }
}

function checkstatedone(lastonlinestatus) {
  // only if req is "loaded"
  if (req3.readyState == 4) {
    // only if "OK"
    if (req3.status == 200 || req3.status == 304) {
		var myresponse = req3.responseText;
		var myresponse = myresponse.substring(2);
   		if (myresponse!=lastonlinestatus) {
			window.location.href = window.location.href;	
		}
    } else {
      //document.getElementById(target).innerHTML="ajax error:\n" +
      //req.statusText;
    }
  }	
}

function checkstate(camstar,lastonlinestatus) {
  // native XMLHttpRequest object 
  var now = new Date();
  var hour        = now.getHours() + '';
  var minute      = now.getMinutes() + '';
  var second      = now.getSeconds() + '';
  var timenow = hour + minute + second;
  url = "checkstate.php?id="+camstar+"&last="+lastonlinestatus;
  //url = url + "&time=" + timenow;
  if (window.XMLHttpRequest) {
    req3 = new XMLHttpRequest();
    req3.onreadystatechange = function() {checkstatedone(lastonlinestatus);};
    req3.open("GET", url + "&time=" + timenow, true);
    req3.send(null);
    // IE/Windows ActiveX version
  } else if (window.ActiveXObject) {
    req3 = new ActiveXObject("Microsoft.XMLHttp");
    if (req3) {
      req3.onreadystatechange = function() {checkstatedone(lastonlinestatus);};
      req3.open("GET", url + "&time=" + timenow, true);
      req3.send(null);
    }
  }
  setTimeout("checkstate('" + camstar + "','" + lastonlinestatus + "')", 5000);
}

function ajax2(url,target) {
  // native XMLHttpRequest object 
  var now = new Date();
  var hour        = now.getHours() + '';
  var minute      = now.getMinutes() + '';
  var second      = now.getSeconds() + '';
  var timenow = hour + minute + second;
  //url = url + "&time=" + timenow;
  if (window.XMLHttpRequest) {
    req4 = new XMLHttpRequest();
    req4.onreadystatechange = function() {ajax2done(target);};
    req4.open("GET", url + "&time=" + timenow, true);
    req4.send(null);
    // IE/Windows ActiveX version
  } else if (window.ActiveXObject) {
    req4 = new ActiveXObject("Microsoft.XMLHttp");
    if (req4) {
      req4.onreadystatechange = function() {ajax2done(target);};
      req4.open("GET", url + "&time=" + timenow, true);
      req4.send(null);
    }
  }
}

function ajax2done(target) {
  // only if req is "loaded"
  if (req4.readyState == 4) {
    // only if "OK"
    if (req4.status == 200 || req4.status == 304) {
		if (target!="null") {
      results = req4.responseText;
      document.getElementById(target).innerHTML = results;
	  colon = results.indexOf(":");
	  if (results.substring(0,colon)=="ZERO") {
		  clearInterval(ajax2int);
			window.location.href="showchargeinfo.php?topup=1&show="+results.substring(colon+1,results.length);  
	  }
	  if (results.substring(0,colon)=="EXIT") {
		  clearInterval(ajax2int);
			window.location.href="showchargeinfo.php?show="+results.substring(colon+1,results.length);  
	  }
	  if (results.substring(0,colon)=="EXITGROUPCHAT") {
		  	window.location.href="showchargeinfogroupchat.php?event=ajax2done-exitgroupchat&session="+results.substring(colon+1,results.length);  
	  }
		}
    }
  }
}

function ajax3(url,target) {
  // native XMLHttpRequest object 
  var now = new Date();
  var hour        = now.getHours() + '';
  var minute      = now.getMinutes() + '';
  var second      = now.getSeconds() + '';
  var timenow = hour + minute + second;
  //url = url + "&time=" + timenow;
  if (window.XMLHttpRequest) {
    req11 = new XMLHttpRequest();
    req11.onreadystatechange = function() {ajax3done(target);};
    req11.open("GET", url + "&time=" + timenow, true);
    req11.send(null);
    // IE/Windows ActiveX version
  } else if (window.ActiveXObject) {
    req11 = new ActiveXObject("Microsoft.XMLHttp");
    if (req11) {
      req11.onreadystatechange = function() {ajax3done(target);};
      req11.open("GET", url + "&time=" + timenow, true);
      req11.send(null);
    }
  }
}

function ajax3done(target) {
  // only if req is "loaded"
  if (req11.readyState == 4) {
    // only if "OK"
    if (req11.status == 200 || req11.status == 304) {
		if (target!="null") {
      results = req11.responseText;
      
      document.getElementById(target).innerHTML = results;
	  colon = results.indexOf(":");
	  if (results.substring(0,colon)=="ZERO") {
			window.location.href="showchargeinfo.php?topup=1&show="+results.substring(colon+1,results.length);  
	  }
	  if (results.substring(0,colon)=="ZEROGROUPCHAT") {
			window.location.href="showchargeinfogroupchat.php?topup=1&session="+results.substring(colon+1,results.length);  
	  }	  
	  if (results.substring(0,colon)=="EXIT") {
			window.location.href="showchargeinfo.php?show="+results.substring(colon+1,results.length);  
	  }
	  if (results.substring(0,colon)=="EXITGROUPCHAT") {
			window.location.href="showchargeinfogroupchat.php?event=ajax3done-exitgroupchat&session="+results.substring(colon+1,results.length);  
	  }

		}
    }
  }
}

function addchattext(url, target, value) {
  // native XMLHttpRequest object 
  document.getElementById('chattext').value = '';
  url = url + '&value=' + escape(value);
  var now = new Date();
  var hour        = now.getHours() + '';
  var minute      = now.getMinutes() + '';
  var second      = now.getSeconds() + '';
  var timenow = hour + minute + second;
  url = url + '&time=' + timenow;
  if (window.XMLHttpRequest) {
    req5 = new XMLHttpRequest();
    req5.onreadystatechange = function() {ajaxaddchatdone(target);};
    req5.open("GET", url, true);
    req5.send(null);
    // IE/Windows ActiveX version
  } else if (window.ActiveXObject) {
    req5 = new ActiveXObject("Microsoft.XMLHttp");
    if (req5) {
      req5.onreadystatechange = function() {ajaxaddchatdone(target);};
      req5.open("GET", url, true);
      req5.send(null);
    }
  }
}



function validateamount(url, target, value) {
  // native XMLHttpRequest object 
  //alert(value);
  //document.getElementById('chattext').value = '';
  url = url + '&floAmount=' + value;
  var now = new Date();
  var hour        = now.getHours() + '';
  var minute      = now.getMinutes() + '';
  var second      = now.getSeconds() + '';
  var timenow = hour + minute + second;
  url = url + '&time=' + timenow;
  if (window.XMLHttpRequest) {
    req6 = new XMLHttpRequest();
    req6.onreadystatechange = function() {validateamountdone(target);};
    req6.open("GET", url, true);
    req6.send(null);
    // IE/Windows ActiveX version
  } else if (window.ActiveXObject) {
    req6 = new ActiveXObject("Microsoft.XMLHttp");
    if (req6) {
      req6.onreadystatechange = function() {validateamountdone(target);};
      req6.open("GET", url, true);
      req6.send(null);
    }
  }
  //setTimeout("ajax('" + url + "','username_label')", 1000);
}

function validateamountdone(target) {
  // only if req is "loaded"
  if (req6.readyState == 4) {
    // only if "OK"
    if (req6.status == 200 || req6.status == 304) {
		if (target!="null") {
      results = req6.responseText;
      document.getElementById(target).innerHTML = results;
		}
    }
  }
}

function updateamount(url, target, amountleft, amountpayed, insertid) {
  url = url + '&amountleft=' + amountleft + '&amountpayed=' + amountpayed + '&insertid=' + insertid;
  var now = new Date();
  var hour        = now.getHours() + '';
  var minute      = now.getMinutes() + '';
  var second      = now.getSeconds() + '';
  var timenow = hour + minute + second;
  url = url + '&time=' + timenow;
  if (window.XMLHttpRequest) {
    req7 = new XMLHttpRequest();
    req7.onreadystatechange = function() {updateamountdone(target);};
    req7.open("GET", url, true);
    req7.send(null);
    // IE/Windows ActiveX version
  } else if (window.ActiveXObject) {
    req7 = new ActiveXObject("Microsoft.XMLHttp");
    if (req7) {
      req7.onreadystatechange = function() {updateamountdone(target);};
      req7.open("GET", url, true);
      req7.send(null);
    }
  }
}

function updateamountdone(target) {
	// only if req is "loaded"
	if (req7.readyState == 4) {
		// only if "OK"
		if (req7.status == 200 || req7.status == 304) {
			if (target!="null") {
				results = req7.responseText;
				document.getElementById(target).innerHTML = results;
				colon = results.indexOf(":");
				if (results.substring(0,colon)=="ZERO") {
					window.location.href="showchargeinfovoyeur.php?topup=1&session="+results.substring(colon+1,results.length);  
				}
				if (results.substring(0,colon)=="ZEROPRIVATE") {
					window.location.href="showchargeinfoprivate.php?topup=1&session="+results.substring(colon+1,results.length);  
				}
				if (results.substring(0,colon)=="EXITPRIVATE") {
					window.location.href="showchargeinfoprivate.php?&session="+results.substring(colon+1,results.length);  
				}
				if (results.substring(0,colon)=="EXIT") {
					window.location.href="showchargeinfovoyeur.php?session="+results.substring(colon+1,results.length);  
				}
			}
		}
	}
}

function strreplace(mysubject, smatch, sreplace) {
	while (mysubject.indexOf(smatch)>-1) {
		mysubject=mysubject.replace(smatch,sreplace);	
	}
	return mysubject;
	
}

function striptags(mysubject) {
	mysubject = strreplace(mysubject,"<","&lt;");
	mysubject = strreplace(mysubject,">","&gt;");
  	return mysubject;
}


function getwatchers(url,target) {
  // native XMLHttpRequest object 
  var now = new Date();
  var hour        = now.getHours() + '';
  var minute      = now.getMinutes() + '';
  var second      = now.getSeconds() + '';
  var timenow = hour + minute + second;
  //url = url + "&time=" + timenow;
  if (window.XMLHttpRequest) {
    chatreq10 = new XMLHttpRequest();
    chatreq10.onreadystatechange = function() {getwatchersdone(target);};
    chatreq10.open("GET", url + "&time=" + timenow, true);
    chatreq10.send(null);
    // IE/Windows ActiveX version
  } else if (window.ActiveXObject) {
    chatreq10 = new ActiveXObject("Microsoft.XMLHttp");
    if (chatreq10) {
      chatreq10.onreadystatechange = function() {getwatchersdone(target);};
      chatreq10.open("GET", url + "&time=" + timenow, true);
      chatreq10.send(null);
    }
  }
  //setTimeout("ajaxchat('" + url + "','" + target + "')", 1000);
}

function getwatchersdone(target) {
// only if req is "loaded"
  if (chatreq10.readyState == 4) {
    // only if "OK"
    if (chatreq10.status == 200 || chatreq10.status == 304) {
		if (target!="null") {
      results = chatreq10.responseText;
	  var oldresults = document.getElementById(target).innerHTML;
      document.getElementById(target).innerHTML = results;
		}
    }
  }
}

function initalarm(camstar) {
	
	setInterval(function() { checkalarm(camstar); }, 5000);	
	
}

function checkalarm(camstar) {
	
  var now = new Date();
  var hour        = now.getHours() + '';
  var minute      = now.getMinutes() + '';
  var second      = now.getSeconds() + '';
  var timenow = hour + minute + second;
  url = "alarm.php?id=" + camstar + "&time=" + timenow;
  if (window.XMLHttpRequest) {
    alarmreq = new XMLHttpRequest();
    alarmreq.onreadystatechange = function() {checkalarmdone();};
    alarmreq.open("GET", url, true);
    alarmreq.send(null);
    // IE/Windows ActiveX version
  } else if (window.ActiveXObject) {
    alarmreq = new ActiveXObject("Microsoft.XMLHttp");
    if (alarmreq) {
      alarmreq.onreadystatechange = function() {checkalarmdone();};
      alarmreq.open("GET", url, true);
      alarmreq.send(null);
    }
  }
}

function checkalarmdone() {
	
  if (alarmreq.readyState == 4) {
    // only if "OK"
    if (alarmreq.status == 200) {
      document.getElementById('sound').innerHTML = alarmreq.responseText;
    }
  }
	
}

function openMembersInfo() {
  window.open("membersinfo.php", "membersinfo", "location=0, status=0, scrollbars=0, width=400, height=380, resize=0"); 
}

function chooseMember() {
  window.open("choosemember.php", "choosemember", "location=0, status=0, scrollbars=0, width=400, height=340, resize=0"); 
}

function chooseCamstar() {
  window.open("choosecamstar.php", "choosecamstar", "location=0, status=0, scrollbars=0, width=400, height=340, resize=0"); 
}

function chooseDate(which) {
  window.open("choosedate.php?which="+which, "choosedate", "location=0, status=0, scrollbars=0, width=400, height=340, resize=0"); 
}

function chooseFilter(which) {
  window.open("choosefilter.php?which="+which, "choosefilter", "location=0, status=0, scrollbars=0, width=400, height=340, resize=0"); 
}

function starCamstar(uid) {
  window.open("choosefilter.php?which=starcamstar&uid="+uid, "choosefilter", "location=0, status=0, scrollbars=0, width=400, height=340, resize=0"); 
}

function frd(rurl) {
	// window.location=rurl;
	window.parent.goaway(rurl);
    
	if (document.getElementById("voyeurcontent")) {
		document.getElementById("vexit").value=1;
	}
		
  var so = new SWFObject("redirect.swf", "redirect", "1", "1", "9", "#FFFFFF");
  so.addVariable("myURL", rurl);
  so.write("frdcontent");
}

function hideWarning() {
	document.getElementById('foo').style.display='none';
	document.getElementById("madechoice").value=1;	
}

function openTod() {
	if (document.getElementById("tod")) {
		document.getElementById("tod").style.display='block';
	}
}

function closeTod() {
	if (document.getElementById("tod")) {
		document.getElementById("tod").style.display='none';
	}
}