// JavaScript Document

/*****************************************************************************************************/
/*                                                                                                   */
/*                                  FUNCTION VALIDATION DATE ANALYSES                                */          
/*                                                                                                   */
/*****************************************************************************************************/
function showAnalysePrices(){
	var sday = Number(document.getElementById('start_day').value);
	var smonth = Number(document.getElementById('start_month').value);
	var syear = Number(document.getElementById('start_year').value);
	
	var start_date = String(sday+"/"+smonth+"/"+syear);
	if (sday == 0 || smonth == 0 || syear == 0){
		//
	}
	else{
		var valid = start_date.isDate(smonth, sday, syear);
		if (valid == false){ 
			alert("Please select a valid start date.");
			return;
		}
	}
	
	
	var eday = Number(document.getElementById('end_day').value);
	var emonth = Number(document.getElementById('end_month').value);
	var eyear = Number(document.getElementById('end_year').value);
	
	var end_date = String(eday+"/"+emonth+"/"+eyear);
	
	if (eday == 0 || emonth == 0 || eyear == 0){
		//
	}
	else{
		var valid = end_date.isDate(emonth, eday, eyear);
		if (valid == false){ 
			alert("Please select a valid end date.");
			return;
		}
	}
	
	
	if (sday == 0 || smonth == 0 || syear == 0 || eday == 0 || emonth == 0 || eyear == 0){
		//	
	}
	else{
		//get number of days (end date - start date)
		var days = DaysBetween2Dates(syear, smonth, sday,
									 eyear, emonth, eday);
		if (days <=0){
			alert("Your selected time period is invalid!");
			return;
		}
	}
	
	
	document.getElementById("show").value = "table";
	
	document.getElementById("form_page").submit();
	//document.location.href="?start_day="+sday+"&start_month="+smonth+"&start_year="+syear+"&end_day="+eday+"&end_month="+emonth+"&end_year="+eyear;
}



/*****************************************************************************************************/
/*                                                                                                   */
/*                                  FUNCTION GRAPHIC ANALYSES                                        */          
/*                                                                                                   */
/*****************************************************************************************************/
function showAnalyseGraphic(){
	var sday = Number(document.getElementById('start_day').value);
	var smonth = Number(document.getElementById('start_month').value);
	var syear = Number(document.getElementById('start_year').value);
	
	var start_date = String(sday+"/"+smonth+"/"+syear);
	if (sday == 0 || smonth == 0 || syear == 0){
		//
	}
	else{
		var valid = start_date.isDate(smonth, sday, syear);
		if (valid == false){ 
			alert("Please select a valid start date.");
			return;
		}
	}
	
	
	var eday = Number(document.getElementById('end_day').value);
	var emonth = Number(document.getElementById('end_month').value);
	var eyear = Number(document.getElementById('end_year').value);
	
	var end_date = String(eday+"/"+emonth+"/"+eyear);
	
	if (eday == 0 || emonth == 0 || eyear == 0){
		//
	}
	else{
		var valid = end_date.isDate(emonth, eday, eyear);
		if (valid == false){ 
			alert("Please select a valid end date.");
			return;
		}
	}
	
	if (sday == 0 || smonth == 0 || syear == 0 || eday == 0 || emonth == 0 || eyear == 0){
		//	
	}
	else{
		//get number of days (end date - start date)
		var days = DaysBetween2Dates(syear, smonth, sday,
									 eyear, emonth, eday);
		if (days <=0){
			alert("Your selected time period is invalid!");
			return;
		}
	}
	
	document.getElementById("ids").value = document.getElementById("ids").value.split(",")[0];
	
	var symbol1 = document.getElementById("compare_company_1").value;
	var symbol2 = document.getElementById("compare_company_2").value;
	
	if (symbol1.length != 0){
		document.getElementById("ids").value += ","+symbol1;
	}
	if (symbol2.length != 0){
		document.getElementById("ids").value += ","+symbol2;
	}
	document.getElementById("show").value = "chart";
	
	document.getElementById("form_page").submit();
	
}



function initChart(){
	//daca este creat obiectul de tip chart
	if (typeof(soChart) != null && typeof(soChart) != "undefined"){
		
		var url = document.location.href.split("?")[1];
		www.post("xml_graph_values.php", url,
			 function(response) {
				 soChart.addVariable("xmlfile", "includes/xml/"+response);
				 soChart.write("flashcontent");
			 }
			 );
	}
}



function DaysBetween2Dates(s_yr, s_mo, s_dy, e_yr, e_mo, e_dy){
	var d, r, t1, t2, t3;            //Declare variables.
	var MinMilli = 1000 * 60         //Initialize variables.
	var HrMilli = MinMilli * 60
	var DyMilli = HrMilli * 24
	
	t1 = Date.UTC(s_yr, s_mo - 1, s_dy)    //Get milliseconds since 1/1/1970.
	t2 = Date.UTC(e_yr, e_mo - 1, e_dy)    //Get milliseconds since 1/1/1970.
	
	if (t2 >= t1) t3 = t2 - t1;
	else return 0;
	
	r = Math.round(t3 / DyMilli);
	
	return(r+1);                       //Return difference.

}