//----------Kick-off / Page Load Functions----------
$(document).ready(function(){
		
	// Input Field Populate
	$("input.populate").each( function(){
		if ( $(this).val() == "" ) {
			$(this).val( $(this).attr("title") );
		}
	});
	$("input.populate").focus(function(){
		if ( $(this).val() == $(this).attr("title") ) {
			$(this).val("");
		}
	});
	$("input.populate").blur(function(){
		if ( $(this).val() == "" ) {
			$(this).val( $(this).attr("title") );
		}
	});

	//Email Encrypt
	$(".email-replace").each(function(){
		var ats, dots, address, i;
		ats = [ ' at ', ' (at) ', ' [at] ' ];
		dots = [ ' dot ', ' (dot) ', ' [dot] ' ];
		address = $(this).html();
		for ( i = 0; i < ats.length; i++ ) {
			address = address.replace(ats[i], '@');
		}
		for ( i = 0; i < dots.length; i++ ) {
			address = address.replace(dots[i], '.');
		}
		$(this).html('<a href="mailto:' + address + '">' + address + '</a>');
	});
	
	// Jumpmenu - just add class .jumpmenu
	var selectElements = document.getElementsByTagName("select");
	for( i = 0; i < selectElements.length; i++ ) {
		// Check for the class and make sure the element has an ID
		if( selectElements[i].className == "jumpmenu" && document.getElementById(selectElements[i].id) != "" ) {
			jumpmenu = document.getElementById(selectElements[i].id);
			jumpmenu.onchange = function() {
				if( this.options[this.selectedIndex].value != '' ) {
					// Redirect
					location.href=this.options[this.selectedIndex].value;
				}
			}
		}
	}
	
	//Popups
	$("a[rel='external']").attr("target", "_blank");


});

//BBB Certify
function Rcertify() 
{ 
	popupWin = window.open('http://www.bbbonline.org/cks.asp?id=101070518215817978', 'Participant'); 
	window.name = 'opener'; 
} 

// Initialize SuperFish Navigation Dropdown
jQuery(function(){
	jQuery('#header .navigation ul').superfish();
});


//------------------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);
}

//JQuery Popup Functions
$(function(){
	// Live Chat Popup
	$("a[rel='liveChat_popup']").click(function(){
		var l=window.open($(this).attr("href"), 'liveChat_popup', 'width=490, height=404, toolbar=no,location=no,directories=no,menubar=no,status=no,scrollbars=no,resizable=yes,replace=no');
		l.focus();
		return false;
	});

	// Topic Page Popup
	$("a[rel='topic_popup']").click(function(){
		var t=window.open($(this).attr("href"), 'topic_popup', 'width=600, height=500, scrollbars=yes, menubar=no, toolbar=no');
		t.focus();
		return false;
	});
	
	// External Page Popup
	$("a[rel='external_popup']").click(function(){
		var e=window.open($(this).attr("href"), 'external_popup', 'width=900, height=500, scrollbars=yes, menubar=yes, toolbar=yes');
		e.focus();
		return false;
	});
	
	// Assurance Page Popup
	$("a[rel='assurance_popup']").click(function(){
		var a=window.open($(this).attr("href"), 'assurance_popup', 'width=600, height=500, scrollbars=yes, menubar=no, toolbar=no');
		a.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 =
        '';
    }
}



//-------------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;
	}
}

function drawDiv( options ) {		
	try {		
		if( options['mode'] == "create" ) {
			try {
				document.body.removeChild( document.getElementById( options['id'] ) );
			}
			catch ( e ) {
			
			}
		
			// February 17, 2009 WSH // GREENWICH // FIRST CREATE A NEW DIV WITH THE SPECIFIED ID
			var tempDiv = document.createElement( "div" );
			tempDiv.setAttribute( "id", options['id'] );				
			document.body.appendChild( tempDiv );		
		}
		else if( options['mode'] == "update" ) {
			// Ferbruary 17, 2009 WSH // GREENWICH // IF NOT CREATING DIV, THEN DIV SHOULD ALREADY EXIST
		}			
		var tempContent = "";
		
		if( options['content'] ) {
			tempContent = options['content'];
			
			$( "#" + options['id'] ).html( tempContent );
			
			if( options['draw_close_button'] ) {
				var closeOutput = "";
				closeOutput += "<div style='width: 100%; text-align: right;'><img src='images_new/button_close.gif' id='close_" + options['id'] +"' style='cursor: pointer; float: right;'></div><br clear='all' />";
				
				var existingOutput = $( "#" + options['id'] ).html();
	
				$( "#" + options['id'] ).html( closeOutput + existingOutput );
				
				$( "#close_" + options['id'] ).click( function() {
					$( "#" + options['id'] ).hide();
				} );
			}
		}
		else if( options['content_url'] ) {
			$.get( options['content_url'], { name: "John", time: "2pm" }, 
				function( data ) { 
					var tempContent = data;
					var existingOutput = tempContent;
			
					$( "#" + options['id'] ).html( tempContent ); 

					// March 5, 2009 WSH // GREENWICH // ADDED ABILITY TO ONLY GRAB A SPECIFIC ELEMENT					
					if( options['element_id'] ) {
						$( "#" + options['id'] ).html( $( "#" + options['element_id'] ).html() );																			
						existingOutput = $( "#" + options['id'] ).html();
					}
					
					if( options['draw_close_button'] ) {
						var closeOutput = "";
						closeOutput += "<div style='width: 100%; text-align: right;'><img src='images_new/button_close.gif' id='close_" + options['id'] +"' style='cursor: pointer; float: right;'></div><br clear='all' />";												
			
						$( "#" + options['id'] ).html( closeOutput + existingOutput );
						
						$( "#close_" + options['id'] ).click( function() {
							$( "#" + options['id'] ).hide();
						} );
					}
				} 
			);
		}
		else {
			tempContent = "";
		}		
	
		if( options['class'] ) {
			$( "#" + options['id'] ).addClass( options['class'] );
		}
		
		
		if( options['width'] ) {
			$( "#" + options['id'] ).css( "width", options['width'] );
		}
		
		if( options['height'] ) {
			$( "#" + options['id'] ).css( "height", options['height'] );
		}
		
		if( options['x'] && options['y'] ) {
			$( "#" + options['id'] ).css( "position", "absolute" );
			$( "#" + options['id'] ).css( "top", options['y'] );
			$( "#" + options['id'] ).css( "left", options['x'] );
		}		
		
		// June 9, 2009 WSH // FOOPLUS // ADDED ABILITY TO SPECIFY REL FOR JQUERY LIGHTBOX
		if( options['rel'] ) {
			$( "#" + options['rel'] ).attr( "rel", options['rel'] );
		}											
	}
	catch ( e ) {
		alert( "drawDiv Exception" + e );
	}
}

// GET WIDTH AND HEIGHT OF WINDOW, ALL BROWSERS - http://www.howtocreate.co.uk/tutorials/javascript/browserwindow
function getWindowSize() {
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} 
	else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} 
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	var toReturn = new Array();
  
	toReturn['width'] = myWidth
	toReturn['height'] = myHeight  
  
	return toReturn;
}

function hideDiv( id ) {
	$('#' + id).hide();
}
