//------------------------------------------------------------------------------
// Miscellaneous functions
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
// Change Images
//------------------------------------------------------------------------------

function changeImages(id, img)
{
   if (document.images)
      document[id].src = eval(img + ".src");
}

//------------------------------------------------------------------------------
// Check Text Box Is Not Empty
//------------------------------------------------------------------------------

function checkIfTextBoxIsEmpty(currform, keyword)
{
   if (!currform || !currform.keyword)
      return true;
   if (currform.keyword.value.length > 0)
      return true;
   alert("Please enter a string to search, then click Find")
   return false;
}

//------------------------------------------------------------------------------
// Test
//------------------------------------------------------------------------------

function convert_temp()
{
   var input_temp = document.Conversion.input_temp.value;
   var output_temp = "0";
      // Celsius - Farenheit - Kelvin
   var input_temp_unit = document.Conversion.input_temp_unit.value;
   var output_temp_unit = document.Conversion.output_temp_unit.value;
      //
   switch (input_temp_unit) {
      case "Celsius" :   if (input_temp < -273.16) { alert ("Number must be positive"); document.Conversion.output_temp.value = "Invalid"; return; }; break;
      case "Farenheit" : if (input_temp < -500) { alert ("Number must be positive"); document.Conversion.output_temp.value = "Invalid"; return; }; break;
      case "Kelvin" :    if (input_temp < 0) { alert ("Number must be positive"); document.Conversion.output_temp.value = "Invalid"; return; }; break;
   }
      //
   switch (input_temp_unit) {
      case "Celsius" : switch (output_temp_unit) {
                          case "Celsius" :   output_temp = input_temp; break;
                          case "Farenheit" : output_temp = (input_temp*9/5)+32; break;
                          case "Kelvin" :    output_temp = (input_temp*1)+273.16; break;
                       }
                       break;
      case "Farenheit" : switch (output_temp_unit) {
                          case "Celsius" :   output_temp = (input_temp-32)*5/9; break;
                          case "Farenheit" : output_temp = input_temp; break;
                          case "Kelvin" :    output_temp = ((input_temp-32)*5/9)+273.16; break;
                       }
                       break;
      case "Kelvin" : switch (output_temp_unit) {
                          case "Celsius" :   output_temp = (input_temp*1)-273.16; break;
                          case "Farenheit" : output_temp = (input_temp*9/5)+32; break;
                          case "Kelvin" :    output_temp = input_temp; break;
                       }
                       break;
   }
   document.Conversion.output_temp.value = Math.round(output_temp*100)/100;
}

function show_temp()
{
   var input_temp_unit = document.Conversion.input_temp_unit.value;
   var output_temp_unit = document.Conversion.output_temp_unit.value;
      //
   switch (input_temp_unit) {
      case "Celsius" : switch (output_temp_unit) {
                          case "Celsius" :   alert("output_temp = input_temp"); break;
                          case "Farenheit" : alert("output_temp = (input_temp*9/5)+32"); break;
                          case "Kelvin" :    alert("output_temp = (input_temp*1)+273.16"); break;
                       }
                       break;
      case "Farenheit" : switch (output_temp_unit) {
                          case "Celsius" :   alert("output_temp = (input_temp-32)*5/9"); break;
                          case "Farenheit" : alert("output_temp = input_temp"); break;
                          case "Kelvin" :    alert("output_temp = ((input_temp-32)*5/9)+273.16"); break;
                       }
                       break;
      case "Kelvin" : switch (output_temp_unit) {
                          case "Celsius" :   alert("output_temp = (input_temp*1)-273.16"); break;
                          case "Farenheit" : alert("output_temp = (input_temp*9/5)+32"); break;
                          case "Kelvin" :    alert("output_temp = input_temp"); break;
                       }
                       break;
   }
}

function open_math()
{
   var url = document.URL;
   var value = document.Conversion.input_temp.value;
   var result = value;
   var winname = "Test";
//   var options = 'scrollbars,resizable,menubar';
//   var win = window.open('', winname, options);
   document.writeln('<html>');
   document.writeln('<head>');
   document.writeln('<title>', winname, '</title>');
   document.writeln('<link rel="stylesheet" href="../css/common.css">');
   document.writeln('<script type="text/javascript" src="../javascripts/printBanner.js"></script>');
   document.writeln('</head>');
   document.writeln('<body>');

   document.writeln('<a name="top"></a>');
   document.writeln('<script type="text/javascript">');
   document.writeln('printBanner("../", "', winname, '")');
   document.writeln('</script>');

   document.writeln('<p>');
   document.writeln('Result: ', result);
   document.writeln('</p>');
   document.writeln('<p>');
   document.writeln('URL: ', url);
   document.writeln('</p>');
   document.writeln('</body>');
   document.writeln('</html>');
   document.close();
}

function make_math_test()
{
   var x;
   var y;
   var input_op = document.Math_Test.input_op.value;
   var digits = document.Math_Test.digits.value;
      //
   x = Math.round(Math.random()*Math.pow(10, digits));
   y = Math.round(Math.random()*Math.pow(10, digits));
      //
   while (input_op == "/" && y == 0)
      y = Math.round(Math.random()*Math.pow(10, digits));
   while (input_op == "-" && x < y) {
      x = Math.round(Math.random()*Math.pow(10, digits));
      y = Math.round(Math.random()*Math.pow(10, digits));
   }
      //
   document.Math_Test.op.value = input_op;
   document.Math_Test.x.value = x;
   document.Math_Test.y.value = y;
   document.Math_Test.result.value = "";
}

function check_math_test()
{
   var x = document.Math_Test.x.value - 0;
   var y = document.Math_Test.y.value - 0;
   var op = document.Math_Test.op.value;
   var result;
      //
   switch (op) {
      case "+" : result = x + y; break;
      case "-" : result = x - y; break;
      case "x" : result = x * y; break;
      case "/" : result = x / y; break;
   }
      //
   if (result == document.Math_Test.result.value)
      alert("Bravo!");
   else
      alert("Try again");
}

