var BuzzerHTTPReq=getHTTPReq();
var getTimeHttpReq;
var SyncNVRTime = 30000;

function replaceTextChild(parent, newText)
{
	if( typeof(parent)=="undefined" || typeof(newText)!="string" ) return;
	else if (parent.hasChildNodes() == false && newText.length!=0) parent.appendChild(document.createTextNode(newText) );
	else if (parent.hasChildNodes())
	{
		if(newText.length!=0) parent.replaceChild(document.createTextNode(newText),parent.firstChild);
		else parent.replaceChild(document.createTextNode(" "),parent.firstChild);
	}
}

function getText(that)
{
	try {
		if( typeof(that)=="undefined" ) return "";
		else if (that.hasChildNodes == false ) return ""; 
		else 
		{
			var result = that.firstChild.data;
			if(typeof(result)=='undefined') return "";
			return result.toString();	
		}
	}
	catch(e) {	return "";	}
}

function getEventTarget(event)
{
	var result;
	if(typeof(event.target)!="undefined") result=event.target;
	else result=event.srcElement;
	return result;
}

function stopbuzzer()
{
	BuzzerHTTPReq=getHTTPReq();
	if (typeof(BuzzerHTTPReq)=="undefined")	return;
	BuzzerHTTPReq.open("POST", "http://" +webipport+ "/buzzerstop.cgi", true);
	BuzzerHTTPReq.send(null);
	document.getElementById("stopbuzzerbutton").style.display="none";
}

function getMonthString(mm)
{
	if(mm==1) return time_jan;
	if(mm==2) return time_feb;
	if(mm==3) return time_mar;
	if(mm==4) return time_apr;
	if(mm==5) return time_may;
	if(mm==6) return time_jun;
	if(mm==7) return time_jul;
	if(mm==8) return time_aug;
	if(mm==9) return time_sep;
	if(mm==10) return time_oct;
	if(mm==11) return time_nov;
	if(mm==12) return time_dec ;
	
	if(mm==time_jan) return "01";
	if(mm==time_feb) return "02";
	if(mm==time_mar) return "03";
	if(mm==time_apr) return "04";
	if(mm==time_may) return "05";
	if(mm==time_jun) return "06";
	if(mm==time_jul) return "07";
	if(mm==time_aug) return "08";
	if(mm==time_sep) return "09";
	if(mm==time_oct) return "10";
	if(mm==time_nov) return "11";
	if(mm==time_dec) return "12" ;
	return "";
}

var ParaYear=0; ParaMonth=0; ParaDay=0; ParaHour=0; ParaMins=0; ParaSec=0;
var SystemUTCTime=new Date();
var TimezoneBias=new Array();
var summerBias=0;

function getSystemDate(chanellstatus)
{	//show time
	var NowTime = new Date();
	if (chanellstatus)
	{
		replaceTextChild(document.getElementById("time"), NowTime.toLocaleString());
		SystemTimer = setTimeout("getSystemDate(true)", 500);
	}
	else
	{
		replaceTextChild(document.getElementById("time"), NowTime.toLocaleString());
//		window.parent.startFrame.document.getElementById("time").value = NowTime.toString();
		SystemTimer = setTimeout("getSystemDate(false)", 500);
	}
}

var PcTimeStamp=0;

function getSVRSystemTime() {
	getTimeHttpReq=getHTTPReq();
	if(typeof(getTimeHttpReq)=="undefined") return;
	getTimeHttpReq.onreadystatechange=getSVRSystemTimeOK;
	getTimeStamp = new Date().getTime();
	getTimeHttpReq.open("POST", "http://" +webipport+ "/param.cgi?action=get&group=svr.system.time", true);
	getTimeHttpReq.send(null);
}

var SystemTimer;
function clearSystemTimer()
{
	if(typeof(SystemTimer)!='undefined')
	{
		clearTimeout(SystemTimer);
		SystemTimer="";
	}
}	

function OpenCamStatus()
{	var CamStatusWin=window.open("CameraStatusConcept.html",'','toolbar=1,location=1,directories=1,status=1,menubar=1,scrollbars=1,resizable=1');	}

//get width and height of browser
function get_broswerHeight()
{
	var height=0;
	if (typeof window.innerWidth != 'undefined') height=window.innerHeight;
	else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) height=document.documentElement.clientHeight;
	else height=document.getElementsByTagName('body')[0].clientHeight;
	return height;
}

function get_broswerWidth()
{
	var width=0;
	if (typeof window.innerWidth != 'undefined') width=window.innerWidth; 
	else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) width=document.documentElement.clientWidth; 
	else width=document.getElementsByTagName('body')[0].clientWidth;
	return width;
}

function getHTTPReq()
{
	var req;
	if( window.XMLHttpRequest) req = new XMLHttpRequest();
	else if(window.ActiveXObject)
	{
		var msxmls = [ "MSXML2.XMLHttp.4.0", "MSXML2.XMLHttp.3.0", "MSXML2.XMLHttp", "Microsoft.XMLHttp"];
		for(i=0; i<=msxmls.length-1; i++)
		{
			try
			{
				req = new ActiveXObject(msxmls[i]);
				break;
			} catch(e) {	}
		}
	}
	return req;
}

//get 0 from "svr.network.dhcp=0 \n" (svr.network.dhcp:paramName)('\n':splitString)
function getParam(returnString, paramName, splitString)
{
	if (returnString.length==0 || paramName.length==0) return "";
	var start = returnString.indexOf(paramName+ "=");
	if (start==-1) return "";
	start += paramName.length+1;
	var end = returnString.substring(start).indexOf(splitString);
	if (end==-1) return returnString.substring(start);
	return returnString.substring(start,start+end);
}
function getValue(returnString, splitString)
{
	if (returnString.length==0 || splitString.length==0) return "";
	var start = returnString.indexOf(splitString);
	if (start==-1) return "";
	return returnString.substring(start+splitString.length);
}

function RefreshTime(StartTime, EndTime){
	return (EndTime-StartTime)*10;
}

function ModifiedPTZSpeedToTen(ptzSpeedRange, ptzSpeed){
	var speedRange;
	if (ptzSpeedRange.indexOf(",") != -1)
	{
		speedRange = ptzSpeedRange.split(",");
		if (parseInt(speedRange[1], 10) == 0) return 0;
		if (speedRange[0] == speedRange[1]) return speedRange[0];
		return Math.round(parseInt(ptzSpeed, 10) * 10 / parseInt(speedRange[1], 10));
	}
	else return 0;
}

function TransNamePass(oNamePass){
	var NewNamePass="";
	for (i=0; i<oNamePass.length; i++)
	{
		switch (oNamePass.charAt(i))
		{
			case "%":
				NewNamePass=NewNamePass+"%25";
				break;
			
			case "=":
				NewNamePass=NewNamePass+"%3D";
				break;

			case "&":
				NewNamePass=NewNamePass+"%26";
				break;

			case "?":
				NewNamePass=NewNamePass+"%3F";
				break;

			case "#":
				NewNamePass=NewNamePass+"%23";
				break;				

			default:
				NewNamePass=NewNamePass+oNamePass.charAt(i);
		}
	}
	return NewNamePass;
}

//parameter
function getFrameRate(int)
{
	if(int==0) return "Full";
	if(int==1) return "1";
	if(int==2) return "5";
	if(int==3) return "10";
	if(int==4) return "15";
	if(int==5) return "20";
	return "";
}

function getShowStr(strOption)
{
	if (strOption == "left") return camera_left;
	if (strOption == "right") return camera_right;
	if (strOption == "both") return camera_both;
	if (strOption == "auto") return camera_auto;
	if (strOption == "simple") return "Full Image";
	if (strOption == "pip") return "Picture in Picture";
	if (strOption == "piz") return "Picture in Zoom";
	if (strOption == "eip") return "Event in Picture";
	return strOption;
}