check = []; //this is an array that stores all the true/false values for each checkbox
/* MAKES THE REQUEST */
var http_request = false;
   function makePOSTRequest(url, parameters, target, saving) {

      http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/xml');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }

      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      http_request.onreadystatechange = function(){ alertContents(target, saving) };
      http_request.open('POST', url, true);
      http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      http_request.setRequestHeader("Content-length", parameters.length);
      http_request.setRequestHeader("Connection", "close");
      http_request.send(parameters); 
   }
   /* SHOW THE CONTENTS AFTER PROCESSING */
function alertContents(target, saving) {
    if (http_request.readyState == 1){
                document.getElementById(target).innerHTML = "Retrieving Information Please Wait"; 
    }
    if (http_request.readyState == 2){
            document.getElementById(target).innerHTML = "Information Retrieved"; 
    }
    if (http_request.readyState == 3){
            document.getElementById(target).innerHTML = "Processing Information Please Wait"; 
    }
    if (http_request.readyState == 4) {
         if (http_request.status == 200) {
            //alert(http_request.responseText);
            var result = new Array;
             result =  http_request.responseText;
             var cont = document.getElementById(target);
              cont.innerHTML = result;
              if (document.height) {
              var h = ((document.height - cont.offsetHeight) / 2);
              cont.style.top = h;
              }  else {
              var h = ((document.body.offsetHeight - cont.offsetHeight) / 2) + document.body.scrollTop ;
              cont.style.top = h;
              }
              
            //document.getElementById(target).innerHTML = result;    
            if (document.getElementById('tts') && saving == 'saving_stuff')  {
              var save = document.getElementById('tts');
              save.style.top = '400px';
              if (document.width) {
                  save.style.left = (document.width - 50) /2;
                  save.style.width = 50;
              } else {
                  save.style.left = (document.body.offsetWidth -50) /2;
                  save.style.width = 50;
              }
              save.style.display = 'block';
              save.innerHTML = 'Saving...';
              setTimeout("hidesave();",500);
            }      
                
         } else {
            alert('There was a problem with the request.');
         }
         tinyMCE.execCommand("mceAddControl", true, "description");
    }
   }
   hex = -9;
    
  
   function hidesave () {
   if (document.getElementById('tts')) {
   var grey = document.getElementById('grey');
   var save = document.getElementById('tts');
   grey.style.display = 'none';
   save.style.display = 'none';
   document.body.style.overflow = 'auto';  
   window.location.reload(true);
   }
  } 
function pedit(id, type, url, page, stat,poststradd){
var container = document.getElementById('tt1');
var grey = document.getElementById('grey');
  container.style.top = '25';
if (stat) {
  if (document.width) {
   if (document.width > 767 )  {
      container.style.left = (document.width - 767) /2 +10;
      container.style.width = 767 -6 ;
    } else {
      container.style.width = document.width -10;
      container.style.left = '5';
    }
  } else {
    if (document.body.offsetWidth >767) {
      container.style.left = (document.body.offsetWidth -767) /2;
      container.style.width = 767 -2;
    } else {
      container.style.width = document.body.offsetWidth -10;
      container.style.left = '5';
    } 
   }
} else {
  if (document.width) {
   if (document.width > 767 )  {
      container.style.left = (document.width - 767) /2 +10;
      container.style.width = 767 -6 ;
    } else {
      container.style.width = document.width -10;
      container.style.left = '5';
    }
  } else {
    if (document.body.offsetWidth >767) {
      container.style.left = (document.body.offsetWidth -767) /2;
      container.style.width = 767 -2;
    } else {
      container.style.width = document.body.offsetWidth -10;
      container.style.left = '5';
    }
   }
}
            if (document.height) {
              var h = ((document.height - container.offsetHeight) / 2);
              container.style.top = h;
            }  else {
              var h = ((document.body.offsetHeight - container.offsetHeight) / 2) + document.body.scrollTop ;
              container.style.top = h;
              grey.style.top = document.body.scrollTop;
            }
if (container.style.height > document.body.offsetHeight)  {
container.style.height = document.body.offsetHeight- 20;
}

  container.style.display = 'block';
  grey.style.display = 'block';

  var poststr = "ID=" + encodeURI(id) +  "&TYPE=" + encodeURI(type)+poststradd;
  document.body.style.overflow = 'hidden'; 


makePOSTRequest(url, poststr, 'tt1');

}


function padd(path, url){

var container = document.getElementById('tt1');
var grey = document.getElementById('grey');
  container.style.top = '200';
  if (document.width) {
    if (document.width > 767 )  {
      container.style.left = (document.width - 767) /2 +10;
      container.style.width = 767 -6 ;
    } else {
      container.style.width = document.width -10;
      container.style.left = '5';
    }
  } else {
    if (document.body.offsetWidth >767) {
      container.style.left = (document.body.offsetWidth -767) /2 +10;
      container.style.width = 767 -6 ;
    } else {
      container.style.width = document.body.offsetwidth -10;
      container.style.left = '5';
    } 
  }
  container.style.display = 'block';
  grey.style.display = 'block';
  if (document.width)
    grey.style.height = document.height;
  else 
    grey.style.Height=document.offsetHeight;
  document.body.style.overflow = 'hidden';  
  var poststr = "PATH=" + encodeURI(path);
makePOSTRequest('add.php', poststr, 'tt1');
}

function pclose() {
var container = document.getElementById('tt1');
var grey = document.getElementById('grey');
grey.style.display = 'none';
container.style.display = 'none';
container.innerHTML = "Error! This function has been improperly accessed.";
document.body.style.overflow = 'auto';  
}

function pclose2() {
var container = document.getElementById('image_div');
var grey = document.getElementById('grey2');
grey.style.display = 'none';
container.style.display = 'none';
container.innerHTML = "Error! This function has been improperly accessed.";

}

function psave(id, type)  {

document.getElementById("description").value = tinyMCE.activeEditor.getContent();
var container = document.getElementById('tt1');
var form = document.getElementById('form');
var title = document.getElementById('title');
var location = document.getElementById('location');
var description = document.getElementById('description');
var item = document.getElementById(id);
var alttext = document.getElementById('alttext');
var companymra = document.getElementById('mra');
var companygta = document.getElementById('gta');
var companygtaes = document.getElementById('gtaes');

var company = "";
if (companymra.checked == true)  {
  company = company + 'mra';
  if (companygta.checked == true)  {
    company = company + ',gta';
  } else if (companygtaes.checked == true) {
    company= company + ',gtaes';
  }
} else {
   if (companygta.checked == true)  {
    company = company + 'gta';
  } else if (companygtaes.checked == true) {
    company= company + 'gtaes';
  }
}

//var img = item.getElementsByTagName('img')[0];
//var link = item.getElementsByTagName('a')[2];
var saving = 'saving_stuff';
var poststr = "ID=" + encodeURI(id) + "&TITLE=" + encodeURIComponent(title.value) + "&ALTTEXT=" + encodeURIComponent(alttext.value) + "&LOCATION=" +  encodeURIComponent(location.value) + "&DESCRIPTION=" + encodeURIComponent(description.innerHTML) + "&COMPANY=" + company;
//link.innerHTML = title.value;
container.style.display = 'none';
container.innerHTML = "Error! This function has been improperly accessed.";

  
makePOSTRequest('save.php', poststr, 'tt1', saving);
}
function pdelete(id)  {
if (confirm("Are you sure you want to delete")) {
  makePOSTRequest('delete.php', poststr, 'tt1', saving);
  pclose();
  }
  
  

}

function idelete(path, image) {
  if (confirm("Are you sure you want to delete image "+ image)) {
    var poststr = "PATH=" + encodeURI(path) + "&IMAGE=" + encodeURI(image);
    makePOSTRequest('delimg.php', poststr);
  
   var image = document.getElementById(image);
   image.style.display = 'none';
  
  }

}
function unique(id, path){
var sect = document.getElementById(id)
var poststr = "PARENT=" + encodeURI(path) + "&SECT=" + encodeURI(sect.value);
makePOSTRequest('check.php', poststr, 'check');
}


function paddsave(path) {
var saving = 'saving_stuff';
var container = document.getElementById('tt1');
var title = document.getElementById('title');
var location = document.getElementById('location');
var description = document.getElementById('description');
var alttext = document.getElementById('alttext');
var type = document.getElementById('type');
var sect = document.getElementById('section')
var menu = document.getElementById('menu');
var poststr = "TITLE=" + encodeURIComponent(title.value) + "&ALTTEXT=" + encodeURIComponent(alttext.value) + "&LOCATION=" +  encodeURIComponent(location.value) + "&DESCRIPTION=" + encodeURIComponent(description.innerHTML) + "&TYPE=" + encodeURIComponent(type.value) + "&SECT=" + encodeURIComponent(sect.value) + "&PARENT=" + encodeURIComponent(path) + "&MENU=" + encodeURIComponent(menu.checked);
container.style.display = 'none';
container.innerHTML = "Error! This function has been improperly accessed.";
makePOSTRequest('save.php', poststr, 'tt1', saving);
}
function simages(sect, parent) {
var imgcont = document.getElementById('image_div');
imgcont.style.width = '100px';
imgcont.style.height = '200px';
imgcont.style.top = '250';
if (document.width) {
 if (document.width > 500 )  {
    imgcont.style.left = (document.width - 767) /2 -8;
    imgcont.style.width = 500;
  } else {
    imgcont.style.width = document.width -10;
    imgcont.style.left = '5';
  }
} else {
  if (document.body.offsetWidth >500) {
    imgcont.style.left = (document.body.offsetWidth -767) /2;
    imgcont.style.width = 500;
  } else {
    imgcont.style.width = document.body.offsetwidth -10;
    imgcont.style.left = '5';
  } 


}
  var grey = document.getElementById('grey2');
  var poststr = "SECT=" + encodeURIComponent(sect) + "&PARENT=" + encodeURIComponent(parent);
  makePOSTRequest('images.php', poststr, 'image_div');
  grey.style.display = 'block'
  imgcont.style.display = 'block';
  
}
function apply(id, career)  {
var imgcont = document.getElementById('image_div');
if (document.width) {
 if (document.width > 767 )  {
    imgcont.style.left = (document.width - 767) /2 -8;
    imgcont.style.width = 767;
  } else {
    imgcont.style.width = document.width -10;
    imgcont.style.left = '5';
  }
} else {
  if (document.body.offsetWidth >767) {
    imgcont.style.left = (document.body.offsetWidth -767) /2;
    imgcont.style.width = 767;
  } else {
    imgcont.style.width = document.body.offsetwidth -10;
    imgcont.style.left = '5';
  } 


}
  var grey = document.getElementById('grey2');
  var poststr = "ID=" + encodeURI(id) +  "&CAREER=" + encodeURI(career);
  makePOSTRequest('careerApply.php', poststr, 'image_div');
  grey.style.display = 'block'
  imgcont.style.display = 'block';
  
}
function checks (field) {
  if (req = field.parentNode.parentNode.getElementsByTagName('span')[0]) {
    if (field.value !== '' && req!='undefined') {
      req.innerHTML = 'valid';
      req.style.color = 'green';   
      return true;
    } else {
      req.innerHTML = '*';
      req.style.color = 'red';
      return false;
    }
  } else {
  return true;
  }
}
function checkall(field)  {
  var forms = document.getElementsByTagName('form')[0];
  var error = '';
  for (var i=0; i<forms.length -3; i++) { 

    if (checks(forms[i])){
      
    } else {
        error = error + "Your selection for '" + forms[i].parentNode.parentNode.getElementsByTagName('td')[0].innerHTML.split(':')[0] +"' is invalid\n";
    }
  }
  if (checks(forms.cresume) || checks(forms.uresume))  {
  } else {  error = error + "Resume is invalid\n";   }
  if (error == '') {
    return true
  } else {
    alert(error);
    return false;
  }
}
