var editmode=0;
var activeF='';

// *********  UI FUNCTIONS  *********

function enableAll(){
var el = document.product.elements;
for(var i=0;i<el.length;i++){
 el[i].disabled=false;
}
}

function showTab(t){
 // first close the opened tag, if there is one
 if(tabstatus=='opened'){
  document.getElementById(tab).style.display='none';
  document.getElementById('nav-'+tab).innerHTML='+';
  tabstatus='closed';
 }else{
   document.getElementById(t).style.display='block';
  document.product.place.value=t;
  document.getElementById('nav-'+t).innerHTML='-';
  tab=t;
  tabstatus='opened';
 }
 if(tab!=t){
  document.getElementById(t).style.display='block';
  document.product.place.value=t;
  document.getElementById('nav-'+t).innerHTML='-';
  tab=t;
  tabstatus='opened';
 }
}

function showElement(el) {
	$(el).setStyle({
		visibility: 'visible'
	});
	
}

function hideElement(el) {
	$(el).setStyle({
		visibility:'hidden'
	})
}

function editlink(tgt){
 h='';
 if(document.product.place){
  h=document.product.place.value;
  tgt=tgt+'&h='+h;
 }
 location=tgt;
}

function delItem(tid){
 if(confirm('Are you sure you want to delete this item?')){
  enableAll();
  document.product.action.value='del';
  document.product.delID.value=tid;
  document.product.submit();
 }
}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  if(document.product.scrolly){ document.product.scrolly.value=scrOfY; }
}


// *********  GENERIC FILE HANDLER  *********

function fileControl(source,type){
 div='file-'+source;
 // hide existing control if there is one
 if(activeF != '' && activeF != div){ document.getElementById(activeF).style.display='none'; }
 // show this control
 rn=Math.floor(Math.random()*1001);
 fForm='Description: <input type="text" name="fDescription_'+rn+'" style="padding:3px; width:250px"><br/>File: <input name="document_file_upload_'+rn+'" id="document_file_upload_'+rn+'" type="file"> <input type="button" value="Upload File" style="font-size:11px" onclick="document.product.other.value=\''+rn+'\'; document.product.action.value=\'genFile\'; getScrollXY(); enableAll(); document.product.save.click()"><input type="hidden" name="fSource_'+rn+'" value="'+source+'"><input type="hidden" name="fType_'+rn+'" value="'+type+'">';
 document.getElementById(div).innerHTML=fForm;
 activeF=div;
}


// ********** FOR ADDING A USER (author, etc.) TO AN OBJECT ***************

function rsearch(qry,type,tdiv){
 if(type=='ns'){
  alert('please choose a user type')
 }else{
  rrc=qry;
  if(rrc.length>2){
   rtv='tmp val';
   rt=rtv+'||'+rrc;
   randomnumber=Math.floor(Math.random()*1001);
   url = '/index.php?id=34&a=searchRel&p='+rt+'&t='+type+'&d=rr&rn='+randomnumber;
   new Ajax.Request(url,
   {
   method: "get",
   onSuccess: function(response){
    document.getElementById(tdiv).innerHTML=response.responseText+'<a href="#" onclick="document.getElementById(\''+tdiv+'\').innerHTML=\'\'">X close</a>';
   }
   }
   );
  }
 }
}

function ajaxUser(uid,type){
 document.product.action.value='addUser';
 document.product.delID.value=uid;
 document.product.other.value=type;
 document.product.save.click();
}

function delUser(rid){
 if(confirm('Are you sure you want to delete this user?')){
  enableAll();
  document.product.action.value='delUser';
  document.product.delID.value=rid;
  document.product.save.click();
 }
}


//************ FOR BOOKS *************
function bookDel(bid){
 if(confirm('***WARNING***\nThis will delete the ENTIRE book from the system!\nAre you sure you want to delete this book?')){
  if(confirm('***THIS BOOK WILL BE COMPLETELY REMOVED FROM THE SYSTEM***\nAre you sure you want to do this?')){
   enableAll();
   document.product.action.value='delBook';
   document.product.delID.value=bid;
   document.product.save.click();
  }
 }
}



// ************ FOR CONTACTS *************
// **** Misleading - these are used to associate users, not contacts ***** //
// **** But I am too lazy to recode all the function names - Justin ****** //

function contactBrowse(sourceid,source){
 randomnumber=Math.floor(Math.random()*1001);
 url = '/index.php?id=34&a=contactbrowse&s='+source+'&i='+sourceid+'&rn='+randomnumber;
 new Ajax.Request(url,
 {
 method: "get",
 onSuccess: function(response){
  document.getElementById('contact-add').innerHTML=response.responseText;
 }
 }
 );
}

function contactAdd(){
 enableAll();
 document.product.action.value='addContact';
 document.product.submit();
}

function contactDel(rid){
 if(confirm('Are you sure you want to delete this contact?')){
  enableAll();
  document.product.action.value='delContact';
  document.product.delID.value=rid;
  document.product.submit();
 }
}

function clk(dv) { 
 if(c==dv){
  //Effect.BlindUp(dv);
  document.getElementById(dv).style.display='none';
  document.product.subplace.value='';
  c='';
 }else{
  if(!c==''){
   //Effect.BlindUp(c);
   document.getElementById(c).style.display='none';
   document.product.subplace.value='';
  }
  //Effect.BlindDown(dv);
  document.getElementById(dv).style.display='block';
  c=dv;
  document.product.subplace.value=dv;
 }
 return false;
}

function delFile(did){
 if(confirm('Are you sure you want to delete this file?')){
  getScrollXY();
  enableAll();
  document.product.action.value='delFile';
  document.product.delID.value=did;
  document.product.submit();
 }
}
