/** Updated on 5.10.2011 // mileht
  * Most of the code moved to common JS file /stc/js/javascript.js
  * This file contains functions:
  *		get_object(id)
  *		electRadiobutton(radio,id)
  *		validateForm(submit_form,id)
  *		calculateLimit()
  *		
  *		include_css(css_file)
  *		printWithSecondStyleSheet()
  *		printWithDefaultStyleSheet()
  */
  

/** returns object by given id
  * works with newest ie 7.0.5730.11 , firefox 2.0.0.13 and opera 9.27
  * @author Petri Norrgrann
  */
function get_object(id) {
	var object = null;
	if( document.layers ) { 
		object = document.layers[id];
	} else if( document.all ) {
		object = document.all[id];
	} else if( document.getElementById ) {
		object = document.getElementById(id);
	}
	return object;
}


/** sets given radiobutton elements enabled and others disabled 
  * works with newest ie 7.0.5730.11 , firefox 2.0.0.13 and opera 9.27
  * @author Petri Norrgrann
  *
  * NAMED ELEMENTS:
  * 	counter_radiobuttonA elements:
  * 		counter_menuA1
  *		counter_menuA2
  * 	counter_radiobuttonB elements:
  *		counter_menuB
  * 	counter_radiobuttonC elements:
  *		counter_textboxC
  */

function selectRadiobutton(radio,id){
	var radiobutton=get_object(radio.id);
	var counter_menuB=get_object("counter"+id+"_menuB");

	radiobutton.checked=true;

	if(radiobutton.id == "counter"+id+"_radiobuttonB" ){
		counter_menuB.disabled=false;
   }
} 


/** validator for counter1 and counter2 form
  * checks that all values are correct by given counter id
	* uses get_object method to get objects
	* uses jQuery html setter to set html intoa counter error message div
  *	@author Petri Norrgrann
	*/

function validateForm(submit_form,id){

   var counter_textbox1=get_object("counter"+id+"_textbox1"); 
   var counter_textboxC=get_object("counter"+id+"_textboxC"); 

	
	// counter_textbox1 is undefined
	if(counter_textbox1==null){
		alert('value1 is undefined');
		return false;
	}
	
	var is_ok=true;
	var error_html="<div class='counter_error_text'>";

	var min=200;
	var max=5000;
	var maxString="10 000";
	if (id==2) {
		min=200;
		max=5000;
		maxString="5000";
	} else if(id == 1) {
		min=500;
		max=10000;
		maxString="10 000";
	} else if(id == 3) {
		min=2000;
		max=10000;
		maxString="10 000";
	}

	//check product name if tuoteluottolaskuri
	if(id == 2 && useCounter != "counter2-staff") {
		var counter_name=get_object("counter"+id+"_name");
		if(counter_name.value=="") {
			createError("counter2_name",virhe_th18);
			is_ok=false;
		}
	}


	// lets check that counter_textbox1 value is correct
	// counter_textbox1 is empty
	if(counter_textbox1.value=="") {
		if(id == 3) {
			error_html=error_html+""+virhe1+"<br />";
		} else if (id==1) {
			createError("counter1_textbox1",virhe_th1);
			$("#counter1_textbox1").addClass("error");
		} else {
			createError("counter2_textbox1",virhe_th1);
			$("#counter2_textbox1").addClass("error");
		}
		is_ok=false;
		
	} else {
		// counter_textbox1 is not a number
		if(isNaN(parseInt(counter_textbox1.value))){
			error_html=error_html+""+virhe2+"<br />";
			is_ok=false;
		} else {
						for (i=0;i<counter_textbox1.value.length;++i) {
								if (isNaN(parseInt(counter_textbox1.value.charAt(i))) && useCounter != "counter2-staff" && (id != 1)) {
										if(id==3) {
												error_html=error_html+""+virhe2+"<br />";
										} else {
											error_html=error_html+""+virhe_th2+"<br />";
										}
										is_ok=false;
								}
						}

			// counter_textbox1 is not between 200 and 5000
			if( (parseInt(counter_textbox1.value) < min || parseInt(counter_textbox1.value) > max) && (useCounter != "counter2-staff" && id != 1)) {
				if(id == 3) {
					error_html=error_html+""+virhe3+" "+min+"-"+maxString+" &#8364; .<br />";
				} else {
					error_html=error_html+""+virhe_th3+" "+min+"-"+maxString+" &#8364; .<br />";
				}
				is_ok=false;			
			} else {

				// if not divided by 100
				if(id==3 || (id==2 && useCounter != "counter2-staff")) {
					if(parseInt(counter_textbox1.value)%100!=0){
					  is_ok=false;						
					}
				}

			}
		}
	}


	var counter_menuB=get_object("counter"+id+"_menuB");
		
	if( jQuery("input[name='counter"+id+"_menuB']:checked").length == 0 ){
		
		createError("paymentSet",virhe6);
		
		is_ok=false;
	}
		
	if(id==2){
		min=0;
		max=5000;
		maxString="5000";
	}else if(id==1){
		min=0;
		max=10000;
		maxString="10 000";
	}
	
	
	// Last check if any errors are visible
	if( jQuery(".errorContainer").length > 0){
		is_ok=false;
	}
	

	if(is_ok){
		var luoton_maara = parseInt(counter_textbox1.value);
		var maksuera;
		//maksuera = luoton_maara*(counter_menuB.options[counter_menuB.selectedIndex].value/100.0);
		maksuera = luoton_maara*( $("input[name='counter"+id+"_menuB']").val()/100.0);
		var korko;
		if(id==1) {
			korko = korko1 / 100.0;
		} else if(id==2) {
			korko = korko2 / 100.0;
		} else if(id==3) {
			korko = korko3 / 100.0;
		}
			
		if(korko*luoton_maara/12.0 >= maksuera){
			if(id==1){
				error_html=error_html+""+virhe13+"<br/>";
			} else {
				error_html=error_html+""+virhe_th13+"<br/>";
			}
			is_ok = false;
		}
		
		
		if(is_ok)
			jQuery("#counter"+id+"_error_message").hide();	
		else{
			error_html=error_html+"</div>";
			jQuery("#counter"+id+"_error_message .redInner").html(error_html);
			jQuery("#counter"+id+"_error_message").show();
		  jQuery("#result"+id+"").hide();
		  jQuery("#counter"+id+"").show();
		}

		return is_ok;

	}else{
		error_html=error_html+"</div>";
		jQuery("#counter"+id+"_error_message .redInner").html(error_html);
		jQuery("#counter"+id+"_error_message").show();
		jQuery("#result"+id+"").hide();
		jQuery("#counter"+id+"").show();
	}

	return is_ok;
}


function calculateLimit(){
	var counter_textbox1=get_object("counter2_textbox1");

	var min=200;
	var max=5000;
	var maxString="5000";
	var is_ok = true;


	// lets check that counter_textbox1 value is correct
	// counter_textbox1 is empty
	if(counter_textbox1.value==""){
		is_ok=false;
	}else{
		// counter_textbox1 is not a number
		if(isNaN(parseInt(counter_textbox1.value))){
			is_ok=false;
		}else{
			// counter_textbox1 is not between 200 and 5000
			if(parseInt(counter_textbox1.value) < min || parseInt(counter_textbox1.value) > max){
				is_ok=false;			
			}else{
				// nop
			}
		}
	}
	

	if(is_ok){
		var html = "";
		var limit = parseInt(counter_textbox1.value);
		if(parseInt(counter_textbox1.value) % 100 != 0)
			limit = parseInt(counter_textbox1.value) - (parseInt(counter_textbox1.value) % 100)  + 100; 
		html=""+limit+" &#8364;";
		jQuery("#counter2_limit").html(html);	
		jQuery("#counter2_limit").show();
	}else{
		jQuery("#counter2_limit").html(th_text);	
		jQuery("#counter2_limit").show();
	}
	
}

/* Styling and Printing functions */
function include_css(css_file){
		var css;
	var html_doc = document.getElementsByTagName('head')[0];
	css = document.createElement('link');
	css.setAttribute('rel', 'alternate stylesheet');
	css.setAttribute('type', 'text/css');
	css.setAttribute('id', 'print_link2');
	css.setAttribute('media', 'print');
	css.setAttribute('href', css_file);
	html_doc.appendChild(css);

	// alert state change
	css.onreadystatechange = function () {
		if (css.readyState == 'complete') {
			alert('CSS onreadystatechange fired');
		}
	}
	css.onload = function () {
		alert('CSS onload fired');
	}
	return false;
}


function printWithSecondStyleSheet(){  
		var print_link;
		var i, a;  
		for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {    
			if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("media") == 'print'){
				print_link = a;
				break;
			}
		}
	print_link.href = "/stc/print2.css";
	print();
}



function printWithDefaultStyleSheet(){  
		var print_link;
		var i, a;  
		for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {    
			if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("media") == 'print'){
				print_link = a;
				break;
			}
		}
		
		print_link.href = "/stc/print.css";
		print();
}



