
var a_clicked = 0;
var selectedCam = 0;
var selectedProd = 0;
var selectedProd_arr = [];

//--------------------------------------------------- AJAX

function refreshProductList(group,selected_product,groupType,prod_list,id,option_no) { 
	
//showProductInfo(0,'product');
//selectProd(0);
//alert("start");
if(document.getElementById('submit_button')){
	document.getElementById('submit_button').disabled=true;
}
if(id){
	if((groupType == "cable_path") || (groupType == "camera_group")){
		if(document.getElementById('groupbox_'+id).checked==false){
			group = 0;
		}	
	}else{
		if(document.getElementById('check_'+id).checked==false){
			group = 0;
		}	
	}
		
}

 
eval(prod_list+'_xmlHttp = GetXmlHttpObject()');

if (eval(prod_list+'_xmlHttp')==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  } 
 
        
eval(prod_list+'_xmlHttp').onreadystatechange=function() {
		writeHTML(prod_list,prod_list+"_xmlHttp");
		
}        

q_string = window.location.search

var url="inc/list.php";
url=url+"?group="+group;
url=url+"&product="+selected_product;
url=url+"&type="+groupType;
url=url+"&option_no="+option_no;
url=url+"&sid="+Math.random();
url=url+"&country="+country;
url=url+"&"+q_string.substring(1);
url=url+"&active=0";




eval(prod_list+'_xmlHttp').open("GET",url,true);
eval(prod_list+'_xmlHttp').send(null);

	
}


//----------------------------------------------------------


function showProductInfo(product,productType){


xmlHttp=GetXmlHttpObject();

if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  } 

xmlHttp.onreadystatechange = function(){
	writeHTML("product_info","xmlHttp");
	}

var info_url="inc/prod_info.php";
info_url=info_url+"?product="+product;
info_url=info_url+"&type="+productType;
info_url=info_url+"&sid="+Math.random();

xmlHttp.open("GET",info_url,true);
xmlHttp.send(null);
	
}

//------------------------------------------------------------


function writeHTML(id,name) {
	var visible = (document.all) ? 'block' : 'block'; //IE or not...
	
	if(eval(name+".readyState")==4) {
     if(eval(name+".responseText")){
     	if(document.getElementById(id)){
     		document.getElementById(id).style.display = visible;
     		document.getElementById(id).innerHTML=eval(name+".responseText");
     		if(document.getElementById("productList"))
     			document.getElementById("productList").style.zoom = "1";
    	}
     	if(document.getElementById(id+'_title')){
     		document.getElementById(id+'_title').style.display = visible;
    	}
    	
    	if(document.getElementById(id+'_footer')){
     		document.getElementById(id+'_footer').style.display = visible;
    	}
     	
     //	eval("alert("+name+".responseText);");
     	//alert(name);
    }else{
    	if(document.getElementById(id)){
    		document.getElementById(id).style.display = 'none';
    		document.getElementById(id).innerHTML="";
    	}
    	if(document.getElementById(id+'_title')){
    		document.getElementById(id+'_title').style.display = 'none';
    	}
    	
    	if(document.getElementById(id+'_footer')){
    		document.getElementById(id+'_footer').style.display = 'none';
    	}
    	
    }
     
     
     var script_str = eval( name+".responseText").match("<script.*>(.*)</script>");
     	if(name!="xmlHttp" && script_str){
     	eval(script_str[1]);
     	
    	}
    	//alert("done");
    	if(document.getElementById('submit_button')){
    		document.getElementById('submit_button').disabled=false;
    	}
    	
    }
 
}

	
//------------------------------------------------------------


function GetXmlHttpObject() {


var xmlHttp=null;

try {  xmlHttp=new XMLHttpRequest();  }    // Firefox, Opera 8.0+, Safari
  catch (e) {
 	try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } // Internet Explorer 
  catch (e)  {      
  			 xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); 
  			 }
	}

return xmlHttp;
	
}


//------------------------------------------------------------


function selectProd(prodId,id){

if(id){

	if(document.getElementById('check_'+id).checked){ //select
		
		selectedProd_arr.push(prodId);
		//selectedProd = prodId;
	
	}else{																					//deselect
	
		var arr_lenght = selectedProd_arr.length;
		for(var j=0; j<arr_lenght; j++){
			//alert("j="+j+"/"+arr_lenght+", "+prodId + " - " + selectedProd_arr);
			if (selectedProd_arr[j]==prodId){
				selectedProd_arr.splice(j,1)
				j=-1;
			}
		}
		
	}
	selectedProd = selectedProd_arr[selectedProd_arr.length-1]
}	

}


//------------------------------------------------------------------------------------


function selectAll() {

var all_elements = document.forms[0].elements.length;
var	selectAllBox = document.getElementById('check_all');

document.getElementById('check_all').indeterminate = false;

if(selectAllBox.checked == true){
	var newstatus = false;
		
}else{
	var newstatus = true;
	
}	


for(var i=0; i<all_elements; i++){
	if( document.forms[0].elements[i].type == "checkbox"){
		document.forms[0].elements[i].checked =  newstatus;
			if(document.forms[0].elements[i].name != "checkbox_all"){
				var check_id = document.forms[0].elements[i].id.substr(6);
				var prod_id = document.forms[0].elements[i].value
				
				selectProd(prod_id,check_id);
			
		}
	}
}

	
}
//----------------------------------------------------------------------------------------------------------------

function validate_form(form){
	
	valid = false;
	var valid_name;
	var missing = 0;
	for(i=0;i<form.elements.length;i++){
			
		if(form.name=="sendmail"){
				
				if(form.elements[i].value ){
					
					if(form.elements[i].name == 'to' || form.elements[i].name == 'from'){
						
						if(	form.elements[i].value.indexOf('@')>0 && 
								form.elements[i].value.indexOf('.') > form.elements[i].value.indexOf('@')+1 &&
								form.elements[i].value.indexOf('.')< form.elements[i].value.length-2){
							
							valid = true;
						}else{
							
							valid = false;
							missing = form.elements[i].name
							break;
						}
					}else{
						
						valid = true;
					}
				}else{
					valid = false;
					missing = form.elements[i].name
					break;
				}
		}else if(form.elements[i].name == "country"){
			if(form.elements[i].value != "Select Country"){
					valid = true;
					missing = 0;
				}else{
					valid = false;
					missing = form.elements[i].name
					break;
				}
		
		}else{
				
		if(	(form.elements[i].type == 'checkbox' || form.elements[i].type =='radio') &&	form.elements[i].checked == true){
			 	valid = true;
			 	valid_name = form.elements[i].name;
			 	missing = 0;
		}else if(	(form.elements[i].type == 'checkbox' || form.elements[i].type =='radio') &&	form.elements[i].checked == false) {
				
				if(valid_name != form.elements[i].name && form.elements[i].name.substring(form.elements[i].name.length-4) != '_all'){
				
					valid = false;
						if(missing == 0){
							missing = form.elements[i].name
						}
				}
	
		}

 }
}

//Fix the bug for the bracket form -------->
bracketForm = false;
bracketCheck = false;
for(i = 0; i < form.elements.length;i++){
	if(form.elements[i].name == 'bracket'){
		bracketForm = true;
		if( form.elements[i].checked == true || (form.elements[i].type == "hidden" && form.elements[i].value != "")){
			//alert(form.elements[i].value);
			bracketCheck = true;
			//missing = 0;
			break;
		}else{
			bracketCheck=false;
			missing = form.elements[i].name;
		}
	}
	else{
		//bracketCheck = false;
		//missing = form.elements[i].name;
	}
}
// <-----------------------------------------
//alert("bracketForm: " + bracketForm + ". bracketCheck: " + bracketCheck);
	if (valid==false || (bracketForm && !bracketCheck)){
		//alert(valid);
		valid = false;
		//alert(missing.length);
		
		if(missing.substring(missing.length-2)=='[]'){
			missing = missing.substring(0,missing.length-2);
		}else if(missing.substring(missing.length-2,missing.length-1)=='_'){
			missing = missing.substring(0,missing.length-2);
		}
		if(form.name=="sendmail"){
			if(missing == 'to' || missing == 'from'){
				alert("Please entrer a correct email address in the "+missing+" field");
			}else{
				alert("Please fill in the "+missing+" field");
			}
		}else{
			//missing = missing.replace("_"," ")
			
			if(eval('typeof(alert_'+missing+')') != 'undefined'){
				var missing_translated = eval('alert_'+missing);
			}else{
				var missing_translated ='';
			}
					
			alert(please_select+": "+missing_translated );
		
		}
	}
	return valid;
	
}



//----------------------------------------------------------------------------------------------------------------




function call_pdf(){

location.href='result.pdf.php'+window.location.search+'&active=0';	
	
}




function changeRowColor(row_id, color, cursor) {
	var row;
	
	row = document.getElementById(row_id);
	row.style.backgroundColor = color;
	row.style.cursor = cursor;
	
}


//----------------------------------------------------------


function toggle_select(check_id, toggle){

var toggle = (toggle == null) ? 0 : toggle;
var box = document.getElementById(check_id);
var all_elements = document.forms[0].elements.length;



if(box.checked){
	if(box.type != "radio") //making radio buttons un-checkable doesn't make sense //stefan 090925
		box.checked = false;
	}else{
		if(toggle){
			var boxes = eval('document.forms[0].'+box.name);
			var box_no = boxes.length;
			
			
			
			for(i=0; i<box_no;i++) {		//set all checkboxes of the same name to false
			boxes[i].checked = false;
			}
		}			
	if(box.disabled==false){		
	box.checked = true;
	}
	}

if(toggle==0){

	var first = 1
	for(i=0; i<all_elements; i++){
		if((document.forms[0].elements[i].type=="checkbox") && (document.forms[0].elements[i].name != "checkbox_all")){
			
		 if(first == 1 ){
		 if(document.getElementById('check_all')){
		  document.getElementById('check_all').indeterminate = false;
		  document.getElementById('check_all').checked = document.forms[0].elements[i].checked;
			}
		  laststate = document.forms[0].elements[i].checked;
		  first = 0;
		  //alert("first "+document.forms[0].elements[i].checked);
		}else{
	
			if(document.forms[0].elements[i].checked != laststate){
				if(document.getElementById('check_all')){
				document.getElementById('check_all').indeterminate = true;
			}
			}
		 //alert(document.forms[0].elements[i].checked);	 
		}
	 
	 }
	}
	
}

	
}

function openWin(URL,width,height) {
	

		window.open(URL,'new','toolbar=1,location=1,directories=1,status=1,menubar=1,scrollbars=1,resizable=1,copyhistory=1,width=' + width + ',height=' + height);
	}

//----------------------------------------------------------