var nclClick = false;


// Toggles the offspring list.  Builds it the first time, then just hides it.
function ShowOffspring(dogId)
{
	if (document.getElementById('imgShowOffspring').src.indexOf("/drop.gif") > -1)
	{
		if (document.getElementById('tdOffspring').innerHTML == "")
			document.getElementById('tdOffspring').innerHTML = "Retrieving offspring... <img src='images/orange-animated-wait.gif'/>";
		document.getElementById('tdOffspring').style.display = "block";
		document.getElementById('imgShowOffspring').src = "images/undrop.gif";
	}
	else
	{
		document.getElementById('imgShowOffspring').src = "images/drop.gif";
		document.getElementById('tdOffspring').style.display = "none";
		return;
	}
		
	var params = "func=go&d=" + dogId;
	$.ajax({
	    url: 'Ajax.aspx',
	    type: 'POST',
	    data: params,
	    async: true,
	    success: function(data) {
	                $('#tdOffspring').html(data);
	             }
	});
}



// This isn't working yet.  Having trouble getting the DOM to play
// nice with the HTML I got from the server.
// It is to be used with the "Add Litter" registrar page.  
function InsertDog()
{
	var request = GetRequestObject();
	var url = "AddLitterDog.aspx";
	var params = "";
	request.open("POST", url, false);
	request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	request.setRequestHeader("Content-length", params.length);
	request.setRequestHeader("Connection", "close");
	request.send(params);
	if (request.responseText) {
		var tbl = document.getElementById("tblLitters");
		//var tbody = tbl.getElementsByTagName('tbody')[0];
		var newTR = document.createElement('tr');
		var newTD = document.createElement('td');
		newTD.innerHTML = request.responseText;
		newTR.appendChild (newTD);
		tbl.appendChild(newTR);		
	}	
}


function GetRequestObject()
{
	// Provide the XMLHttpRequest class for IE 5.x-6.x:
	if ( typeof XMLHttpRequest == "undefined" ) 
	{
	try { return new ActiveXObject("Msxml2.XMLHTTP.6.0") } catch(e) {}
	try { return new ActiveXObject("Msxml2.XMLHTTP.3.0") } catch(e) {}
	try { return new ActiveXObject("Msxml2.XMLHTTP") } catch(e) {}
	try { return new ActiveXObject("Microsoft.XMLHTTP") } catch(e) {}
	
	throw new Error( "This browser does not support XMLHttpRequest." );
	}
	else
	{
		return new XMLHttpRequest();
	}
}


function OpenPedCert(id)
{
	window.open('CertPedigree.aspx?id=' + id);
}

function OpenPupCert(id)
{
	window.open('CertDogRegistration.aspx?id=' + id);
}

function OpenLitterCert(id)
{
	window.open('CertLitter.aspx?id=' + id);
}

function placeTypeAhead(srchField) {
    var f = srchField[0];
    var loc = srchField.offset();

	var ncl = $('#ncl')[0];
	ncl.style.left = loc.left;
	ncl.style.top = loc.top + f.offsetHeight;
	ncl.style.width = f.offsetWidth;
	ncl.style.width = "400px";
}

function SearchFieldKeystroke(fld, func, parm1)
{
	if (fld.value.length < 3)
	{
		$('#ncl').css('display', 'none');
		return;
	}


	var params = "func=" + func + "&fldVal=" + fld.value + "&sex=" + parm1;
	$.ajax({
	    url: 'Ajax.aspx',
	    type: 'POST',
	    data: params,
	    async: true,
	    
	    success: function(data) {
	        $('#ncl').html(data);
	        $('#ncl').css('display', '');},
	    error: function(data) {
	        $('#ncl').css('display', 'none')
	    }
	});
}


// Hides the "ncl" or search popup element.  This requires some
// logic, because clicking on the ncl is technically an onblur of
// some other element, but we treat that event specially.
function HideSearchPopup(evt)
{
    var ncl = $('#ncl')[0];
	
	var x = evt.x;
	var y = evt.y;
	var t = ncl.offsetTop;
	var l = ncl.offsetLeft;
	var h = ncl.offsetHeight;
	var w = ncl.offsetWidth;


	var xx = evt.pageXOffset;
	var yy = evt.pageYOffset;
	
	if (evt.offsetX >= 0 && evt.offsetX <= w && evt.offsetY >= 0 && evt.offsetY <= h)
	{
		// we clicked on the ncl, don't hide it
	}
	else
	{
		// TODO:  Uncomment this when you can get it to work correctly in Safari  
		// ncl.style.display = 'none';
	}
}


function UnhideSearchPopup()
{
    var ncl = $('#ncl')[0];
	if ((ncl.innerText && ncl.innerText != '') 
	|| (ncl.textContent && ncl.textContent != ''))
	{
		ncl.style.display = '';
	}
}




