function onOtherClick(event)
{	if (!document.getElementById("sg_divDropDown") && !document.getElementById("sg_divDropDownLab")  && !document.getElementById("sg_divDropDownNews") && !document.getElementById("sg_divDropDownRes")) return;

        if (document.getElementById('menu_specialities')) closeUm(event);

        if (window.event.srcElement.id == "dd_button" || window.event.srcElement.id == "dd_button_lab" || window.event.srcElement.id == "dd_button_news" || window.event.srcElement.id == "dd_button_res") return; 
        bbClicked = false;
        bbClickedLab = false;
        bbClickedNews = false;
        bbClickedRes = false;
	if (document.getElementById("sg_divDropDown")) document.getElementById("sg_divDropDown").style.display = "none";
	if (document.getElementById("sg_divDropDownLab")) document.getElementById("sg_divDropDownLab").style.display = "none";
	if (document.getElementById("sg_divDropDownNews")) document.getElementById("sg_divDropDownNews").style.display = "none";
	if (document.getElementById("sg_divDropDownRes")) document.getElementById("sg_divDropDownRes").style.display = "none";

}
function onSearch ()
{
	var rad = document.searchForm.searchRad;
	var site = "";
	for( i = 0; i < rad.length; i++)
	{	if (rad[i].checked) {
			site = rad[i].value;
		}
	}
	if (site == 'local') { document.forms['searchForm'].action='/search/search-local.aspx';}
	document.forms['searchForm'].submit();
}

function onSearchEvent ()
{
	var rad = document.searchForm2.searchRad;
	var site = "";
	for( i = 0; i < rad.length; i++)
	{	if (rad[i].checked) {
			site = rad[i].value;
		}
	}
	if (site == 'local') { document.forms['searchForm2'].action='/search/search-local.aspx';}
	document.forms['searchForm2'].submit();
}

function doPrint()
{
	var doc = document.getElementById("sg_int_content");
 	if (doc)
	{
		var content = doc.innerHTML;

	}
}
function openEmailPopup(id, url, obj)
{
  	//alert(id + " : " + url);
        var dropObj = document.getElementById("emailWindow");
	dropObj.x=getposOffset(obj, "left") -570;
	dropObj.y=getposOffset(obj, "top") +52;
	dropObj.style.left=dropObj.x +"px" ;
	dropObj.style.top=dropObj.y +"px";
        dropObj.style.display = "block";
        var vs = document.getElementById('iframeid');
        vs.src = "http://www.surgery.ucsf.edu/utilities/send-email.aspx?page_id=" + id;
}

function getposOffset(what, offsettype){
	var totaloffset=(offsettype=="left")? what.offsetLeft : what.offsetTop;
	var parentEl=what.offsetParent;
	while (parentEl!=null){
	    totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
	    parentEl=parentEl.offsetParent;
        }
	return totaloffset;
}

function doEmail()
{
   return;
   //if (validate()) alert("ready");
   //   document.getElementById("form1").submit();
   

} 

function showResponse(responseText, statusText)  { 
    // for normal html responses, the first argument to the success callback 
    // is the XMLHttpRequest object's responseText property 
 
    // if the ajaxForm method was passed an Options Object with the dataType 
    // property set to 'xml' then the first argument to the success callback 
    // is the XMLHttpRequest object's responseXML property 
 
    // if the ajaxForm method was passed an Options Object with the dataType 
    // property set to 'json' then the first argument to the success callback 
    // is the json data object returned by the server 
 
    //alert('status: ' + statusText + '\n\nresponseText: \n' + responseText); 
    var formObj = document.getElementById("theForm");
    formObj.style.display = "none";
    var successObj = document.getElementById("successDiv");
    successObj.innerHTML = responseText;
    successObj.style.display = "block";
} 

function changeWindows()
{
    var formObj = document.getElementById("theForm");
    formObj.style.display = "block";
    var successObj = document.getElementById("successDiv");
    successObj.style.display = "none";

}

function validate(formData, jqForm, options) 
{ 
    var errors = "";
    for (var i=0; i < formData.length; i++) 
    { 
        //if (!formData[i].value) 
	//{ 
         //   str += formData[i].name + ' missing\n'; 
             
        //}

	if (formData[i].name == 'emailTo' && !validateEmail(formData[i].value)) {
	      errors += "The 'To' Email address is invalid or was not entered\n\n";
	} 
	if (formData[i].name == 'emailFrom' && !validateEmail(formData[i].value)) {
		errors += "The 'From' Email address is invalid or was not entered\n\n";
	} 
  
	if (formData[i].name == 'emailSubject' && formData[i].value == "") errors += "The Email Subject is missing\n\n";

	if (formData[i].name == 'emailComment' && formData[i].value.length > 200) errors += "The Maximum length of the Comment is 200 character\n\n";

	if (formData[i].name == 'emailComment' && !chkComment(formData[i].value)) errors += "I'm sorry but you are only allowed 2 URL's in the Comments, more may signify SPAM!\n\n";

    }
    if (errors) 
    {	    alert (errors);
   	    return false;
    }
    return true;
} 

// test to see if there a lot of URL's in the comments - usually means SPAM
function chkComment(txt)
{
        if (txt.length == 0)return true;
	var arr = txt.match(/http:\/\//g);
	//var arr2 = txt.match("www");
        if (arr && arr.length > 2 ) return false;
        return true;
}

// Email Validation Javascript
// copyright 23rd March 2003, by Stephen Chapman, Felgall Pty Ltd

// You have permission to copy and use this javascript provided that
// the content of the script is not changed in any way.

function validateEmail(addr) {

	if (addr == '')	return false;
	var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
	for (i=0; i<invalidChars.length; i++)
	{
		if (addr.indexOf(invalidChars.charAt(i),0) > -1)
		{
			return false;
		}
	}
	for (i=0; i<addr.length; i++)
	{
		if (addr.charCodeAt(i)>127)
		{
			return false;
		}
	}

	var atPos = addr.indexOf('@',0);
	if (atPos == -1)
	{
		return false;
	}
	if (atPos == 0)
	{
		return false;
	}
	if (addr.indexOf('@', atPos + 1) > - 1)
	{
		return false;
	}
	if (addr.indexOf('.', atPos) == -1)
	{
		return false;
	}
	if (addr.indexOf('@.',0) != -1)
	{
		return false;
	}
	if (addr.indexOf('.@',0) != -1)
	{
		return false;
	}
	if (addr.indexOf('..',0) != -1)
	{
		return false;
	}
	var suffix = addr.substring(addr.lastIndexOf('.')+1);
	if (suffix.length != 2 && suffix != 'com' && suffix != 'net' && suffix != 'org' && suffix != 'edu' && suffix != 'int' && suffix != 'mil' && suffix != 'gov' & suffix != 'arpa' && suffix != 'biz' && suffix != 'aero' && suffix != 'name' && suffix != 'coop' && suffix != 'info' && suffix != 'pro' && suffix != 'museum')
	{
		return false;
	}
	return true;
}

/* Login stuff */
function openLoginPopup(obj)
{

  	//alert(id + " : " + url);
        var dropObj = document.getElementById("loginWindow");
	dropObj.x=getposOffset(obj, "left") - 252;
	dropObj.y=getposOffset(obj, "top") -230;
	dropObj.style.left=dropObj.x +"px" ;
	dropObj.style.top=dropObj.y +"px";
        dropObj.style.display = "block";
        grayer("on");
}

function getposOffset(what, offsettype){
	var totaloffset=(offsettype=="left")? what.offsetLeft : what.offsetTop;
	var parentEl=what.offsetParent;
	while (parentEl!=null){
	    totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
	    parentEl=parentEl.offsetParent;
        }
	return totaloffset;
}
function doClose(obj)
{	document.getElementById('loginWindow').style.display='none';
        grayer("off");
        window.location = "/";

}


function grayer(type)
{
	var grayer = document.getElementById("grayer");
	if (type == "on") { grayer.style.visibility = "visible"; grayer.style.display = "block"; }
	else {grayer.style.visibility = "hidden"; grayer.style.display = "none"; }
}


