
// COPY THIS FILE INTO THE HTML DIRECTORY OF THE WEB-SERVER !!!!!!!!!
// ==================================================================

// all scripts copyrighted by Dr. Mathias D. Mueller, meteoblue TM

// ========================================================================
// derive the temporary userfile-name from the email-address
// ========================================================================


  function email2userid(which){
     var userfile;
     userfile=which;
     userfile=userfile.replace(".","");
     userfile=userfile.replace(".","");
     userfile=userfile.replace(".","");
     userfile=userfile.replace(".","");
     userfile=userfile.replace(".","");
     userfile=userfile.replace(".","");
     userfile=userfile.replace("@","_at_");
     return userfile;
}



// ========================================================================
// change parameter scale for selected parameter
// ========================================================================
 
  function changeScale(){

   var ptype = document.forms[0].ptypef.value;
  

   if  (ptype =="XY" || ptype =="CR"){  
     var a = radioWert(document.forms[0].fields);
     var b=document.forms[0];

     if (a =="f_tt" ) {b.smin.value=-20; b.smax.value="30";} // temperature
     if (a =="f_ws" ) {b.smin.value=0; b.smax.value="20";}
   }
 }

// ========================================================================
// return value of checked radiobutton
// ========================================================================

 function radioWert(rObj) {
    for (var i=0; i<rObj.length; i++) if (rObj[i].checked) return rObj[i].value;
    return false;
  }

// ========================================================================
// ask if file shoud be deleted or not
// ========================================================================
 
  function ask_delete(){
     var a = document.forms[0].CHOICE.selectedIndex;
     var del = confirm("Delete Map:> "+ document.forms[0].CHOICE[a].text);
    return del;
 }


 function win_open(url){
   a=window.open(url);
}


// =============================================================================
// set the bounded coordinates of the map into the form which is then submitted
// =============================================================================

function setMapBounding(form) {

   var a = document.forms[0]; 

   var lat1= document.map.get_y_hi();
   var lon0=document.map.get_x_lo();


//   if(lat1 < 85 && lon0 > -50){     //this is very important because it prevents taking the values from java
//                                                    map if the map has not been manipulated

    a.lat0.value = document.map.get_y_lo();
    a.lat1.value = lat1;
    a.lon0.value = lon0;
    a.lon1.value = document.map.get_x_hi();

//    }


}

// ==============================================================================
// set the bounding box of the coordinates in the form onto the java graphics map
// ==============================================================================

 function applyMapBounding(domain,ptype){

   var a = document.forms[0];
   var lon0= eval(a.lon0.value);
   var lon1=eval(a.lon1.value);
   var lat0=eval(a.lat0.value);
   var lat1=eval(a.lat1.value);

   document.applets.map.positionTool(0, lon0, lon1 , lat0, lat1);


//set point,crosssection or map status
//    var tmap= document.getElementById('tmap').checked;
//     if (tmap){document.map.setTool('XY');}
 a.ptypef.value=ptype;
 setval=ptype;
 if (ptype =="TR")setval="PT";
    document.map.setTool(setval);

// set model domain so that it can be submitted to other documents
     a.model.value=domain;
        document.applets.map.positionTool(0, lon0, lon1 , lat0, lat1);

}


// ==============================================================================
// set the my maps selcetion to create new ...
// ==============================================================================
  function set_create(){
   var a = document.forms[0];
   a.CHOICE.selectedIndex=0;
   if (a.citylist != null){
     a.citylist.selectedIndex=-9; //for meteograms
     a.city.value='Create new ...';
   }
  }



function checkTimer(){
	if (document.Timer.timerBox.checked){
		var newIndex = 0;
		if (index < imgSrc.length -1) {
			newIndex = index +1;
		}
		document.image1.src=imgSrc[newIndex].src;
                index=newIndex;
		var timeoutID = setTimeout("checkTimer()",500);  
	}
        else {return}
}


// =======================================================================================
// recpol, polrec, sphere distance (the usual spherical geometry crap) - do not modify this
// =======================================================================================
 
function recpol(x,y){
        a=0.0;

        if (x != 0 || y != 0){
          a=Math.atan2(y,x);}
        
        if (a < 0){
        a=a+2*3.1415927;}      
     
        return a;
}

function polrec_x(r,a){
		 
	 x = r*Math.cos(a);
	 return x;
}	

function polrec_y(r,a){
	
	 y = r*Math.sin(a);
	 return y;
}	


mapa = new Image();
mapa.src = "../my/img/mapa.jpg";
mapb = new Image();
mapb.src = "../my/img/mapb.jpg";
meteograma = new Image();
meteograma.src = "../my/img/meteograma.jpg";
meteogramb = new Image();
meteogramb.src = "../my/img/meteogramb.jpg";
crossa = new Image();
crossa.src = "../my/img/crossa.jpg";
crossb = new Image();
crossb.src = "../my/img/crossb.jpg";
trajectorya = new Image();
trajectorya.src = "../my/img/trajectorya.jpg";
trajectoryb = new Image();
trajectoryb.src = "../my/img/trajectoryb.jpg";

function changeImg(cImg,ref) {
 document.images[cImg].src = ref.src
}



function sphdist(long1, lat1, long2, lat2){

	cf = 57.2958;
        rxy = polrec_x(1.0,lat1/cf);
        z1  = polrec_y(1.0,lat1/cf);

        x1=polrec_x(rxy, long1/cf);
        y1=polrec_y(rxy, long1/cf);

        rxy=polrec_x( 1.0, lat2/cf);
        z2=polrec_y( 1.0, lat2/cf);

        x2=polrec_x(rxy, long2/cf);
        y2=polrec_y(rxy, long2/cf);

       
	//--- Compute vector dot product for both points. ---
	cs = x1*x2 + y1*y2 + z1*z2;

	//--- Compute the vector cross product for both points. ---
	xc = y1*z2 - z1*y2;
	yc = z1*x2 - x1*z2;
	zc = x1*y2 - y1*x2;
        d=xc*xc + yc*yc + zc*zc;
	sn = Math.sqrt(d);

	//--- Convert to polar.  ------
	a=recpol(cs, sn);
        a=cf*a;
	return a;
}


// =================================================================================
// compute map/meteogram etc. image size
// =================================================================================

function mapimg_size(ptype){
   //get settings from html
   var a = document.forms[0]; 

   ptype = a.ptypef.value;

   lon0 = a.lon0.value;
   lon1 = a.lon1.value;
   lat0 = a.lat0.value;
   lat1 = a.lat1.value;

   b = document.forms[0].imgsize.selectedIndex;
   pix=document.forms[0].imgsize[b].text;
   lat1=parseFloat(lat1);
   lat0=parseFloat(lat0);
   lon1=parseFloat(lon1);
   lon0=parseFloat(lon0);
   pix=parseFloat(pix);

xs=pix;
ys=pix;

// if meteogram use a fixed height to width ratio
     if (ptype == "PT"){
       var aa = radioWert(document.forms[0].fields);
       if (aa =="meteogr"){
         if (pix < 700)pix=700; 
       }
       if (aa =="tephi"){
          if (pix < 500)pix=500;
       }
       ys = 1.0*pix;
       xs = pix;  
     }
   

// if crosssection use a fixed height width ratio depending on the vertical extent of the plot
       if (ptype == "CR"){
        
       b = document.forms[0].flevel.selectedIndex;
       topheight=document.forms[0].flevel[b].text;
       topheight=parseFloat(topheight);


       ys = 0.9*pix*(1000-topheight)/1000;
       if (ys < pix/2)ys=pix/2;
       xs = pix;  
     }



// set value into hidden text field for submit
    xs=parseInt(xs);
    ys=parseInt(ys);
    a.imgxs.value = xs;
    a.imgys.value = ys;

}


