var cpm_selCam=0;

function ptzcontrol(selpcam, type, action, lens)
{	
	HTTPReq=getHTTPReq();
	if (typeof(HTTPReq)=="undefined")	return false;
	if (lens.length != 0 && lens.indexOf("CH") == -1) lens = "&lensmode=" +lens + "&imagesizex=" + var_CameraLiveResolution[selpcam].split("x")[0] + "&imagesizey=" + var_CameraLiveResolution[selpcam].split("x")[1];
	else if (lens.length != 0 && lens.indexOf("CH") != -1) lens = "&channel=" + (parseInt(lens.replace("CH", ""), 10)-1).toString();
	else lens = "";
	
	var query="http://" +webipport+ "/ptz/ptz.cgi?camera=" +selpcam;
	
	if(type==0) {	HTTPReq.open("POST", query+ "&zoom=" +action + lens, true);	}
	else if(type==1)
	{	if(action<2) { HTTPReq.open("POST", query+ "&focus=" +action + lens, true); var_CameraPTZAutoFocus[selpcam] = 0;}
		else if(action==2) {	query="http://" +webipport+ "/param.cgi?action=set&camera_c" +selpcam+ ".ptz.autofocus=1";	HTTPReq.open("POST", query + lens, true);	}
		else if(action==3) {	query="http://" +webipport+ "/param.cgi?action=set&camera_c" +selpcam+ ".ptz.autofocus=0";	HTTPReq.open("POST", query + lens, true);	}
		else if(action==4)
		{	cpm_selCam=selpcam;
			HTTPReq.onreadystatechange=changeAF;
			query="http://" +webipport+ "/param.cgi?action=set&camera_c" +selpcam+ ".ptz.autofocus=1" + lens;
			HTTPReq.open("POST", query + lens, true);
	  		var_CameraPTZAutoFocus[cpm_selCam]= 1;
		}
		else if(action==5)
		{	cpm_selCam=selpcam;
			HTTPReq.onreadystatechange=changeAF;
			query="http://" +webipport+ "/param.cgi?action=set&camera_c" +selpcam+ ".ptz.autofocus=0" + lens;
	  		//query= query+ (var_CameraPTZAutoFocus[selpcam]=="0"? "1":"0");
			HTTPReq.open("POST", query + lens, true);
	  		var_CameraPTZAutoFocus[cpm_selCam]= 0;
		}
	} else if(type==2)
	{	if(action<2) HTTPReq.open("POST", query+ "&iris=" +action + lens, true);
		else if(action==2) {	query="http://" +webipport+ "/param.cgi?action=set&camera_c" +selpcam+ ".ptz.autoiris=1" + lens;	HTTPReq.open("POST", query, true);	}
		//else if(action==3) {	query="http://" +webipport+ "/param.cgi?action=set&camera_c" +selpcam+ ".ptz.autoiris=0";	HTTPReq.open("POST", query, true);	}
	} else if(type==3)
	{	
	//if(action==0) {	HTTPReq.open("POST", query+ "&gotopresetno=21", true);	}
		//else {	
		HTTPReq.open("POST", query+ "&move=" +action + lens, true);	
	}
	else if(type==4) {	HTTPReq.open("POST", query+ "&gotopresetno=" +action, true);	}
	else if(type==5)
	{	if(action==0) {	query="http://" +webipport+ "/param.cgi?action=set&camera_c" +selpcam+ ".ptz.presetseq=0&camera_c" +selpcam+ ".ptz.presetseqenabled=0" ;}
		else if(action==1) {	query="http://" +webipport+ "/param.cgi?action=set&camera_c" +selpcam+ ".ptz.presetseq=1&camera_c" +selpcam+ ".ptz.presetseqenabled=1";	}
		HTTPReq.open("POST", query, true);
	}
	else if(type==6)
	{	HTTPReq.open("POST", query+ "&autopan=" +action, true);		}
	HTTPReq.send(null);
	return true;
}

function changeAF()
{	if (HTTPReq.readyState == 4) 
	{	if(HTTPReq.responseText.toString().substring(0,2)=="OK")
		{	//var_CameraPTZAutoFocus[cpm_selCam]= 1;	
		}
	}
}

function ptzaction(cam, type, action, lens)
{	
	if(PTZChanPri[cam]==false)	
	{
		alert(err_noauthorized);
		return 0;
	}
	
	if(type==0)	//zoom
	{	
		if(var_CameraPTZZoomCap[cam]==0) 
		{	
			alert(seqview_notsupport);
			return 0;
		}
	}
	else if(type==1)	//focus
	{	
		if(var_CameraPTZFocusCap[cam]==0) 
		{	
			alert(seqview_notsupport);
			return 0;
		}
		if((action == 4 || action == 5) && var_CameraPTZAutoFocusCap[cam] == 0) 
		{	
			alert(seqview_notsupport);
			return 0;
		}
/*		if(action!=2 && action!=3)
		{	if(var_CameraPTZAutoFocus[cam]==0) action=2;
			else if(var_CameraPTZAutoFocus[cam]==1) action=3;
			else return 0;
		}*/
	}
	else if(type==2)	//iris
	{	if(var_CameraPTZIrisCap[cam]==0) {	alert(seqview_notsupport);	return 0;	}
		if(action == 2 && var_CameraPTZAutoIrisCap[cam] == 0) {	alert(seqview_notsupport);	return 0;	}
		//if(var_CameraPTZAutoIris[cam]==0) action=2;
		//else if(var_CameraPTZAutoIris[cam]==1) action=3;
		//else return 0;
	}
	else if(type==3)	//goto home
	{	
		if(var_CameraPTZDirCap[cam]==0) {	alert(seqview_notsupport);	return 0;	}
		else if (action > var_CameraPTZMoveCap[cam]) {	alert(seqview_notsupport);	return 0;	}
		else if (var_PTZPanCap[cam]==0 && (action==1 || action==2))
		{
			alert(seqview_notsupport);
			return 0;
		}
		else if (var_PTZTiltCap[cam]==0 && (action==3 || action==4))
		{
			alert(seqview_notsupport);
			return 0;
		}		
	}
	else if(type==4)	//gotopresetno
	{	if(var_CameraPTZPresetCap[cam]==0) {	alert(seqview_notsupport);	return 0;	}
		action=document.getElementById("presetposlist").value;
		if(action.length==0) {	alert(seqview_notselprepos);	return 0;	}
	}
	else if(type==5)	//set presetseq start or stop
	{	if(var_CameraPTZPresetCap[cam]==0) {	alert(seqview_notsupport);	return 0;	}	}
	
	else if(type==6)	//auto pan
	{	
		if(var_CameraPTZAutoPanCap[cam]==0) {	alert(seqview_notsupport);	return 0;	}	
		if(action == 1 && (var_CameraPTZAutoPanCap[cam]&1) == 0) {	alert(seqview_notsupport);	return 0;	}	
		if(action == 2 && (var_CameraPTZAutoPanCap[cam]&2) == 0) {	alert(seqview_notsupport);	return 0;	}	
		if(action == 3 && (var_CameraPTZAutoPanCap[cam]&4) == 0) {	alert(seqview_notsupport);	return 0;	}			
	}
	
	if(ptzcontrol(cam, type, action, lens)==true) return cam;
	else return 0;
}