function newWin(mypage,myname,w,h,scroll,pos){
  if(pos=="random"){
    LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;
    TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;
  }
  if(pos=="center"){
    LeftPosition=(screen.width)?(screen.width-w)/2:100;
    TopPosition=(screen.height)?(screen.height-h)/2:100;
  } else if((pos!="center" && pos!="random") || pos==null){
    LeftPosition=0;
    TopPosition=20;
  }
  if(scroll=='yes') scroll=',scrollbars=yes';
  else scroll=',scrollbars=no';
  settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';
  win=window.open(mypage,myname,settings);
}
var charset_digits="0123456789";
function inArray(x,a){
  for(j=0;j<a.length;j++){
    if(a[j]==x){
      return true;
    }
  }
  return false;
}
function inArrayPos(x,a){
  for(j=0;j<a.length;j++){
    if(a[j]==x){
      return j;
    }
  }
  return -1;
}
function inValidCharSet(str,charset){
	var result = true;
	// Note: doesn't use regular expressions to avoid early Mac browser bugs	
	for (var i=0;i<str.length;i++)
		if (charset.indexOf(str.substr(i,1))<0){
			result = false;
			break;
		}
	return result;
}
function allDigits(str){
	return inValidCharSet(str,charset_digits);
}
function SetCookie(cookieName,cookieValue,nMin) {
 var today = new Date();
 var expire = new Date();
 if (nMin==null || nMin==0) nDays=1;
 expire.setTime(today.getTime() + 60000*nMin);
 document.cookie = cookieName+"="+escape(cookieValue)
                 + ";expires="+expire.toGMTString();
}

function toggle_vo(prefix) {
  v=document.getElementById('vo');
  if(v.style.display=='')v.style.display='none';
    else v.style.display='';
//  view_options_form.view_options_opened.value=vo.style.display=='none'?0:1;
  SetCookie(prefix+'view_options_opened',v.style.display=='none'?0:1,60);
}
function highlight(w) {
  w.style.background='#E4EDE9';
}
function un_highlight(w) {
  w.style.background='white';
}

function enable_custom_range(f,prefix,v){
  eval('f.'+prefix+'fromday').disabled=v;
  eval('f.'+prefix+'frommonth').disabled=v;
  eval('f.'+prefix+'fromyear').disabled=v;
  eval('f.'+prefix+'today').disabled=v;
  eval('f.'+prefix+'tomonth').disabled=v;
  eval('f.'+prefix+'toyear').disabled=v;
}
function range_change(f,prefix){
  rs=eval('f.'+prefix+'range_select');
  if(rs.value==0)
    enable_custom_range(f,prefix,false);
  else enable_custom_range(f,prefix,true);
}

function cc(){
  alert('r');
}

function InitDataView(table,colorOver, colorOut, colorCl,colorOutCl){
	var tbl  = document.getElementById(table);
  var ccc=new Array();
	for(var i=0; i<tbl.rows.length; i++){
    row = tbl.rows[i];
    if(row == null || row == 'undefined'){
			continue;
		}
		
    row.onmouseover = function(){
      color = this.dpcolor ?  colorCl : colorOver;
//      this.style.backgroundColor = colorOver;
      this.bgColor = color;
      this.mo=true;
    }

    row.onmouseout = function(){
      color = this.dpcolor ?  colorOutCl : colorOut;
//      this.style.backgroundColor = color;
      this.bgColor = color;
      this.mo=false;
    }
    
    row.cb=document.getElementById('cb'+(i-1));
    if(row.cb==null || row.cb=='undefined'){
      row.cb='undefined';
    }else{
      if(row.cb.checked){
        row.dpcolor= true;
        row.bgColor=colorOutCl;
      }
      row.style.cursor=cursorhand;
      row.onclick = function(){ 
        if(this.cb!=null && this.cb!='undefined'){
          this.dpcolor= !this.dpcolor; 
          this.cb.checked = !this.cb.checked;
          this.dpcolor=this.cb.checked;
          if(row.mo){
            color = this.dpcolor ?  colorOutCl : colorOut;
          }else{
            color = this.dpcolor ?  colorCl : colorOver;
          }
          this.bgColor = color;
        }
      }
    }
  }
}

function SelectAll(f,table){
  var tbl= document.getElementById(table);
  for(i=0;i<f.elements.length;i++){
    cb=document.getElementById('cb'+i);
    rid=document.getElementById('rid'+i);
    if(cb == null || cb == 'undefined'){
			break;
		}
    if(!cb.checked){
      cb.checked = !cb.checked;
      rid.dpcolor=cb.checked;
      rid.bgColor = '#A5AFC7';
    }
  }
}
function DeselectAll(f,table){
  var tbl= document.getElementById(table);
  for(i=0;i<f.elements.length;i++){
    cb=document.getElementById('cb'+i);
    rid=document.getElementById('rid'+i);
    if(cb == null || cb == 'undefined'){
			break;
		}
    if(cb.checked){
      cb.checked = !cb.checked;
      rid.dpcolor=cb.checked;
      rid.bgColor = 'white';
    }
  }
}
function InvertSelection(f,table){
  var tbl= document.getElementById(table);
  for(i=0;i<f.elements.length;i++){
    cb=document.getElementById('cb'+i);
    rid=document.getElementById('rid'+i);
    if(cb == null || cb == 'undefined'){
			break;
		}
    if(!cb.checked){
      cb.checked = !cb.checked;
      rid.dpcolor=cb.checked;
      rid.bgColor = '#A5AFC7';
    }else{
      cb.checked = !cb.checked;
      rid.dpcolor=cb.checked;
      rid.bgColor = 'white';
    }
  }
}

function selCount(f){
  count=0;
  for(i=0;i<f.elements.length;i++){
    cb=eval('f.cb'+i);
    if(cb == null || cb == 'undefined'){
			break;
		}
    if(cb.checked)count++;
  }
  return count;
}

// ------------------------------------- users -------------------------------------

function editUserClick(f){
  if(selCount(f)<1){
    alert('Please, select at least one user');
    return false;
  }
  f.action.value='editusers';
  f.submit();
  return true;
}

function deleteUsersClick(f){
  sc=selCount(f);
  if(sc<1){
    alert('Please, select at least one user');
    return false;
  }
  if(!confirm('Are you sure you want to delete '+sc+' selected users?')){
    return false
  }
  f.action.value='deleteusers';
  f.submit();
  return true;
}

function activateUsersClick(f,a){
  if(selCount(f)<1){
    alert('Please, select at least one user');
    return false;
  }
  f.action.value=a?'activateusers':'deactivateusers';
  f.submit();
  return true;
}

function blurAll(where){
  var tags = document.getElementsByTagName('A');
  for (i=0;i<tags.length;i++)
  {
    tags[i].onfocus=
      function(){
      this.blur();
    }
  }
//  alert(where.getElement);
  

/*  if(document.all){
    for(i=0;i<document.all.length;i++){
      tag=document.all[i];
      if(tag.tagName=='A'){
        tag.onfocus=function(){
          this.blur();
        }
      }
    }//for
  }//if document.all*/
}

function saveUsersClick(f){
  var user=null;
  var name=null;
  for(i=0;i<f.userscount.value;i++){
    name=document.getElementById('names'+i);
    if(name.value==''){
      alert('Realname is empty for user No '+(i+1)+'.');
      name.focus();
      return false;
    }
    user=document.getElementById('usernames'+i);
    if(user.value==''){
      alert('Username is empty for user No '+(i+1)+'.');
      user.focus();
      return false;
    }
    level=document.getElementById('levels'+i);
    if(level.value==''){
      alert('Level field is empty for user No '+(i+1)+'.');
      level.focus();
      return false;
    }
    if(!allDigits(level.value)){
      alert('Level field must be number for user No '+(i+1)+'.');
      level.focus();
      level.select();
      return false;
    }
    company=document.getElementById('companies'+i);
    if(company.value==''){
      alert('Company is empty for user No '+(i+1)+'.');
      company.focus();
      return false;
    }
    email=document.getElementById('emails'+i);
    if(email.value==''){
      alert('e-mail is empty for user No '+(i+1)+'.');
      email.focus();
      return false;
    }
    phone=document.getElementById('phones'+i);
    if(phone.value==''){
      alert('Phone is empty for user No '+(i+1)+'.');
      phone.focus();
      return false;
    }
  }
  return true;
}

function usActiveClick(w){
  a=document.getElementById('active');
  u=document.getElementById('unactive');
  if(!a.checked && !u.checked){
    if(w==1) u.checked=true;
      else a.checked=true;
  }
}
function passChangeClick(n){
  p1=document.getElementById('passwords1'+n);
  p2=document.getElementById('passwords2'+n);
  pc=document.getElementById('passchange'+n);
  p1.disabled=!pc.checked;
  p2.disabled=!pc.checked;
  if(pc.checked){
    p1.focus();
    p1.select();
  }
}

// ------------------------------------- Products -------------------------------------

function editProductsClick(f){
  if(selCount(f)<1){
    alert('Please, select at least one product');
    return false;
  }
  f.action.value='editproducts';
  f.submit();
  return true;
}
function editProductFinishesClick(f){
  if(selCount(f)<1){
    alert('Please, select at least one product');
    return false;
  }
  f.action.value='editproductfinishes';
  f.submit();
  return true;
}
function editProductPricesClick(f){
  if(selCount(f)<1){
    alert('Please, select at least one product');
    return false;
  }
  f.action.value='editproductprices';
  f.submit();
  return true;
}

function countTags(n) { // n is a Node
 var numtags = 0; // Initialize the tag counter
 if (n.nodeType == 1 /*Node.ELEMENT_NODE*/) // Check if n is an Element 
numtags++; // Increment the counter if so 
var children = n.childNodes; // Now get all children of n 
for(var i=0; i < children.length; i++) { // Loop through the children
 numtags += countTags(children[i]); // Recurse on each one 
} 
return numtags; // Return the total number of tags 
} 

function hideIds(n,hide,show){
  if(n.nodeType != 1) return true;
 var children = n.childNodes; // Now get all children of n 
  if(n.className==hide) n.style.display='none';
  if(n.className==show) {
    n.style.display='';
//    if(!confirm(n.tagName+' - '+n.className+' | show '+show+' | hide '+hide))return false;
  }
for(var i=0; i < children.length; i++) { // Loop through the children
  curtag=children[i];
  if(curtag.nodeType != 1) continue;
  if(!hideIds(curtag,hide,show))return false;
} 
/*  for(i=0;i<num_el;i++){
    alert(t.tagName);
    break;
    curel=t[i];
    if(curel.className==x){
      curel.style.display='none';
    }
    alert(t.tagName);
    if(i>10)break;
  }
*/
  return true;
}
function tabTableClick(clickCell){
  tbl=document.getElementById('tabsTable');
  num_cells=tbl.rows[0].cells.length;
  for(i=0;i<num_cells;i++){
    cell=tbl.rows[0].cells[i];
    if(cell.className=='tab_selected'){
      prevSel=i;
      break;
    }
  }
  for(i=0;i<num_cells;i++){
    cell=tbl.rows[0].cells[i];
    if(cell==clickCell){
      cell.className='tab_selected';
      hideIds(document.getElementById('dataView'),tab_ids[prevSel],tab_ids[i]);
    }else{
      cell.className='tab_normal';
    }
  }
}


function saveRepsClick(f){
/*  if(f.isusa.options[f.isusa.selectedIndex].value==1 && f.state.options[f.state.selectedIndex].value=='NA'){
    alert('Select state.');
    f.state.focus();
    return false;
  }*/

  if(f.name.value==''){
    alert('Name is empty.');
    f.name.focus();
    return false;
  }
  if(f.location.value==''){
    alert('Location is empty.');
    f.location.focus();
    return false;
  }
  if(f.phone.value==''){
    alert('Phone field is empty.');
    f.phone.focus();
    return false;
  }
  if(f.email.value==''){
    alert('email field is empty.');
    f.email.focus();
    return false;
  }
  return true;
}
function saveBindersClick(f){
/*  if(f.isusa.options[f.isusa.selectedIndex].value==1 && f.state.options[f.state.selectedIndex].value=='NA'){
    alert('Select state.');
    f.state.focus();
    return false;
  }

  if(f.name.value==''){
    alert('Name is empty.');
    f.name.focus();
    return false;
  }
  if(f.location.value==''){
    alert('Location is empty.');
    f.location.focus();
    return false;
  }
  if(f.phone.value==''){
    alert('Phone field is empty.');
    f.phone.focus();
    return false;
  }
  if(f.email.value==''){
    alert('email field is empty.');
    f.email.focus();
    return false;
  }*/
  return true;
}
function editRepsClick(f){
  if(selCount(f)<1){
    alert('Please, select at least one row');
    return false;
  }
  f.action.value='editreps';
  f.submit();
  return true;
}
function deleteRepsClick(f){
  sc=selCount(f);
  if(sc<1){
    alert('Please, select at least one row');
    return false;
  }
  if(!confirm('Are you sure you want to delete '+sc+' selected rows?')){
    return false
  }
  f.action.value='deletereps';
  f.submit();
  return true;
}
function deleteBindersClick(f){
  sc=selCount(f);
  if(sc<1){
    alert('Please, select at least one row');
    return false;
  }
  if(!confirm('Are you sure you want to delete '+sc+' selected rows?')){
    return false;
  }
  f.action.value='deletebinders';
  f.submit();
  return true;
}
function editBindersClick(f){
  if(selCount(f)<1){
    alert('Please, select at least one row');
    return false;
  }
  f.action.value='editbinders';
  f.submit();
  return true;
}

function deleteRepsClick_tab(f){
  sc=selCount(f);
  if(sc<1){
    alert('Please, select at least one row');
    return false;
  }
  if(!confirm('Are you sure you want to delete '+sc+' selected rows?')){
    return false;
  }
  f.action.value='deletereps_tab';
  f.submit();
  return true;
}
function editRepsClick_tab(f){
  if(selCount(f)<1){
    alert('Please, select at least one row');
    return false;
  }
  f.action.value='editreps_tab';
  f.submit();
  return true;
}

function saveRepsAddClick(f){
/*  if(f.isusa.options[f.isusa.selectedIndex].value==1 && f.state.options[f.state.selectedIndex].value=='NA'){
    alert('Select state.');
    f.state.focus();
    return false;
  }*/

  if(f.rep.value==''){
    alert('Rep Name is empty.');
    f.rep.focus();
    return false;
  }
  if(f.comp.value==''){
    alert('Company field is empty.');
    f.comp.focus();
    return false;
  }
  
  if(f.email.value==''){
    alert('Email field is empty.');
    f.email.focus();
    return false;
  }
  if(f.user.value==''){
    alert('User Name is empty.');
    f.user.focus();
    return false;
  }
  if(f.pass.value==''){
    alert('Password field is empty.');
    f.pass.focus();
    return false;
  }
  //f.action.value='addreps_tab';
  //f.submit();
  return true;
}
