var $j = jQuery.noConflict();

$j(document).ready(function() {

	/*prepend numbers to subnav
	if (document.getElementById("sub_nav")) {
		var subNavLinks = document.getElementById("sub_nav").getElementsByTagName("a");
		for (i=1; i<subNavLinks.length+1; i++) {
			if (subNavLinks[i-1].parentNode.className != "btn") {
				var thisText = subNavLinks[i-1].firstChild.nodeValue;
				var thisNumber;
				if (i < 10) {
					thisNumber = "0" + i;
				} else {
					thisNumber = i;
				}
				subNavLinks[i-1].innerHTML = "<span>" + thisNumber + "</span>" + thisText;
			}
		}
	}*/
	
	// handle testimonial quote images
	var blockquotes = document.getElementsByTagName("blockquote");
	for (j=0; j<blockquotes.length; j++)
	{
		var thisBlockInfo = blockquotes[j].innerHTML;
		var firstQuote = "<span class=\"quote_lt\">\"</span>";
		var midQuote = "<span class=\"quote_lastword\">";
		var lastQuote = "<img class=\"quote_rt\" src=\"assets/images/ui_framework/bg_quoteright.gif\" width=\"15\" height=\"11\" /></span>";
		//var thisBlockBuilder = "<span class=\"quote_lt\">\"</span>" + thisBlockInfo[0].nodeValue.substring(1, thisBlockInfo[0].nodeValue.length);
		var quoteSplit = thisBlockInfo.split(" ");
		//alert(quoteSplit[0].substring(1, quoteSplit[0].length));
		quoteSplit[0] = firstQuote + quoteSplit[0].substring(1, quoteSplit[0].length);
		quoteSplit[quoteSplit.length-1] = midQuote + quoteSplit[quoteSplit.length-1].substring(0, quoteSplit[quoteSplit.length-1].length-1) + lastQuote;
		
		var thisQuoteJoined = quoteSplit.join(" ");
		blockquotes[j].innerHTML = thisQuoteJoined;
	}
	
	// handle first items in a select drop-down list
	$j("select option:first-child").addClass("first");
	
	// handle form selection class change when an item is selected
	$j("select").change(function() {
		if (this.selectedIndex == 0)
		{
			$j(this).removeClass();
		}
		else
		{
			$j(this).addClass("active");
		}
	});
	
	// handle select style on page load
	function initSelectCurrent () {
		var selectArray = $j("select");
		for (var i = 0; i < selectArray.length; i++)
		{
			if (selectArray[i].selectedIndex != 0)
			{
				selectArray[i].className = "active";
			}
		}
	}
	
	// handle radio button click event
	$j(":radio").click(function() {

		var siblingLabels = $j(this).siblings("label");
		for (var i=0; i< siblingLabels.length; i++)
		{
			if (siblingLabels[i].className.indexOf("last") == -1)
			{
				siblingLabels[i].className = "";
			}
			else
			{
				siblingLabels[i].className = "last";
			}
		}
								
		var thisId = this.getAttribute('id');
		$j(this).siblings("label[for='"+thisId+"']").addClass("current");
	});
	
	// handle radio button style on page load
	function initRadioCurrent () {
		var thisRadioChecked = $j(":radio:checked");
		for (var i = 0; i < thisRadioChecked.length; i++)
		{
			var thisId = thisRadioChecked[i].getAttribute('id');
			$j("label[for='"+thisId+"']").addClass("current");
		}
	}
	
	// make sure current radio buttons get the current class
	initRadioCurrent();
	initSelectCurrent();
	
	// apply functionality to all "form1" forms
	function initForm1 () {
		if (!document.getElementById) return;  	
		
		var labels, id, field;
		
		// Set focus and blur handlers to hide and show 
		// LABELs with 'overlabel' class names.
		textInputs = $j("fieldset.form1 :text, fieldset.form1 :password, fieldset.form1 textarea");
		for (var i = 0; i < textInputs.length; i++)
		{
			textInputs[i].onfocus = function()
			{
				this.className = 'focused';
			}

			textInputs[i].onblur = function()
			{
				this.className = 'default';
			}
		}
	};
	
	initForm1();
	// end "form1" form functionality
	
	// apply functionality to all "form3" forms
	function initForm3 () {
		if (!document.getElementById) return;  	
		
		var labels, id, field;
		
		// Set focus and blur handlers to hide and show 
		// LABELs with 'overlabel' class names.
		textInputs = $j("fieldset.form3 :text, fieldset.form3 :password, fieldset.form3 textarea");
		for (var i = 0; i < textInputs.length; i++)
		{
			textInputs[i].onfocus = function()
			{
				this.className = 'focused';
			}

			textInputs[i].onblur = function()
			{
				this.className = 'default';
			}
		}
	};
	
	initForm3();
	// end "form3" form functionality
	
	// apply functionality to all "form2" forms
	function initOverLabels () {
		if (!document.getElementById) return;  	
		
		var labels, id, field;
		
		// Set focus and blur handlers to hide and show 
		// LABELs with 'overlabel' class names.
		labels = $j("fieldset.form2 label.overlabel");
		for (var i = 0; i < labels.length; i++)
		{
			// Skip labels that do not have a named association
			// with another field.
			id = labels[i].htmlFor || labels[i].getAttribute('for');
			if (!id || !(field = document.getElementById(id)))
			{
				continue;
			}
			
			if (labels[i].className.indexOf("error") == -1)
			{
				// Change the applied class to hover the label 
				// over the form field.
				labels[i].className = 'overlabel-apply';
			}
			else
			{
				// Change the applied class to hover the label 
				// over the form field, with error
				labels[i].className = 'overlabel-apply error';
			}
			
			// Hide any fields having an initial value.
			if (field.value !== '')
			{
				if (labels[i].className.indexOf("error") == -1)
				{
					hideLabel(field.getAttribute('id'), true);
				}
				labels[i].style.textIndent = '-1000px';
			}
			
			// Set handlers to show and hide labels.
			field.onfocus = function()
			{
				hideLabel(this.getAttribute('id'), true);
				this.className = 'focused';
			};
			
			field.onblur = function()
			{
				if (this.value === '')
				{
					hideLabel(this.getAttribute('id'), false);
				}
				this.className = 'default';
			};
			
			// Handle clicks to LABEL elements (for Safari).
			labels[i].onclick = function()
			{
				var id, field;
				id = this.getAttribute('for');
				if (id && (field = document.getElementById(id)))
				{
					field.focus();
				}
			};
		}
	};
	
	function hideLabel (field_id, hide) {
		var field_for;
		var labels = $j("fieldset.form2 label");
		for (var i = 0; i < labels.length; i++) {
			field_for = labels[i].htmlFor || labels[i].getAttribute('for');
			if (field_for == field_id)
			{
				labels[i].style.textIndent = (hide) ? '-1000px' : '0px';
				labels[i].className = 'overlabel-apply';
				return true;
			}
		}
	}
	
	initOverLabels();
	// end "form2" form functionality

});

function delete_cookie(cookie_name) {
    var cookie_date = new Date();  // current date & time
    cookie_date.setTime(cookie_date.getTime() - 1);
    document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}

// next section is for Contact Us Page:

function stateChange(objState) {

    //Name
    //Email
    //Phone
    //Cell Phone
    //Fax
    //ImageURL
    //Locale

    var salesReps = [
			
			["Stephen Parker", "sparker@usfi.com", "214.673.4475", "-", "-", "http://www.usfi.com/sites/all/themes/usfi_v2/images/employeePhotos/Stephen.jpg"],
			["Darren Gorman", "dgorman@usfi.com", "-", "303.241.2907", "972.402.8139", "http://www.usfi.com/sites/all/themes/usfi_v2/images/employeePhotos/Darren.jpg"],
			["Faryal Khan", "faryal@usfi.com", "866.574.8734", "951.545.7980", "909.948.7572", "http://www.usfi.com/sites/all/themes/usfi_v2/images/employeePhotos/Faryal.jpg"],
			["Nick Bendiak", "nbendiak@usfi.com ", "617.780.5106 ", "-", "-", "http://www.usfi.com/sites/all/themes/usfi_v2/images/employeePhotos/Nick.jpg"],
			["Barbara Atkinson", "batkinson@usfi.com ", "800.380.8580 ", "804.347.9178 ", "804.559.0902", "http://www.usfi.com/sites/all/themes/usfi_v2/images/employeePhotos/Barbara.jpg"],
			["Jessica Kew", "jkew@usfi.com", "954.340.5511", "813.728.6496", "972.402.8139", "http://www.usfi.com/sites/all/themes/usfi_v2/images/employeePhotos/Jessica.jpg"],
			["Will Barthel", "wbarthel@usfi.com", "312.339.5364", "312-339-5364", "773-697-7903", "http://www.usfi.com/sites/all/themes/usfi_v2/images/employeePhotos/Will.jpg"],
			["Chris St-Amour", "chris@lcsupply.ca", "514.510.9469", "514.915.4980", "-", "http://www.usfi.com/sites/all/themes/usfi_v2/images/employeePhotos/ChrisStAmour.jpg"],
			["Tim Van Cleave", "tvancleave@usfi.com", "972.501.9981", "214.679.2333", "972.402.8139", "http://www.usfi.com/sites/all/themes/usfi_v2/images/employeePhotos/Tim.jpg"],
			["", "", "-", "-", "-", "http://www.usfi.com/greenworks/images/img_card_picture.jpg"],
		];


    switch (objState.options[objState.selectedIndex].value) {

        case "arkansas": case "georgia": case "louisiana": case "new mexico":
        case "oklahoma": case "south carolina": case "tennessee": case "texas":
            salesRepID = 0; // Stephen Parker
            break;
		

        case "arizona": case "alaska": case "colorado": case "idaho": case "kansas": case "montana": case "nebraska": case "north dakota":
        case "south dakota": case "utah": case "wyoming":
            salesRepID = 1; // Darren Gorman
            break;

        case "california": case "hawaii": case "nevada": case "oregon": case "washington":
            salesRepID = 2; // Faryal Khan
            break;

        
            salesRepID = 3; // Nick Bendiak
            break;

        case "d.c.": case "delaware": case "maryland": case "new jersey": case "virginia": case "west virginia":
            salesRepID = 4; // Barbara Atkinson
            break;

        case "florida":
            salesRepID = 5; // Jessica Kew
            break;

        case "illinois": case "indiana": case "iowa": case "michigan": case "minnesota": case "missouri":
        case "wisconsin": case "kentucky": case "ohio": 
            salesRepID = 6; // Will Barthel
            break;

        case "alberta": case "british columbia": case "manitoba": case "new brunswick": case "newfoundland": case "northern mariana island":
        case "northwest territories": case "nova scotia": case "ontario": case "palau island": case "prince edward island": case "quebec":
        case "saskatchewan": case "yukon territory":
            salesRepID = 7; // Chris St-Amour
            break;
			
		case "connecticut": case "maine": case "massachusetts": case "mississippi": case "new hampshire": case "new york": case "north carolina":
		case "pennsylvania": case "rhode island": case "vermont":  case "alabama": 
		    salesRepID = 8; // Tim Van Cleave
            break;
		
        default:
            salesRepID = 9 //Unknown Rep
    }

    hide();
    document.getElementById("rep-Name").innerHTML = "<h3>" + salesReps[salesRepID][0] + "</h3>";
    document.getElementById("rep-Email").innerHTML = "<label>E-mail:</label><span><a href='mailto:greenmeetings@usfi.com;" + salesReps[salesRepID][1] + "'>" + salesReps[salesRepID][1] + "</a></span>";
    document.getElementById("rep-Img").innerHTML = "<img src='" + salesReps[salesRepID][5] + "' class='picture' width='136' height='115' border='0' />";
    document.getElementById("rep-Phone").innerHTML = "<label>Phone:</label><span>" + salesReps[salesRepID][2] + "</span>";
    document.getElementById("rep-CPhone").innerHTML = "<label>Cell:</label><span>" + salesReps[salesRepID][3] + "</span>";
    document.getElementById("rep-Fax").innerHTML = "<label>Fax:</label><span>" + salesReps[salesRepID][4] + "</span>";
    document.getElementById("rep-Locale").innerHTML = "Your <strong>" + objState.options[objState.selectedIndex].text + "</strong> Contact for all inquiries is:";



}

function hide() {

    document.getElementById("RepBusinessCard").style.display = "block";
    document.getElementById("defaultCopy").style.display = "none";

} 


