function showTooltip(e, flag, divId){
	var tooltipDiv = document.getElementById(divId);
	if (flag == true)  {
		if (!e) {var e = window.event;}
		
		var offsetTop = document.body.scrollTop;
		if (offsetTop == 0)
		{
			 if (window.pageYOffset)
				offsetTop = window.pageYOffset;
			else
				offsetTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
		}
		var offsetlLeft = document.body.scrollLeft;
		if (offsetlLeft == 0)
		{
			 if (window.pageXOffset)
				offsetlLeft = window.pageXOffset;
			else
				offsetlLeft = (document.body.parentElement) ? document.body.parentElement.scrollLeft : 0;
		}
		
		var left = offsetlLeft + e.clientX + 25;
		var top  = offsetTop + e.clientY + 4;
		tooltipDiv.style.left = left + "px";
		tooltipDiv.style.top = top + "px";
		tooltipDiv.style.display="block"; 
	} 
	else 
	{
		tooltipDiv.style.display="none";
	};

}

/**
*
* Hidden component according to existing information. 
*
* @author  Gergana Pancheva
*/


function hideComponent(existNumber, showComp, hiddenComp){
        
        var existNumber = document.getElementById(existNumber).value;
        
        if(existNumber == 0) {
              // if there are no manage phone numbers - table with phone number and information about them wil not display
              document.getElementById(showComp).style.display = 'none';              
              document.getElementById(hiddenComp).style.display = 'block';
              
        } else {
              document.getElementById(hiddenComp).style.display = 'none';
         }
        
}


/**
 * @author Nikolay Taskov
 * 
 * Redirect to specify url
 * 
 * @param url 
 */
function openPage(url) 
{	
	window.location.replace(url);
}

/**
 * @author Nikolay Taskov
 * 
 * Checked and unchecked the all checkboxes in specify table
 * 
 * @param checkBoxName 
 */
function checkedCheckBoxes(checkBoxName)
{	
	var checked = document.getElementById("checkbox_checked").checked;
	
	count = document.getElementsByName(checkBoxName).length;
	for(var index = 0; index < count; index++)
	{
		document.getElementsByName(checkBoxName)[index].checked = checked;
	}
}

/**
 * @author Krum Stoilov
 * 
 * Show tolltip specified for checked an unchecked variant
 * 
 * @param e - mouse event 
 * @param checkDivId -  id of the div element contains check all text  
 * @param uncheckDivId - id of the div element contains uncheck all text  
 */
function showCheckBoxAllTolltip(e, checkDivId, uncheckDivId)
{
	var checkDiv = document.getElementById(checkDivId);
	var uncheckDiv = document.getElementById(uncheckDivId);
	if(document.getElementById("checkbox_checked").checked) 
	{
		checkDiv.style.display= "none";
		showTooltip(e, true, uncheckDivId);
	}
	else
	{
		uncheckDiv.style.display= "none";
		showTooltip(e, true, checkDivId);
	}
}

/**
 * 
 *get valid phone number for list entry
 * 
 * @param e - phone -the user input phone number 
 * @return the valid phone number or null if number is not valid  
 */
function getValidPhoneNumber(phone) 
{
	if(phone == null || phone.length == 0)
	{
		return null;	
	}
		
	var validPhoneNumber = null;

	var phoneReplaceSpace = phone.replace(/ /gi, "");		
	var phoneReplacedR = phoneReplaceSpace.replace(/\//gi, "");		
	var phoneReplacedL = phoneReplacedR.replace(/\\/gi, "");
	var phoneReplacedT = phoneReplacedL.replace(/\-/gi, "");
	var phoneReplacedLeftBracket = phoneReplacedT.replace(/\(/gi, "");
	var phoneReplacedRightBracket = phoneReplacedLeftBracket.replace(/\)/gi, "");
			
			
	if(phoneReplacedRightBracket.length == 0) {
		return null;
	}
	var phoneValid = "";
	var endString = "";
	var beginString = "";
	var maxlength = 16;
	var minlength = 3;
	if(phoneReplacedRightBracket.charAt(phoneReplacedRightBracket.length-1) == '$')
	{
		endString = "$";
		phoneReplacedRightBracket = phoneReplacedRightBracket.substr(0, phoneReplacedRightBracket.length-1);
		maxlength--;
		minlength--;
	}
	
	if(phoneReplacedRightBracket.charAt(0) == '+')
	{
		var phoneValid = phoneReplacedRightBracket.substr(1);
		beginString = "+";
	}
	else
	{
		phoneValid = phoneReplacedRightBracket;
	}
	var currentIndex = 0
	while(currentIndex < phoneValid.length && phoneValid.charAt(currentIndex) == '0') {
		//phoneValid = phoneValid.substr(1);
		minlength++;
		currentIndex++;
	}

	if(phoneValid.length > maxlength || phoneValid.length < minlength)
	{
		return null;
	}
	
	var myRegxp = new RegExp(/[^0-9\s]/);

	if(myRegxp.test(phoneValid))
	{
		return null;
	}
	else
	{	
		validPhoneNumber = beginString + phoneValid + endString;
	}
	return validPhoneNumber;
}

/**
 * 
 *get valid phone description
 * 
 * @param description -the description that user input
 * @return true if desription is valid , false if description is not valid 
 */
function validateDescription(description) {
	
		var reg = new RegExp(/[<>{}]/);
		if(!reg.test(description)) {
			return true;
		} else {
			return false;
		}
}

//return true if client browser have cookie enabled, else return false
function checkCookieEnabled() {
	var testcookieName = "test1";
	document.cookie = testcookieName + "=none;";
	var result = (document.cookie.indexOf(testcookieName) != -1)? true : false;
	var expiresDate = new Date(0);
	document.cookie = testcookieName + "=none;expires=" + expiresDate.toGMTString() + ";";
	return result;
}

//for reload login  pages after 10 minutes or when JSESSIONID cookie is not set
function reloadLoginPage(mssUri) {
	//check browser enable cookie
	if (!checkCookieEnabled()) {
		window.location.replace(mssUri + "/nocookie.jsp?cookDisabled=true");
	} else {
		//for opera browser is needed to reload page after close and open browser
		//because JSESSIONID cookie is removed
	     if (document.cookie.indexOf("JSESSIONID") == -1) {
	     	if(document.cookie.indexOf("reload") == -1) {
	     		//if cookie reload is expired then can reload again
	     		var currDate = new Date();
	     		var expiresDate = new Date(currDate.getTime() + 2000);//current time + 2 seconds.
				document.cookie =  "reload" + ";expires=" + expiresDate.toGMTString() + ";";
	     		window.location.reload();
	     	} else {
	     		document.body.innerHTML="";
	     	}
	     }
	}
	//reload page to not lose the session after 10 minutes
	setTimeout("window.location.reload()", 10*60*1000);
}

var first = true;
//for ajax reload innerHTML of element with id divId evry countSeconds , starting after countSeconds.
//Form with id formId must contain only hidden inputs
function reloadContent(countSeconds, divId, formId, url) {
	if(first) {
		first = false;
	} else {
		var sendParams = null;
	    var inputForm = document.getElementById(formId);
	    if (inputForm) {
	    	for(var i = 0; i< inputForm.elements.length; i++) {
		    	var inputField = inputForm.elements[i];
		    	if(i==0) {
		    		sendParams = "";
		    	} else {
		    		sendParams = sendParams + "&";
		    	}
		    	sendParams = sendParams + inputField.name + "=" + inputField.value;
		    }
	    }

	    /* Set up the request */
	    var xmlhttp =  new XMLHttpRequest();
	    xmlhttp.open('POST', url, true);
	    
	    /* The callback function */
	    xmlhttp.onreadystatechange = function() {
	        if (xmlhttp.readyState == 4) {
				var textDiv = document.getElementById(divId);
				if(xmlhttp.status != 404) {
					textDiv.innerHTML = xmlhttp.responseText;  
				} else {
					textDiv.innerHTML = "Can not found requested resource!";
				}
	        }
	    }
	    /* Send the POST request */
	    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	    xmlhttp.send(sendParams);
    }
    setTimeout("reloadContent(" + countSeconds + ", '" + divId + "', '" +  formId + "', '" + url + "')", countSeconds*1000);
}