var speed = 8;

function toggle_reveal(id, elem_id, final_height, message_a, message_b, closedinitially){

   // Get the element
   var elem = document.getElementById(elem_id);
   var anc = document.getElementById(id + "anchor");

   // Setup heights
   var status;
   if(closedinitially){
      if(elem.status != 0 && elem.status != 1 && elem.status != -1){ 
         elem.style.height = 0;
      }
   }
   else{

      if(elem.status != 0 && elem.status != 1 && elem.status != -1){ 
         elem.style.height = final_height + 'px';
      }
   }

   // Clear timeout if exists
   if(elem.movement){ clearTimeout(elem.movement); }

   // Get current height
   var height = parseInt(elem.style.height);

   // Get status
   if(elem.status == 0 || elem.status == 1){ status = elem.status; }

   else{

      if(height == 0){
         status = 1;
         if(anc != undefined){ anc.innerHTML = message_b; }
         elem.status = status;
      }

      else{
         status = 0;
         if(anc != undefined){ anc.innerHTML = message_a; }
         elem.status = status;
      }

   }
   // Opening

   if(status == 1){
      // If the final height has been reached and we are not just starting
      if(height == final_height) {
      	//if(anc != undefined){ anc.innerHTML = message_b; }
         elem.status = -1;
         return;
      }
      var dist = Math.ceil( (final_height - height) / speed);
      height = height + dist;
      elem.style.height = height + "px";
   }

   // Closing

   else{
      // If the final height has been reached and we are not just starting
      if (height == 0){
         //if(anc != undefined){ anc.innerHTML = message_a; }
         elem.status = -1;
         return;
      }

      var dist = Math.ceil(height / speed);
      height = height - dist;
      elem.style.height = height + "px";
   }

   // Set timeout
   repeat = "toggle_reveal('" + id + "', '" + elem_id + "', '" + final_height + "', '" + message_a + "', '" + message_b + "', '" + closedinitially + "')";
   elem.movement = setTimeout(repeat, 1);

}


function create_darken(){

  var bodyWidth, bodyHeight, frameWidth, frameHeight;

	// Set up darken
	darken = document.createElement('div');
	darken.style.backgroundColor = "rgb(43,45,46)";
	
	if (document.body.clientWidth){
		bodyWidth = document.body.clientWidth;
		bodyHeight = document.body.clientHeight;
	} else {
		bodyWidth = document.body.scrollHeight;
		bodyHeight = document.body.scrollWidth
	}
	
	if (self.innerWidth){
		frameWidth = self.innerWidth;
		frameHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientWidth){
		frameWidth = document.documentElement.clientWidth;
		frameHeight = document.documentElement.clientHeight;
	}	else if (document.body){
		frameWidth = document.body.clientWidth;
		frameHeight = document.body.clientHeight;
	}
	
	if(bodyWidth < frameWidth){ bodyWidth = frameWidth; }
	if(bodyHeight < frameHeight){ bodyHeight = frameHeight; }
	
	darken.style.width = bodyWidth + "px";
	darken.style.height = bodyHeight + "px";
	darken.style.width = '100%';
	darken.style.position = "absolute";
	darken.style.top = "0";
	darken.style.left = "0";
	limit = 85;
	set_darken_opacity();
	//getDoc().childNodes[1].appendChild(darken);
	document.body.appendChild(darken);
}

function getDoc(){
	if(document.childNodes[1]) return document.childNodes[1];
	else return document.childNodes[0];
}

var opacity = 0;
var darken;
var limit;

function set_darken_opacity(){
	change_opac(opacity, darken);
	if (opacity > limit) opacity -= Math.ceil(opacity / 2.5);
	else opacity += Math.ceil((limit - opacity) / 2.5);
	if (opacity != limit) setTimeout('set_darken_opacity()',40);
	//if (opacity == 1) getDoc().childNodes[1].removeChild(darken);
	if (opacity == 1) document.body.removeChild(darken);
}

function remove_darken(){
	confirmation_box_container.style.display = "none";
	limit = 1;
	set_darken_opacity();
}

function yes_chosen(){

  // lighten screen?
  remove_darken();
  // remove confirmation box
  //getDoc().childNodes[1].removeChild(confirmation_box_container);
	document.body.removeChild(confirmation_box_container);
  // submit form
  current_form.submit();

}

function no_chosen(){

  // lighten screen?
  remove_darken();
  // remove confirmation box
  //getDoc().childNodes[1].removeChild(confirmation_box_container);
	document.body.removeChild(confirmation_box_container);
  // remove reference to form          
  current_form == null;
  
  return false;

}

var confirmation_box_container;

function showTheconfirm(){

  create_darken();

  var frameWidth, frameHeight;
  // build confirmation box
  confirmation_box_container = document.getElementById('confirm_box_login');

	// ...position box
	confirmation_box_container.style.position = "absolute";
  confirmation_box_container.style.display = "block";
	
	confirmation_box_container.style.height = "238px";
	confirmation_box_container.style.width  = "314px";
	
	if (self.innerWidth){
		frameWidth = self.innerWidth;
		frameHeight = self.innerHeight;
	}

	else if (document.documentElement && document.documentElement.clientWidth){
		frameWidth = document.documentElement.clientWidth;
		frameHeight = document.documentElement.clientHeight;
	}

	else if (document.body){
		frameWidth = document.body.clientWidth;
		frameHeight = document.body.clientHeight;
	}

	confirmation_box_container.style.top = (frameHeight / 2) - (parseInt(confirmation_box_container.style.height) / 2 ) + getScrollY() + "px";
	confirmation_box_container.style.left =  (frameWidth / 2)  - (parseInt(confirmation_box_container.style.width) / 2 )  + getScrollX() + "px";

	// ...add to document      
	//getDoc().childNodes[1].appendChild(confirmation_box_container);
	document.body.appendChild(confirmation_box_container);
	// stop the form submitting
	return false;

}

function change_opac(opacity, elem) {
  var object = elem.style;
  object.opacity = (opacity / 100);
  object.MozOpacity = (opacity / 100);
  object.KhtmlOpacity = (opacity / 100);
  object.filter = "alpha(opacity=" + opacity + ")";
}

function getScrollX(){
	return document.documentElement.scrollLeft;
}

function getScrollY(){
	return document.documentElement.scrollTop;
}


function TabPane(tabpane){
	var tab_initiators = getElementsByClass("tab_initiator", tabpane);
	for(var i = 0; i < tab_initiators.length; i++){
		var tab_initiator = tab_initiators[i];
		var self = this;
		tab_initiator.onmousedown = function(e){ self.show_tab(this.id, tabpane, e); }
	}
}
TabPane.prototype.show_tab = function(tab_initiator_id, tabpane, e) {

	// Turn off all tabs
	var tabs = getElementsByClass("tab", tabpane);
	for(var i = 0; i < tabs.length; i++){
		var tab = tabs[i];
		tab.style.display = "none";
	}

	// Get the required tab and turn it on
	var tab = document.getElementById(tab_initiator_id + "-content");
	tab.style.display = "block";

	// set all tabs initiators to not current
	var tab_initiators = getElementsByClass("tab_initiator", tabpane);
	for(var i = 0; i < tab_initiators.length; i++){
		var tab_initiator = tab_initiators[i];
		tab_initiator.className = tab_initiator.className.replace(/current/, "");
	}

	var tab_initiator = document.getElementById(tab_initiator_id);
	tab_initiator.className = tab_initiator.className + " current";
	
	return false;
}

function setupTabpanes(){
	var tabpane_elements = getElementsByClass("tabpane");
	for(var i = 0; i < tabpane_elements.length; i++){
		var element = tabpane_elements[i];
		new TabPane(element);
	}
}

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if(node == null) node = document;
    if(tag == null) tag = '*';
	var els = node.getElementsByTagName(tag);
	for(var i = 0, j = 0; i < els.length; i++){
		if(stringContains(els[i].className, searchClass)){
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function stringContains(str, search){
	var pattern = new RegExp('(^|\\s)'+search+'(\\s|$)');
	return (pattern.test(str));
}

function gotoIndustry(){
  var s = document.getElementById('industrylist');
	if(s[s.selectedIndex].value != 'none') window.location = "/company-intelligence/industries/"+s[s.selectedIndex].value;
}
function gotoSector(){
  var s = document.getElementById('sectorlist');
	if(s[s.selectedIndex].value != 'none') window.location = "/company-intelligence/sectors/"+s[s.selectedIndex].value;
}


window.onload = function(){
	setupTabpanes();
	externalLinks();
	
	var form = document.getElementById('register_member_form');
	if(form){
		form.onsubmit = function(){
			var checkboxUpgrade = document.getElementById('chkboxchk');
			if(checkboxUpgrade){
				if (document.getElementById('m_field_id_1').value == ''){ alert ('Please fill in your company name'); return false }
				else if (document.getElementById('m_field_id_8').value == ''){ alert ('Please fill in your first name'); return false }
				else if (document.getElementById('m_field_id_9').value == ''){ alert ('Please fill in your last name'); return false }
				else if (document.getElementById('m_field_id_12').value == ''){ alert ('Please fill in your telephone number'); return false }
				else if(!checkboxUpgrade.checked){ alert('You neeed to agree to the terms and conditions'); return false }
				return true
			}
			else{
				if(document.getElementById('email').value != document.getElementById('email-confirm').value){ 
					alert ('The email address fields do not match');
					return false 
				}
				var fullDetails = document.getElementById('full_details');
				if(fullDetails){
					if (document.getElementById('m_field_id_1').value == ''){ alert ('Please fill in your company name'); return false }
				  else if (document.getElementById('m_field_id_8').value == ''){ alert ('Please fill in your first name'); return false }
				  else if (document.getElementById('m_field_id_9').value == ''){ alert ('Please fill in your last name'); return false }
				  else if (document.getElementById('m_field_id_12').value == ''){ alert ('Please fill in your telephone number'); return false }
				}
				return true;
			}
		}
	}

	var togglers = getElementsByClass('toggler');
	for(var i = 0; i < togglers.length; i++){
		var content = document.getElementById(togglers[i].id+'-content');
		togglers[i].content_height = content.offsetHeight;
		content.style.height = '0px';
		
		togglers[i].onclick = function(){
			if(currently_open != null && currently_open != this) currently_open.onclick();
			if(currently_open == this) currently_open = null;
			else currently_open = this;		
			toggle_reveal(this.id, this.id+'-content', this.content_height, this.innerHTML, this.innerHTML, true);
		}
	}
	
	var opened_toggler = document.getElementById('opened_toggler');
	
	if(opened_toggler){
	  document.getElementById(opened_toggler.innerHTML).onclick();
	}
}

var currently_open = null;


function externalLinks(){ 
  if (!document.getElementsByTagName) return; 
    var anchors = document.getElementsByTagName("a"); 
    for(var i=0; i<anchors.length; i++){
      var anchor = anchors[i]; 
      if(anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") anchor.target = "_blank"; 
    }
}


function toggleDisplay(elem, text){

  var box = document.getElementById('box-'+elem);
	var lnk = document.getElementById('link-'+elem);
	if (box.style.display == 'block'){
		box.style.display = 'none';
		lnk.innerHTML = 'See companies within '+text;
	} else {
		box.style.display = 'block';
		lnk.innerHTML = 'Hide companies within '+text;
	}

}

function toggleDisplay2(elem){

  var box = document.getElementById('box-'+elem);
	var lnk = document.getElementById('link-'+elem);
	if (box.style.display == 'block'){
		box.style.display = 'none';
		lnk.innerHTML = 'See related companies';
	} else {
		box.style.display = 'block';
		lnk.innerHTML = 'Hide related companies';
	}

}







/**********************************************************

 * Validate contact forms                                 *

 **********************************************************/

 

function validateString(field, msg, min, max) {

if (!min) { min = 1 }

if (!max) { max = 65535 }

if (!field.value || field.value.length < min || 

field.value.max > max) {

alert(msg);

field.focus();

field.select();

return false;

}



return true;

}



function validateEmail(email, msg, optional) {

if (!email.value && optional) {

return true;

}

var re_mail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z])+$/;

if (!re_mail.test(email.value)) {

alert(msg);

email.focus();

email.select();

return false;

}

return true;

}



function validateEmailsSame(emaila, emailb, msg) {

if (emaila.value!=emailb.value) {

   alert(msg);

   emailb.focus();

   emailb.select();

   return false;

} 

return true;

}

