function validate_email(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(email) == false) {
      //alert(email + ' Invalid Email Address');
      return false;
   }
   return true;
}
function validate_phone(string) {
   var reg = /[\+0-9 ]*$/;
   if(reg.test(string) == false) {
      //alert(email + ' Invalid Email Address');
      return false;
   }
   return true;
}
function ChangeImage(element_id, compid) {
  //alert(compid);
  var url_name = "rotate.php?ajax=1&compid="+compid;
//  dojo.byId(element_id).innerHTML = "<p align = 'center'><img src='img/ajax-loader_yellow.gif'></p>";
  //alert(dojo.byId(element_id).src);
  dojo.xhrGet({
    url: url_name,
	preventCache: true,
	timeout: 5000,
	load: function (data) {
	  dojo.byId(element_id).src = data;
	},
	error: function(err, ioArgs){
      console.error(err); // display the error
      alert(err);
	}
  })
}

function MouseOverOld(Which, FontC, BgC)
{
  window.bgc = Which.style.backgroundColor;
  window.fc = Which.style.color;

  Which.style.color = FontC;
  Which.style.backgroundColor = BgC;
}

function MouseOver(Which, FontC, BgC, BcgPict)
{

  if (BcgPict != undefined) {
    Which.style.background = "url(" + BcgPict + ")";
  }
  window.bgc = Which.style.backgroundColor;
  window.fc = Which.style.color;

  Which.style.color = FontC;
  Which.style.backgroundColor = BgC;

}
function MouseOut(Which, BcgPict)
{
  Which.style.color = window.fc;
  Which.style.backgroundColor = window.bgc; //variable was set in MouseOver function
  if (BcgPict != undefined) {
    Which.style.background = "url(" + BcgPict + ")";
  }
}
function tf(value) {
  if (value == true || value == false) {
    if (value == true) {
	  return 1;
	} else {
	  return 0;
	}
  }
}

function set_size(set_element, get_element) {
  //alert(document.body.offsetHeight + " " + dojo.byId(get_element).offsetHeight + " " + document.body.parentNode.clientHeight);
  var head_h = 210;
  //alert(head_h);
  if (get_element == "info_banner") {
    dojo.byId("info_banner").style.top = head_h + "px";
  	return;
  }
  head_h = head_h + dojo.byId('info_banner').offsetHeight + 5;
  dojo.byId("right_weather").style.top = head_h + "px";

  if (get_element == "right") {
    head_h = head_h + 203;
  }
  dojo.byId(get_element).style.top = head_h + "px";

  if (document.getElementById(set_element) == null) {
  	return;
  }

  dojo.byId(set_element).style.height = "100%";
  document.body.style.height = "100%";
  dojo.byId("bg").style.height = "100%";
  var element_height = dojo.byId(get_element).offsetHeight + head_h;
  if (element_height < 650) {
  	element_height = 700;
  }
  //alert(element_height + " right " + dojo.byId("right").offsetHeight + " center " + dojo.byId("right").offsetHeight + " get " + get_element)
  
  if (element_height < dojo.byId("right").offsetHeight + head_h) {
    element_height = 210 + dojo.byId("right").offsetHeight + head_h;
  }
  if (element_height < dojo.byId("center").offsetHeight + head_h) {
    element_height = 210 + dojo.byId("center").offsetHeight + head_h;
  }
  
  if (dojo.byId("right").offsetHeight + 203 > dojo.byId("center").offsetHeight) {
    element_height = 210 + dojo.byId("right").offsetHeight + head_h;
  } else {
    element_height = dojo.byId("center").offsetHeight + head_h;
  }
  //alert(dojo.byId("center").offsetHeight + "-" + dojo.byId("right").offsetHeight + "-" + document.body.offsetHeight + "-" + element_height);
  //alert(dojo.byId("pict").offsetHeight);
  //alert(document.body.offsetHeight);
  if (document.body.offsetHeight <= element_height) {
    dojo.byId(set_element).style.height = element_height + 'px';
    dojo.byId("bg").style.height = element_height + 'px';
    document.body.style.height = element_height + 'px';
  }
  //alert(document.body.style.height);
}

function fill_articles(menu_id, position, compid) {
  //alert(compid);
  show_right_column("");

  var url_name = "fill_articles.php?menu_id="+menu_id+"&position="+position+"&compid="+compid//+"&dojo.preventCache=" + (new Date()).getTime();
  //alert(url_name);
  dojo.byId(position).innerHTML = "<p align = 'center'><img src='img/ajax-loader_yellow.gif'></p>";
  
  dojo.xhrGet({
    url: url_name,
    preventCache: true,
	timeout: 5000,
	load: function (data) {
	  //alert(data);
	  dojo.byId(position).innerHTML = data;
	  if (data.search(/id="pict"/i) > -1 ) {
		ChangeImage("pict", compid);
	  }
	  /*
	  if (menu_id == 0 && position == "center") {
	    ChangeImage("pict", compid);
	  }
	  */
	  set_size("main", position);
	  //alert("2");
    },
	error: function(err, ioArgs){
      console.error(err); // display the error
      alert(err);
	}
  })
}

function contact(menu_id, position) {
  show_right_column("");

  var url_name = "contact.php?menu_id="+menu_id+"&position="+position;
  //alert(url_name);
  dojo.byId(position).innerHTML = "<p align = 'center'><img src='img/ajax-loader_yellow.gif'></p>";
  
  dojo.xhrGet({
    url: url_name,
	preventCache: true,
	timeout: 5000,
	load: function (data) {
	  dojo.byId(position).innerHTML = data;
	  set_size("main", position);
	},
	error: function(err, ioArgs){
      console.error(err); // display the error
      alert(err);
	}
  })
}

function check_contact() {
  var ret_value = true;
  var temp = "";

  temp = dojo.byId("email").value;
  if (!validate_email(temp)) {
    dojo.byId("lbl_email").style.color="red";
	ret_value = false;
  } else {
    dojo.byId("lbl_email").style.color="black";
  }

  temp = dojo.byId("body").value;
  if (temp.length < 5) {
    dojo.byId("lbl_body").style.color="red";
	ret_value = false;
  } else {
    dojo.byId("lbl_body").style.color="black";
  }

  return ret_value;
}


function contact_save(edit) {
  if (edit == 0) {
    var url_name = "save_contact.php?tbl=manufacturer";
  } else {
    var url_name = "save_contact.php?tbl=pilots&ed="+dojo.byId("id").value;
  }
  if (!check_contact()){
	alert("Please check marked items on form.");
    return;
  }
  dojo.rawXhrPost({
	url: url_name,
	content: {
	  "email":dojo.byId("email").value,
	  "body":dojo.byId("body").value,
	  "code":dojo.byId("code").value
	},
	load: function(data,ioargs){
	  if (data.substring(0,4) == "Secu") {
	    document.getElementById('siimage').src = 'securimage/securimage_show.php?sid=' + Math.random();
	    alert(data);
	  } else {
	    //alert("ok");
	    alert("Thanks for your question. Expect answer ASAP.")
		window.setTimeout("fill_articles(0, 'center')", 500)
	  }
	  console.log(data);
	}
  });
}

function registration(menu_id, position, compid) {
  //alert(year);
  show_right_column("");
  if (menu_id == "new_reg") {
    var url_name = "registration.php?menu_id="+menu_id+"&compid="+compid;
  } else {
    var url_name = "registration.php?menu_id="+menu_id+"&position="+position+"&compid="+compid;
  }
  //alert("Registration is closed.");
  //break;
  //alert(url_name);
  dojo.byId(position).innerHTML = "<p align = 'center'><img src='img/ajax-loader_yellow.gif'></p>";
  
  dojo.xhrGet({
    url: url_name,
	preventCache: true,
	timeout: 5000,
	load: function (data) {
	  //alert(data.indexOf("#out#"));
	  if (data.indexOf("#out#") > 0) {
	  	data = data.substring(0, data.indexOf("#out#"));
	  	var tmpText = "Sign on waiting list";
	  } else {
	  	var tmpText = "Register";
	  }
	  //končana registracija
	  var tmpText = "Registration is finished!";
	  //var tmpText = "Registration will start soon!";
	  //končana registracija	  
	  dojo.byId(position).innerHTML = data;
	  //alert(compid);
	  //alert(typeof(dojo.byId("new_reg")));
//	  if (typeof(dojo.byId("new_reg"))!="undefined") {
	  if (dojo.byId("new_reg")!=null) {
	    dojo.byId("new_reg").value = tmpText;
	    if (compid != 'aeros_2012') {
		  dojo.byId("new_reg").style.visibility="hidden";
	    } else {
		  dojo.byId("new_reg").style.visibility="visible";
	    }
	  }
	  //končana registracija	  
	  dojo.byId("new_reg").disabled=true;
	  //končana registracija	  
	  set_size("main", position);
	},
	error: function(err, ioArgs){
      console.error(err); // display the error
      alert(err);
	}
  })
}
function check_registration() {
  var ret_value = true;
  var temp = "";
  temp = dojo.byId("name").value;
  if (temp.length < 3) {
    dojo.byId("lbl_name").style.color="red";
	ret_value = false;
  } else {
    dojo.byId("lbl_name").style.color="black";
  }

  temp = dojo.byId("surname").value;

  if (temp.length < 3) {
    dojo.byId("lbl_surname").style.color="red";
	ret_value = false;
  } else {
    dojo.byId("lbl_surname").style.color="black";
  }
  
  temp = dojo.byId("address").value;
  if (temp.length < 3) {
    dojo.byId("lbl_address").style.color="red";
	ret_value = false;
  } else {
    dojo.byId("lbl_address").style.color="black";;
  }

  temp = dojo.byId("city").value;
  if (temp.length < 3) {
    dojo.byId("lbl_city").style.color="red";
	ret_value = false;
  } else {
    dojo.byId("lbl_city").style.color="black";
  }

  temp = dojo.byId("nation").value;
  if (temp == 0) {
    dojo.byId("lbl_nation").style.color="red";
	ret_value = false;
  } else {
    dojo.byId("lbl_nation").style.color="black";
  }

  temp = dojo.byId("email").value;
  if (!validate_email(temp)) {
    dojo.byId("lbl_email").style.color="red";
	ret_value = false;
  } else {
    dojo.byId("lbl_email").style.color="black";
  }

  temp = dojo.byId("gsm").value;
  if (temp.length < 9) {
    dojo.byId("lbl_gsm").style.color="red";
	ret_value = false;
  } else {
    dojo.byId("lbl_gsm").style.color="black";
  }

  temp = dojo.byId("emergency_contanct").value;
  if (temp.length < 3) {
    dojo.byId("lbl_emergency_contanct").style.color="red";
	ret_value = false;
  } else {
    dojo.byId("lbl_emergency_contanct").style.color="black";
  }

  temp = dojo.byId("emergency_phone").value;
  if (temp.length < 9) {
    dojo.byId("lbl_emergency_phone").style.color="red";
	ret_value = false;
  } else {
    dojo.byId("lbl_emergency_phone").style.color="black";
  }

  temp = dojo.byId("hgman").value;
  if (temp.length < 3) {
    dojo.byId("lbl_hgman").style.color="red";
	ret_value = false;
  } else {
    dojo.byId("lbl_hgman").style.color="black";
  }

  temp = dojo.byId("hgmodel").value;
  if (temp.length < 3) {
    dojo.byId("lbl_hgmodel").style.color="red";
	ret_value = false;
  } else {
    dojo.byId("lbl_hgmodel").style.color="black";
  }

  temp = dojo.byId("pilotlicence").value;
  if (temp.length < 3) {
    dojo.byId("lbl_pilotlicence").style.color="red";
	ret_value = false;
  } else {
    dojo.byId("lbl_pilotlicence").style.color="black";
  }

  temp = dojo.byId("failicence").value;
  if (temp.length < 3) {
    dojo.byId("lbl_failicence").style.color="red";
	ret_value = false;
  } else {
    dojo.byId("lbl_failicence").style.color="black";
  }

  return ret_value;
}

function registration_save(edit, compid) {
  if (edit == 0) {
    var url_name = "save.php?tbl=manufacturer";
  } else {
    var url_name = "save.php?tbl=pilots&ed="+dojo.byId("id").value;
  }
  if (dojo.byId("gender_m").checked) {
    var gender = 0;
  } else {
    var gender = 1;
  }
  if (!check_registration()){
	alert("Please check marked items on form.");
    return;
  }
  dojo.rawXhrPost({
	url: url_name,
	content: {
	  "name":dojo.byId("name").value,
	  "surname":dojo.byId("surname").value,
	  "gender":gender,
	  "address":dojo.byId("address").value,
	  "city":dojo.byId("city").value,
	  "nation":dojo.byId("nation").value,
	  "club":dojo.byId("club").value,
	  "email":dojo.byId("email").value,
	  "gsm":dojo.byId("gsm").value,
	  "emergency_contanct":dojo.byId("emergency_contanct").value,
	  "emergency_phone":dojo.byId("emergency_phone").value,
	  "hgman":dojo.byId("hgman").value,
	  "hgmodel":dojo.byId("hgmodel").value,
	  "pilotlicence":dojo.byId("pilotlicence").value,
	  "failicence":dojo.byId("failicence").value,
	  "hgmodel":dojo.byId("hgmodel").value,
	  "trans_up":tf(dojo.byId("trans_up").checked),
	  "trans_retrival":tf(dojo.byId("trans_retrival").checked),
	  "code":dojo.byId("code").value,
	  "compid":compid
	},
	load: function(data,ioargs){
	  if (data.substring(0,4) == "Secu") {
	    document.getElementById('siimage').src = 'securimage/securimage_show.php?sid=' + Math.random();
	    alert(data);
	  } else {
	    //alert("ok");
		window.setTimeout("registration(0, 'center', '"+compid+"')", 500)
	  }
	  console.log(data);
	}
  });
}

function show_home()
{
  alert("home");
  dojo.byId("sub_1").innerHTML = "<p align = 'center'><img src='img/ajax-loader_yellow.gif'></p>";
  dojo.byId("sub_1").innerHTML="home";
}
function show_menu(ed){

  var url_name = "show_menu.php?id_parent="+ed;
  //dojo.byId("menu").innerHTML = "<p align = 'center'><img src='img/ajax-loader_yellow.gif'></p>";
  /*
  dojo.xhrGet({
    url: url_name,
	timeout: 5000,
	load: function (data) {
	  dojo.byId("menu").innerHTML = data;
	},
	error: function(err, ioArgs){
      console.error(err); // display the error
      alert(err);
	}
  })
*/
}

function DateDiff() {
  //Set the two dates
  today=new Date()
  var comp=new Date(2010, 2, 10) //Month is 0-11 in JavaScript
  if (today.getFullYear()>=2010 && today.getMonth()>=2 && today.getDate()>10) { //if Christmas has passed already
    //christmas.setFullYear(christmas.getFullYear()+1) //calculate next year's Christmas
    return "No results yet??";
  }
	//Set 1 day, hour, minute,second in milliseconds
    var one_day=1000*60*60*24;
    var one_hour=1000*60*60;
    var one_minute=1000*60;
    var one_second=1000;

//Calculate difference btw the two dates, and convert to days
   return Math.ceil((comp.getTime()-today.getTime())/(one_minute));

}

function show_basics()
{
  alert("basics");
}

function show_locals()
{
  alert("locals");
}

function show_registration()
{
  alert("registration");
}

function show_right_column(show) {
  dojo.byId("right").style.visibility = show;
  dojo.byId("right_weather").style.visibility = show;
  if (show == "hidden") {
    dojo.byId("center").style.width = "990px";
  } else {
    dojo.byId("center").style.width = "700px";
  }
}

function show_results(menu_id, position, file_name, dir)
{
  if (String(file_name).substring(0,1) == "d") {
    //alert(file_name.substring(1,file_name.length));
    var url_name = "results.php?dir="+file_name.substring(1,file_name.length);
  } else {
    var url_name = "results.php?file_name="+dir+file_name+"&dir="+String(dir).substring(8,12);
  }
  //alert(url_name);

  dojo.byId(position).innerHTML = "<p align = 'center'><img src='img/ajax-loader_yellow.gif'></p>";
  show_right_column("hidden");
  dojo.xhrGet({
    url: url_name,
	preventCache: true,
	timeout: 5000,
	load: function (data) {
	  dojo.byId(position).innerHTML = data;
	  if (typeof(file_name) != "undefined") {
	  	//dojo.byId("right").style.visibility = "hidden";
	  	//dojo.byId("right_weather").style.visibility = "hidden";
	  	//dojo.byId("center").style.width = "990px";
	  } else {
	  	//dojo.byId("right").style.visibility = "";
	  	//dojo.byId("right_weather").style.visibility = "";
	  	//dojo.byId("center").style.width = "700px";
	  }
	  set_size("main", position);
      if (data == "No results yet!") {
	    alert("We expect first results in " + DateDiff() + " minutes. :)");
      }
	},
	error: function(err, ioArgs){
      console.error(err); // display the error
      alert(err);
	}
  })
}
