var tmp_width_cm	= 0;
var tmp_height_cm 	= 0;
var tmp_width_px 	= 0;
var tmp_height_px 	= 0;
var tmp_dpi 		= 0;
						
function IsNumeric(sText)
{
	var ValidChars = "0123456789.";
	var IsNumber=true;
	var Char;

	if(sText != null && sText != "" && sText != 0){
		for (i = 0; i < sText.length && IsNumber == true; i++) 
		  { 
		  Char = sText.charAt(i); 
		  if (ValidChars.indexOf(Char) == -1) 
			 {
			 IsNumber = false;
			 }
		  }
	   return IsNumber;
   }
	else{return false;}
}

function calc_pxg(width_cm, height_cm, width_px, height_px, dpi, calc_id) {
	var calculate_cm 	= false;
	var calculate_px 	= false;
	var dpi_numeric  	= false;
	var is_numeric_cm 	= false;
	var is_numeric_px 	= false;


	if(IsNumeric(dpi)){
		dpi_numeric  = true;
	}
	
	if(IsNumeric(width_cm)){
		if(IsNumeric(height_cm)){
			is_numeric_cm = true;
		}
	}
	
	if(IsNumeric(width_px)){
		if(IsNumeric(height_px)){
			is_numeric_px = true;
		}
	}

	if((is_numeric_px == true || is_numeric_cm == true) && dpi_numeric == true){

		
		if((width_px != tmp_width_px || height_px != tmp_height_px) && is_numeric_px == true){
			calculate_px = true;
		}								
		
		if((width_cm != tmp_width_cm || height_cm != tmp_height_cm) && is_numeric_cm == true){
			calculate_cm 	= true;
		}
		
		if(width_px == tmp_width_px && height_px == tmp_height_px && width_cm == tmp_width_cm && height_cm == tmp_height_cm && tmp_dpi != dpi){
			calculate_cm 	= true;
			tmp_dpi = dpi;
		}											
		
		switch(calc_id){
			case 'frm_leinwand_pxg':
					if(dpi > 71 && dpi < 1401){
						if(calculate_cm == true){
							tmp_width_cm 	= width_cm;
							tmp_height_cm 	= height_cm;
						
							tmp_width_px 	= Math.round(width_cm * dpi / 2.54);
							tmp_height_px 	= Math.round(height_cm * dpi / 2.54);

							document.getElementById(calc_id).l_width_px.value 	= tmp_width_px;
							document.getElementById(calc_id).l_height_px.value 	= tmp_height_px;
							
							tmp_width_cm 	= Math.round((tmp_width_px / dpi * 2.54)*10) / 10;
							tmp_height_cm 	= Math.round((tmp_height_px / dpi * 2.54)*10) / 10;
						
							document.getElementById(calc_id).l_width_cm.value 	= tmp_width_cm;
							document.getElementById(calc_id).l_height_cm.value 	= tmp_height_cm;																
						}
						else {
							tmp_width_px 	= width_px;
							tmp_height_px 	= height_px;
						
							tmp_width_cm 	= Math.round((width_px / dpi * 2.54)*10) / 10;
							tmp_height_cm 	= Math.round((height_px / dpi * 2.54)*10) / 10;
						
							document.getElementById(calc_id).l_width_cm.value 	= tmp_width_cm;
							document.getElementById(calc_id).l_height_cm.value 	= tmp_height_cm;
						}
					}
					else{alert("Bitte geben Sie einen dpi Wert zwischen 72 und 1400 ein.");}
				break;

			case 'frm_tapete_pxg':
					
					if(dpi > 24 && dpi < 61){
						if(calculate_cm == true){
							tmp_width_cm 	= width_cm;
							tmp_height_cm 	= height_cm;
						
							tmp_width_px 	= Math.round(width_cm * dpi / 2.54);
							tmp_height_px 	= Math.round(height_cm * dpi / 2.54);

							document.getElementById(calc_id).t_width_px.value 	= tmp_width_px;
							document.getElementById(calc_id).t_height_px.value 	= tmp_height_px;
							
							tmp_width_cm 	= Math.round((tmp_width_px / dpi * 2.54)*10) / 10;
							tmp_height_cm 	= Math.round((tmp_height_px / dpi * 2.54)*10) / 10;
						
							document.getElementById(calc_id).t_width_cm.value 	= tmp_width_cm;
							document.getElementById(calc_id).t_height_cm.value 	= tmp_height_cm;																
						}
						else {
							tmp_width_px 	= width_px;
							tmp_height_px 	= height_px;
						
							tmp_width_cm 	= Math.round((width_px / dpi * 2.54)*10) / 10;
							tmp_height_cm 	= Math.round((height_px / dpi * 2.54)*10) / 10;
						
							document.getElementById(calc_id).t_width_cm.value 	= tmp_width_cm;
							document.getElementById(calc_id).t_height_cm.value 	= tmp_height_cm;
						}
					}
					else{alert("Bitte geben Sie dpi Wert zwischen 25 und 60 ein.");}
				break;
		}
	}
	else {
		alert("Bitte geben Sie alle Werte zur Berechnung von cm oder px ein (Höhe, Breite, dpi).");
	}
}
                                