// JavaScript Document

var searchresults

window.onload = init;

function init()
{
	var searchinput = document.getElementById('smart');
	searchresults = document.getElementById('ssresults');
	
	searchinput.onkeyup = searchinput.onfocus = function()
	{
		sendsmartSearch(searchinput.value);
	}
	
	searchresults.onblur = searchinput.onblur = function()
	{
		hideThatMenu = setTimeout("hideMenu()",500)
	}

}

function pullsmartSearch()
{
	if( xmlHttp.readyState == 4 )
	{
		searchresults = document.getElementById('ssresults');
		var seeall = document.getElementById('smart')
		var results = ''
		var matches = ''
		var sMatch = ''
		 matches = xmlHttp.responseText.split('*|*');
		 
		for( i=0;i<(matches.length-1);i++ )
			{	
				sMatch = matches[i].split("||");
				results += '<a href="pageview.asp?id='+sMatch[0]+'">'+sMatch[1]+'</a>'
			}
		results += '<a class="all" href="smartSearch.asp?ssphrase='+seeall.value+'">See all results</a>'
		searchresults.innerHTML = results
		
		if( results.value != '' )
		{
			delaymenu = setTimeout("showMenu()",3000);
		}
		//else
//		{
//			searchresults.style.display = 'none'
//		}
	}
}

function sendsmartSearch(searchphrase)
{
	checkAJAX();
	//alert(searchphrase)
	var url = "/Scripts/smartsearch.ajax.asp";
	url += "?smart="+searchphrase;
	xmlHttp.onreadystatechange = pullsmartSearch;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function showMenu()
{
	searchresults = document.getElementById('ssresults');
	searchresults.style.display = 'block'
}

function hideMenu()
{
	var hide = document.getElementById('smart')
	searchresults = document.getElementById('ssresults');
	
	searchresults.style.display = 'none'
	hide.value = ''
			
}

// find Y position
function findPosY(obj)
{
	var curtop = 0;
	if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
}

function openNews(id)
{
	news = document.getElementById(id)
	win = document.getElementById("openclosewindow")
	news.style.display = ( news.style.display == "none" ) ? "block" : "none";
	win.innerHTML = 'click to close'
}

function openSubNav(id)
{
	subnav = document.getElementById(id)
	subnav.style.display = ( subnav.style.display == "none" ) ? "block" : "none";
}

function jobSearch()
	{
		var jobinput = document.getElementById('jobinput').value
		//jobinput.onclick = function()
//		{
			document.location.href='/Scripts/genericSearch.asp?action=jobsearch&value='+jobinput	
	}


// browser detection
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: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			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.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

//function getElementByClassName(findclass,form)
//{
//	var tag=document.body.getElementsByTagName('*');
//	var errormsg = document.getElementById('errormsg')
//	errormsg.style.display = 'block'
//	for(var i=0; i<tag.length; i++) 
//	{
//		if(tag[i].className == findclass)
//			{
//				tag[i].classname == 'ERROR'
//				//errormsg.style.display = 'block'
//				return false;
//			}
//		return false;
//	}
//	return false;
//}
//				

function validate(form,findclass)
{
	
	var tag=document.body.getElementsByTagName('*');
	var errormsg = document.getElementById('errormsg')
	var defaultErrMsg = errormsg.innerHTML;
	var txt = ''
	for(var i=0; i<tag.length; i++) 
	{
		if(tag[i].className == findclass)
			{
				//tag[i].style.className = 'ERROR'
					//confirm(tag[i].name)
				if(tag[i].value == '')
				{
					errormsg.style.display = "block"
					tag[i].style.border = '#C00 solid 3px'
					tag[i].focus();
					return false;
				}
			}
	}
	
		
	//sendForm()
}

var xmlHttp

function babyAJAX(oForm){
	
	xmlHttp=GetXmlHttpObject();
	if(xmlHttp==null){
  		alert ("Your browser does not support AJAX!");
  		return;
  	}
	
	var url="babySearch.ajax.asp?1=1";
	url=url+"&type=" + oForm.babySearchMethod.value;
	url=url+"&search=" + oForm.babyNameInput.value;
	
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function babyAJAXViewAll(){
	
	var oForm = document.getElementById('babyForm');
	
	xmlHttp=GetXmlHttpObject();
	if(xmlHttp==null){
  		alert ("Your browser does not support AJAX!");
  		return;
  	}
	
	var url="babySearch.ajax.asp?1=1";
	url=url+"&type=Parents";
	url=url+"&search=";
	
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	
}

function stateChanged() { 
	if(xmlHttp.readyState==4){ 
		document.getElementById("babyContainer").innerHTML= xmlHttp.responseText;
	}
}




function nextBaby(theMax){
	var temp = getByClass('babyRow');
	var currentContainer = parseInt(temp.getAttribute('container'));
	if(currentContainer<parseInt(theMax)){
		hideBabies(currentContainer);
		showBabies(currentContainer + 1);
	}else{
		return;
	}

}
function previousBaby(){
	var temp = getByClass('babyRow');
	var currentContainer = parseInt(temp.getAttribute('container'));
	if(currentContainer > 1){
		hideBabies(currentContainer);
		showBabies(currentContainer - 1);		
	}else{
		return;
	}
}
function thisBaby(theBaby){
	var temp = getByClass('babyRow');
	var currentContainer = parseInt(temp.getAttribute('container'));
	if(parseInt(theBaby)==currentContainer){
		return;
	}else{
		hideBabies(currentContainer);
		showBabies(theBaby);
	}
}

function getByClass(theClass){
	
	var allDivTags = new Array();
	var allDivTags=document.getElementsByTagName("div");
	
	for (i=0; i<allDivTags.length; i++) {
		if(allDivTags[i].className == theClass){
			return allDivTags[i];
		}
	}	
}

function hideBabies(theNum){
	
	var allDivTags = new Array();
	var allDivTags=document.getElementsByTagName("div");
	
	for (i=0; i<allDivTags.length; i++) {
		if(allDivTags[i].getAttribute('container') == theNum){
			allDivTags[i].className = 'babyRow hidden';
		}
	}	
	
}

function showBabies(theNum){
	
	var allDivTags = new Array();
	var allDivTags=document.getElementsByTagName("div");
	
	for (i=0; i<allDivTags.length; i++) {
		if(allDivTags[i].getAttribute('container') == theNum){
			allDivTags[i].className = 'babyRow';
		}
	}	
	
}

function patientVal(form,findclass)
{
	//alert("yes")
	var tag=document.body.getElementsByTagName('*');
	var errormsg = document.getElementById('errormsg')
	var txt = ''
	
	//Accept Privacy Policy
	var accPP = document.getElementById('acceptPP')
	if(accPP.checked == false)
	{
		errormsg.style.color = "#FF0000"
		errormsg.innerHTML = "You must accept the Privacy Policy to continnue with our online registration."
		return false;
	}
	
	//loop thorugh all the tags 
	for(var i=0; i<tag.length; i++) 
	{
		//Generic REQUIRED Feilds
		if(tag[i].className == findclass)
			{
				if(tag[i].value == '')
				{
					errormsg.style.color = "#FF0000"
					errormsg.innerHTML = "There were errors found on this page. Please correct the selected boxes before continuing to the next page."
					tag[i].style.border = '#C00 solid 3px'
					tag[i].focus();
					return false;
				}
			}
			
	
	}
	//sendForm()
}




/*************************************************************
All of the following function deal with the registration form
*************************************************************/


//This function determines which validation to run and moves to next part of form if the validation passes
function regNext(form,val){
	
	var pass;
	var errormsg = document.getElementById("errormsg");
	
	//determine which validation function to run dependent on which section of form we are on
	switch (val - 1)
	{
		case 1:
			pass = validate1(form,errormsg);
			break;
		case 2:
			pass = validate2(form,errormsg);
			break;
		case 3:
			pass = validate3(form,errormsg);
			break;
		case 4:
			pass = validate4(form,errormsg);
			break;
	}
	
	//If validation passes then move on to next part of form and hide the error div if its showing
	if(pass==true){
		document.getElementById("errormsg").style.display = 'none';
		document.getElementById("regForm" + (val - 1)).style.display = 'none';
		document.getElementById("regForm" + (val)).style.display = 'block';
	}
}

function regPrevious(val){
	document.getElementById("regForm" + val).style.display = 'none';
	document.getElementById("regForm" + (val - 1)).style.display = 'block';
}

//This function is the validation to first part of registration form
function validate1(form,errormsg){
	//validate part one, return false if validation fails
	if(validateCheck(form.acceptPP,errormsg,'You must accept the privacy policy before you can continue')==false||
	validateDate(form.appointdate,errormsg,'Please enter a valid date')==false||
	validateInput(form.appointtime,errormsg,'Please select an appointment time')==false||
	validateInput(form.physician,errormsg,'Please enter attending physician')==false||
	validateInput(form.familyphysician,errormsg,'Please enter family physician')==false){
		return false;
	}
	return true;
}

//This function is the validation to second part of registration form
function validate2(form,errormsg){
	//validate part two, return false if validation fails
	if(validateInput(form.fname,errormsg,'Please enter a first name')==false||
	validateInput(form.mname,errormsg,'Please enter a middle name')==false||
	validateInput(form.lname,errormsg,'Please enter a last name')==false||   
	validateInput(form.street_address,errormsg,'Please enter an address')==false||
	validateInput(form.city,errormsg,'Please enter a city')==false||
	validateZip(form.zip,errormsg,'Please enter a valid zip code')==false||
	validatePhone(form.hphone,errormsg,'Please enter a valid phone number')==false||
	validateSocial(form.s,errormsg,'Please enter a valid social security number')==false||										 
	validateDate(form.dob,errormsg,'Please enter date of birth in a valid format')==false||
	validateInput(form.marital,errormsg,'Please select a marital status')==false||
	validateCheckDependent(form.guarantoryes,form.guarantorfname,errormsg,'If the guarantor is not the same as the patient, please supply the desired guarantor information',false)==false||
	validateCheckDependent(form.guarantoryes,form.guarantormname,errormsg,'If the guarantor is not the same as the patient, please supply the desired guarantor information',false)==false||
	validateCheckDependent(form.guarantoryes,form.guarantorlname,errormsg,'If the guarantor is not the same as the patient, please supply the desired guarantor information',false)==false||
	validateCheckDependent(form.guarantoryes,form.guarantorstate,errormsg,'If the guarantor is not the same as the patient, please supply the desired guarantor information',false)==false||
	validateCheckDependent(form.guarantoryes,form.gaurantorphone,errormsg,'If the guarantor is not the same as the patient, please supply the desired guarantor information',false)==false){
		return false;
	}
	return true;
}


function validate3(form,errormsg){
	if(validateCheckGroup(form.advdir,errormsg,'Please select an advanced directive option')==false||
	validateCheckGroup(form.advdironfile,errormsg,'Please select a prevous file option')==false){
		return false;
	}
	return true;
}
function validate4(form,errormsg){
	if(validateCheckGroup(form.visitduetoaccident,errormsg,'Please select an accident option')==false||
	validateCheckGroup(form.workrelated,errormsg,'Please select a work related option')==false||
	validateCheckDependent(form.visitduetoaccident[0],form.accidentdate,errormsg,'If accident is work related please provide the request work information',true)==false||
	validateCheckGroup(form.workerscomp,errormsg,'Please select an option from workers comp')==false||
	validateCheckDependent(form.visitduetoaccident[0],form.workersaddress,errormsg,'If accident is work related please provide the requested work information',true)==false||
	validateCheckDependent(form.visitduetoaccident[0],form.workerscity,errormsg,'If accident is work related please provide the requested work information',true)==false||
	validateCheckDependent(form.visitduetoaccident[0],form.workersstate,errormsg,'If accident is work related please provide the requested work information',true)==false||
	validateCheckDependent(form.visitduetoaccident[0],form.workerszip,errormsg,'If accident is work related please provide the requested work information',true)==false){
		return false;
	}
	return true;
}