//global variables that can be used by ALL the function son this page.
var inputs;
var imgFalse = '/images/false.png';
var imgTrue = '/images/true.png';

var imgFalse2 = '/images/false.png';
var imgTrue2 = '/images/true2.png';

//this function runs when the page is loaded, put all your other onload stuff in here too.
function init() {
	replaceChecks();
	replaceRadios();
}

function replaceChecks() {
	
	//get all the input fields on the page
	inputs = document.getElementsByTagName('input');

    var vv = ''
	//cycle trough the input fields
	for(var i=0; i < inputs.length; i++) {

		//check if the input is a checkbox
		if(inputs[i].getAttribute('type') == 'checkbox') {
			
			//create a new image
			var img = document.createElement('img');
			
			//check if the checkbox is checked
			if(inputs[i].checked) {
				img.src = imgTrue;
			} else {
				img.src = imgFalse;
			}
            
            
            var id = inputs[i].id;
            if(id.indexOf("cblNBs") > -1)
                id= "cblNBs";
            else if(id.indexOf("cblBeedrooms") > -1)
                id= "cblBeedrooms";
                
			//set image ID and onclick action
			img.id = 'checkImage'+i;
			
			if(vv == id)
			{	
			    img.value = id;
			}
			else
			{
			    img.value = id +"_all";
			}
			
			vv = id;
			
			myid = "#"+id;
		
			unid = $(myid).parent('span').attr("title");
			
			//set image 
			img.onclick = new Function('checkChange('+i+','+unid+')');
			//place image in front of the checkbox
			inputs[i].parentNode.insertBefore(img, inputs[i]);
			
			//hide the checkbox
			inputs[i].style.display='none';
		}
	}
}

function replaceRadios() {
	
	//get all the input fields on the page
	inputs = document.getElementsByTagName('input');

	//cycle trough the input fields
	for(var i=0; i < inputs.length; i++) {

		//check if the input is a checkbox
		if(inputs[i].getAttribute('type') == 'radio') {
			
			//create a new image
			var img = document.createElement('img');
			
			//check if the checkbox is checked
			if(inputs[i].checked) {
				img.src = imgTrue2;
			} else {
				img.src = imgFalse2;
			}

            img.value = inputs[i].id;
			//set image ID and onclick action
			img.id = 'radioImage'+i;
			
			//set image 
			img.onclick = new Function('radioChange('+i+')');
			//place image in front of the checkbox
			inputs[i].parentNode.insertBefore(img, inputs[i]);
			
			//hide the checkbox
			inputs[i].style.display='none';
		}
	}
}

function clearRadios() {
	
	//get all the input fields on the page
	inputs = document.getElementsByTagName('input');

	//cycle trough the input fields
	for(var i=0; i < inputs.length; i++) {

		//check if the input is a checkbox
		if(inputs[i].getAttribute('type') == 'radio') {
			
			document.getElementById('radioImage'+i).src=imgFalse2;

		
			
		}
	}
}


//change the checkbox status and the replacement image
function radioChange(i) {

    clearRadios();
    
    inputs[i].checked = 'checked';
    document.getElementById('radioImage'+i).src=imgTrue2;
    
    if(document.getElementById('radioImage'+i).value.indexOf("ContextThr_1") > -1)
	{
		$("#txtEmail_Phone").val("telephone");
	}
	else if(document.getElementById('radioImage'+i).value.indexOf("ContextThr_0") > -1)
	{
		$("#txtEmail_Phone").val("email");
	}
	
}


//change the checkbox status and the replacement image
function checkChange(i,vv) {


if(document.getElementById('checkImage'+i).value == "cblNBs_all" && !inputs[i].checked)
{
    imgs = document.getElementsByTagName('img');

	//cycle trough the input fields
	for(var j=0; j < imgs.length; j++) {

       if(	imgs[j].value == "cblNBs")
		{
		    //inputs[j].checked = '';
		    imgs[j].src=imgTrue;
		    id = imgs[j].id.replace("checkImage","");
		    inputs[id].checked = 'checked';
		}
	}
}

if(document.getElementById('checkImage'+i).value == "cblNBs" && inputs[i].checked)
{
    imgs = document.getElementsByTagName('img');

	//cycle trough the input fields
	for(var j=0; j < imgs.length; j++) {

       if(	imgs[j].value == "cblNBs_all")
		{
		    //inputs[j].checked = '';
		    imgs[j].src=imgFalse
		    id = imgs[j].id.replace("checkImage","");
		    inputs[id].checked = '';
		}
	}
}

if(document.getElementById('checkImage'+i).value == "cblBeedrooms_all" && !inputs[i].checked)
{
    imgs = document.getElementsByTagName('img');

	//cycle trough the input fields
	for(var j=0; j < imgs.length; j++) {

       if(	imgs[j].value == "cblBeedrooms")
		{
		    //inputs[j].checked = '';
		    imgs[j].src=imgTrue;
		    id = imgs[j].id.replace("checkImage","");
		    inputs[id].checked = 'checked';
		}
	}
}

if(document.getElementById('checkImage'+i).value == "cblBeedrooms" && inputs[i].checked)
{
    imgs = document.getElementsByTagName('img');

	//cycle trough the input fields
	for(var j=0; j < imgs.length; j++) {

       if(	imgs[j].value == "cblBeedrooms_all")
		{
		    //inputs[j].checked = '';
		    imgs[j].src=imgFalse
		    id = imgs[j].id.replace("checkImage","");
		    inputs[id].checked = '';
		}
	}
}


	if(inputs[i].checked) {
		inputs[i].checked = '';
		document.getElementById('checkImage'+i).src=imgFalse;
		if(document.getElementById('checkImage'+i).value.indexOf("cbCompare") > 0)
		{
		    UpdateUnitsCompare(vv,'remove');
		}
		
	} else {
		inputs[i].checked = 'checked';
		document.getElementById('checkImage'+i).src=imgTrue;
		if(document.getElementById('checkImage'+i).value.indexOf("cbCompare") > 0)
		{
		    UpdateUnitsCompare(vv,'add');
		}
	
	}
}

function UpdateUnitsCompare(id,ty)
{
        dataURL = "unit="+id+"&type="+ty;
    	
	    $.ajax({
			    type: "POST",
			    url: "/ajax.aspx",
			    data: dataURL,
    			
			    success: function(msg){
    					
			    }
	    }); 
}

window.onload = init;