//Common Javascript functions

//onMouseOver="tooltip(event, 'Tooltip text', null, -30);" onMouseOut="tooltip(null);
function tooltip(e, contents, offsetX, offsetY)
{
    var tt = document.getElementById('tooltip');
    if(!e){
        tt.style.visibility = "hidden";
        return;
    }
    offsetX = offsetX ? offsetX : 30;
    offsetY = offsetY ? offsetY : -20;
	
    if (e.pageX || e.pageY) 	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}

	
    tt.innerHTML = contents;
    tt.style.left = posx +"px";
    var newtop = eval(posy) + offsetY;
    tt.style.top = newtop +"px";
    tt.style.visibility = "visible";

}

function checkForMatch(origField, destFieldName)
{
	var orig = origField;
	var dest = document.getElementById(destFieldName);
	if(orig.value == dest.value)
	{
		orig.style.background = "#bae389";
	}else{
		orig.style.background = "#f5adad";
	}
	
}

function validate_email(field, alerttxt)
{
	validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
	with (field)
	{
		// search email text for regular exp matches
	    if (value.search(validRegExp) == -1) 
	   	{
	      	alert(alerttxt);
	      	return false;
	    }else{
	    	return true; 	
	    }
	}
}


function validate_register(thisform)
{
	
	with (thisform)
	{
	 	
	 	if(isEmpty(upd_login.value))
	 	{
	 		alert('You must enter a login.');
	 		upd_login.select();
	 		return false;
	 	}
		
	 	if(isEmpty(upd_passwd.value))
	 	{
	 		alert('You must enter a password.');
	 		upd_passwd.select();
	 		return false;
	 	}
	 	
	 	if (validate_email(upd_email, "Not a valid e-mail address!") == false)
	 	{
	  		upd_email.select();
	  		return false;
	 	}
	 		
	 	if(!accept_terms.checked)
	 	{
	 		alert('You must accept the Terms & Conditions to submit.');
	 		return false;
	 	}
	}
}

function validate_quote(thisform)
{
	
	with (thisform)
	{
		if (validate_email(email, "Not a valid e-mail address!") == false)
	 	{
	  		email.select();
	  		return false;
	 	}
	 	
	 	if(isEmpty(city.value))
	 	{
	 		city.select();
	 		alert('City is required');
	 		return false;
	 	}
	 	if(isEmpty(postal_code.value))
	 	{
	 		postal_code.select();
	 		alert('Postal Code is required');
	 		return false;
	 	}
	 	if(isEmpty(country.value))
	 	{
	 		country.select();
	 		alert('Country is required');
	 		return false;
	 	}
	 	if(!isAlpha(name_first.value))
	 	{
	 		name_first.select();
	 		alert('First name must be Alpha characters only.');
	 		return false;
	 	}
	 	if(contact_method.value == 'skype' && isEmpty(skype_id.value))
	 	{
	 		skype_id.select();
	 		alert('Skype ID required when selected as preferred contact method.');
	 		return false;
	 	}
	 	if(contact_method.value == 'phone' && isEmpty(number_phone.value))
	 	{
	 		number_phone.select();
	 		alert('Phone # required when selected as preferred contact method.');
	 		return false;
	 	}
	}
}

function isEmpty(value) {
   if ((value.length==0) || (value==null)) 
   {
      return true;
   }
   else { return false; }
}

function isNumeric(value) 
{
	return !isNaN(value);
}

function isAlpha(value) 
{
    var re = /^[a-zA-Z]+$/;
    return value.match(re);
}

function submitForm(elem){
	while (elem.parentNode && elem.parentNode.tagName != "FORM"){
	elem = elem.parentNode;
	}
	var oForm = elem.parentNode;
	oForm.submit();
}

function showPopup(p)
{
	greyout(true);
	document.getElementById(p).style.visibility = 'visible';
	document.getElementById(p).style.display = 'block';
}
function hidePopup(p)
{
	greyout(false);
	document.getElementById(p).style.visibility = 'hidden';
	document.getElementById(p).style.display = 'none';
}
function appendElement(node,tag,id,htm)
{
	var ne = document.createElement(tag);
	if(id) ne.id = id;
	if(htm) ne.innerHTML = htm;
	node.appendChild(ne);
}

function greyout(d,z)
{
	var obj = document.getElementById('greyout');
	
	if(!obj)
	{
		appendElement(document.body,'div','greyout');
		var body = document.getElementsByTagName("body");
		obj = document.getElementById('greyout');
		obj.style.position = 'absolute';
		obj.style.top = '0px';
		obj.style.left = '0px';
		obj.style.background = '#000';
		obj.style.opacity = '.5';
		obj.style.filter = 'alpha(opacity=50)'
	}
	if(d)
	{
		var ch = document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
		var cw = document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth;
		var sh = document.documentElement.scrollHeight ? document.documentElement.scrollHeight : document.body.scrollHeight;
		if(document.body.scrollHeight) sh = Math.max(sh,document.body.scrollHeight)
		var sw = document.documentElement.scrollWidth ? document.documentElement.scrollWidth : document.body.scrollWidth;
		if(document.body.scrollWidth) sh = Math.max(sh,document.body.scrollWidth)
		var wh = window.innerHeight ? window.innerHeight : document.body.offsetHeight;
		if(!z){ z = 50 }
		obj.style.zIndex = z;
		obj.style.height = Math.max(wh,Math.max(sh,ch))+'px';
		obj.style.width  = Math.max(sw,cw)+'px';
		obj.style.display = 'block';		
	} else {
		obj.style.display = 'none';   
	}
}
