// $Id:$ */
// Janmedia Interactive 

FAILED_INPUT_BACKGROUND_COLOR = "#FEDDBC";

function checkInput(input,message)
{
    if (input.value.length==0) 
    {
        focusFailedInput(input,message);
        return false;
    }
    return true;
}

function checkSelect(input,message)
{
    if (input.selectedIndex==0) 
    {
        focusFailedInput(input,message);
        return false;
    }
    return true;
}

function focusFailedInput(input, message)
{
    input.oldOnBlur = input.onblur;
    input.oldBackgroundColor = input.style.backgroundColor;
    input.style.backgroundColor = FAILED_INPUT_BACKGROUND_COLOR;
    if (message) alert(message);
    input.onblur = onAfterBlurFailedInput; 
    input.focus();
}

function markFailedInput(input)
{
    input.style.border = "1px solid red";
}

function checkEmail(input,message)
{
    if (!_checkEmail(input.value)) 
    {
        focusFailedInput(input,message);
        return false;
    }
    return true;
}

function _checkEmail(email)
{
	if (email == "") return false;
  template=/^[0-9a-z]+[0-9a-z.-_]*\@[0-9a-z]+[0-9a-z.-_]*\.[0-9a-z]{2,}$/i;
  if (template.test(email) == false) return false;
	return true;
}

function checkPhone(input,message, required)
{
    if (!_checkPhone(input.value, required)) 
    {
        focusFailedInput(input,message);
        return false;
    }
    return true;
}

function _checkPhone(phone, required)
{
	if (phone == "") return !required;
		template=/^\([0-9]{3}\).[0-9]{3}-[0-9]{4}$/i;
	if (template.test(phone) == false) return false;
	return true;
}

function checkCheckboxes(input,message)
{
    if (input.length)
    {
        var i;
        for (i=0; i<input.length; i++)
        {
            if (input[i].checked)
            {
                return true;
            }
        }
        alert(message);
        input[0].focus();
        return false;
    }
    return true;
}

function onAfterBlurFailedInput()
{
    if (this.oldOnBlur)
    {
        this.onblur = this.oldOnBlur; 
    }
    if (this.oldBackgroundColor!=null)
    {
        this.style.backgroundColor = this.oldBackgroundColor;
    }
}

function setCountry(countryinput, stateinput){
  if (stateinput.value != "none") countryinput.value="US";
}
function setState(countryinput, stateinput){
  if (countryinput.value != "US") stateinput.value = "none";
}

function checkRegisterForm(form)
{
	var res = form.job_function.value == 'Residential Consumer';
	return (
		checkSelect(form.role,"Please select your role") &&
		checkSelect(form.job_function,"Please select your job function") &&				
		(res || (checkSelect(form.sector,"Please select your sector") &&				
		checkSelect(form.industry,"Please select your job function") &&
		checkSelect(form.revenue,"Please select your annual revenue") &&
		checkSelect(form.company_size,"Please select your company size"))) &&
		checkInput(form.first_name,"Please enter your first name") &&
		checkInput(form.first_name,"Please enter your first name") &&
		checkInput(form.last_name,"Please enter your last name") &&
		checkPhone(form.phone, "Please enter your phone number using the following format: (000) 000-0000",false) &&
		checkPhone(form.fax, "Please enter your fax number using the following format: (000) 000-0000",false) &&
		checkInput(form.email,"Please enter your email address") &&
		checkEmail(form.email,"Please enter a correct email address") &&
		checkInput(form.password,"Please enter your password")
	);				
}

function showTooltip (nextTo, tip) {
  var tt = document.getElementById('tooltip');
  tt.innerHTML = tip;
  tt.style.left = (getPageLeft(nextTo) + nextTo.offsetWidth) + 'px';
  tt.style.top = getPageTop(nextTo) + 'px';
  tt.style.visibility = 'visible';
} 
function hideTooltip () {
  document.getElementById('tooltip').style.visibility = 'hidden';
} 
function getPageLeft (el) {
  var left = 0;
  do 
    left += el.offsetLeft;
  while ((el = el.offsetParent));
  return left;
}
function getPageTop (el) {
  var top = 0;
  do 
    top += el.offsetTop;
  while ((el = el.offsetParent));
  return top;
}

function checkLoginFormsFilled(form)
{
	if (form)
	{
		if (form.login && form.login.value != '')
		{
			form.login.style.backgroundImage = 'none';
		}
		
		if (form.password && form.password.value != '')
		{
			form.password.style.backgroundImage = 'none';		
		}
	}
}

window.onload = function() { setTimeout("checkLoginFormsFilled(document.loginForm)", 5); }