var refreshList=new Array();
function addRefreshElement(item){
  refreshList[refreshList.length]= item;
}

function refreshElements(){
 for(var i=0; i<refreshList.length; i++){
   hide(refreshList[i]);   show(refreshList[i]);
 }
}


function embed_control(location,file,w,h){
  document.getElementById(location).innerHTML='<EMBED SRC="'+file+'" width='+w+' height='+h+'>';
}

function fillName(fl_name, file_name_field){
  var pos=0;
  var name=fl_name;
  pos=fl_name.lastIndexOf('/');
  if(pos == -1){
    pos=fl_name.lastIndexOf('\\');
  }
  if(pos == -1){
    pos=fl_name.lastIndexOf(':');
  }  
  if((pos != -1)){
    name=fl_name.substring(pos+1, fl_name.length);
  }  
  file_name_field.value=name;
  file_name_field.value=file_name_field.value.replace(/[^a-zA-Z0-9_\.]/g,"_");
}
function auto_fill(file_name_field,file_field){
  if(file_name_field.value.length>3) fillName(file_name_field.value,file_name_field);
  else fillName(file_field.value,file_name_field);
}

function ExpandNC(ID){ hide(ID+"_show"); show(ID+"_hide");show(ID);}
function Expand(ID){
  hide(ID+"_show"); show(ID+"_hide");show(ID);
  SetCookie(ID,'on');
}
function ContractNC(ID){ hide(ID+"_hide"); show(ID+"_show");hide(ID); }
function Contract(ID){
  hide(ID+"_hide"); show(ID+"_show");hide(ID);
  DeleteCookie(ID);
}
function CheckMenu(ID){
  if(GetCookie(ID)=='on')Expand(ID);
}

function hide(ID1){
  obj1=document.getElementById(ID1);
  if(obj1!=null){    obj1.style.visibility='hidden';obj1.style.position='absolute'; }

}
function show(ID1){
  obj2=document.getElementById(ID1);
  if(obj2!=null){ obj2.style.visibility=''; obj2.style.position=''; }
}


var TabObjects = new Array();
function TabObject(group,name){
  this.group=group;
  this.name=name;
  return this;
}

function TabCreate(group,name){
  var found=0;
  for(var i=0; i<TabObjects.length; i++){
    if(TabObjects[i].group==group) found=1;
  }
  TabObjects[TabObjects.length] = new TabObject(group,name);
  if(!found) ExpandNC(name);
  else ContractNC(name);
} 

function TabClick(group,name){
  for(var i=0; i<TabObjects.length; i++){
    if(TabObjects[i].group==group) ContractNC(TabObjects[i].name);
  }
  ExpandNC(name);
}




function scroll_to(id){
  pos=document.getElementById(id);
  var total_x=0;
  var total_y=0;
  while (pos!=null){
   total_x+=pos.offsetLeft;
   total_y+=pos.offsetTop;
   pos=pos.offsetParent;
  }
  window.scroll(0,total_y); 
}

var expire_today = new Date();
var expire_year = new Date(expire_today.getTime()+365*24*60*60*1000);

function getCookieVal(offset){
  var endstr=document.cookie.indexOf(";", offset);
  if(endstr==-1){endstr=document.cookie.length;}
  return unescape(document.cookie.substring(offset,endstr));
}

function GetCookie(name){
  var arg=name+"=";
  var alen=arg.length;
  var clen=document.cookie.length;
  var i=0;
  var j;
  while(i<clen){
    j=i+alen;
    if(document.cookie.substring(i,j)==arg){
      return getCookieVal(j);
    }
    i=document.cookie.indexOf(" ",i)+1;
    if(i==0)break;
  }
  return null;
}

function DeleteCookie(name,path,domain){
  if(GetCookie(name)){
    document.cookie=name+"="+
      ((path)?"; path="+path : "")+
      ((domain)?"; domain="+domain:"")+
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";      
  }
}

function SetCookie(name,value,expires,path,domain,secure){
  document.cookie=name+"="+escape(value)+"; expires="+
    ((expires)?expires.toGMTString():expire_year.toGMTString())+
    ((path)?"; path="+path : "")+
    ((domain)?"; domain="+domain:"")+
    ((secure)?"; secure":"");   
}


var required_fields =new Array();
var file_upload_fields=new Array();
function required_field_object(group,field,message,required_length,string_match,isSelectField){
  this.group=group;
  this.field=field;
  this.alert_message=message;
  this.required_length=required_length;
  this.string_match=string_match;
  this.isSelectField=isSelectField;
  return this;
}
function file_upload_field_object(group,name_field, file_field){
  this.group=group;
  this.file_field=file_field;
  this.name_field=name_field;
  return this;
}
function add_required_field(group,field, message, required_length, string_match){
  required_fields[required_fields.length] = new required_field_object(group,field, message, required_length, string_match,0);
}
function add_required_select_field(group,field, message){
  required_fields[required_fields.length] = new required_field_object(group,field, message,0,'',1);
}
function add_file_upload_field(group,file_name_field,file_field){
  file_upload_fields[file_upload_fields.length]=new file_upload_field_object(group,file_name_field,file_field);
}

function check_required_fields(group){
  first_field=-1;
  for(var i=0; i<required_fields.length; i++){
    if(required_fields[i].group==group){
      required_fields[i].field.className="normal-input";
      if(required_fields[i].isSelectField){
        if(required_fields[i].field.selectedIndex==0){
          required_fields[i].field.className="required-input";
          if(first_field<0)first_field=i;
        }
      }else{
        if( required_fields[i].field.value.length<required_fields[i].required_length
         || (required_fields[i].string_match!=""&&
              required_fields[i].field.value.indexOf(required_fields[i].string_match)<0) ){
           required_fields[i].field.className="required-input";
           if(first_field<0)first_field=i;
        }
      }
    }
  }
  if(first_field>=0){
    alert(required_fields[first_field].alert_message);
    required_fields[first_field].field.focus();
    return false;
  }else   return true;
}

function check_file_upload_fields(group){
  var upload=false;
  for(var i=0; i<file_upload_fields.length; i++){
    if(file_upload_fields[i].group==group&&file_upload_fields[i].file_field.value!=''){       
       auto_fill(file_upload_fields[i].name_field, file_upload_fields[i].file_field);    
       if(file_upload_fields[i].name_field.value.length>3) upload=true;
    }
  }
  return upload;
}

  /*
    returns whether a character is numeric or not
  */
function is_numeric(character){
  var digits="0123456789";
  if (digits.indexOf(character)!=-1) return true;
  else return false;
}
  /*
    remove everything except digits
  */
function strip_letters(mixed_text){
  var char_only="";
  for (var i=0;i<mixed_text.length;i++){
    if (is_numeric(mixed_text.charAt(i)))  char_only+=mixed_text.charAt(i);
  }
  return char_only;
}
  
  /*
    this is necessary because focus() is broken in NS 7.x from any of the event handlers.  
    it simply puts together a line of the focus and select calls for the given form element, 
    and then calls a setTimeout of 1/10th of a second.
  */
function focus_and_select(box){
    var full_field_path="document."+box.form.name+"."+box.name+".";
    setTimeout(full_field_path+"focus();"+full_field_path+"select();",100);
}
  
  /*
    force text value into (555) 555-5555 format
  */
function phone_filter(input_box, first,second, third, ext_char,x1,x2,x3 ){
  if(!x1)x1=3;
  if(!x2)x2=3;
  if(!x3)x3=4;
  var prefix="";   var area_code="";   var first_three="";   var last_four="";  var extension="";    
  var phone_text=input_box.value.toLowerCase();
  var ext_pos=phone_text.indexOf("x");  // grab extension 
  if(phone_text=="") return;
  if(ext_pos>=0){
    extension=strip_letters(phone_text.substr(ext_pos+1,phone_text.length));    
    phone_text=phone_text.substr(0,ext_pos);
  }
  phone_text=strip_letters(phone_text);
  if (phone_text.length==x2+x3||phone_text.length>=x1+x2+x3){
    input_box.value="";
    last_four=phone_text.substr(phone_text.length-x3,x3);  //next, grab the last 4 digits       
    first_three=phone_text.substr(phone_text.length-x3-x2,x2); //then, grab the first 3 digits

    //try for an area code
    if (phone_text.length>=x1+x2+x3){
      area_code=phone_text.substr(phone_text.length-(x1+x2+x3),x1);

      //last, look for anything at the beginning (country code, etc)
      if(phone_text.length>x1+x2+x3){
        prefix=phone_text.substr(0,phone_text.length-(x1+x2+x3));
        input_box.value+=prefix+" ";
      }
      input_box.value+=first+area_code+second;
    }
    input_box.value+=first_three+third+last_four;
    if (extension!="") input_box.value+=ext_char+extension;

  }else{
    alert("Please enter a "+(x2+x3)+" or "+(x1+x2+x3)+" digit phone number.");
    focus_and_select(input_box);
  }
} 
  
  /*
    force text value into 55555 or 55555-5555 format
  */
function zip_filter(input_box){       
  var zip_text=strip_letters(input_box.value);
  if (!(zip_text.length==5||zip_text.length==9)&&zip_text.length>0){      
    alert("Please enter a 5 or 9 digit zip code.");                  
    focus_and_select(input_box);
  }else{  
    if(zip_text.length==9) input_box.value=zip_text.substr(0,5)+"-"+zip_text.substr(5,4)   
    else input_box.value=zip_text
  }
}  

  /*
    Numeric
  */
function numeric_filter(input_box){     
  input_box.value=strip_letters(input_box.value);
}
  
         
  /*  
    make every character upper case
  */
function ucase_filter(input_box){
  input_box.value=input_box.value.toUpperCase();    
}
  
  /*
    make every character lower case
  */
function lcase_filter(input_box){
  input_box.value=input_box.value.toLowerCase();
}
  
  /*
    capitalize the first character of each word
  */
function capitalized_filter(input_box){
  var input_text="";
  var last_char_is_space=1;
  for (var i=0;i<input_box.value.length;i++){
    if(last_char_is_space) input_text+=input_box.value.charAt(i).toUpperCase();
    else input_text+=input_box.value.charAt(i).toLowerCase(); 
    if(input_box.value.charAt(i)==' ')last_char_is_space=1;
    else last_char_is_space=0;
  }
  input_box.value=input_text;
}

function date_conv(st){
  var invalid=false;
  var date_array = st.split("/");
  var month_st=date_array[0];
  var day_st=date_array[1];
  var year_st = date_array[2];
  if(year_st==null||month_st==null||day_st==null){
    alert("Date must be in the form mm/dd/yyyy");	return "";
  }
  if(month_st.length>2){invalid=true;}
  if(day_st.length>2){invalid=true;}
  if(year_st.length>4){
    invalid=true;
  }
  if(year_st.length==2){
    if(year_st<50){	  year_st=year_st*1+2000;	}else{	  year_st=year_st*1+1900;	}
  }
  if((year_st>2200)||(year_st<1900)){
    invalid=true;
  }    
  var month_st=date_array[0];
  if((month_st>12)||(month_st<1)){
    invalid=true;
  }
  var day_st=date_array[1];
  if((day_st>31)||(day_st<1)){
    invalid=true;
  }
  if(invalid){
    alert("Date must be in the form mm/dd/yyyy");	return "";
  }else{
    year_st=month_st+"/"+day_st+"/"+year_st;
    return year_st;
  }
}



function setMouseOver(input,prefix){
    input.className=prefix+"-button";
    input.onmouseover=function(){this.className=prefix+'-button-alt';};
    input.onfocus=function(){this.className=prefix+'-button-alt';}; 
    input.onblur=function(){this.className=prefix+'-button';};
    input.onmouseout=function(){this.className=prefix+'-button';};
}


function getPictureFrame(div,img_dir){
  var table, tbody, tr, td;
  table=document.createElement('table'); table.style["borderCollapse"]="collapse"; table.style["padding"]="0px"; table.style["margin"]="0px";
  tbody=document.createElement('tbody');
    tr=document.createElement('tr');
      td=document.createElement('td'); td.style["fontSize"]="1px"; td.style["lineHeight"]="0px"; td.style["margin"]="0px"; td.style["padding"]="0px";
        td.style["width"]="26px"; td.style["height"]="28px";
        td.style["backgroundImage"]="url("+img_dir+"/FormsFac_03.jpg)";
    tr.appendChild(td);
      td=document.createElement('td'); td.style["fontSize"]="1px"; td.style["lineHeight"]="0px"; td.style["margin"]="0px"; td.style["padding"]="0px";
        td.style["height"]="28px";
        td.style["backgroundImage"]="url("+img_dir+"/FormsFac_04.jpg)";
    tr.appendChild(td);
      td=document.createElement('td'); td.style["fontSize"]="1px"; td.style["lineHeight"]="0px"; td.style["margin"]="0px"; td.style["padding"]="0px";
        td.style["width"]="25px"; td.style["height"]="28px";
        td.style["backgroundImage"]="url("+img_dir+"/FormsFac_05.jpg)";
    tr.appendChild(td);
  tbody.appendChild(tr);
    tr=document.createElement('tr');
      td=document.createElement('td'); td.style["fontSize"]="1px"; td.style["lineHeight"]="0px"; td.style["margin"]="0px"; td.style["padding"]="0px";
        td.style["width"]="26px"; 
        td.style["backgroundImage"]="url("+img_dir+"/FormsFac_08.jpg)";
    tr.appendChild(td);
      td=document.createElement('td'); td.style["backgroundColor"]="white"; td.style["lineHeight"]="0px"; td.style["margin"]="0px";
      td.appendChild(div);
    tr.appendChild(td);
      td=document.createElement('td'); td.style["fontSize"]="1px"; td.style["lineHeight"]="0px"; td.style["margin"]="0px"; td.style["padding"]="0px";
        td.style["width"]="25px";
        td.style["backgroundImage"]="url("+img_dir+"/FormsFac_07.jpg)";
    tr.appendChild(td);
  tbody.appendChild(tr);
    tr=document.createElement('tr'); 
      td=document.createElement('td'); td.style["fontSize"]="1px"; td.style["lineHeight"]="0px"; td.style["margin"]="0px"; td.style["padding"]="0px";
        td.style["width"]="26px"; td.style["height"]="23px";
        td.style["backgroundImage"]="url("+img_dir+"/FormsFac_10.jpg)";
    tr.appendChild(td);
      td=document.createElement('td'); td.style["fontSize"]="1px"; td.style["lineHeight"]="0px"; td.style["margin"]="0px"; td.style["padding"]="0px";
        td.style["height"]="23px";
        td.style["backgroundImage"]="url("+img_dir+"/FormsFac_11.jpg)";
    tr.appendChild(td);
      td=document.createElement('td'); td.style["fontSize"]="1px"; td.style["lineHeight"]="0px"; td.style["margin"]="0px"; td.style["padding"]="0px";
        td.style["width"]="25px"; td.style["height"]="23px";
        td.style["backgroundImage"]="url("+img_dir+"/FormsFac_12.jpg)";
    tr.appendChild(td);
  tbody.appendChild(tr);
  table.appendChild(tbody);
  return table;
}

function createPanel(content,title,helpContext){
  var table, tbody,tr,td, input;
  table=ce('table'); table.className="tan-table"; table.style["width"]="100%"; table.style["borderCollapse"]="collapse";  table.style["marginBottom"]="15px";
   tbody=ce('tbody');
    tr=ce('tr');
     td=ce('td'); td.className="tan-header"; td.innerHTML=title; tr.appendChild(td);     
     td=ce('td'); td.className="tan-header"; td.style["textAlign"]="right";
       if(helpContext&&helpContext!=""){
         input=ce('input'); input.type="button"; input.value="Help"; input.style["fontSize"]="11px"; 
         setMouseOver(input,'black'); input.onclick=function(){help_popup(helpContext);};
         td.appendChild(input);
       } 
    tr.appendChild(td); tbody.appendChild(tr); tr=ce('tr');
     td=ce('td'); td.colSpan=2; td.className="tan-cell";  td.appendChild(content);  tr.appendChild(td);     
   tbody.appendChild(tr);
  table.appendChild(tbody);
  return table;
}       

function help_popup(context){
  url = "popup.iml?mdl=knowledgebase/opc_help.mdl&show_instructions=1&Popup_Title=Online%20Help:%20"+escape(context)+"&CONTEXT="+escape(context);
  helpWindow=window.open(url,"Email","height=450,width=450,resizable=1,scrollbars=1,status=no,toolbar=no,location=no,menubar=no");
    helpWindow.focus(); 
}
