﻿//----------Popup Functions----------
function popuptopicfooter(title,topic,w,h,scrollbars)
{
    newPopup = window.open('popup.aspx?title=' + title + '&topic=' + topic,'Popup17906','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=' + scrollbars + ',resizable=no,copyhistory=no,width=' + w + ',height=' + h);
    newPopup.focus();
    return (true);
}

$(function(){
	//General Popup
	$("a[@rel~='popup']").click(function(){
		var d=window.open($(this).attr("href"), 'popup', 'width=650, height=500, scrollbars=yes');
		d.focus();
		return false;
	});
	// External Pages Popup
	$("a[@rel~='external_popup']").click(function(){
		var e=window.open($(this).attr("href"), 'external_popup', 'width=650, height=550, scrollbars=yes');
		e.focus();
		return false;
	});
	
	//More Details Popup
	$("a[@rel~='details_popup']").click(function(){
		var m=window.open($(this).attr("href"), 'details_popup', 'width=550, height=450, scrollbars=yes');
		m.focus();
		return false;
	});
	
	//Assurance Popup 
	$("a[@rel~='assurance_popup']").click(function(){
		var d=window.open($(this).attr("href"), 'assurance_popup', 'width=530, height=425, scrollbars=yes');
		d.focus();
		return false;
	});
});


//----------Switches Visibility on Items----------
function switchVisibility(obj) {
    var el = document.getElementById(obj);

    if ( el.style.display != "none" ) {
        el.style.display =
        'none';
    }

    else {
        el.style.display =
        '';
    }
}

function switchVisibilityCheckbox(obj, checkbox) {
    var el = document.getElementById(obj);

    if ( el.style.display != "none" && document.getElementById(checkbox).checked == true) {
        el.style.display =
        'none';
    }

    else if (document.getElementById(checkbox).checked == false) {
        el.style.display =
        '';
    }
}

function switchVisibilityCheckout(obj, checkbox) {
    var el = document.getElementById(obj);

    var style_attrib = window.document.createAttribute("style");
        style_attrib.nodeValue = "display: none;";
    //scDiv.attributes.setNamedItem(style_attrib);

    if (document.getElementById(checkbox).checked == true) {
        el.style.display =
        'none';
    }

    else if (document.getElementById(checkbox).checked == false) {
        el.style.display =
        '';
    }
}

//Used for Other box on checkout (for Dentist Referral)
function setClassName(objId, className) {
    	document.getElementById(objId).className = className;
} 
function checkOther(myvalue) {
	if(myvalue == "My Dentist"){
		setClassName("otherbox", "visible");		
	} else {
		setClassName("otherbox", "hidden");		
	}
}

//----------Input Field from 456BereaStreet--------
/**
 * Copy the value of an input field's title attribute to its value attribute.
 * Clear the input field on focus if its value is the same as its title.
 * Repopulate the input field on blur if it is empty.
 * Hide the input field's associated label if it has one.
 */
var autoPopulate = {
	sInputClass:'populate', // Class name for input elements to autopopulate
	sHiddenClass:'structural', // Class name that gets assigned to hidden label elements
	bHideLabels:true, // If true, labels are hidden
	/**
	 * Main function
	 */
	init:function() {
		// Check for DOM support
		if (!document.getElementById || !document.createTextNode) {return;}
		// Find all input elements with the given className
		var arrInputs = autoPopulate.getElementsByClassName(document, 'input', autoPopulate.sInputClass);
		var iInputs = arrInputs.length;
		var oInput;
		for (var i=0; i<iInputs; i++) {
			oInput = arrInputs[i];
			// Make sure it's a text input
			if (oInput.type != 'text') { continue; }
			// Hide the input's label
			if (autoPopulate.bHideLabels) { autoPopulate.hideLabel(oInput.id); }
			// If value is empty and title is not, assign title to value
			if ((oInput.value == '') && (oInput.title != '')) { oInput.value = oInput.title; }
			// Add event handlers for focus and blur
			autoPopulate.addEvent(oInput, 'focus', function() {
				// If value and title are equal on focus, clear value
				if (this.value == this.title) {
					this.value = '';
					this.select(); // Make input caret visible in IE
				}
			});
			autoPopulate.addEvent(oInput, 'blur', function() {
				// If the field is empty on blur, assign title to value
				if (!this.value.length) { this.value = this.title; }
			});
		}
	},
	hideLabel:function(sId) {
		var arrLabels = document.getElementsByTagName('label');
		var iLabels = arrLabels.length;
		var oLabel;
		for (var i=0; i<iLabels; i++) {
			oLabel = arrLabels[i];
			if (oLabel.htmlFor == sId) {
				oLabel.className = oLabel.className + ' ' + autoPopulate.sHiddenClass;
			}
		}
	},
	/**
	 * getElementsByClassName function included here for portability.
	 * Remove if you are already using one.
	 * Written by Jonathan Snook, http://www.snook.ca/jonathan
	 * Add-ons by Robert Nyman, http://www.robertnyman.com
	 */
	getElementsByClassName:function(oElm, strTagName, strClassName) {
	    var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName);
	    var arrReturnElements = new Array();
	    strClassName = strClassName.replace(/\-/g, "\\-");
	    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	    var oElement;
	    for(var i=0; i<arrElements.length; i++){
	        oElement = arrElements[i];      
	        if(oRegExp.test(oElement.className)){
	            arrReturnElements.push(oElement);
	        }   
	    }
	    return (arrReturnElements)
	},
	/**
	 * addEvent function included here for portability.
	 * Remove if you are already using an addEvent/DOMReady function.
	 * Found at http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html
	 */
	addEvent:function(obj, type, fn) {
		if (obj.addEventListener)
			obj.addEventListener(type, fn, false);
		else if (obj.attachEvent) {
			obj["e"+type+fn] = fn;
			obj[type+fn] = function() {obj["e"+type+fn](window.event);}
			obj.attachEvent("on"+type, obj[type+fn]);
		}
	}
};

/**
 * Init on window load.
 * Replace this with a call to your own addEvent/DOMReady function if you use one.
 */
autoPopulate.addEvent(window, 'load', autoPopulate.init);



//-------------Validate Function For Quantity Selection Kit Group-------------
function validatebox()
{
	var msg1='Please verify your quantities and select exactly';
    var kitgroup=document.forms['kit'].elements['kgid'];
    
    var kid=kitgroup.value;
    var kitgroupid=new Array();
    kitgroupid=kid.split(",");
   
	for(var j=0; j<kitgroupid.length-1; j++)
	{
	    var gid=document.forms['kit'].elements['groupid_' + kitgroupid[j]];
	    group_id=gid.value;
	    var mid=document.forms['kit'].elements['max_number_' + kitgroupid[j]];
	    max_number=mid.value;
	    var tid=document.forms['kit'].elements['textboxid_' + kitgroupid[j] ];
	    var textboxid1=tid.value
	    var mval=document.forms['kit'].elements['max_value_' + kitgroupid[j] ];
	    var max_value=parseInt(mval.value);
	    var ids =new Array();
	    ids=textboxid1.split(",");
	    var sum=0;
	    var i=0; 
	      
	    while(max_number > 0)
	    {
		    if(ids[i]!="")
		    {
			    var value='KitGroupID_' + group_id + '_TextOption_' + ids[i];
			    var sum1=document.forms['kit'].elements['KitGroupID_' + group_id + '_TextOption_' + ids[i]];
			    i=i+1;
	            if (sum1.value!="")
                {
          	        sum=sum + parseInt(sum1.value);
			    }
			    max_number=max_number-1;
		    }
	    }
	
	    if (parseInt(sum) < parseInt(max_value) )
	    {
		    window.alert(msg1 + ' ' + max_value + '.');
	    	return false;
	    }
	    else if (parseInt(sum) > parseInt(max_value) )
	    {
		    window.alert(msg1 + ' ' + max_value + '.');
	   		return false;
	    }
	    
	    
	    if(ids.length>=0)
	    {
	    	ids.pop();
	    }
	    
	    sum=0;
	    group_id=0;
		max_number=0;
		max_values=0;
	}
}



$(document).ready(function(){
	// subnav magic.
	$("#navbar ul li").hover(function(){
		$(this).children().filter("ul").css("display","block");
		$("#need-assistance").css("z-index","90");
	}, function(){
		$(this).children().filter("ul").css("display","none");
	});
});


$(document).ready( function() {
   
    $('A[rel="external"]').click( function() {
        window.open( $(this).attr('href') );
        return false;
    });
   
});
