function IncludeJavaScript(jsFile)
{
	document.write('<script type="text/javascript" src="'
			+ jsFile + '"></script>'); 
}

IncludeJavaScript('js/date.js');

function format_number(pnumber,decimals){ 
    if (isNaN(pnumber)) { return 0}; 
    if (pnumber=='') { return 0}; 
    var snum = new String(pnumber); 
    var sec = snum.split('.'); 
    var whole = parseFloat(sec[0]); 
    var result = ''; 
    if(sec.length > 1){ 
        var dec = new String(sec[1]); 
        dec = String(parseFloat(sec[1])/Math.pow(10,(dec.length - decimals))); 
        dec = String(whole + Math.round(parseFloat(dec))/Math.pow(10,decimals)); 
        var dot = dec.indexOf('.'); 
        if(dot == -1){ 
            dec += '.'; 
            dot = dec.indexOf('.'); 
        } 
        while(dec.length <= dot + decimals) { dec += '0'; } 
        result = dec; 
    } else{ 
        var dot; 
        var dec = new String(whole); 
        dec += '.'; 
        dot = dec.indexOf('.');        
        while(dec.length <= dot + decimals) { dec += '0'; } 
        result = dec; 
    }    
    return result; 
}

//check if input value is an Integer
function isInteger(val) {
	
	var numberToTest = val.value;
	var isInteger = /^-?\d+$/.test(numberToTest);
	return isInteger;
}


 function Quake(id, lat, lon, magnitude, quakeDate, significance,content)
 {
	 this.id=id;
	 this.lat=lat;
	 this.lon=lon;
	 this.magnitude = magnitude;
	 this.quakeDate = quakeDate;
	 this.significance = significance;
	 this.content = content;
 }
 
 
function getQuakeIcon(utc4hoursAgo,utc24hoursAgo,quakeDate, magnitude, significant, dateformat)
{
/*
	alert('original date str: '+quakeDate);
	var date = Date.parseDate(quakeDate, 'd-m-Y,H:i:s',true);
	alert('original date: '+date);
*/
	if(significant)
	{
		return getSignficantIcon(utc4hoursAgo,utc24hoursAgo,quakeDate, magnitude, dateformat);
	}else
	{
		return getNonSignficantIcon(utc4hoursAgo,utc24hoursAgo,quakeDate, magnitude, dateformat);
	}
}

function getSignficantIcon(utc4hoursAgo,utc24hoursAgo,quakeDate, magnitude, dateformat)
{
	return 's'+getNonSignficantIcon(utc4hoursAgo,utc24hoursAgo,quakeDate, magnitude, dateformat);
	
}

var MILLS_HOUR = 1000*60*60;
var MILLS_DAY = MILLS_HOUR*24;


function getNonSignficantIcon(utc4hoursAgo,utc24hoursAgo,quakeDate, magnitude, dateformat)
{
//	var utcnow = getUtcDateOfNow();
//	var utc4hoursAgo = new Date(utcnow.getTime()-MILLS_HOUR*4);
//	var utc24hoursAgo = new Date(utcnow.getTime()-MILLS_HOUR*24);
	//alert('utc4hoursAgo>utc24hoursAgo='+(utc4hoursAgo>utc24hoursAgo))

	//newest
	//alert('quakeDate:'+quakeDate+' magnitude: '+magnitude);
	var timePrefix;
//	if(quakeDate>utc4hoursAgo)

	if(compareTwoDates(quakeDate, utc4hoursAgo, dateformat))
	{
		timePrefix = 'newest';
		//biggest
		if(magnitude>=8)
		{
			return timePrefix+'biggest';
		} else if (magnitude>=6)
		{
			return timePrefix+'bigger';
		} else if (magnitude>=5)
		{
			return timePrefix+'small';
		}else if (magnitude>=3)
		{
			return timePrefix+'smaller';
		}else
		{
			return timePrefix+'smallest';
		}
		
	} else if (compareTwoDates(quakeDate, utc24hoursAgo, dateformat) && compareTwoDates(utc4hoursAgo, quakeDate, dateformat))		
//	} else if (quakeDate>utc24hoursAgo && quakeDate<=utc4hoursAgo)
	{
		timePrefix = 'old';
		if(magnitude>=8)
		{
			return timePrefix+'biggest';
		} else if (magnitude>=6)
		{
			return timePrefix+'bigger';
		} else if (magnitude>=5)
		{
			return timePrefix+'small';
		}else if (magnitude>=3)
		{
			return timePrefix+'smaller';
		}else
		{
			return timePrefix+'smallest';
		}
	} else // quake is older
	{
		timePrefix = 'older';
		if(magnitude>=8)
		{
			return timePrefix+'biggest';
		} else if (magnitude>=6)
		{
			return timePrefix+'bigger';
		} else if (magnitude>=5)
		{
			return timePrefix+'small';
		}else if (magnitude>=3)
		{
			return timePrefix+'smaller';
		}else
		{
			return timePrefix+'smallest';
		}
	}

	
}

function getUtcDateOfNow()
{
	var date = new Date();
	//alert('original date: '+date);

	var utcdate = new Date(date.getUTCFullYear(),date.getUTCMonth(),date.getUTCDate(),date.getUTCHours(),date.getUTCMinutes(),date.getUTCSeconds(),date.getUTCMilliseconds());
	//alert('original date: '+date+'\n'+'utc date: '+utcdate);
	return utcdate;
	
}
function compareTwoDates(date1,date2, dateformat) {
	//var dateformat = "d-MM-yyyy,HH:mm:ss";
	var dateInt = compareDates(date1,dateformat,date2,dateformat)
	if(dateInt == 1){
		return true;
	}
	return false;
}


function getUtcDateOfToday()
{
	var date = new Date();
	//alert('original date: '+date);

	var utcdate = new Date(date.getUTCFullYear(),date.getUTCMonth(),date.getUTCDate(),0,0,0,0);
	//alert('original date: '+date+'\n'+'utc date: '+utcdate);
	return utcdate;
	
}
/*
 * This functions returns an array containing 36 points to draw an
 * ellipse.
 *
 * @param x {double} X coordinate
 * @param y {double} Y coordinate
 * @param a {double} Semimajor axis
 * @param b {double} Semiminor axis
 * @param angle {double} Angle of the ellipse
 */
 function calculateEllipse(x, y, a, b, angle, steps) 
 {
   if (steps == null)
     steps = 36;
   var points = [];
  
   // Angle is given by Degree Value
   var beta = -angle * (Math.PI / 180); //(Math.PI/180) converts Degree Value into Radians
   var sinbeta = Math.sin(beta);
   var cosbeta = Math.cos(beta);
  
   for (var i = 0; i < 360; i += 360 / steps) 
   {
     var alpha = i * (Math.PI / 180) ;
     var sinalpha = Math.sin(alpha);
     var cosalpha = Math.cos(alpha);
  
     var X = x + (a * cosalpha * cosbeta - b * sinalpha * sinbeta);
     var Y = y + (a * cosalpha * sinbeta + b * sinalpha * cosbeta);
  
     points.push(new OpenLayers.Geometry.Point(X, Y));
    }
  
   return points;
 }
 
 
 function toggleCheckBoxesForAddRemoveFields(eId,isEnabled) { 
	    var el = document.getElementById(eId); 
	    if(el == undefined)
	    {
	    	return;
	    }
	    var chk = el.getElementsByTagName('input'); 
	    var len = chk.length; 
	    if(len>0)
	    {
	    	for (var i = 0; i < len; i++) { 
	            if (chk[i].type === 'checkbox') 
	            { 
	            	if(chk[i].disabled==false)
	            	{
	                	chk[i].checked = isEnabled;
	                } 
	                continue;
	            } 
	        }
	    }
	             
	    if (el.childNodes && el.childNodes.length > 0) 
		{
			for (var x = 0; x < el.childNodes.length; x++) 
			{
				//toggleDisabled(el.childNodes[x],isEnabled);
				toggleCheckBoxesForAddRemoveFields(el.childNodes[x].id,isEnabled);
			}
		}
	   
	}
