//Error Messages
blank_err = 'This field is mandatory! It cannot be left blank';
taken_err = 'Already taken! please choose a different usename';
space_err = 'space characters not allowed';
captcha_err = 'security codes do not match';

function validate_form(f){ //on form submit: validate field names starting with "req" (mandatory fields)
  var err_cnt=0;
  for(j=0;j<document.getElementById(f).elements.length;j++){
    x = document.getElementById(f).elements[j].id;
    if(x.substr(0,3)=='req'){
      if(is_blank(x)){ //check for blank entry
        infopanel_on(x); //highlight infopanel 
        show_error_msg(x,blank_err);
        err_cnt++;
      }else if(msg=is_valid(x)){ //validate field
        infopanel_on(x); //highlight infopanel 
        show_error_msg(x,msg);
        err_cnt++;
      }else{ //show green tick
        show_green_tick(x);
      }
    }
  }
  if(err_cnt==0) document.getElementById(f).submit();
}

function is_valid(x){
  //evaluates the field dynamically
  return(eval('validate_'+x+'(\''+x+'\')'));
}

function available(x,a){ //check data collision in db
  availability = true;
  for(i=0;i<a.length;i++){
    if(document.getElementById(x).value.toUpperCase() == a[i].toUpperCase()) {
      availability = false;
      break;
    }
  }
  return(availability);
}

function do_blur(x){ //evaluates the field when field focus is lost from it
  //trim field value
  document.getElementById(x).value=trim(document.getElementById(x).value);
  type=x.substr(0,3);
  
  if(type=='req'){ //mandatory field
    if(is_blank(x)){ //check for blank entry
      show_error_msg(x,blank_err);
    }else if(msg=is_valid(x)){ //validate field
      show_error_msg(x,msg);
    }else{ //show green tick
      show_green_tick(x);
    }
  }else{ //optional field
    if(is_blank(x)){ //check for blank entry
      core_id = get_core_id(x);
      document.getElementById('infobox_'+x).innerHTML='';  
    }else if(msg=is_valid(x)){ //validate field
      show_error_msg(x,msg);
    }else{ //show green tick
      show_green_tick(x);
    }  
  }
  
}

function do_focus(x){ //provide assistance on field focus
  if(is_blank(x)){
    show_help_assitant(x);
  }else{
    do_blur(x);
  }
}

function get_core_id(x){
  //trim id to extract core id (e.g.. req_username -> username)
  if(x.substr(0,3)=="req")
    return(x.substr(4));
  else
    return(x);
}

function format_core_id(x){
  //replaces '_' with ' 's globally (e.g.. first_name -> first name)
  return(x.replace(/_/g,' '));
}

function is_blank(x){
  //checks if a field is left blank
  return(!document.getElementById(x).value);
}

function hide_info(x){
  //fades out the info
  opacity('infobox_'+x,90,0,1000);
}

function show_info(x){
  //fades in the info
  opacity('infobox_'+x,0,90,100);
}

function opacity(id, opacStart, opacEnd, millisec){
  //sets opacity of an DOM object
  //speed for each frame
  var speed = Math.round(millisec / 100);
  var timer = 0;

  //determine the direction for the blending, if start and end are the same nothing happens
  if(opacStart > opacEnd) {
    for(i = opacStart; i >= opacEnd; i--) {
      setTimeout("change_opacity(" + i + ",'" + id + "')",(timer * speed));
      timer++;
    }
  } else if(opacStart < opacEnd) {
    for(i = opacStart; i <= opacEnd; i++) {
      setTimeout("change_opacity(" + i + ",'" + id + "')",(timer * speed));
      timer++;
    }
  }
}

function change_opacity(opacity, id) {
  //change opacity (cross browsers)
  var object = document.getElementById(id).style;
  object.opacity = (opacity / 100);
  object.MozOpacity = (opacity / 100);
  object.KhtmlOpacity = (opacity / 100);
  object.filter = "alpha(opacity=" + opacity + ")";
}

function infopanel_on(x){
  document.getElementById('div_'+x).className='cp_infopanel_on';
}

function infopanel_off(x){
  document.getElementById('div_'+x).className='cp_infopanel_off';
}

function show_green_tick(x){
  infopanel_off(x);
  obox = document.getElementById('infobox_'+x);
  obox.innerHTML='<img src="images/icon_mini_tick.gif" align="left" />';
  show_info(x);
  //setTimeout(function(){hide_info(x)},2000);
}

function show_error_msg(x,msg){
  core_id = get_core_id(x);
  formatted_core_id = format_core_id(core_id);

  obox = document.getElementById('infobox_'+x);
  obox.innerHTML='';
  obox.innerHTML+='<img src="images/icon_mini_exclamation.gif" align="left" />';
  obox.innerHTML+='<div class="red"><b>Invalid '+formatted_core_id+':</b><br />'+msg+'</div>';
  show_info(x);
  //setTimeout(function(){hide_info(x)},2000);
}

function show_help_assitant(x){
  core_id = get_core_id(x);
  formatted_core_id = format_core_id(core_id);

  obox = document.getElementById('infobox_'+x);
  obox.innerHTML='';
  obox.innerHTML+='<img src="images/icon_mini_pointer.gif" align="left" />';
  obox.innerHTML+='<div class="green"><b>'+formatted_core_id+':</b><br />Please enter the '+formatted_core_id+'</div>';
  show_info(x);
  //setTimeout(function(){hide_info(x)},2000);
}

function trim(str){
  s=str.replace(/^(\s)*/, '');
  s=s.replace(/(\s)*$/, '');
  return(s);
}

function disable_button(x){
  document.getElementById(x).disabled=true;
  document.getElementById(x).className='cp_button_disabled';
}

function enable_button(x){
  document.getElementById(x).disabled=false;
  document.getElementById(x).className='cp_button';
}

String.prototype.reverse = function(){ //reverses a string: syntax r=s.reverse();
  splitext = this.split("");
  revertext = splitext.reverse();
  reversed = revertext.join("");
  return reversed;
}


/*msgpanel ------------------------------------------------------------------------------------------------------ */
function confirm_delete(m,l){
  m='Delete [<b>'+m+'</b>] permenantly?';
  update_msgpanel('icon_stop.png',m,'yesno','red',l);
  show_msgpanel();
}

function update_status(m){
  update_msgpanel('icon_mini_info.png',m,'ok','green');
  show_msgpanel();
}

function show_msgpanel(){
  opacity('msgpanel',0,90,200);  
}

function hide_msgpanel(){
  opacity('msgpanel',90,0,1000);
}

function show_msgpanel_btn(b){
  document.getElementById('msgpanel_btn_'+b).style.visibility='visible';
}

function hide_msgpanel_btn(b){
  document.getElementById('msgpanel_btn_'+b).style.visibility='hidden';
}

function update_msgpanel(i,m,b,c,l){
  document.getElementById('msgpanel_icon').src='images/'+i;
  document.getElementById('msgpanel_text').innerHTML='<span class="'+c+'">'+m+'</span>';
  if(b=='yesno'){
    hide_msgpanel_btn('ok');
    show_msgpanel_btn('yes');
    show_msgpanel_btn('no');
  }
  if(b=='ok'){
    hide_msgpanel_btn('yes');
    hide_msgpanel_btn('no');
    show_msgpanel_btn('ok');
  }
  if(l){ document.getElementById('msgpanel_btn_yes').onclick=function(){hide_msgpanel();window.location.href=l}; }
}


//keep msgpanel invisible on page load
opacity('msgpanel',90,0,100);


/*captcha ------------------------------------------------------------------------------------------------------*/
var captcha_src; //this var will be populated with the captcha src

function validate_req_security_code(x){
  if(captcha_src.substr(captcha_src.length-captcha_src.reverse().indexOf('/'))!=document.getElementById(x).value){
    return(captcha_err);
  }else{
    return(false);
  }
}

function get_captcha_src(x){ //called from within iframe: inter frame data transfer
  captcha_src=x;
}

function captcha_unlink(){ //delete stray captcha image
  document.getElementById('captcha_unlink').src='captcha_unlink.php?f='+captcha_src.substr(captcha_src.length-captcha_src.reverse().indexOf('/'));
}

function captcha_refresh(){
  document.getElementById('captcha_iframe').src='captcha.php?refresh='+captcha_src; //force refresh
  setTimeout("captcha_unlink()",2000);
}

setTimeout("captcha_unlink()",2000);
