/******************************************************************************/
/******************************************************************************/




/******************************************************************************/
function hide_form(form_id){
   var form = document.getElementById(form_id);
   if(!form){ console.log("form \"" + form_id + "\" not found in document"); return; }
   
   // Hide the hidable section of the form
   //var hidable = form.getElementsByClassName("form_hidable");
   var hidable = getElementsByClassName(form, "form_hidable");
   //hidable[0].style.display = "none";
   for(i = 0; i < hidable.length;i++){
      hidable[i].style.display = "none";
   }
   
   
   // Dispaly the form toggle
   //var formshowform = form.getElementsByClassName("form_show_form");
   var formshowform = getElementsByClassName(form, "form_show_form");
   formshowform[0].style.display = "block";
}


/******************************************************************************/
function show_form(form_id){
   var form = document.getElementById(form_id);
   if(!form){ console.log("form \"" + form_id + "\" not found in document"); return; }
   
   //var hidable = form.getElementsByClassName("form_hidable");
   var hidable = getElementsByClassName(form, "form_hidable");
   hidable[0].style.display = "block";
   
   //var formshowform = form.getElementsByClassName("form_show_form");
   var formshowform = getElementsByClassName(form, "form_show_form");
   formshowform[0].style.display = "none";
}

/******************************************************************************/
function show_form_output(form_id, type, text){   
   var form = document.getElementById(form_id);
   if(!form){ console.log("form \"" + form_id + "\" not found in document"); return; }
   
   //var formout = form.getElementsByClassName("form_output");
   var formout = getElementsByClassName(form, "form_output");
   
   switch(type.toUpperCase()){
   case "NOTICE":
      if(text){ formout[0].innerHTML = text; }
      break;
   case "ERROR":
      if(text){ formout[0].innerHTML = "<span style=\"color:red;\">" + text + "</span>"; }
      break;
   case "LOAD":
      var str = "";
      if(text){
         str += text;
      }
      str = "<img src=\"images/ui/loading2.gif\" alt=\"\" class=\"centered\" style=\"height:30px;width:30px;\" />"
      formout[0].innerHTML = str;
      break;
   }
   
   formout[0].style.display = "block";
}
         
/******************************************************************************/
function hide_form_output(form_id){
   var form = document.getElementById(form_id);
   if(!form){ console.log("form \"" + form_id + "\" not found in document"); return; }
   
   var formout = getElementsByClassName(form, "form_output");
   formout[0].style.display = "none";
}

/******************************************************************************/
function set_form_action(form_id, action, action_text){
   var form = document.getElementById(form_id);
   if(!form){ console.log("form \"" + form_id + "\" not found in document"); return; }
   
   var input_actions = getElementsByClassName(form, "form_action");
   input_actions[0].value = action;
   
   var action_buttons = getElementsByClassName(form, "form_action_button");
   if(action_text){
      action_buttons[0].innerHTML = action_text;
   }else{
      action_buttons[0].innerHTML = action;
   }
}

/******************************************************************************/
function toggle_sellect_on_value(input, otherid, value){
   var other_input = document.getElementById(otherid);
   var item_div = other_input.parentNode;
   
   if(input.value == value){
      item_div.style.display = "block";
   }else{
      item_div.style.display = "none";
   }
}

/******************************************************************************/
function show_form_input_invalid(input_div, isvalid, message){
   if(isvalid){
      input_div.style.borderColor = "transparent";
      input_div.style.backgroundColor = "transparent";
      
      //var item_error_div = input_div.getElementsByClassName("form_item_error");
      var item_error_div = getElementsByClassName(input_div, "form_item_error");
      if(item_error_div.length > 0){
         item_error_div[0].innerHTML = "";
      }
   }else{
      input_div.style.borderColor = "red";
      input_div.style.backgroundColor = "#ffffe1";
      
      //var item_error_div = input_div.getElementsByClassName("form_item_error");
      var item_error_div = getElementsByClassName(input_div, "form_item_error");
      if(item_error_div.length > 0){
         item_error_div[0].innerHTML = message;
      }
   }
}

/******************************************************************************/
function set_select_by_id(select, id){
   for(i = 0; i < select.options.length; i++){
      if(select.options[i].value == id){
         select.selectedIndex = i;
         break;
      }
   }
}


/******************************************************************************/

/******************************************************************************/
function validate_input_with_vfunc(input){
   var item_div = input.parentNode;
   
   var result = [false, "init"];
   
   for(var i = 1; i < validate_input_with_vfunc.arguments.length; i++){
      var arg = validate_input_with_vfunc.arguments[i];
      if(typeof arg == 'function'){
         result = arg(input.value);
         if(!result[0]){ break; }
      }else{
         // not a function?
         //console.log(arg);
      }
   }
   
   show_form_input_invalid(item_div, result[0], result[1]);
   
}

/******************************************************************************/
function allowFloat(elem, evt){
   var charset = "0123456789.";
   return allowCharset(elem, evt, charset);
}

function allowURL(elem, evt){
   //
   return true;
}

function allowEmailAddress(elem, evt){
   var charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_@.%+-";
   return allowCharset(elem, evt, charset);
}

function allowPlainName(elem, evt){
   var charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\', -";
   return allowCharset(elem, evt, charset);
}

function allowDate(elem, evt){
   var charset = "0123456789/-.";
   return allowCharset(elem, evt, charset);
}

function allowPhone(elem, evt){
   var charset = "0123456789() -.+*#";
   return allowCharset(elem, evt, charset);
}

function allowCharset(elem, evt, charset){
   evt = (evt) ? evt : ((event) ? event : null);
   if(!evt){ return true; }
   
   /*if(window.event){
      key = window.event.keyCode;
   }else if(e){
      key = e.which;
   }else{
      return true;
   }*/
   
   var charCode = (evt.charCode) ? evt.charCode : evt.keyCode;
   var charStr = String.fromCharCode(charCode);
   
   if(((charset).indexOf(charStr) > -1)){
      return true;
   }
   
   // FF and Safari(iPhone) issue keypressed events
   // for special chars
   if(true){
      if(charCode == 8){ return true; } // Backspace
      if(charCode == 9){ return true; } // Tab
      if(charCode == 32){ return true; } // Space
      if(charCode == 33){ return true; } // Page Up
      if(charCode == 34){ return true; } // Page Down
      if(charCode == 35){ return true; } // End
      if(charCode == 36){ return true; } // Home
      if(charCode == 37){ return true; } // Left
      if(charCode == 38){ return true; } // Up
      if(charCode == 39){ return true; } // Right
      if(charCode == 40){ return true; } // Down
      if(charCode == 46){ return true; } // Delete
   }
   
   return false;
   
}














