//global variables that can be used by ALL the function son this page.
var today = new Date(); 
var zero_date = new Date(0,0,0); 
today.setTime(today.getTime() - zero_date.getTime()); 
var cookie_expire_date = new Date(today.getTime() + (259200));

var inputs;
var imgFalse = 'img/check_off.jpg';
var imgTrue = 'img/check_on.jpg';

//this function runs when the page is loaded, put all your other onload stuff in here too.



function replaceChecks() {
	
	//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') == '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;
			}

			//set image ID and onclick action
			

			//set image 
			if(inputs[i].name == 'viewdescription'){
         img.id = 'checkDescription';
          if (Get_Cookie('viewdescription_ID')) {
            inputs[i].checked = true;
  		      img.src=imgTrue;
          }
         img.onclick = new Function('check("viewdescription", "checkDescription")');
      } else if(inputs[i].name == 'viewpicture') {
        img.id = 'checkPicture';
        if (Get_Cookie('viewpicture_ID')) {
            inputs[i].checked = true;
  		      img.src=imgTrue;
          }
        img.onclick = new Function('check("viewpicture", "checkPicture")');
      } else { 
			 img.id = 'checkImage'+i;
       img.onclick = new Function('checkChange('+i+')');
      }
			//place image in front of the checkbox
			inputs[i].parentNode.insertBefore(img, inputs[i]);
			
			//hide the checkbox
			inputs[i].style.display='none';
		}
	}
	getDescriptionClass();
	hidePicture();
}



function replaceAjaxChecks(){
	inputs = document.getElementsByTagName('input');
	for(var i=0; i < inputs.length; i++) {
		if(inputs[i].getAttribute('type') == 'checkbox' && inputs[i].style.display != 'none') {
			var img = document.createElement('img');
			
			//check if the checkbox is checked
			if(inputs[i].checked) {
				img.src = imgTrue;
			} else {
				img.src = imgFalse;
			}

			//set image ID and onclick action
			img.id = 'checkImage'+i;

			//set image 
			img.onclick = new Function('checkChange('+i+')');

			//place image in front of the checkbox
			inputs[i].parentNode.insertBefore(img, inputs[i]);
			
			//hide the checkbox
			inputs[i].style.display='none';
		}
	}
}


function check(value, imgname) {
  var name = $(value);
  if(name.checked) {
  		name.checked = '';
  		$(imgname).src=imgFalse;
  		Delete_Cookie((value + "_ID"));
  } else {
  		name.checked = 'checked';
  		$(imgname).src=imgTrue;
  		Set_Cookie((value + "_ID"),Math.random(),cookie_expire_date); 
  }
  if(value == 'viewdescription')
    getDescriptionClass();
  else if(value == 'viewpicture')
    hidePicture();
}



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

	var value = '';
	if(inputs[i].name == 'teemad') {
		checkAll('theme');
	} else if(inputs[i].name == 'valjaanned') {
		checkAll('release');
	}
	
	if(inputs[i].checked) {
		inputs[i].checked = '';
		document.getElementById('checkImage'+i).src=imgFalse;
	} else {
		inputs[i].checked = 'checked';
		document.getElementById('checkImage'+i).src=imgTrue;
	}
	
	if(inputs[i].name == 'theme[]'){
		value = checkBoxControl('theme');
		for(var x=0;x<inputs.length;x++){
			if(inputs[x].name == 'teemad'){
				if(value){
					inputs[x].checked = '';
					document.getElementById('checkImage'+x).src=imgFalse;
				} else {
					inputs[x].checked = 'checked';
					document.getElementById('checkImage'+x).src=imgTrue;
				}
			}
		}
	} else if(inputs[i].name == 'release[]'){
		value = checkBoxControl('release');
		for(var x=0;x<inputs.length;x++){
			if(inputs[x].name == 'valjaanned'){
				if(value){
					inputs[x].checked = '';
					document.getElementById('checkImage'+x).src=imgFalse;
				} else {
					inputs[x].checked = 'checked';
					document.getElementById('checkImage'+x).src=imgTrue;
				}
			}
		}
	}
	
	if(inputs[i].name == 'teemad' || inputs[i].name == 'valjaanned'){
		for(var j=0;j<inputs.length;j++){
			var image = document.getElementById('checkImage'+j);
			if(image){
				if(inputs[j].checked){
					image.src = imgTrue;
				} else {
					image.src = imgFalse;
				}
			}
		}
	}
	/*
	if(inputs[i].name == 'viewdescription'){
    getDescriptionClass();
  }
  
  if(inputs[i].name == 'viewpicture'){
    hidePicture();
  }
  */
}

function checkAll(field){
	var ch = document.getElementsByName(field+'[]');
	var checkedOn = 0;
    var checkValue = true;
    
    if(!ch) return;
    var count = ch.length;

    for(var j = 0;j < count; j++){
    	if(ch[j].checked) checkedOn++;
    }
    if(checkedOn == count)
    	checkValue = false;
    else 
    	checkValue = true;
    	
    if(!count) {
      ch.checked = checkValue;
    } else {
      for(var i = 0; i < count; i++){
        ch[i].checked = checkValue;
      }
   	}
}

function checkBoxControl(field){
	var ch = document.getElementsByName(field+'[]');
	var checkedOn = 0;
    var checkValue = true;
    
    if(!ch) return;
    var count = ch.length;

    for(var j=0;j < count; j++){
    	if(ch[j].checked) checkedOn++;
    }
    //alert(count + '=' + checkedOn);
    if(checkedOn == count)
    	checkValue = false;
    else 
    	checkValue = true;
    
    return checkValue;
}


/* Description ON/OFF */

function getDescriptionClass(){
    if(!$('contentContainer')) return true;
  	var divs = $('contentContainer').getElementsByTagName('div');
  	
	   var d = "";
     var descr = $('viewdescription');
     if(!descr) return;
     if(descr.checked){
      d = "none";
     } else {
      d = "";
     }
    for(var i = 0; i<divs.length; i++){
      if(divs[i].className == 'news_description_blok' || divs[i].className == 'news_description_blok_new' || divs[i].className == 'news_description_blok_click'){
        divs[i].style.display = d;
      }
    }
}

function hidePicture(){
  if(!$('contentContainer')) return false;
  var divs = $('contentContainer').getElementsByTagName('div');
	   var d = "";
     var picture = $('viewpicture');
     if(!picture) return;
     if(picture.checked){
      d = "none";
     } else {
      d = "";
     }
    for(var i = 0; i<divs.length; i++){
      if(divs[i].className == 'news_photo' || divs[i].className == 'news_photo_klikitum'){
        divs[i].style.display = d;
      }
    }
    return true;
}


function Get_Cookie(name) { 
   var start = document.cookie.indexOf(name+"="); 
   var len = start+name.length+1; 
   if ((!start) && (name != document.cookie.substring(0,name.length))) return null; 
   if (start == -1) return null; 
   var end = document.cookie.indexOf(";",len); 
   if (end == -1) end = document.cookie.length; 
   return unescape(document.cookie.substring(len,end)); 
} 


function Set_Cookie(name,value,expires,path,domain,secure) { 
    var cookieString = name + "=" +escape(value) + 
       ( (expires) ? ";expires=" + expires.toGMTString() : "") + 
       ( (path) ? ";path=" + path : "") + 
       ( (domain) ? ";domain=" + domain : "") + 
       ( (secure) ? ";secure" : ""); 
    document.cookie = cookieString; 
} 


function Delete_Cookie(name,path,domain) { 
   if (Get_Cookie(name)) document.cookie = name + "=" + 
      ( (path) ? ";path=" + path : "") + 
      ( (domain) ? ";domain=" + domain : "") + 
      ";expires=Thu, 01-Jan-70 00:00:01 GMT"; 
} 

