// JavaScript Document	
// JavaScript Document	

$(document).ready( function() {

//forms/////////////////////////////////////////////////////////

		$("#requestImg, #requestImgCt, #requestImgIdx").click(function() {
				$( "#dialog" ).dialog( "open" );
		});
		
		$("#commentImgCt").click(function() {
				$( "#dialogComment" ).dialog( "open" );
		});

// forms' common functions/////////////////////////////////////////////////
		function updateTips( t ) {
			tips
				.text( t )
				.addClass( "ui-state-highlight" );
			setTimeout(function() {
				tips.removeClass( "ui-state-highlight", 1500 );
			}, 500 );
		}

		function checkLength( o, n, min, max ) {
			if ( o.val().length > max || o.val().length < min ) {
				o.addClass( "ui-state-error" );
				updateTips( "Length of " + n + " must be between " +
					min + " and " + max + "." );
				return false;
			} else {
				return true;
			}
		}

		function checkRegexpNot( o, regexp, n ) {
			if ( !( regexp.test( o.val() ) ) ) {
				o.addClass( "ui-state-error" );
				updateTips( n );
				return false;
			} else {
				return true;
			}
		}
		
		function checkRegexp( o, regexp, n ) {
			if (regexp.test( o.val() )) {
				o.addClass( "ui-state-error" );
				updateTips( n );
				return false;
			} else {
				return true;
			}
		}

//request form////////////////////////////////////////////////////////////////////////////		
		var first_name = $( "#first_name_req" ),
			last_name = $( "#last_name_req" ),
			email = $( "#emailAddress_req" ),
			phone = $("#phone_req"),
			msg = $( "#msg_req" ),
			address = $( "#address_req" ),
			city = $( "#city_req" ),
			zip = $( "#zip_req" ),
			allFields = $( [] ).add( first_name ).add( last_name ).add( email ).add( phone ).add( msg ).add( address ).add( city ).add( zip ),
			tips = $( ".validateTips" );
		
		$("#dialog").dialog({
			autoOpen: false,
			height: 410,
			width: 650,
			modal: true,
			title: 'Synergy Solar and Electrical Systems, Inc.',
			buttons: {
				"Send": function() {
					var bValid = true;
					allFields.removeClass( "ui-state-error" );

					bValid = bValid && checkLength(first_name, "First Name", 1, 25);
					bValid = bValid && checkRegexp( first_name, /(bcc:|http:|https:|cc:|content-type:|mime-version:|content-transfer-encoding:)/i, "First name is not formated correctly." );
					bValid = bValid && checkLength(last_name, "Last Name", 1, 25);
					bValid = bValid && checkRegexp( last_name, /(bcc:|http:|https:|cc:|content-type:|mime-version:|content-transfer-encoding:)/i, "Last name is not formated correctly." );
					bValid = bValid && checkLength(email, "Email", 7, 50);
					// From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
					bValid = bValid && checkRegexpNot( email, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "Valid email format required, i.e.: myemail@domain.com" );
					if($("#phone_req").val() != ""){
						bValid = bValid && checkLength(phone, "Phone", 1, 15);
						bValid = bValid && checkRegexp( phone, /([^0-9\-\+\'\ \(\)])/, "Phone field only allows: 0 thru 9, '(', ')', '-'" );
					}
					if($("#address_req").val() != ""){
						bValid = bValid && checkLength(address, "Address", 1, 80);
						bValid = bValid && checkRegexp( address, /(bcc:|http:|https:|cc:|content-type:|mime-version:|content-transfer-encoding:)/i, "Address is not formated correctly." );
					}
					if($("#city_req").val() != ""){
						bValid = bValid && checkLength(city, "City", 1, 50);
						bValid = bValid && checkRegexp( city, /(bcc:|http:|https:|cc:|content-type:|mime-version:|content-transfer-encoding:)/i, "City is not formated correctly." );
					}
					if($("#zip_req").val() != ""){
						bValid = bValid && checkLength(zip, "Zip", 1, 10);
						bValid = bValid && checkRegexp( zip, /([^0-9\-\ \(\)])/, "Zip may only consist of: 0 thru 9, and - " );
					}
					if($("#msg_req").val() != ""){
						bValid = bValid && checkLength(msg, "Message", 1, 4000);
						bValid = bValid && checkRegexp( msg, /(bcc:|http:|https:|cc:|content-type:|mime-version:|content-transfer-encoding:)/i, "Your message is not formated correctly." );
					}

					if ( bValid ) {
						$.ajax({
							 type: "POST",
							 url: "emailRequest.php",
							 data: "first_name=" + $('[name=first_name_req]').val() + "&last_name=" + $('[name=last_name_req]').val() + "&emailAddress=" + $('[name=emailAddress_req]').val() + "&phone=" + $('[name=phone_req]').val() + "&address=" + $('[name=address_req]').val() + "&city=" + $('[name=city_req]').val() + "&zip=" + $('[name=zip_req]').val() + "&msg=" + $('[name=msg_req]').val(),
							 success: function(){
							 }
					   });
						$( this ).dialog( "close" );
						}
				},
				Cancel: function() {
					$( this ).dialog( "close" );
				}
			},
			close: function() {
				allFields.val( "" ).removeClass( "ui-state-error" );
			}
		});
		
//comment form/////////////////////////////////////////////////////////////		
		var first_nameC = $( "#first_name" ),
			last_nameC = $( "#last_name" ),
			emailC = $( "#emailAddress" ),
			phoneC = $("#phone"),
			msgC = $( "#msg" ),
			allFieldsC = $( [] ).add( first_nameC ).add( last_nameC ).add( emailC ).add( phoneC ).add( msgC );
		
		$("#dialogComment").dialog({
			autoOpen: false,
			height: 420,
			width: 650,
			modal: true,
			title: 'Synergy Solar and Electrical Systems, Inc.',
			buttons: {
				"Send": function() {
					var bValidC = true;
					allFieldsC.removeClass( "ui-state-error" );

					bValidC = bValidC && checkLength(first_nameC, "First Name", 1, 25);
					bValidC = bValidC && checkRegexp( first_nameC, /(bcc:|http:|https:|cc:|content-type:|mime-version:|content-transfer-encoding:)/i, "First name is not formated correctly." );
					if($("#last_name").val() != ""){
						bValidC = bValidC && checkLength(last_nameC, "Last Name", 1, 25);
						bValidC = bValidC && checkRegexp( last_nameC, /(bcc:|http:|https:|cc:|content-type:|mime-version:|content-transfer-encoding:)/i, "Last name is not formated correctly." );
					}
					bValidC = bValidC && checkLength(emailC, "Email", 6, 50);
					// From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
					bValidC = bValidC && checkRegexpNot( emailC, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "Valid email format required, i.e.: myemail@domain.com" );
					if($("#phone").val() != ""){
						bValidC = bValidC && checkLength(phoneC, "Phone", 1, 15);
						bValidC = bValidC && checkRegexp(phoneC, /([^0-9\-\+\'\ \(\)])/, "Phone field only allows: 0 thru 9, '(', ')', '-'" );
					}
					if($("#msg").val() != ""){
						bValidC = bValidC && checkLength(msgC, "Message", 1, 4000);
						bValidC = bValidC && checkRegexp( msgC, /(bcc:|http:|https:|cc:|content-type:|mime-version:|content-transfer-encoding:)/i, "Your message is not formated correctly." );
					}

					if ( bValidC ) {
						$.ajax({
							 type: "POST",
							 url: "email.php",
							 data: "first_name=" + $('[name=first_name]').val() + "&last_name=" + $('[name=last_name]').val() + "&emailAddress=" + $('[name=emailAddress]').val() + "&phone=" + $('[name=phone]').val() + "&msg=" + $('[name=msg]').val(),
							 success: function(){
							   //alert("Your message has been sent to Synergy.");
							 }
					   });
						$( this ).dialog( "close" );
						}
				},
				Cancel: function() {
					$( this ).dialog( "close" );
				}
			},
			close: function() {
				allFieldsC.val( "" ).removeClass( "ui-state-error" );
			}
		});		

//end of contact forms////////////////////////////////////////////////////////////////////////////

	//Initially closes sccordians
	$(function(){
		  $('.sectionHeading').next().hide(0); 
		  $('.sectionHeading').toggleClass('closed'); 
	});
	
	// Setup toggles for sccordians
	$('.section').find('.sectionHeading').click(function(){												 
		  $(this).toggleClass('closed');
		  $(this).next().slideToggle(400);    
	});	
	
	
//definitions page////////////////////////////////////////////////////////////////////////////////////	
	var defArr = new Array();
		//Def
		defArr[0] = ""; 
		//AC
		defArr[1] = "The type of electricity used in most homes and businesses."; 
		//CEC-ACRating
		defArr[2] = "A more actual rating of what a solar system will produce, includes energy loss of panels and inverter in less than perfect conditions.";
		//DC
		defArr[3] = "The type of energy solar panels and batteries produce."; 
		//Grid
		defArr[4] = "All the components that make up the electrical process, from the customers meter, to the plant producing energy.  This includes transmission lines, power plants, transformers, relay stations etc."; 
		//Inverter
		defArr[5] = "A device that converts DC electricity (from the solar panels) to AC electricity (to connect to the electrical system)."; 
		//Kilowatt
		defArr[6] = "Is usually measured in hours (kWh = kilowatt hours).  One Kilowatt-hour is equal to 1,000 watts.  Another way to look at it is a kilowatt of electricity can run 10, 100-watt light bulbs for an hour."; 
		//OffPeakUsage
		defArr[7] = "Is the lowest cost usage, and is everything not in the peak or partial peak time periods."; 
		//PartialPeakUsage
		defArr[8] = "Is usually broken into two main categories, Summer Partial Peak, and Winter Partial peak.  For PG&amp;E summer partial peak is defined as May 1 to October 31, and Winter as November 1 through April 30.  Partial peak during the summer is usually the hours just before and just after peak rates (for example 10:00 am to 1:00 pm and 7:00 pm to 9:00 pm for residential E6 users), and on weekends in the late afternoon, (for example from 5 pm to 8 pm for residential E6 customers).";
		//Peak
		defArr[9] = "Defined differently by each utility.  PG&amp;E defines the peak time of the year as May - October, Monday through Friday.   For a business  (A6) the time is from noon to 6 pm and for residential (E6) from 1 pm to 7 pm.";   
		//PGETariffRates
		defArr[10] = "The Rates PG&amp;E charges for a customer.  These can be residential, commercial or agriculture, and many rate schedules exist in each category.<br />See <a href=\"http://www.pge.com/tariffs/ERS.SHTML#ERS\" onclick=\"return OpenBlank(href)\"> PG&amp;E ELECTRIC SCHEDULES</a>";
		//STCRating
		defArr[11] = "The factory rating in KiloWatts of a solar system (number of panels times their wattage)."; 
		//TOU
		defArr[12] = "Time of Use metering is a rate tariff that bills differently, depending on the time of year, and the time of day.  Most TOU meters break up billing into summer and winter, and further into partial peak, and off peak."; 
		//Transformer
		defArr[13] = "Usually used to raise or lower voltage and current, while keeping the frequency of the energy the same.   A typical use of a transformer is to raise voltage and current over long electrical runs (i.e. power lines), to minimize the wire size and voltage drop (loss), and then lowering the voltage and current so a home or business can utilize it."; 
		
		
		
	$('select.definitions').change(function(){
			var defNum = $(".definitions option").index($(".definitions option:selected"));  //gets the selected option index
			if(defNum != 0)
			{	
				$('.defDisplay').css("display", "block").hide().html(defArr[defNum]).fadeIn('slow');
			}
			else
				$('.defDisplay').css("display", "none");
	});								
//end definitions page/////////////////////////////////////////////////////////////////////////////////////
	
//partners page///////////////////////////////////////////////////////////////////////////////////////////////////////////	
	var descript = new Array();
		descript[0] = "The leading manufacturer of photovoltaic inverters and monitoring systems.";
		descript[1] = "Tigo Energy delivers more energy, active management, and enhanced safety for utility, commercial, and residential solar arrays.";
		descript[2] = "SEF is a non-profit, 501(c)(3) fundraising organization whose purpose is to secure and distribute funds for the benefit of students in the Sebastopol Union School District.";
		descript[3] = "Our mission is to promote academic and personal success, responsible citizenship, and life long learning in a cooperative environment.";
		descript[4] = "The Leaf Exchange simplifies the REC registration and reporting process for California solar system owners.  We provide a simple and transparent way to get the best value possible for your Renewable Energy Certificates (RECs).";
		/*descript[2] = "UniRac offers a complete product line of solar energy mounting equipment ensuring the appropriate renewable energy technology for any photovoltaic solar panel system installation.";
		descript[3] = "Power where you need it!";
		descript[4] = "Attach almost anything to standing seam metal roofs without piercing the panel!";*/
		descript[5] = "Solar mounting made simple.";
		descript[6] = "EcoDog's home energy management products will enable residential, small business, solar power and alternative energy users to easily take control of their energy consumption to save money and reduce energy demand.";
		descript[7] = "Enphase energy introduces the solar industry's first microinverter system, a revolutionary next generation solar power solution.";
		descript[8] = "A world leading solar company. The sun is the answer.";
		descript[9] = "DC Power Systems is a full service wholesale distributor of renewable energy products such as solar panels, wind turbines, inverters, generators, water pumps, charge controllers and monitoring systems.";
		descript[10] = "Specializing in tree and stump removal, we are a full service tree care company offering a wide variety of arboricultural services, including pruning, planting, fertilization and insect control.";
		descript[11] = "We are a local Sebastopol-based company offering solar panel washing to Sonoma, Marin, and Napa counties.  We clean all brands of photovoltaic systems and solar hot water panels - residential and commercial, large and small.";
		descript[12] = "Our school is known for its strong academics,<br /> extra-curricular offerings, and strong scholar-<br />athletic teams. We believe in relationships, <br />relevance, and rigor.";
		
		$('#logos').find('.logo').mouseenter(function(){									  
				var num = $(this).attr('id');
				var offst = $(this).offset();
				num = num.replace("logo", ""); //strips the id to only its number component
				$('.text1').hide();
				$('.text1').css({top: offst.top + 55, left: offst.left - 200, border: "1px solid navy"});
				$('.text1').html(descript[num]).fadeIn('slow');		
		});		
		$('#logos').find('.logo').mouseleave(function(){
				$('.text1').fadeOut('fast').html("");												 
		});
})
//end partners page/////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
//end of jQuery section////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////

	function OpenBlank(url) {var blank = window.open(url); return false;}
		
	function Thanks()
	{
		alert("Thank you.\r\n  Your message has been sent.");
	}
	
	function Warning()
	{
		alert("Invalid email format.  Either your name or phone number is missing.  No information has been sent.  Please fill in the form again, or call Synergy at (707) 823-8003.");
	}

	
	function getCookie(attribute)
	{	var strStart = "";
		var strEnd = "";
		if (document.cookie.length > 0)
		{	strStart = document.cookie.indexOf(attribute + "%3D");	//"%3D" is ASCII hex for "="
	  		if (strStart != -1)
	    	{   strStart = strStart + attribute.length + 3;
	    		strEnd = document.cookie.indexOf("%3B", strStart);		//"%3B" is ASCII hex for ";"
	    		if (strEnd == -1) 
				{	strEnd = document.cookie.length;
	   			}
			return unescape(document.cookie.substring(strStart, strEnd));
	    	} 
	  	}
		return "";
	}
	

