var BrowserDetect = {
    init: function () {
        this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
        this.version = this.searchVersion(navigator.userAgent)
            || this.searchVersion(navigator.appVersion)
            || "an unknown version";
        this.OS = this.searchString(this.dataOS) || "an unknown OS";
    },
    searchString: function (data) {
        for (var i = 0; i < data.length; i++) {
            var dataString = data[i].string;
            var dataProp = data[i].prop;
            this.versionSearchString = data[i].versionSearch || data[i].identity;
            if (dataString) {
                if (dataString.indexOf(data[i].subString) != -1)
                    return data[i].identity;
            }
            else if (dataProp)
                return data[i].identity;
        }
    },
    searchVersion: function (dataString) {
        var index = dataString.indexOf(this.versionSearchString);
        if (index == -1) return;
        return parseFloat(dataString.substring(index + this.versionSearchString.length + 1));
    },
    dataBrowser: [
        {
            string: navigator.userAgent,
            subString: "Chrome",
            identity: "Chrome"
        },
        { string: navigator.userAgent,
            subString: "OmniWeb",
            versionSearch: "OmniWeb/",
            identity: "OmniWeb"
        },
        {
            string: navigator.vendor,
            subString: "Apple",
            identity: "Safari",
            versionSearch: "Version"
        },
        {
            prop: window.opera,
            identity: "Opera"
        },
        {
            string: navigator.vendor,
            subString: "iCab",
            identity: "iCab"
        },
        {
            string: navigator.vendor,
            subString: "KDE",
            identity: "Konqueror"
        },
        {
            string: navigator.userAgent,
            subString: "Firefox",
            identity: "Firefox"
        },
        {
            string: navigator.vendor,
            subString: "Camino",
            identity: "Camino"
        },
        {        // for newer Netscapes (6+)
            string: navigator.userAgent,
            subString: "Netscape",
            identity: "Netscape"
        },
        {
            string: navigator.userAgent,
            subString: "MSIE",
            identity: "Explorer",
            versionSearch: "MSIE"
        },
        {
            string: navigator.userAgent,
            subString: "Gecko",
            identity: "Mozilla",
            versionSearch: "rv"
        },
        {         // for older Netscapes (4-)
            string: navigator.userAgent,
            subString: "Mozilla",
            identity: "Netscape",
            versionSearch: "Mozilla"
        }
    ],
    dataOS: [
        {
            string: navigator.platform,
            subString: "Win",
            identity: "Windows"
        },
        {
            string: navigator.platform,
            subString: "Mac",
            identity: "Mac"
        },
        {
            string: navigator.userAgent,
            subString: "iPhone",
            identity: "iPhone/iPod"
        },
        {
            string: navigator.platform,
            subString: "Linux",
            identity: "Linux"
        }
    ]

};
BrowserDetect.init();


function passInfo(obj)
{
	try
	{
	var objname = obj.id;
	var ojbnamearr = objname.split("select");
	var childnum = ojbnamearr[0];
	
	var childString = obj.value;
	
	var childArr = childString.split(",");
	var firstnameid = "#"+childnum.toLowerCase() + "firstname";
	var lastnameid = "#"+childnum.toLowerCase() + "lastname";
	var scheduleid = "#"+childnum.toLowerCase() + "schedule";
	var dateid = "#"+childnum.toLowerCase() + "date";
	var regdateid = "#"+childnum.toLowerCase() + "regdate";
	//var registeredName = "input[name="+childnum.toLowerCase() + "reg]";


	
	$(firstnameid).val(childArr[0]);
	$(lastnameid).val(childArr[1]);
	$(scheduleid).val(childArr[3]);
	$(dateid).val(childArr[2]);
	$(regdateid).val(childArr[4]);
	
	
	
	
//	if(childArr[4]==0)
//	{
//		$(registeredName.toString()+'[value=no]').attr('selected', true);
//	
//	}
//	else
//	{
//		$(registeredName.toString()+'[value=yes]').attr('checked', true);
//	}
	}
	catch(err)
	{
		alert(err.description);
	}
	

}



function openUpdateControl(id)
{
	window.open(
	     		
	       'worksheetUpdateControl.php?id='+id,  	// the url to the php script
	       'UpdateControl',                            			// some name for the window
	       'status=0,width=1000,height=1000'                    	// display options for the window
	     );
	return false;

}
function openUpdateImmunization(id)
{
	window.open(
	     		
	       'worksheetUpdateImmunization.php?id='+id,  	// the url to the php script
	       'UpdateControl',                            			// some name for the window
	       'status=0,width=1000,height=1000'                    	// display options for the window
	     );
	return false;

}

function refreshAndClose ()
{
	window.opener.location.reload();
	window.close();
}

function showAddChild(childInputNumber,provId) 
{
		
    window.open(
    		
      'worksheetAddChild.php?childNum='+childInputNumber+'&provId='+provId,  	// the url to the php script
      'Add_New_Child',                            			// some name for the window
      'status=0,width=1000,height=1000'                    	// display options for the window
    );
  return false;

}

function showAddExistingChild(childInputNumber,provId) 
{
		
    window.open(
    		
      'worksheetAddExistingChild.php?childNum='+childInputNumber+'&provId='+provId,  					// the url to the php script
      'Add_Existing_Child',                            			// some name for the window
      'status=0,width=1000,height=1000'                    	// display options for the window
    );
  return false;

}

function showUpdateSingleChild(childId) 
{
	window.location.href = 'worksheetUpdateSingleChild.php?childId='+childId
	//self.close();
   // window.open(
//    		
//      'worksheetUpdateSingleChild.php?childId='+childId,  					// the url to the php script
//      'Update_Single_Child',                            			// some name for the window
//      'status=0,width=1000,height=1000'                    	// display options for the window
//    );
//   self.close();
  return false;

}

function returnChildValues (fname,lname,month,day,year,registered,immunized,id)
{
	/*
	 * Debug
	 *alert("In My Function!");
	 *alert("The Day value is: " +day);
	 *alert("The id value is: " + id);
	*/
	
	try
	{
		/*
		 * Debug
		 * alert("idval is " + idval);
		 * alert("Parent Window Value for month: " + window.opener.document.getElementById('child'+idval+'ageMonth').value);
		*/
		
		var idval = id;
		var monthSelect = 	window.opener.document.getElementById('child'+idval+'ageMonth');
		var daySelect = 	window.opener.document.getElementById('child'+idval+'ageDay');
		var yearSelect = 	window.opener.document.getElementById('child'+idval+'ageYear');
		var regRadio = 		window.opener.document.getElementsByName('child'+idval+'reg');
		var immuRadio = 	window.opener.document.getElementsByName('child'+idval+'immunized');
		var firstNameText =	window.opener.document.getElementById('child'+idval+'firstname');
		var lastNameText = 	window.opener.document.getElementById('child'+idval+'lastname');
		//window.opener.document.getElementById('child'+idval+'firstname').value = fname;
		//window.opener.document.getElementById('child'+idval+'lastname').value = lname;
		
		setCheckedValue(immuRadio, immunized);
		setCheckedValue(regRadio, registered);
		setSelectValue(monthSelect, month);
		setSelectValue(daySelect, day);
		setSelectValue(yearSelect, year);
		setTextBoxValue(firstNameText,fname);
		setTextBoxValue(lastNameText,lname);

	}
	catch(err)
	{
		alert("There has been an error: " + err.description + ". \n This window will now close.");
		window.close();
	}
	
	
	window.close();
}

function returnChildValuesGrandparent (fname,lname,month,day,year,registered,immunized,id)
{
	/*
	 * Debug
	 *alert("In My Function!");
	 *alert("The Day value is: " +day);
	 *alert("The id value is: " + id);
	*/
	var idval = id;
	this.test(idval);
	var URL = 	parent.parent.location;
	this.test(URL);
	try
	{
		/*
		 * Debug
		 * alert("idval is " + idval);
		 * alert("Parent Window Value for month: " + window.opener.document.getElementById('child'+idval+'ageMonth').value);
		*/
		
		var idval = id;
		var monthSelect = 	window.opener.parent.document.getElementById('child'+idval+'ageMonth');
		var daySelect = 	window.opener.parent.document.getElementById('child'+idval+'ageDay');
		var yearSelect = 	window.opener.parent.document.getElementById('child'+idval+'ageYear');
		var regRadio = 		window.opener.parent.document.getElementsByName('child'+idval+'reg');
		var immuRadio = 	window.opener.parent.document.getElementsByName('child'+idval+'immunized');
		var firstNameText =	window.opener.parent.document.getElementById('child'+idval+'firstname');
		var lastNameText = 	window.opener.parent.document.getElementById('child'+idval+'lastname');
		//window.opener.document.getElementById('child'+idval+'firstname').value = fname;
		//window.opener.document.getElementById('child'+idval+'lastname').value = lname;
		
		//setCheckedValue(immuRadio, immunized);
		//setCheckedValue(regRadio, registered);
		//setSelectValue(monthSelect, month);
		//setSelectValue(daySelect, day);
		//setSelectValue(yearSelect, year);
		setTextBoxValue(firstNameText,fname);
		setTextBoxValue(lastNameText,lname);

	}
	catch(err)
	{
		alert("There has been an error: " + err.description + ". \n This window will now close.");
		window.close();
	}
	
	
	window.close();
}

function test(input)
{
	
	alert("The Test function value input is: " +input);
}

function setCheckedValue(radioObj, newValue)
{
	if(!radioObj)
	{
		return;
	}
	var radioLength = radioObj.length;
	if(radioLength == undefined) 
	{
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) 
	{
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString())
		{
			radioObj[i].checked = true;
		}
	}
}

function setSelectValue(SelectName, Value) 
{
	if(!SelectName)
	{
		return;
	}
	 var selectLength = SelectName.length;
	 if(selectLength == undefined)
	 {
		SelectName.selectedIndex = (SelectName.value == Value.toString());
		return;
	 }
	for(index = 0;  index < SelectName.length; index++) 
		{
		   if(SelectName[index].value == Value)
			   SelectName.selectedIndex = index;
		}
}

function setTextBoxValue(textName, Value)
{
	if(!textName)
	{
		return;
	}
	
	textName.value = Value;
}



