

/* ********** jQuery ********** */


if(typeof $ == "undefined"){
	$ = function(){};
	LoadJs("/scripts/jquery.js");
}


/* ********** AJAX ********** */


// function ajaxObject()
function ajaxObject() {
	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){
	try { return new XMLHttpRequest(); } catch(e){
	throw new Error( "This browser does not support XMLHttpRequest." );
	}}}}}
}


// function ajax_request()
function ajax_request(url, div_processing){
	if(div_processing != null && div_processing != ""){
		if(document.getElementById(div_processing) != null){
			document.getElementById(div_processing).style.display = "inline";
		}
	}
	
	var response = ajax_request2(url, "GET", "", "", "", "", "");
	
	if(div_processing != null && div_processing != ""){
		if(document.getElementById(div_processing) != null){
			document.getElementById(div_processing).style.display = "none";
		}
	}
	
	return response;
}


// function ajax_request2()
function ajax_request2(url, method, params, async_callback, last_modified, optional_headers){
	if(method != "POST"){
		method = "GET";
		params = null;
	}
	if(last_modified == ""){last_modified = null;}
	//if(optional_headers == ""){optional_headers = null;}
	
	var request = new ajaxObject();
	
	var async = !(async_callback == null || async_callback == undefined || async_callback == "" || async_callback == false);
	if(async){
		request.onreadystatechange = function(){
			if(request.readyState == 4){
				var response = request.responseText;
				eval(async_callback);
			}
		}
	}
	
	request.open(method, url, async);
	
	if(last_modified != null){	
		request.setRequestHeader("If-Modified-Since", last_modified);
	}
	
	//if(optional_headers != null && optional_headers != ""){
		//for(var i in optional_headers){
			// request.setRequestHeader(i, optional_headers[i]);
		//}
	//}
	
	if(method == "POST"){
		request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		request.setRequestHeader("Content-length", params.length);
		request.setRequestHeader("Connection", "close");	
	}
	
	request.send(params);
	
	if(!async){
		if(!request.getResponseHeader("Date") && last_modified == null){
			var cached = request;
			last_modified = cached.getResponseHeader("Last-Modified");
			last_modified = (last_modified) ? last_modified : new Date(0); // January 1, 1970
			request = ajax_request2(url, method, params, async, last_modified);
			if(request.status == 304){
				request = cached;
			}
		}
		
		var response = request.responseText;
	}
	
	return response; // undefined if async
}


// function ajax()
function ajax(name, action, variables, async_callback){
	var div_processing = '';
	var div_response = '';
	
	if(name != null && name != ""){
		if(document.getElementById('div_processing_'+name) != null){
			div_processing = 'div_processing_'+name;
		}
		if(document.getElementById('div_response_'+name) != null){
			div_response = 'div_response_'+name;
		}
	}
	
	if(div_response != ""){ document.getElementById(div_response).innerHTML = ""; }
	
	var url = "?ajax="; // +action
	if(action.substring(0,1) == "?"){
		url += action.substring(1,action.length);
	} else {
		url = "/ajax.php"+url+action;
	}
	if(!!variables && variables.length > 0){
	  for(var i=0; i<variables.length; i++){
	    if(i%2 == 0){url += "&"+variables[i]+"=";}
		else{url += variables[i];}
	  }
	}
	
	if(div_processing != ""){ document.getElementById(div_processing).style.display = "inline"; }
	
	var finish = "";
	if(div_processing != ""){ finish += "document.getElementById('"+div_processing+"').style.display = 'none';" }
	if(div_response != ""){ finish += "document.getElementById('"+div_response+"').innerHTML = response;" }
	
	var async = !(async_callback == null || async_callback == undefined || async_callback == "" || async_callback == false);
	var async_callback_new = "";
	if(async){
		async_callback_new = finish;
		if(async_callback != true){
			async_callback_new += async_callback;
		}
	}
	
	var response = ajax_request2(url, "GET", "", async_callback_new, "", "", "");
	
	if(!async){
		eval(finish);
	}
	
	return response; // undefined if async
}


/* **********  ********** */


// function AddEventHandler()
function AddEventHandler(exec, object, event_type, exec_return){
	var new_handler = function(event){
		if (typeof exec == 'function') {
			return_val = exec(event);
		} else {
			return_val = eval(exec);
		}
		if(return_val != null){ return return_val; }
	}
	
	if (object.addEventListener && !exec_return) {
		object.addEventListener(event_type, new_handler, false);
	} else if (object.attachEvent && !exec_return) {
		object.attachEvent("on"+event_type, new_handler);
	} else {
		var old_handler = eval("object.on"+event_type);
		var new_handler2 = function(event){
			if (typeof old_handler == 'function') {
				return_val_old = old_handler(event);
			}
			return_val_new = new_handler(event);
			if(typeof return_val_new != "undefined" && return_val_new != null && return_val_new != undefined){
				return return_val_new;
			}
			if(typeof return_val_old != "undefined" && return_val_old != null && return_val_old != undefined){
				return return_val_old;
			}
		}
		eval("object.on"+event_type+" = new_handler2");
	}
}


// function AddOnload()
function AddOnload(exec, object){
	if(!object){ object = window; }
	AddEventHandler(exec, object, "load");
}


// function LoadJs()
function LoadJs(src, object, onload){
	if(!object){ object = document.getElementsByTagName("head")[0]; } // document.body
	var element = document.createElement("script");
	element.type = "text/javascript";
	element.src = src;
	object.appendChild(element);
	if(onload){ AddOnload(onload, element); }
	return element;
}



/* **********  ********** */


// function _GET()
function _GET(key){
	value = false;
	ex = new RegExp("[\?&]"+key+"=", ""); 
	a = location.search.split(ex);
	if(a[1]){
		a = a[1].split("&");
		value = a[0];
	}
	return value;
}


// function StripNonDigits()
function StripNonDigits(value){
	value += ""; // make into string
	return value.replace(/[^0-9]/g, '');
}


// function is_int()
function is_int(input, strict){
	result = ( !isNaN(input) && parseInt(input)==input );
	if(strict==true && typeof(input)!='number'){ result = false; }
	return result;
}


// function is_float()
function is_float(input){
	input = input.replace('.', '');
	result = is_int(input);
	return result;
}


// function ValidUSPhone()
function ValidUSPhone(value){
	value += ""; // make into string
	value = StripNonDigits(value);
	valid = value.match(/^[0-9]{10}$/);
	valid = (valid != null);
	return valid;
}


// function ValidEmail()
function ValidEmail(value){
	value += ""; // make into string
	valid = value.match(/^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i);
	valid = (valid != null);
	return valid;
}

// function FixEntities()
function FixEntities(str){
	str = str.replace(/\&amp;/g,'&');
	str = str.replace(/\&lt;/g,'<');
	str = str.replace(/\&gt;/g,'>');
	str = str.replace(/\&#39;/g,"'");
	return str;
}


// function GetElementPosition()
function GetElementPosition(element){
	var position = [0, 0, 0, 0];
	if(element == null || element == undefined){
		return position;
	}
	for(var i=0; element != null; i++){
		// Check for style position
		position[0] += element.offsetLeft;
		position[1] += element.offsetTop;
		if(i==0){
			position[2] += element.offsetWidth;
			position[3] += element.offsetHeight;
		}
		if(element.offsetParent != null && element.offsetParent != undefined){
			element = element.offsetParent;
		}
		else{
			break;
		}
	}
	return position;
}


/* **********  ********** */


// function SelectAll()
function SelectAll(id){
	document.getElementById(id).focus();
	document.getElementById(id).select();
}


// function select_radio()
function select_radio(radio, option_value){
	if(radio == null){return;}
	var radio_length = radio.length;
	if(radio_length == undefined){
		radio.checked = (radio.value == option_value);
		return;
	}
	for(var i=0; i<radio_length; i++){
		if(radio[i].value == option_value){
			radio[i].checked = true;
		}
		else{
			radio[i].checked = false;
		}
	}
}


// function select_option()
function select_option(select, option_value){
	if(select == null){return;}
	var options_length = select.options.length;
	for(var i=0; i<options_length; i++){
		if(select.options[i].value == option_value){
			//select.options[i].selected = true;
			select.selectedIndex = i;
			return select.options[i];
		}
		else{
			//select.options[i].selected = false;
		}
	}
}


// function toggle_checkbox()
function toggle_checkbox(checkbox){
	if(checkbox == null){return;}
	checkbox.checked = !checkbox.checked;
}


// function jq_toggle_checkbox_span()
function jq_toggle_checkbox_span(span, type){
	$(span).parent().children(':'+type).each( function(){ $(this).attr('checked', (type=="radio" || !$(this).attr('checked')) ); } );
}


/* **********  ********** */


// function track_url()
var tracked_urls = new Array();
function track_url(url, once){
	var track = true;
	if(once != null && once == true){
		if(tracked_urls[url] != null){
			track = false;
		}
		else{
			 tracked_urls[url] = true;
		}
	}
	if(track == true){
		ajax_request('script_tracking.php?url='+url)
	}
}


/* **********  ********** */


// function popUpVideo()
function popUpVideo(URL, width, height){
	day = new Date();
	id = day.getTime();
	if(width == null || width == undefined || width == "" || width == 0){width = 685;}
	if(height == null || height == undefined || height == "" || height == 0){height = 480;}
	eval("page" + id + " = window.open(URL, id, 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width="+width+",height="+height+"');"); 
}


// function search_sort()
function search_sort(sort_value){
	if(document.form_select){
		if(document.form_select.sort.options[document.form_select.sort.selectedIndex].value == sort_value){
			order_index = document.form_select.order.selectedIndex + 1;
			if(order_index == document.form_select.order.options.length){ order_index = 0; }
			select_option(document.form_select.order, document.form_select.order.options[order_index].value);
		} else {
			sort_option = select_option(document.form_select.sort, sort_value);
			sort_option.onclick();
		}
		document.form_select.submit.click();
	}
}


// function ToggleNotesPopup()
function ToggleNotesPopup(div_id, div_type, close_others){
	//document.getElementById('div_notespopup_'+div_id).style.left = document.getElementById('span_notespopup_'+div_id).style.left;
	if(div_type == null){
		div_type = "hover";
	}
	if(close_others == null){
		if(div_type == "normal"){
			close_others = true;
		}
		else{
			close_others = false;
		}
	}
	if(document.getElementById('div_notespopup_'+div_id).style.visibility == 'visible'){
		if(div_type == "normal"){
			document.last_div_normal = null;
		}
		document.getElementById('div_notespopup_'+div_id).style.visibility = 'hidden';
		document.getElementById('div_notespopup_'+div_id).style.display = 'none';
	}
	else{
		if(close_others == true){
			if(document.last_div_normal != null){
				document.getElementById('div_notespopup_'+document.last_div_normal).style.visibility = 'hidden';
				document.getElementById('div_notespopup_'+document.last_div_normal).style.display = 'none';
			}
			document.last_div_normal = div_id;
		}
		document.getElementById('div_notespopup_'+div_id).style.visibility = 'visible';
		document.getElementById('div_notespopup_'+div_id).style.display = 'inline';
		
		// calculate top and left
		var new_top = 0;
		var new_left = 0;
		var element = document.getElementById('span_notespopup_'+div_id);
		while(element.offsetParent != null){
			if(element.id != null && element.id.substring(0, 15) == "div_notespopup_"){break;}
			new_top += element.offsetTop;
			new_left += element.offsetLeft;
			element = element.offsetParent;
		}
		
		document.getElementById('div_notespopup_'+div_id).style.top = new_top+"px";
		document.getElementById('div_notespopup_'+div_id).style.left = new_left+"px";
	}
}


// function ValidateFormLead()
function ValidateFormLead(form, type, phone_optional){
	errors = "";
	if(type == "infusion"){
		name_field = "Contact0FirstName";
		email_field = "Contact0Email";
		phone_field = "Contact0Phone1";
	} else {
		name_field = "name";
		email_field = "from";
		phone_field = "custom Phone";
	}
	name = $("input[name="+name_field+"]",form).val();
	email = $("input[name="+email_field+"]",form).val();
	phone = $("input[name="+phone_field+"]",form).val();
	if(name == ""){ errors += "\nMissing: Your Name"; }
	if(email == ""){ errors += "\nMissing: Your Email"; }
	else if(!ValidEmail(email)){ errors += "\nInvalid: Your Email"; }
	if(phone != undefined && phone_optional.length == 0){
		if(phone == ""){ errors += "\nMissing: Your Phone"; }
		//else if(!ValidUSPhone(phone)){ errors += "\nInvalid: Your Phone"; }
		else if(StripNonDigits(phone).length < 3){ errors += "\nInvalid: Your Phone"; }
	}
	if(errors != ""){
		alert(errors.substring(1,errors.length));
		return false;
	}
	return true;
}


/* ********** onLoad Events ********** */

//LoadJs("/scripts/include_js_onload.js.php");

// function OnLoadNotesPopup()
function OnLoadNotesPopup(){
	$('.notespopup_container_hover').each(function () {
		var distance = 10;
		var time = 250;
		var hideDelay = 100;

		var hideDelayTimer = null;

		var beingShown = false;
		var shown = false;
		var container = this;
		var trigger = $(this).children('.notespopup_trigger');
		var info = $(this).children('.notespopup_popup').css('opacity', 0);


		$(container).mouseenter(function (event) {
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
			if (beingShown || shown) {
				// don't trigger the animation again
				return;
			} else {
				container.new_top = event.pageY + distance;
				container.new_left = event.pageX - 30;
				
				popup_parents = $(container).parents('.notespopup_popup');
				if(popup_parents.length>0){
					popup_parents.each(function(){
						container.new_top -= $(this).css("top").replace("px","");
						container.new_left -= $(this).css("left").replace("px","");
					});
				}
				
				// reset position of info box
				beingShown = true;

				info.css({
					top: container.new_top,
					left: container.new_left,
					display: 'block',
					zIndex: 1000+(popup_parents.length*10)
				}) .animate({
					top: '-=' + distance + 'px',
					opacity: 1
				}, time, 'swing', function() {
					beingShown = false;
					shown = true;
				})  ;
			}

			return false;
		}).mouseleave(function () {
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
			hideDelayTimer = setTimeout(function () {
				hideDelayTimer = null;
				info.animate({
					top: '-=' + distance + 'px',
					opacity: 0
				}, time, 'swing', function () {
					shown = false;
					info.css('display', 'none');
				});

			}, hideDelay);

			return false;
		});
	});
}

AddOnload(OnLoadNotesPopup);

