



PVGIS = {
alt:'',


initialize: function() {
$.get('/calculation/index.php/pvgis/getsessid',function(msg){
$('#sessid').val(msg);
});
},

setForm:function(latitude,longitude,alt){
this.alt=alt;
var lat= this.dec2min(latitude,'latitude');
$('#lat_deg').val(lat[0]);
$('#lat_min').val(lat[1]);
$('#lat_sec').val(lat[2]);
$('#lat_ns').val(lat[3]);
var lng=this.dec2min(longitude,'longitude');
$('#long_deg').val(lng[0]);
$('#long_min').val(lng[1]);
$('#long_sec').val(lng[2]);
$('#long_ew').val(lng[3]);
},

 dec2min:function(dec, dir)//dir can be latitude or longitude
{
    var plus = Math.abs(dec);
    var degr = Math.floor(plus);
    var minu = Math.floor(60 * (plus - degr));
    var sec = Math.floor(60 * (60 * (plus - degr) - minu));
    var direction = "?";
    if (minu < 10) {
        minu = "0" + minu;
    }
    if (sec < 10) {
        sec = "0" + sec;
    }
    if (dir == 'latitude') {
        direction = dec < 0 ? "south" : "nord";
    }
    else {
        direction = dec < 0 ? "west" : "east";
    }
    var decconverted = new Array();
    decconverted[0] = degr;
    decconverted[1] = minu;
    decconverted[2] = sec;
    decconverted[3] = direction;
    decconverted[4] = degr + "° " + minu + "' " + sec + '" ' + direction;
    return decconverted;
},


 getData:function(lat,lng,alt){
 this.setForm(lat,lng,alt);
 //var formValues= $('#coordform').serialize();
$('#climatedata').html('');
common.setWaitON();
var params = { 'params':$('#coordform').serialize() };

$.ajax({
	type: "POST",
	url: '/calculation/index.php/pvgis/getData',
	data: params,
	success: function(msg){
	query=PVGIS.getQuery(msg);
	//console.log(query);	
	PVGIS.getWBV(query);
	}
});
},
getWBV:function(query){
	if (query) {
		var params = {
			'params': query,
			'location':$('#fname').val()
		};
		$.ajax({
			type: "POST",
			url: '/calculation/index.php/pvgis/getWBV',
			data: params,
			success: function(link){
			if (link.length<40){$('#climatedata').html($('#out_of_service').val());}
				else{$('#climatedata').html(link+'<br>'+$('#download_hint').val());}
				common.setWaitOFF();
			}
		});
	}
},

getQuery:function(msg){
	var clima=$.evalJSON(msg);
		var T='';
		var G='';
		for (var i = 1; i <= 12; i++) {
			G = 'g' + i;
			T = 't' + i;
			if (common.isNumber(clima.t[i - 1])) {
			
				CLIMATE[T] = clima.t[i - 1];
				CLIMATE[G] = clima.g[i - 1] / 24;
			}
			else {
				//$('#climatedata').html('sorry - no data');
				$('#climatedata').html($('#out_Of_Region_Msg').val());
				return false;
			}
		}
			CLIMATE.from = 1981;
			CLIMATE.to = 1990;
			CLIMATE.alt = clima.alt;
			CLIMATE.lng = $('#lng').val() * -1;
			CLIMATE.lat = $('#lat').val();
			
			CLIMATE.plz = "";//$('#plz').val();
			CLIMATE.tz = $('#tz').val();
			CLIMATE.datasource="PVGIS";
			CLIMATE.name = $('#fname').val();//.toUpperCase();
			//CLIMATE.name = filename.toUpperCase();
			if (CLIMATE.name.length == 0) {
				CLIMATE.name = clima.city;//.toUpperCase();
			}
			CLIMATE.country = $('#country').val();
			//CLIMATE.country = countrycode;
			var query = "";
			$.each(CLIMATE, function(key, val){
				query = query + key + "=" + val + "&";
			});
			return query;
},


 setOrientation:function(lat,lng){
 var t=$('#lat_deg');
if(lat<0){$('#lat_ns').val('south');}else{$('#lat_ns').val('north');}
if(lng<0){$('#long_ew').val('west');}else{$('#long_ew').val('east');}
},
saveData:function(){
this.extractLocation();	
if ($('#lat_deg').val().length<2){$('#lat_deg').val('0'+$('#lat_deg').val());}
if ($('#long_deg').val().length<2){$('#long_deg').val('0'+$('#long_deg').val());}
var EW=$('#long_ew').val();
var NS=$('#lat_ns').val();
var clima=$('#climatedata').innerHTML;


var location = {name:$('#locationname').innerHTML,
lat:$('#lat_deg').val()+$('#lat_min').val()+NS,
lng:$('#long_deg').val()+$('#long_min').val()+EW,
alt:this.alt};
/*
var jLocation=Object.toJSON(location);
new Ajax.Request('/calculation/index.php/pvgis/savedata', {
parameters:{loc:jLocation,clima:clima}

    }
    */

 $.ajax({
 	type: "POST",
 	url: '/calculation/index.php/pvgis/savedata',
 	data: location
 }
 
  );


},
globwidth:'',
checkdata_and_open:function()
	{

this.oldFunctions();
	$('#horirrad').val("horirrad");
	$('#avtemp').val("avtemp");
	PVGIS.getData();
	},
	oldFunctions:function(){
		var width=120;
	var propertystring;

	var minwidth=500;
	var ok=true;

	if(width<minwidth)
		{
		width = minwidth;
		}

	globwidth=width;

	propertystring = "height=700,width=" + globwidth + ",toolbar=no,scrollbars=yes,resizable";
//	window.open("","datawindow",propertystring);


	var v= parseFloat($('#lat_deg').val());

//$('#long_min').val($('#long_min').val()*0.6);
//$('#lat_min').val($('#lat_min').val8)*0.6);

	if(isNaN(v))
		{
		alert("The latitude degree value is not a number.			");		
		return false;
		}
	else if((v<0.) || (v>90.))
		{
		alert("The latitude degree value must be between 0 and 90 degs.		");
		return false;
		}
	if($('#lat_min').val()=="")
		{
		$('#lat_min').val("0");
		}
	v= parseFloat($('#lat_min').val());
	if(isNaN(v))
		{
		alert("The latitude minute value is not a number.			");		
		return false;
		}
	else if((v<0.) || (v>59))
		{
		alert("The latitude minute value must be between 0 and 59		");
		return false;
		}
	if($('#lat_sec').val()=="")
		{
		$('#lat_sec').val("0");
		}
	v= parseFloat($('#lat_sec').val());
	if(isNaN(v))
		{
		alert("The latitude second value is not a number.			");		
		return false;
		}
	else if((v<0.) || (v>59))
		{
		alert("The latitude second value must be between 0 and 59		");
		return false;
		}
	 v= parseFloat($('#long_deg').val());
	if(isNaN(v))
		{
		alert("The longitude degree value is not a number.			");		
		return false;
		}
	else if((v<-180.) || (v>180.))
		{
		alert("The longitude degree value must be between -180 and 180 degs.		");
		return false;
		}
	if($('#long_min').val()=="")
		{
		$('#long_min').val("0");
		}
	v= parseFloat($('#long_min').val());
	if(isNaN(v))
		{
		alert("The longitude minute value is not a number.			");		
		return false;
		}
	else if((v<0.) || (v>59))
		{
		alert("The longitude minute value must be between 0 and 59		");
		return false;
		}
	if($('#long_sec').val()=="")
		{
		$('#long_sec').val("0");
		}
	v= parseFloat($('#long_sec').val());
	if(isNaN(v))
		{
		alert("The longitude second value is not a number.			");		
		return false;
		}
	else if((v<0.) || (v>59))
		{
		alert("The longitude second value must be between 0 and 59		");
		return false;
		}
	propertystring = "height=700,width=" + globwidth + ",toolbar=no,scrollbars=yes,resizable";
	//window.open("","datawindow",propertystring);
	}
	

}


function warn()
	{
	alert("First click the Submit button in the upper left frame of the main window.");
	}
	
	
common = {
        setWaitON: function(){
        
            $('#climatedata').html('<img src="/calculation/climate/images/sanduhr_web.gif" >');
        },
        setWaitOFF: function(){
            //$('#wait').text('');
        },
        isNumber: function(value){
            return isFinite((value * 1.0));
        }
        
    }
	
	
