var STATES = [];
STATES['AB'] = 'CA';
STATES['AK'] = 'US';
STATES['AL'] = 'US';
STATES['AR'] = 'US';
STATES['AZ'] = 'US';
STATES['BC'] = 'CA';
STATES['CA'] = 'US';
STATES['CO'] = 'US';
STATES['CT'] = 'US';
STATES['DC'] = 'US';
STATES['DE'] = 'US';
STATES['FL'] = 'US';
STATES['GA'] = 'US';
STATES['HI'] = 'US';
STATES['IA'] = 'US';
STATES['ID'] = 'US';
STATES['IL'] = 'US';
STATES['IN'] = 'US';
STATES['KS'] = 'US';
STATES['KY'] = 'US';
STATES['LA'] = 'US';
STATES['MA'] = 'US';
STATES['MB'] = 'CA';
STATES['MD'] = 'US';
STATES['ME'] = 'US';
STATES['MI'] = 'US';
STATES['MN'] = 'US';
STATES['MO'] = 'US';
STATES['MS'] = 'US';
STATES['MT'] = 'US';
STATES['NB'] = 'CA';
STATES['NC'] = 'US';
STATES['ND'] = 'US';
STATES['NE'] = 'US';
STATES['NH'] = 'US';
STATES['NJ'] = 'US';
STATES['NL'] = 'CA';
STATES['NM'] = 'US';
STATES['NS'] = 'CA';
STATES['NT'] = 'CA';
STATES['NU'] = 'CA';
STATES['NV'] = 'US';
STATES['NY'] = 'US';
STATES['OH'] = 'US';
STATES['OK'] = 'US';
STATES['ON'] = 'CA';
STATES['OR'] = 'US';
STATES['PA'] = 'US';
STATES['PE'] = 'CA';
STATES['PR'] = 'US';
STATES['QC'] = 'CA';
STATES['RI'] = 'US';
STATES['SC'] = 'US';
STATES['SD'] = 'US';
STATES['SK'] = 'CA';
STATES['TN'] = 'US';
STATES['TX'] = 'US';
STATES['UT'] = 'US';
STATES['VA'] = 'US';
STATES['VT'] = 'US';
STATES['WA'] = 'US';
STATES['WI'] = 'US';
STATES['WV'] = 'US';
STATES['WY'] = 'US';
STATES['YT'] = 'CA';
var jQuery;

function $(id)
{
	return document.getElementById(id);
}

var mandatoryFieldsList = [];

function main_body_onloaded()
{
	// call bodyOnLoad() function, if defined
	if (self.bodyOnLoad)
	{
		bodyOnLoad();
	}
	// initialize mandatory fields
	if ( checkBrowser() )
	{
		for (var i = 0; i < mandatoryFieldsList.length; ++i)
		{
			var id = mandatoryFieldsList[i];
			$(id).setAttribute('onblur', 'mandatory(["' + id + '"])');
		}
	}
	else
	{
		for (var i = 0; i < mandatoryFieldsList.length; ++i)
		{
			$(mandatoryFieldsList[i]).setAttribute('onblur', function () {mandatory([(window.event.srcElement || window.event.target).getAttribute('id')])});
		}
	}
	// disable forms on unload
	setOnUnload( 'disable_all_forms' );
}

function setOnUnload( _funcName )
{
	other_unloads = document.body.getAttribute('onunload');
	if ( other_unloads )
	{
	    document.body.setAttribute('onunload', other_unloads + _funcName + '();');
	}
	else
	{
	    document.body.setAttribute('onunload', _funcName + '();');
	}
}

function LoadPopupBase( url, w, h, namepop )
{
	if (!w) w=400;
	if (!h) h=400;
	if (!namepop) namepop="Popup";
  	var win = null;
    win = window.open( url, namepop, 'scrollbars=yes,status=no,toolbar=0,menubar=0,titlebar=0,resizable=1,dependent=1,z-lock=1,width='+w+',height='+h );
    try {        
        win.moveTo( screen.width/2-w/2, screen.height/2-h/2);
        win.focus();
    } catch (ex) {
        try {     
            win = null;
            win = window.open( url, namepop, 'scrollbars=yes,status=no,toolbar=0,menubar=0,titlebar=0,resizable=1,dependent=1,z-lock=1,width='+w+',height='+h );
	        win.moveTo( screen.width/2-w/2, screen.height/2-h/2);
		    win.focus();
        } catch (exc) {
        }
    }

	if ( !win )
	{
		alert('It appears that pop-up blockers are enabled on your computer, which may prevent\nsome components from functioning properly. Please temporarily disable any\npop-up blockers.\n\nTo disable the Windows XP pop-up blocker, from the Tools menu in Internet Explorer,\nselect Pop-up Blocker, then Turn Off Pop-up Blocker. To disable other pop-up blockers\nthat are part of a search engine or toolbar, right-click on a toolbar in Internet Explorer\nand click to deselect all toolbars except Standard Buttons, Address Bar, and Links.');
        return null;
	}
	
	return win;
}

function disable_all_forms()
{
	// on submit, disable all forms submit controls
	var forms = document.getElementsByTagName('form');
	for (var i = 0 ; i < forms.length; ++i)
	{
		forms[i].setAttribute('onsubmit', 'return false');
	}
}

function canDelete()
{
	return confirm('Are you sure you want to delete this record?');
}

// returns TRUE if all the fields with the given id's have values, FALSE otherwise
// param string[] field_list
// examples: mandatory(['name'])  ;  mandatory(['first_name', 'last_name']);
// loops through all the fields:
//   if a field's value is empty, appends a <br> and a <span> with the 'This field is mandatory' message
//   if a field has a value and it already has a 'mandatory' <span>, the <br> and <span> are removed
// WARNING: the given fields must not have any other siblings!
function mandatory(field_list, regexp, err_msg)
{
	var ok = true; // assume all values are ok
	var element;
	var parent;
	var warn;
	// default expression: field must not be empty
	if ((typeof regexp) == 'undefined')
	{
		regexp = /(.+)/;
	}
	// default message: This field is mandatory
	if ((typeof err_msg) == 'undefined')
	{
		err_msg = 'This field is mandatory';
	}
	// if the first parameter isn't an array of fields, make it
	if ((typeof field_list) == 'string')
	{
		field_list = [field_list];
	}
	// loop through the fields list, checking if the values match the given regexp
	for (var i = 0; i < field_list.length; ++i)
	{
		// get each element (if it's a string, get the element with that id)
		if ((typeof field_list[i]) == 'string')
		{
			element = document.getElementById(field_list[i]);
		}
		else
		{
			element = field_list[i];
		}
		if(!element) {
			continue;
		}
		// find the parent to which we will attach the warning message
		parent = element.parentNode;
		if ( parent )
		{
    		// remove dummy text nodes (as W3C specifies, blank characters are included in the nodes tree by some browsers)

		}
		var has_warn = (element.nextSibling && element.nextSibling.nextSibling && element.nextSibling.nextSibling.getAttribute && (element.nextSibling.nextSibling.getAttribute('name') == 'mandatory'));

        // empty value & warn doesn't exist: display warning
		if (!regexp.test(element.value))
		{
			ok = false;
			// warn doesn't exist: create it
			if (!has_warn)
			{
				parent.appendChild(document.createElement('BR'));
				warn = document.createElement('SPAN');
				warn.appendChild(document.createTextNode(err_msg));
				warn.setAttribute('name', 'mandatory');
				warn.style.color = 'red';
				parent.appendChild(warn);
			}
			// warn exists: change/update it's text
			else
			{
				// parent.firstChild.nextSibling.nextSibling.innerHTML = err_msg;
			}
		}
		// non-empty value & warn exists: remove warning
		else
		{
			if (has_warn)
			{
				parent.removeChild(element.nextSibling);
				parent.removeChild(element.nextSibling);
			}
		}
	}
	return ok;
}
function mandatory3(field_list, regexp, err_msg)
{
	var ok = true; // assume all values are ok
	var element;
	var parent;
	var warn;
	// default expression: field must not be empty
	if ((typeof regexp) == 'undefined')
	{
		regexp = /(.+)/;
	}
	// default message: This field is mandatory
	if ((typeof err_msg) == 'undefined')
	{
		err_msg = 'This field is mandatory';
	}
	// if the first parameter isn't an array of fields, make it
	if ((typeof field_list) == 'string')
	{
		field_list = [field_list];
	}
	// loop through the fields list, checking if the values match the given regexp
	for (var i = 0; i < field_list.length; ++i)
	{
		// get each element (if it's a string, get the element with that id)
		if ((typeof field_list[i]) == 'string')
		{
			element = document.getElementById(field_list[i]);
		}
		else
		{
			element = field_list[i];
		}
		// find the parent to which we will attach the warning message
		parent = element.parentNode;
		if ( parent )
		{
    		// remove dummy text nodes (as W3C specifies, blank characters are included in the nodes tree by some browsers)

		}
		var has_warn = (element.nextSibling && element.nextSibling.nextSibling);
        // empty value & warn doesn't exist: display warning
		if (!regexp.test(element.value))
		{
		    ok = false;
			// warn doesn't exist: create it
			if (!has_warn)
			{
				parent.appendChild(document.createElement('BR'));
				warn = document.createElement('SPAN');
				warn.appendChild(document.createTextNode(err_msg));
				warn.setAttribute('name', 'mandatory');
				warn.style.color = 'red';
				parent.appendChild(warn);
			}
			// warn exists: change/update it's text
			else
			{
				// parent.firstChild.nextSibling.nextSibling.innerHTML = err_msg;
			}
		}
		// non-empty value & warn exists: remove warning
		else
		{
			if (has_warn)
			{
				parent.removeChild(element.nextSibling);
				parent.removeChild(element.nextSibling.nextSibling);
			}
		}
	}
	return ok;
}

function mandatory2(field_list, regexp, err_msg)
{
	var ok = true; // assume all values are ok
	var element;
	var parent;
	var warn;
	var error_string = '';
	var not_filled = new Array();
	// default expression: field must not be empty
	if ((typeof regexp) == 'undefined')
	{
		regexp = /(.+)/;
	}
	// default message: This field is mandatory
	if ((typeof err_msg) == 'undefined')
	{
		err_msg = 'This field is mandatory';
	}
	// if the first parameter isn't an array of fields, make it
	if ((typeof field_list) == 'string')
	{
		field_list = [field_list];
	}
	// loop through the fields list, checking if the values match the given regexp
	for (var i = 0; i < field_list.length; ++i)
	{
		// get each element (if it's a string, get the element with that id)
		if ((typeof field_list[i]) == 'string')
		{
			element = $(field_list[i]);
		}
		else
		{
			element = field_list[i];
		}
		// find the parent to which we will attach the warning message
		parent = element.parentNode;
		// remove dummy text nodes (as W3C specifies, blank characters are included in the nodes tree by some browsers)
		while (element.nextSibling && (element.nextSibling.nodeType == 3))
		{
			parent.removeChild(element.nextSibling);
		}
		var has_warn = (element.nextSibling && element.nextSibling.nextSibling && element.nextSibling.nextSibling.getAttribute && (element.nextSibling.nextSibling.getAttribute('name') == 'mandatory'));
		// empty value & warn doesn't exist: display warning
		if (!regexp.test(element.value))
		{
			ok = false;
			error_string += 'Field "' + field_list[i] + '" is required!\n';
			array_push(not_filled, field_list[i]);
			// warn doesn't exist: create it
			if (!has_warn)
			{
				parent.appendChild(document.createElement('br'));
				warn = document.createElement('span');
				warn.appendChild(document.createTextNode(err_msg));
				warn.setAttribute('name', 'mandatory');
				warn.style.color = 'red';
				parent.appendChild(warn);
			}
			// warn exists: change/update it's text
			else
			{
				// parent.firstChild.nextSibling.nextSibling.innerHTML = err_msg;
			}
		}
		// non-empty value & warn exists: remove warning
		else
		{
			if (has_warn)
			{
				parent.removeChild(element.nextSibling);
				parent.removeChild(element.nextSibling);
			}
		}
	}
	if (error_string)
	{
	    error_string += 'Please fill in the fields having red warning text below them!';
	    if (not_filled.length)
	    {
		document.getElementById(not_filled[0]).focus();
	    }

	}
	return ok;
}

function array_push(vector)
{
    var i;
    var argv = arguments;
    var argc = argv.length;

    for (i = 1; i < argc; ++i)
    {
	vector[vector.length++] = argv[i];
    }
    return vector.length;
}

// any number of digits
function checkNumeric(elements, is_optional)
{
	var exp;
	if (is_optional)
	{
		exp = /^\d*$/;
	}
	else
	{
		exp = /^\d+$/;
	}
	return mandatory(elements, exp, 'Invalid number');
}

// five digits or: AdA dAd (A => capital letter, d => digit)
var _validation_state = false;
function checkZip(elements, is_optional, _validation_state)
{
	var exp;
	if ( _validation_state != false )
	{

        _validation_state = $(_validation_state).value;
		if ( !_validation_state && is_optional )
		{
			return true;
		}
	    var state = STATES;
        validation_type = state[_validation_state];

        if ( !validation_type )
        {
            alert('Select a state first!');
        }

    	if (is_optional)
    	{
    	    if ( validation_type == 'US' )
    	    {
        		exp = /^(\d{5})?$/;
    	    }
        	else if ( validation_type == 'CA' )
        	{
        	    exp = /^([A-Za-z]\d[A-Za-z] ?\d[A-Za-z]\d)?$/;
        	}
    	}
    	else
    	{
    	    if ( validation_type == 'US' )
    	    {
        		exp = /^(\d{5})$/;
    	    }
        	else if ( validation_type == 'CA' )
        	{
        	    exp = /^([A-Za-z]\d[A-Za-z] ?\d[A-Za-z]\d)$/;
        	}
    	}
	}
	else
	{
    	if (is_optional)
    	{
       	    exp = /^((\d{5})|([A-Za-z]\d[A-Za-z] ?\d[A-Za-z]\d))?$/;
    	}
    	else
    	{
            exp = /^((\d{5})|([A-Za-z]\d[A-Za-z] ?\d[A-Za-z]\d))$/;
    	}
	}
	return mandatory(elements, exp, 'Invalid zip code');
}

function checkZip2(elements, is_optional)
{
	var exp;
	if (is_optional)
	{
		exp = /^((\d{5})|([A-Z]\d[A-Z] \d[A-Z]\d))?$/;
	}
	else
	{
		exp = /^((\d{5})|([A-Za-z]\d[A-Za-z] ?\d[A-Za-z]\d))$/;
	}
	return mandatory2(elements, exp, 'Invalid zip code');
}


function checkZipUS(elements, is_optional)
{
	var exp;
	if (is_optional)
	{
		exp = /^(\d{5})?$/;
	}
	else
	{
		exp = /^(\d{5})$/;
	}
	return mandatory(elements, exp, 'Invalid zip code');
}


//added function to handle the tabbing between the fields for the Phone numbers
//and saving them in a hidden field
function autotab(fieldname, original, destination){
	var phone_number = "("+document.getElementById(fieldname+'_1').value+") ";
	phone_number += document.getElementById(fieldname+'_2').value + "-";
	phone_number += document.getElementById(fieldname+'_3').value;
	if ( !document.getElementById(fieldname+'_1').value
	  && !document.getElementById(fieldname+'_2').value
	  && !document.getElementById(fieldname+'_3').value ){
		phone_number = '';
	}
	document.getElementById(fieldname).value = phone_number;
	if(fieldname == 'phone2' || fieldname == 'phone4' || fieldname == 'fax'){
		if(document.getElementById(fieldname).value.length > 4){
			//if(fieldname == "fax") checkFax(fieldname, true);
			//else checkPhone(fieldname, true);
		}else{
			phone_number = "";
			document.getElementById(fieldname).value = phone_number;
			//if(fieldname == "fax") checkFax(fieldname, true);
			//else checkPhone(fieldname, true);
		}
	}
	else{
		//checkPhone(fieldname, true);
	}
	if (document.getElementById(fieldname).value.length > 13) {
		checkPhone(fieldname, true);
	}
	original.focus();
	if(original.name == fieldname+"_1" || original.name == fieldname+"_2"){
		if (original.getAttribute&&original.value.length==original.getAttribute("maxlength")){
			document.getElementById(destination).focus();
		}
	}
}
function checkPhone3(elements, is_optional)
{
	var exp;
	if (is_optional)
	{
		exp = /^(\(?\d{3}\)?(\s|-)?\d{3}(\s|-)?\d{4})?$/;
	}
	else
	{
		exp = /^(\(?\d{3}\)?(\s|-)?\d{3}(\s|-)?\d{4})$/;
	}
	var phone_number = "("+document.getElementById('phone_1').value+") ";
	phone_number += document.getElementById('phone_2').value + "-";
	phone_number += document.getElementById('phone_3').value;
	document.getElementById(elements).value = phone_number;
	return mandatory3(elements, exp, 'Invalid phone number');
}

// (000) ddd-dddd (brackets, space and minus are optional)
function checkFax3(elements, is_optional)
{
	var exp;
	if (is_optional)
	{
		exp = /^(\(?\d{3}\)?(\s|-)?\d{3}(\s|-)?\d{4})?$/;
	}
	else
	{
		exp = /^(\(?\d{3}\)?(\s|-)?\d{3}(\s|-)?\d{4})$/;
	}
	var fax_number = "("+document.getElementById('fax_1').value+") ";
	fax_number += document.getElementById('fax_2').value + "-";
	fax_number += document.getElementById('fax_3').value;
	if( document.getElementById('fax_1').value!='' || document.getElementById('fax_2').value!='' ||document.getElementById('fax_3').value!='' )
	   document.getElementById(elements).value = fax_number;
	else{

	    element = document.getElementById(elements);
        parent = element.parentNode;
	    if ( element.nextSibling && element.nextSibling.nextSibling ){
            parent.removeChild(element.nextSibling);
    		parent.removeChild(element.nextSibling.nextSibling);
    	}
        return true
    }
	return mandatory3(elements, exp, 'Invalid fax number');
}

// (000) ddd-dddd (d => digit, space => optional space)
function checkPhone(elements, is_optional)
{
	var exp, el, p1, p2, p3;
	for (var i = 0; i < elements.length; i++) {
		el = document.getElementById(elements[i]);
		if (!el) {
			continue;
		}
		p1 = document.getElementById(elements[i] + '_1');
		p2 = document.getElementById(elements[i] + '_2');
		p3 = document.getElementById(elements[i] + '_3');
		if (!p1 || !p2 || !p3) {
			continue;
		}
		el.value = '(' + p1.value + ') ' + p2.value + '-' + p3.value;
	}
	if (is_optional)
	{
		exp = /^(\(?\d{3}\)?(\s|-)?\d{3}(\s|-)?\d{4})?$/;
	}
	else
	{
		exp = /^(\(?\d{3}\)?(\s|-)?\d{3}(\s|-)?\d{4})$/;
	}
	return mandatory(elements, exp, 'Invalid phone number');
}

// (000) ddd-dddd (brackets, space and minus are optional)
function checkFax(elements, is_optional)
{
	var exp;
	if (is_optional)
	{
		exp = /^(\(?\d{3}\)?(\s|-)?\d{3}(\s|-)?\d{4})?$/;
	}
	else
	{
		exp = /^(\(?\d{3}\)?(\s|-)?\d{3}(\s|-)?\d{4})$/;
	}
	return mandatory(elements, exp, 'Invalid fax number');
}

function checkFax2(elements, is_optional)
{
	var exp;
	if (is_optional)
	{
		exp = /^(\(\d{3}\)\s?\d{3}-\d{4})?$/;
	}
	else
	{
		exp = /^(\(\d{3}\)\s?\d{3}-\d{4})$/;
	}
	return mandatory2(elements, exp, 'Invalid fax number');
}


// Aa_-0.Aa_-0.. @ Aa_-0.Aa (email address)
function checkMail(elements, is_optional)
{
	var exp;
	if (is_optional)
	{
		exp = /^(([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4}))?$/;
	}
	else
	{
		exp = /^(([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4}))$/;
	}
	return mandatory(elements, exp, 'Invalid email address');
}

function isFloat(element_id, is_optional)
{
	if (is_optional)
	{
		return /^(\d+(\.\d+)?)?$/.test($(element_id).value);
	}
	else
	{
		return /^\d+(\.\d+)?$/.test($(element_id).value);
	}
}

function isSerial(element_id, is_optional)
{
    if (is_optional)
    {
        return /^([0-9]{8,9})?$/.test($(element_id).value);
    }
    else
    {
        return /^[0-9]{8,9}$/.test($(element_id).value);
    }
}

function IE6()
{
	return (navigator.appName=="Microsoft Internet Explorer");
}

function verifySerial(serial)
{
	if (!IsNumeric(serial) ||
					serial.length < 8 ||
					serial.length > 9 ||
					(parseInt(serial, 10) <= 0) )
	{
		return false;
	}
	return true;
}

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function checkBrowser()
{
    return (
        navigator.appName == 'Netscape'
        || navigator.appName == 'Opera'
        || (navigator.appName == 'Microsoft Internet Explorer' && appVersion() >= 8)
    );
}

function appVersion()
{
    var versMajor = parseInt(navigator.appVersion,10);
    var appVers = navigator.appVersion;
    var pos, versMinor = 0;

    if ((pos = appVers.indexOf("MSIE")) > -1) {
        versMinor = parseFloat(appVers.substr(pos+5));
    } else {
        versMinor = parseFloat(appVers);
    }

    return (versMinor);
}

function formatName( _field )
{
	var name = '';
	var str = _field.value;
	var words = str.split(' ');
	for ( var i in words )
	{
		if ( words[i] )
		{
			name += words[i].substr(0, 1).toUpperCase() + words[i].slice(1).toLowerCase();
			name += ' ';
		}
	}
	_field.value = name.substr(0, name.length-1);
}

function loadJquery() {
	if (!jQuery) {
		var protocol = document.location.href.split(':')[0];
		var s = document.createElement('script');
		s.setAttribute('src', protocol + '://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js');
		s.setAttribute('type', 'text/javascript');
		document.getElementsByTagName('head')[0].appendChild(s);
		setTimeout('$j = jQuery.noConflict()', 2000);
	}
}

function showFindPet(pet, el) {
	loadJquery();
	var container = document.createElement('div');
	container.setAttribute('id', 'found_pet_' + pet);
	container.setAttribute('class', 'found_pet');
	container.style.position = 'absolute';
	container.style.zIndex = 10;
	var us_states = '', ca_states = '';
	for (state in STATES) {
		if (STATES[state] == 'US') {
			us_states += '<option value="' + state + '">' + state + '</option>';
		}
		if (STATES[state] == 'CA') {
			ca_states += '<option value="' + state + '">' + state + '</option>';
		}
	}
	var state, html = '\
		<table width="100%">\
		<tr>\
			<td width="100" align="right"><label for="finder_name_"' + pet + '>Name:</label></td>\
			<td width="200" align="left"><input class="lg-input2" type="text" id="finder_name_' + pet + '"/></td>\
		</tr>\
		<tr>\
			<td align="right"><label for="finder_phone_' + pet + '1">Phone:</label></td>\
			<td align="left" class="phone-input"><div class="phone-input2"><input class="phone-input1" type="text" size="4" maxlength="3" id="finder_phone_' + pet + '_1"/>\
				<input type="text" class="phone-input2" size="4" maxlength="3" id="finder_phone_' + pet + '_2"/>\
				<input type="text" class="phone-input3" size="5" maxlength="4" id="finder_phone_' + pet + '_3"/>\
			</div></td>\
		</tr>\
		<tr>\
			<td align="right"><label for="finder_email_' + pet +'">Email:</label></td>\
			<td align="left"><input class="lg-input2" type="text" id="finder_email_' + pet + '"/></td>\
		</tr>\
		<tr>\
			<td align="right"><label for="finder_city_' + pet + '">City:</label></td>\
			<td align="left"><input class="lg-input2" type="text" id="finder_city_' + pet + '"/></td>\
		</tr>\
		<tr>\
			<td align="right"><label for="finder_state_' + pet + '">State:</label></td>\
			<td align="left"><select class="input-select" id="finder_state_' + pet + '"><option value="">Choose state/province</option><optgroup label="United States">' + us_states +
		'</optgroup><optgroup label="Canada">' + ca_states + '</optgroup></select></td>' + 
		'</tr>\
		<tr>\
			<td align="right"><p><span class="red">* </span>Text below</p></td>\
			<td align="left">\
			<p><input class="lg-input2" type="text" name="captcha_code_' + pet + '" id="captcha_code' + pet + '" maxlength="6" /></p>\
			<img id="siimage_' + pet + '" style="border: 1px solid #000; margin-right: 15px" src="/src/lib/securimage/securimage_show.php?sid=' + Math.random() * 10000 +  ' " alt="CAPTCHA Image" align="left">\
			</td>\
		</tr>\
		<tr>\
			<td align="left" style="padding-left:30px;"><button style="border:0;background:url(/images/send_verde.gif) no-repeat top left;width:76px;height:26px;" type="button" onclick="sendFinderData(' + pet + ');return false;">&nbsp;</button></td>\
			<td align="right" style="padding-right:30px;"><button style="border:0;background:url(/images/admin/but_cancel.gif) no-repeat top left;width:103px;height:26px;" type="button" onclick="cancelFindPet(' + pet + ');return false;">&nbsp;</button></td>\
		</tr>\
		<tr>\
			<td align="center" style="padding-top:5px;font-size:10px;" colspan="2"><sup>*</sup>None of this data will be stored by IDTag<br/> and will only be sent to the pet\'s owner in order to contact you back.</td>\
		</tr>\
		</table>';
	container.innerHTML = html;
	//if (el.nextSibling) {
	if( document.getElementById( "found_pet_" + pet ) ) {
		//el.parentNode.insertBefore(container, el.nextSibling);
// 		document.getElementById( "boxContainer_" + pet ).appendChild(container);
		document.getElementById( "found_pet_" + pet ).innerHTML = container.innerHTML;
	} else {
		el.appendChild(container);
	}
}

function sendFinderData(pet) {
	var data = {};
	data['pet'] = pet;
	data['name'] = $('finder_name_' + pet) ? $('finder_name_' + pet).value : '';
	data['phone1'] = $('finder_phone_' + pet + '_1') ? $('finder_phone_' + pet + '_1').value : '';
	data['phone2'] = $('finder_phone_' + pet + '_2') ? $('finder_phone_' + pet + '_2').value : '';
	data['phone3'] = $('finder_phone_' + pet + '_3') ? $('finder_phone_' + pet + '_3').value : '';
	data['email'] = $('finder_email_' + pet) ? $('finder_email_' + pet).value : '';
	data['city'] = $('finder_city_' + pet) ? $('finder_city_' + pet).value : '';
	data['state'] = $('finder_state_' + pet) ? $('finder_state_' + pet).options[$('finder_state_' + pet).selectedIndex].value : '';
	data['captcha_code'] = $('captcha_code' + pet ).value;
	latestPet = pet;
	if (!data['name']) {
		alert('Your name cannot be empty');
		return;
	}
	if (!data['email'] && !data['phone1'] && !data['phone2'] && !data['phone3']) {
		alert('At least a phone number or an email address are required for the pet owner to contact you back');
		return;
	}
	jQuery.noConflict();
	jQuery.ajax({
		url: '/found-pet-contact',
		data: data,
		dataType: 'html',
		type: 'post',
		success: function(t) {
			var pet = t.responseText ? t.responseText : t;
			if (pet == 'fail') {
				alert('Unable to contact owner');
			}
			if (pet == 'Invalid code') {
				alert('Invalid code');
				jQuery('#siimage_' + latestPet ).attr( 'src' , "/src/lib/securimage/securimage_show.php?sid=" + (Math.random()*100000) );
			}
			if (jQuery('#found_pet_' + pet).length) {
				jQuery('#found_pet_' + pet).attr('align', 'left');
				jQuery('#found_pet_' + pet).html('Thank you,<br/><br/>You will shortly be contacted by the pet\'s owner or by our Customer Care Department');
				setTimeout('cancelFindPet(' + pet + ');', 5000);
			}
		},
		failure: function() {
			alert('Unable to contact owner');
		}
	});
}

function cancelFindPet(pet) {
	if (jQuery('#found_pet_' + pet).length) {
		jQuery('#found_pet_' + pet).html('');
	}
}

function autotabSerial( currentField, maxLength, nextField ) {
	if( document.getElementById( currentField ).value.length >= maxLength ) {
		document.getElementById( nextField ).focus();
	}
}
