var inputs;
var imgFalse = 'images/buttons/unselected.gif';
var imgTrue = 'images/buttons/selected.gif';




function replaceChecks() {
	
	//get all the input fields on the page
	inputs = document.getElementsByTagName('input');

	//cycle trough the input fields
	var j=0;
	for(var i=0; i < inputs.length; i++) {

		//check if the input is a checkbox
		if(inputs[i].getAttribute('type') == 'checkbox') {
		j++;
			//create a new image
			var img = document.createElement('img');
			img.setAttribute('style','vertical-align:middle');
			img.style.verticalAlign = "middle";
			
			//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'+j;
			//set image 
			img.onclick = new Function('checkChange('+j+','+i+')');
			//place image in front of the checkbox
			inputs[i].parentNode.insertBefore(img, inputs[i]);
			
			//hide the checkbox
			if(!jQuery.browser.msie){
				inputs[i].style.display='none';
			}else{
				inputs[i].style.position='absolute';
				inputs[i].style.left='-2200px';
			}
		}
	}
}

function checkChange(j,i) {
	if(inputs[i].checked) {
		inputs[i].checked = '';
		document.getElementById('checkImage'+j).src=imgFalse;
	} else {
		inputs[i].checked = 'checked';
		document.getElementById('checkImage'+j).src=imgTrue;
	}
}


window.onload = init;

function init() {
	replaceChecks();

   $('.checkbox-field').each(function(a,b) { disableSelection(b); } );
}


function checkNreplace(i){
//	inputs = document.getElementsByTagName('input');

	//cycle trough the input fields
	var img = document.getElementById('checkImage'+i);
	if(document.getElementById('ck'+i).checked) {
		img.src = imgFalse;
	} else {
		img.src = imgTrue;
	}

}

function disableSelection(target){
	if (jQuery.browser.msie) {
			target.onselectstart=function(){return false}
	}else if (jQuery.browser.mozilla) {
			target.style.MozUserSelect="none"
	}else {
		target.style.cursor = "default"
	}
}


function validate_prod_dld_form(){

  errors = [];
  postdata = { product_download_form: "sendMail"};

  $('input').each(function(a,b) {
	  if(b.type == 'text') {
            eval('postdata.' + b.name + ' = jQuery.trim(b.value);');
	  }else if(b.type=='checkbox'){
   	    eval('postdata.' + b.id + ' = b.checked');
	  }
  });

  postdata.add_comments = $('#add_comments')[0].value;

 //validation 
  var validemail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,8})+$/;
  if (postdata.name.length==0) {
	errors[errors.length] = 'Name is mandatory';
	document.getElementById('mand1').className = 'product_downloads_input_error';
  }else{
	document.getElementById('mand1').className = 'product_downloads_input';
  }
  if (postdata.email.length==0) {
	errors[errors.length] = 'Email is mandatory';
	document.getElementById('mand2').className = 'product_downloads_input_error';
  }else {
	  if (!validemail.test(postdata.email)) {
		errors[errors.length] = 'Email invalid';
		document.getElementById('mand2').className = 'product_downloads_input_error';
	  }else{
  		document.getElementById('mand2').className = 'product_downloads_input';
	  }
  }
  if (postdata.company.length==0) {
	errors[errors.length] = 'Company is mandatory';
	document.getElementById('mand3').className = 'product_downloads_input_error';
  }else{
	document.getElementById('mand3').className = 'product_downloads_input';
  }
  if (postdata.findus.length==0) {
	errors[errors.length] = 'The "How did you find us" field is mandatory';
	document.getElementById('mand4').className = 'product_downloads_input_error';
  }else{
	document.getElementById('mand4').className = 'product_downloads_input';
  }


 //onerrors
 
  if (errors.length >0)
  {
	  $('#validated').hide();

	  $('#invalidated').html(' Some errors occured, please check the mandatory fields above!');
	  $('#invalidated').show();
	  $('#validated').hide();
  }else{
	  $('#invalidated').hide();
  //if no errors then 
	 //submit

	 start_loader();
	 $('#product_form')[0].submit();

  }


}

//	  $.post("product_form.php", postdata,
//	  function(data){
//		 end_loader();
//		 if (data.error.length==0)
//		 {
//			 $('#validated').html(' The Form was send sucessfully!');
//			 $('#validated').show();
//			 $('#invalidated').hide();
//			 clear_fields();
//		 }else{
//			  $('#invalidated').html(data.error);
//			  $('#invalidated').show();
//			  $('#validated').hide();
//		 }
// 		 
//	  }, "json");


/*
  postdataStr = '';
  for (postf in postdata){
     postdataStr +=  postf + '=' + postdata[postf] + '&';
  }
  postdataStr = postdataStr.substring(0,postdataStr.length-1);

	 $.ajax({
	   type: "POST",
	   url: "product_form.php",
	   data: postdata,
	   success: function(reqtx) {
			 data = eval(reqtx);
			 end_loader();
			 if (data.error.length==0)
			 {
				 $('#validated').html(' The Form was send sucessfully!');
				 $('#validated').show();
				 alert(data.mailtext); 			 //:: debug line ::
				 clear_fields();
			 }else{
				  $('#invalidated').html(data.error);
				  $('#invalidated').show();
			 }
		  }

	 });

*/


function validate_prod_dld_form2(){

  errors = [];
  postdata = { product_download_form: "sendMail"};

  $('input').each(function(a,b) {
	  if(b.type == 'text') {
            eval('postdata.' + b.name + ' = jQuery.trim(b.value);');
	  }else if(b.type=='checkbox'){
   	    eval('postdata.' + b.id + ' = b.checked');
	  }
  });

  postdata.add_comments = $('#add_comments')[0].value;

 //validation 
  var validemail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,8})+$/;
  if (postdata.name.length==0) {
	errors[errors.length] = 'Name is mandatory';
	document.getElementById('mand1').className = 'product_downloads_input_error';
  }else{
	document.getElementById('mand1').className = 'product_downloads_input';
  }
  if (postdata.email.length==0) {
	errors[errors.length] = 'Email is mandatory';
	document.getElementById('mand2').className = 'product_downloads_input_error';
  }else {
	  if (!validemail.test(postdata.email)) {
		errors[errors.length] = 'Email invalid';
		document.getElementById('mand2').className = 'product_downloads_input_error';
	  }else{
  		document.getElementById('mand2').className = 'product_downloads_input';
	  }
  }
  if (postdata.company.length==0) {
	errors[errors.length] = 'Company is mandatory';
	document.getElementById('mand3').className = 'product_downloads_input_error';
  }else{
	document.getElementById('mand3').className = 'product_downloads_input';
  }
  if (postdata.findus.length==0) {
	errors[errors.length] = 'The "How did you find us" field is mandatory';
	document.getElementById('mand4').className = 'product_downloads_input_error';
  }else{
	document.getElementById('mand4').className = 'product_downloads_input';
  }


 //onerrors
 
  if (errors.length >0)
  {
	  $('#validated').hide();

	  $('#invalidated').html(' Some errors occured, please check the mandatory fields above!');
	  $('#invalidated').show();
	  $('#validated').hide();
  }else{
	  $('#invalidated').hide();
  //if no errors then 
	 //submit

	 start_loader();

	  $.post("product_form2.php", postdata,
	  function(data){
		 end_loader();
		 if (data.error.length==0)
		 {
			 $('#validated').html(' The Form was send sucessfully!');
			 $('#validated').show();
			 $('#invalidated').hide();
			 clear_fields();
			 window.location='product_downloads.php?mailbsk=132'+(new Date).getTime();
		 }else{
			  $('#invalidated').html(data.error);
			  $('#invalidated').show();
			  $('#validated').hide();
		 }
 		 
	  }, "json");
  }

}





function start_loader() {
	$('#loader').show();

}
function end_loader() {
	$('#loader').hide();
}

function clear_fields(){
  $('input').each(function(a,b) {
	  if(b.type == 'text') {
            b.value = '';
	  }else if(b.type=='checkbox'){
   	    b.checked = false;
		i = parseInt(b.id.substring(2, b.id.length));
		$('#checkImage'+i)[0].src = imgFalse;

	  }
  });
  $('#add_comments')[0].value = '';

}

function hide_showbox(){
	$('#mail_showbox').hide();
}