﻿function GetXmlHttpObject()
{
	if (window.XMLHttpRequest)
	  {
	  // code for IE7+, Firefox, Chrome, Opera, Safari
	  return new XMLHttpRequest();
	  }
	if (window.ActiveXObject)
	  {
	  // code for IE6, IE5
	  return new ActiveXObject("Microsoft.XMLHTTP");
	  }
	return null;
}

function ajaxInit()
{
	//Function sending the first Ajax-request
	xmlhttp = GetXmlHttpObject();
	xmlhttp.onreadystatechange=firstStateChanged;
	url = "/products/video/selector/listing.php"; //full path needed due to language folders
	//url += "?"+makeQueryString();
	if(document.getElementById("listTypeList").checked)
	   	url += "?listType=list";
	else
		url += "?listType=img";
	if(_get_cookie('prodSelectorProdType') == "")
		_set_session_cookie('prodSelectorProdType', 'camera');

	var ptFromCookie = _get_cookie('prodSelectorProdType');
	/*var prodsFromCookie = _get_cookie('prodSelectorCookie');
	prodsFromCookie = prodsFromCookie.split(",");
	//alert(prodsFromCookie);
	for(i = 1; i <= prodsFromCookie.length; i++){
		url += "&cmp"+i+"="+prodsFromCookie[i-1];
	}*/
	if(ptFromCookie != ""){ //cookie is set
		url += "&prodType="+ptFromCookie;
	}else{
		url += "&prodType=cameras";
	}
	timestamp = new Date();
	timestamp = timestamp*1; //gives milliseconds
	timestamp = Math.round((((timestamp/1000)/60)/60)/24);
	url += "&sid="+timestamp;
	//alert(url);
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);

}

function sendAjaxRequest(id, value)
{
	if((id == "distanceFromCamSlider" || id == "sceneWidthSlider" || id == "sceneHeightSlider") && !document.getElementById("chkFovActivate").checked)
		return;
	if((id == "resDistanceFromCamSlider" || id == "selSurvObj") && !document.getElementById("chkResActivate").checked)
		return;
	xmlhttp = GetXmlHttpObject();
	xmlhttp.onreadystatechange=stateChanged;
	url = "/products/video/selector/listing.php";
	url += "?"+makeQueryString();
	if(document.getElementById("listTypeList").checked)
	   	url += "&listType=list";
	else
		url += "&listType=img";
	if(value != false && value != "select"){
		//send the click along for stats
		if(value != true){
			//must be a dropdown, include both id and value
			url += "&filter=" + id +"-"+ value;
		}
		else{
			//must be a checkbox, just include id
			url += "&filter=" + id;
		}
	}
//	alert("id: " + id + ", value: " + value);
	timestamp = new Date();
	timestamp = timestamp*1; //gives milliseconds
	timestamp = Math.round((((timestamp/1000)/60)/60)/24);
	url += "&sid="+timestamp;
	//url += "&sid="+Math.random();
	//alert(url);
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}


function stateChanged()
{
	if(xmlhttp.readyState==4)
		  {
			  // Get data from the server's response
			pos = xmlhttp.responseText.toLowerCase().indexOf(('notfound').toLowerCase());
			if (pos == -1){
				document.getElementById("selectionList").innerHTML = xmlhttp.responseText;
				document.getElementById("errorMsg").innerHTML = "";
				document.getElementById("ajaxLoadIcon").style.display = 'none';
			}
			else{
				document.getElementById("ajaxLoadIcon").style.display = 'none';
				document.getElementById("errorMsg").innerHTML = xmlhttp.responseText;
				document.getElementById("selectionList").innerHTML = ""; // NO PRODUCTS WERE RETURNED, BLANK PAGE
			}
		  }
	if(xmlhttp.readyState==3 || xmlhttp.readyState==2 || xmlhttp.readyState==1)
		  {
			  document.getElementById("ajaxLoadIcon").style.display = 'block';
		  }

}

function firstStateChanged()
{
	//for the first ajax request (init)
	if(xmlhttp.readyState==4)
		  {
			  // Get data from the server's response
			pos = xmlhttp.responseText.toLowerCase().indexOf(('notfound').toLowerCase());
			if (pos == -1){
				document.getElementById("selectionList").innerHTML = xmlhttp.responseText;
				document.getElementById("errorMsg").innerHTML = "";
				document.getElementById("ajaxLoadIcon").style.display = 'none';
			}
			else{
				document.getElementById("ajaxLoadIcon").style.display = 'none';
				document.getElementById("errorMsg").innerHTML = xmlhttp.responseText;
			}
			setProdType();
			
		  }
	if(xmlhttp.readyState==3 || xmlhttp.readyState==2 || xmlhttp.readyState==1)
		  {
			  document.getElementById("ajaxLoadIcon").style.display = 'block';
		  }

}



function makeQueryString(){
	qstring = "";
	if(document.getElementById("chkDayNight").checked){
		if(qstring != "")
			qstring += "&";
		qstring += "chkDayNight=true";
	}
	if(document.getElementById("chkPoe").checked){
		if(qstring != "")
			qstring += "&";
		qstring += "chkPoe=true";
	}
	if(document.getElementById("chkAudio").checked){
		if(qstring != "")
			qstring += "&";
		qstring += "chkAudio=true";
	}
	if(document.getElementById("chkProgScan").checked){
		if(qstring != "")
			qstring += "&";
		qstring += "chkProgScan=true";
	}
	if(document.getElementById("chkWdr").checked){
		if(qstring != "")
			qstring += "&";
		qstring += "chkWdr=true";
	}
	if(document.getElementById("chkPtzSup").checked){
		if(qstring != "")
			qstring += "&";
		qstring += "chkPtzSup=true";
	}
	if(document.getElementById("chkPtzOp").checked){
		if(qstring != "")
			qstring += "&";
		qstring += "chkPtzOp=true";
	}
	if(document.getElementById("chkMotionDet").checked){
		if(qstring != "")
			qstring += "&";
		qstring += "chkMotionDet=true";
	}
	if(document.getElementById("chkTampAlarm").checked){
		if(qstring != "")
			qstring += "&";
		qstring += "chkTampAlarm=true";
	}
	if(document.getElementById("chkInputOutput").checked){
		if(qstring != "")
			qstring += "&";
		qstring += "chkInputOutput=true";
	}
	if(document.getElementById("chkLocalStorage").checked){
		if(qstring != "")
			qstring += "&";
		qstring += "chkLocalStorage=true";
	}
	if(document.getElementById("chkAVHS").checked){
		if(qstring != "")
			qstring += "&";
		qstring += "chkAVHS=true";
	}
	if(document.getElementById("chkTwowayAudio").checked){
		if(qstring != "")
			qstring += "&";
		qstring += "chkTwowayAudio=true";
	}
	if(document.getElementById("chkVarifocal").checked){
		if(qstring != "")
			qstring += "&";
		qstring += "chkVarifocal=true";
	}
	if(document.getElementById("chkChngLens").checked){
		if(qstring != "")
			qstring += "&";
		qstring += "chkChngLens=true";
	}
	if(document.getElementById("chkDigZoom").checked){
		if(qstring != "")
			qstring += "&";
		qstring += "chkDigZoom=true";
	}
	if(document.getElementById("chkAudioDetection").checked){
		if(qstring != "")
			qstring += "&";
		qstring += "chkAudioDetection=true";
	}
	if(document.getElementById("chkWireless").checked){
		if(qstring != "")
			qstring += "&";
		qstring += "chkWireless=true";
	}
	if(document.getElementById("chkVandalRes").checked){
		if(qstring != "")
			qstring += "&";
		qstring += "chkVandalRes=true";
	}
	if(document.getElementById("chkRuggedized").checked){
		if(qstring != "")
			qstring += "&";
		qstring += "chkRuggedized=true";
	}
	if(document.getElementById("prodType").value){
		if(qstring != "")
			qstring += "&";
		qstring += "prodType="+document.getElementById("prodType").value;
	}
	if(document.getElementById("prodFamilyCams").value != "select"){
		if(qstring != "")
			qstring += "&";
		qstring += "prodFamilyCams="+document.getElementById("prodFamilyCams").value;
	}
	if(document.getElementById("prodFamilyEncoders").value != "select"){
		if(qstring != "")
			qstring += "&";
		qstring += "prodFamilyEncoders="+document.getElementById("prodFamilyEncoders").value;
	}
	if(document.getElementById("prodFamilyThermal").value != "select"){
		if(qstring != "")
			qstring += "&";
		qstring += "prodFamilyThermal="+document.getElementById("prodFamilyThermal").value;
	}

	/*if(document.getElementById("selInOutDoor").value != "select"){
		if(qstring != "")
			qstring += "&";
		qstring += "selInOutDoor="+document.getElementById("selInOutDoor").value;
	}*/
	if(document.getElementById("chkOutdoorReady").checked){
		if(qstring != "")
			qstring += "&";
		qstring += "chkOutdoorReady=true";
	}
	if(document.getElementById("chkOutdoorReadyThermal").checked){
		if(qstring != "")
			qstring += "&";
		qstring += "chkOutdoorReadyThermal=true";
	}
	if(document.getElementById("chkColorPalette").checked){
		if(qstring != "")
			qstring += "&";
		qstring += "chkColorPalette=true";
	}

	if(document.getElementById("selMaxRes").value != "select"){
		if(qstring != "")
			qstring += "&";
		qstring += "selMaxRes="+document.getElementById("selMaxRes").value;
	}
	if(document.getElementById("selMaxResThermal").value != "select"){
		if(qstring != "")
			qstring += "&";
		qstring += "selMaxResThermal="+document.getElementById("selMaxResThermal").value;
	}
	if(document.getElementById("selCompression").value != "select"){
		if(qstring != "")
			qstring += "&";
		qstring += "selCompression="+document.getElementById("selCompression").value;
	}
	if(document.getElementById("selChannels").value != "select"){
		if(qstring != "")
			qstring += "&";
		qstring += "selChannels="+document.getElementById("selChannels").value;
	}
	if(document.getElementById("selSerialPort").value != "select"){
		if(qstring != "")
			qstring += "&";
		qstring += "selSerialPort="+document.getElementById("selSerialPort").value;
	}
	if(document.getElementById("selOptZoom").value != "select"){
		if(qstring != "")
			qstring += "&";
		qstring += "selOptZoom="+document.getElementById("selOptZoom").value;
	}
	if(document.getElementById("selDetRange").value != "select"){
		if(qstring != "")
			qstring += "&";
		qstring += "selDetRange="+document.getElementById("selDetRange").value;
		//qstring += "selDetRangePerson="+document.getElementById("selDetRangePerson").value+"&selDetRangeVehicle="+document.getElementById("selDetRangeVehicle").value;
	}
	/*if(document.getElementById("txtSceneWidth").value != ""){
		txtSceneWidth = document.getElementById("txtSceneWidth").value;
		if(isNumber("txtSceneWidth")){
			if(qstring != "")
				qstring += "&";
			qstring += "txtSceneWidth="+document.getElementById("txtSceneWidth").value;
		}
	}
	if(document.getElementById("txtSceneHeight").value != ""){
		txtSceneHeight = document.getElementById("txtSceneHeight").value;
		if(isNumber("txtSceneHeight")){
			if(qstring != "")
				qstring += "&";
			qstring += "txtSceneHeight="+document.getElementById("txtSceneHeight").value;
		}
	}
	if(document.getElementById("txtDistanceFromCam").value != ""){
		txtDistanceFromCam = document.getElementById("txtDistanceFromCam").value;
		if(isNumber("txtDistanceFromCam")){
			if(qstring != "")
				qstring += "&";
			qstring += "txtDistanceFromCam="+document.getElementById("txtDistanceFromCam").value;
		}
	}
	*/
	// ----------------------------- SLIDERS BEGIN ------------------------------------------
	if(document.getElementById("distanceFromCamSlider").value != "" && $("#distanceFromCamSlider" ).slider( "option", "value" ) > 0 && document.getElementById("chkFovActivate").checked){
		//distanceFromCamSlider = $("#distanceFromCamSlider" ).slider("option", "value");
		//if(isNumber("distanceFromCamSlider ")){
			if(qstring != "")
				qstring += "&";
			qstring += "txtDistanceFromCam="+$("#distanceFromCamSlider" ).slider( "option", "value" );
		//}
	}
	if(document.getElementById("sceneHeightSlider").value != "" && $("#sceneHeightSlider" ).slider( "option", "value" ) > 0 && document.getElementById("chkFovActivate").checked){
		//distanceFromCamSlider = $("#sceneHeightSlider" ).slider( "option", "value" );
		//if(isNumber("distanceFromCamSlider ")){
			if(qstring != "")
				qstring += "&";
			qstring += "txtSceneHeight="+$("#sceneHeightSlider" ).slider( "option", "value" );
		//}
	}
	if(document.getElementById("sceneWidthSlider").value != "" && $("#sceneWidthSlider" ).slider( "option", "value" ) > 0 && document.getElementById("chkFovActivate").checked){
		//distanceFromCamSlider = $("#sceneWidthSlider" ).slider( "option", "value" );
		//if(isNumber("distanceFromCamSlider ")){
			if(qstring != "")
				qstring += "&";
			qstring += "txtSceneWidth="+$("#sceneWidthSlider" ).slider( "option", "value" );
		//}
	}
	if(document.getElementById("resDistanceFromCamSlider").value != "" && $("#resDistanceFromCamSlider" ).slider( "option", "value" ) > 0 && document.getElementById("chkResActivate").checked){
		//distanceFromCamSlider = $("#sceneWidthSlider" ).slider( "option", "value" );
		//if(isNumber("distanceFromCamSlider ")){
			if(qstring != "")
				qstring += "&";
			qstring += "resDistanceFromCam="+$("#resDistanceFromCamSlider" ).slider( "option", "value" );
		//}
	}
	// ----------------------------- SLIDERS END ---------------------------------------------
	
	if(document.getElementById("selSurvObj").value != "select" && document.getElementById("chkResActivate").checked){
		if(qstring != "")
			qstring += "&";
		qstring += "selSurvObj="+document.getElementById("selSurvObj").value;
	}



	if(Comparison.slots[0] != "" && Comparison.slots[0] != undefined)
		qstring += "&cmp1="+Comparison.slots[0];
	if(Comparison.slots[1] != "" && Comparison.slots[1] != undefined)
		qstring +="&cmp2="+Comparison.slots[1];
	if(Comparison.slots[2] != "" && Comparison.slots[2] != undefined)
		qstring += "&cmp3="+Comparison.slots[2];
	if(Comparison.slots[3] != "" && Comparison.slots[3] != undefined)
		qstring += "&cmp4="+Comparison.slots[3]
	if(Comparison.slots[4] != "" && Comparison.slots[4] != undefined)
		qstring +="&cmp5="+Comparison.slots[4];
		
	return qstring;
}

function isNumber(id){
	validChars = new Array("0","1","2","3","4","5","6","7","8","9");
	txtVal = document.getElementById(id).value;
	if(txtVal != ""){
		for(i = 0; i < txtVal.length; i++){
			tmp = txtVal.charAt(i);
			if(tmp in validChars){
				//ok so far
				valid = true;
			}else{
				//not a valid  number!
				valid = false;
				break;
			}
		}
		if(valid){
			return true;
		}else{
			document.getElementById(id).value = document.getElementById(id).value.substring(0,document.getElementById(id).value.length-1); //strip the last entered character
			secondCheck = isNumber(id);
			alert("Only numbers allowed!");
			return secondCheck;
		}
	}
	else{
		return false;
		}
}
