/**
 * @package ClickATest
 * @subpackage JavaScript
 */

function enterName(ot,a){
  var b = '';
  while (typeof(b)=='string' && b=='') {
    b = prompt('Please, enter the Group name',String(a));
  }
  a = (typeof(b)=='string')?(a==b?'':b):'';
  ot.value = a;
  return Boolean(a);
}


function cbxSelectAll (t,name) {
  var a = t.form.getElementsByTagName('input');
  aLength = a.length;
  for (var i = 0; i < aLength; i++) {
    if (a[i].type=='checkbox' && a[i].name == name && !a[i].disabled) a[i].checked = t.checked;
  }
}



/* 
*  emails validation
*/

// just some prevalidation
function isValidEmail(address) {
    if (address != '' && address.search) {
      if (address.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1) return true;
      else return false;
    }
   // allow empty strings to return true - screen these with either a 'required' test or a 'length' test
   else return true;
}

/* HERE IS the MAIN function that we must call from any ClickATest page to validate emails */
function checkmail(s_email) {
    var errorMess = "Please enter a valid e-mail address";
    if (!isValidEmail(s_email)) {
        alert(errorMess);
        return false;
    }
    var domain = s_email.substring(s_email.indexOf('@') + 1);
    if (domain.indexOf('.') == -1) {
        alert(errorMess);
        return false;
    }
    if (domain.indexOf('.') == 0 || domain.indexOf('.') == domain.length - 1) {
        alert(errorMess);
        return false;
    }
    return true;
}

/* HERE IS the MAIN function without alerts */
function checkmail_without_alerts(s_email) {
    if (!isValidEmail(s_email)) return false;
    var domain = s_email.substring(s_email.indexOf('@') + 1);
    if (domain.indexOf('.') == -1) return false;
    if (domain.indexOf('.') == 0 || domain.indexOf('.') == domain.length - 1) return false;
    return true;
}

/**
 * Class to work with multiple css classes like <tr class="single tablerow selected">
 * 
 * @author Victor Fokin
 */
function classNames(strClasses)
{
	this.classes=strClasses.split(' ');
	
	/**
	 * Change class 'source' for the class 'result'
	 *
	 */
	this.change = function(source, result)
	{
		//alert('source= '+source+'; result = '+result);
		var changed = false;
		var found = false;
		for (var j = 0; j < this.classes.length; j++) 
		{
      		if (this.classes[j] == source) 
			{
				//alert('source found, changing');
				this.classes[j]=result;
				var changed=true;
			}
			else if (this.classes[j] == result)
			{
				//alert('Result already present');
				found=true;
			}
      	}
		if(!changed && !found){
			// No class were specified
			//alert('Source not found and result not present. Adding');
			this.classes.push(result);
		}
		return this._toStr(this.classes);
	}
	
	/**
	 * Adds new css class
	 */
	this.add = function(class_name)
	{
		var found=false;
		for (var j = 0; j < this.classes.length; j++) 
		{
      		if (this.classes[j] == class_name) 
			{
				this.classes[j]=class_name;
				var found=true;
			}
      	}
		if (!found)	this.classes.push(class_name);
		return this._toStr(this.classes);
	}
	
	/**
	 * Removes class from the list
	 */
	this.remove = function(class_name)
	{
		var resultClasses = new Array();
		for (var j = 0; j < this.classes.length; j++) 
		{
      		if (this.classes[j] != class_name) resultClasses.push(this.classes[j]);
      	}
		return this._toStr(resultClasses);
	}
    
    /**
     * Returns true if this class name is present
     */ 
    this.contains = function(class_name)
    {
        if (this.classes.contains(class_name)) return true;
        else return false;
    }
	
	this._toStr = function(arr)
	{
		return arr.join(' ');
	}
}


/**
 * 
 */

function expand_collapse(id, btn_name) {
	var elm = document.getElementById(id);
	if (elm) {
		if (elm.style.display=='none') {
			elm.style.display = "";
			drawCollapse(btn_name);
		} else {
			elm.style.display = "none";
			drawExpand(btn_name);
		}
	}
}

function drawCollapse(btn_name) {
	var btn = document.getElementById(btn_name);
	if (btn) {
		btn.src="/images/icon/icon_collapse.gif";
	}
}
function drawExpand(btn_name) {
	var btn = document.getElementById(btn_name);
	if (btn) {
		btn.src="/images/icon/icon_expand.gif";
	}
}


/**
 * Updates 'Order' menu button with the numbers
 */
function update_order_button(clear) 
{
		var text = "";
		var reports_count = 0;
		var candidates_count = 0;
		var cost = "0.00";
		if (!clear) {
			reports_count = Order.reports_count;
			candidates_count = Order.candidates_count;
			cost = Order.cost;
		}

		if (qualification) {
      text += reports_count+"&nbsp;/&nbsp;" + "&pound;"+cost+"&nbsp;";
		} else {
      text += candidates_count+"&nbsp;x&nbsp;" + reports_count+"&nbsp;/&nbsp;" + "&pound;"+cost+"&nbsp;";
		}
		
	  //clear button
		if (reports_count > 0 || (!qualification && candidates_count > 0)) {
			text += "<a href='/"+(qualification ? "qualification" : "member")+"/order/clear/' onclick='return confirm(\"Are you sure you want to clear order?\")'><img src='/images/icon/delete_icon.gif' alt=\"tooltip: Clear order\" /></a>";
		}
		$("order_data").innerHTML = text;
}

