window.onload = changeform;

function changeform() {
	
	//contact form
	function changeformwrap() {
	var $contactIFrame = $('#brandContactForm');
	$contactIFrame.contents().find('select.selectField').wrap('<span class="border"></span>');
		
	}
	
	
	if (window.location.pathname === '/contact-us/index.html') {
	
		var $contactIFrame = $('#brandContactForm');
		$contactIFrame.contents().find('select.selectField').wrap('<span class="border"></span>');
		
			
	} 
	
	//onsubmit rerun the function to wrap the select fields on contact form
	if ($('#brandContactForm').length > 0) {
		var $contactSubmit = $contactIFrame.contents().find('body p#submitWrapper input#submit');
		function contactrewrap() {
			var $contactIFrame = $('#brandContactForm');
			$contactIFrame.contents().find('select.selectField').wrap('<span class="border"></span>');	
		}
		
		$($contactSubmit).click(function() {
		
			setTimeout(contactrewrap, 500);
					
			});
	}
	
	//WTB form
	var $wtbIFrame = $('#winefinder_iframe');
	if (window.location.pathname === '/where-to-buy-livingston-cellars.html') {
		if ($wtbIFrame.length > 0)  {
		$wtbIFrame.contents().find('#winefinderForm P#findWinePara input[name="findWineOption"]')[0].checked = true;
		$wtbIFrame.contents().find('select#productList').wrap('<span class="border"></span>');
		}
	}
	
	//make sure the new footer code exists - WTB in footer
	if ($('#specialcase').length > 0) {
		
		var $specialIFrame = $(window.frames[ "specialiframe" ].document);
		//var $specialIFrame = $('#specialiframe');
		$specialIFrame.contents().find("head").append('<link rel="stylesheet" href="/resources/css/wineFinderSpecialForm.css" type="text/css" />');
		var $currentIFrame = $('#winefinder_iframe');
		$('#specialcase').css('display', 'block');
		
		$specialIFrame.contents().find('#validationErrDiv').remove('#validationErrDiv');
		$specialIFrame.contents().find('#validationErrMsg').remove('#validationErrMsg');
		//$specialIFrame.contents().find('#flavorPara').append('Choose Flavor: ');
		$specialIFrame.contents().find('#flavorPara').append('<div id="showflavor">Choose Flavor: </div>');
		
		var $flavorlist = $specialIFrame.contents().find('#flavorPara').after('<div id="selectWinePopup"><div class="bg"><ul></ul></div></div>');
	
		$specialIFrame.contents().find('#zipPara #zipCode').val('Enter Zip Code');
		$specialIFrame.contents().find('#milesPara #radius').val('# Miles');
		$specialIFrame.contents().find('#winefinderForm #submitPara').after('<P id="validationErrDiv"></P>');
		$specialIFrame.contents().find('#winefinderForm #submitPara').after('<P id="validationErrMsg"></P>');
	
		$specialIFrame.contents().find('#winefinderForm P#findWinePara input[name="findWineOption"]')[0].checked = true;
		$specialIFrame.contents().find('#winefinderForm P#findWinePara').css('display','none');	
		
		
			
		//$specialIFrame.contents().find('#winefinderForm P#flavorPara span.border').css('border','none');
		
		//you can now use a json call if you prefer
		function getFlavors() {
			var allFlavors = new Array();
			var allFlavorsNames = new Array();
	
			$specialIFrame.contents().find("select.#productList > option").each(function(index) {
			allFlavorsNames[index] = $(this).text();
			});
			$specialIFrame.contents().find("select.#productList > option").each(function(index){
			allFlavors[index] = $(this).val();
			});
	
			for(i=0; i<allFlavors.length; i++) {
		
				$specialIFrame.contents().find('#selectWinePopup .bg ul').append('<li><a href="#'+allFlavors[i]+'" class="wineselect" title="Search for stores with">'+allFlavorsNames[i]+'</a></li>');
				//$specialIFrame.contents().find('#selectWinePopup').css('display','none');
		
			}
			$specialIFrame.contents().find('#selectWinePopup').css('display','none');
			//start footer special code - see original code in gallo.combined.js for reference
			// bottom winefinder tool
			// open 'select list'
	
			$specialIFrame.contents().find('#flavorPara').click(function() {
				//you have to edit some css to move this field into place
				$('#specialcase').css('top','-510px');
				$('#winefinderfooter_iframe').css('height','600px');
				$specialIFrame.contents().find('#flavorPara').css('top','510px');
				$specialIFrame.contents().find('#wineFinder #zipPara').css('top','510px');
				$specialIFrame.contents().find('#wineFinder #milesPara').css('top','510px');
				$specialIFrame.contents().find('#wineFinder #submitPara').css('top','510px');
				$(this).css("background-image","none");
				//precedence issue requires this css treatment
				$(this).addClass('active');
				$(this).css("background-image","url('/resources/images/ftrSelectWineActive.jpg')");
				$specialIFrame.contents().find('#selectWinePopup').show();
		
			});
	
			// select new varietal
			$specialIFrame.contents().find('#selectWinePopup a').click(function() {
				//var $varietal_code = $(this).attr('href').replace('#','');
				var $varietal_code_all = $(this).attr('href');
				$varietal_code_all = $varietal_code_all.split("#");
				$varietal_code = $varietal_code_all[1];
				//alert($varietal_code);
				var $varietal_name = $(this).text();
			
				var strip = function(s) {
    					return String(s).replace(/^\s+|\s+$/g, '');
				};

			$varietal_code = strip($varietal_code);
			//alert('code [' + $varietal_code + '] name ' + $varietal_name);
			//$specialIFrame.contents().find('#flavorPara #productList > option[value="'+$varietal_code+'"]').attr("selected", "selected");
			
			
			//alert($specialIFrame.contents().find("#flavorPara #productList option[value="+$varietal_code+"]"));
			$specialIFrame.contents().find("#flavorPara #productList option[value="+$varietal_code+"]").attr("selected", "selected");
			//$specialIFrame.contents().find("#flavorPara select#productList option[value=430]").attr("selected", "selected");
			
			//$select[0].selectedIndex = 1
			
			//$specialIFrame.contents().find('#flavorPara select#productList option:selected').val($varietal_code);
			//$specialIFrame.contents().find('#flavorPara select#productList option').val($varietal_code).attr("selected", "selected");
			
			//$specialIFrame.contents().find('#flavorPara select#productList option:selected').text($varietal_name);
			//$specialIFrame.contents().find("#flavorPara select#productList option[value="+$varietal_code+"]").selectedIndex = 1;
			//alert($varietal_code);
			$specialIFrame.contents().find('#selectWinePopup').hide();
			$specialIFrame.contents().find('#flavorPara').removeClass('active');
			$specialIFrame.contents().find('#flavorPara').css("background-image","url('/resources/images/ftrWineSelect.jpg')");
			
			
			//var $starttext = $specialIFrame.contents().find('#flavorPara #showflavor').text();
			var $newtext = $specialIFrame.contents().find('#flavorPara select#productList option:selected').text($varietal_name);
			var $valtext = $specialIFrame.contents().find('#flavorPara select#productList option:selected').val();
			//alert($newtext);
			
			
			$specialIFrame.contents().find('#flavorPara #showflavor').text($varietal_name);
			//alert($valtext);
			//alert('code ' + $varietal_code + ' name ' + $varietal_name);
			
		});


	

		// hide 'select list' on global clicks - this doesn't work
		$specialIFrame.contents().find('#winefinderForm').bind('click', function(e) {
			if($(e.target).attr('class') != 'wineselect' && $(e.target).attr('id') != 'flavorPara') {
				$('#selectWinePopup').hide();
				$('#flavorPara').removeClass('active');
				
				
			}
		});
		$(document).bind('click', function(e) {
			if($(e.target).attr('class') != 'wineselect' && $(e.target).attr('id') != 'selectWine') {
				$specialIFrame.contents().find('#selectWinePopup').hide();
				$specialIFrame.contents().find('#flavorPara').removeClass('active');
				$specialIFrame.contents().find('#flavorPara').css("background-image","url('/resources/images/ftrWineSelect.jpg')");
				//go back to original height
				$('#specialcase').css('top','-10px');
				$('#winefinderfooter_iframe').css('height','100px');
				$specialIFrame.contents().find('#flavorPara').css('top','10px');
				$specialIFrame.contents().find('#wineFinder #zipPara').css('top','10px');
				$specialIFrame.contents().find('#wineFinder #milesPara').css('top','10px');
				$specialIFrame.contents().find('#wineFinder #submitPara').css('top','10px');
				
			
			}
		});
	
		// remove default zip code text
		$specialIFrame.contents().find('#zipPara #zipCode').focus(function() {
			if($(this).val() == 'Enter Zip Code') {
				$(this).val('');
			}
		});
	
		// return default zip code text
		$specialIFrame.contents().find('#zipPara #zipCode').blur(function() {
			if($(this).val() == '' || $(this).val().length != 5) {
				$(this).val('Enter Zip Code');
			}
		});
	
		// remove default miles text
		$specialIFrame.contents().find('#milesPara #radius').focus(function() {
			if($(this).val() == '# Miles') {
				$(this).val('');
			}
		});
	
		// return default miles text
		$specialIFrame.contents().find('#milesPara #radius').blur(function() {
			if($(this).val() > 100) {
				alert('Maximum search radius is 100 miles');
				$(this).val('100');
			} else if($(this).val() == '') {
				$(this).val('# Miles');
			}
		});
		
		
	
		// validate then submit 
		$specialIFrame.contents().find('#winefinderForm #submitPara #find').click(function(e) {
		//alert($specialIFrame.contents().find('#flavorPara #productList option[value='+$varietal_code+']').attr('selected', 'selected').val());	
		
		//alert($specialIFrame.contents().find('#flavorPara select#productList option:selected').val());
		var $flavorval = $specialIFrame.contents().find('#flavorPara select#productList option:selected').val();
		var $milesval = $specialIFrame.contents().find('#milesPara #radius').val();
		var $zipval = $specialIFrame.contents().find('#zipPara #zipCode').val();
		//alert($flavorval + $milesval + $zipval);
		//e.preventDefault();
		//var src = "/WineFinderBrands/locateWine.action?WebSiteId=15&zipCode="+$zipval+"&radius=10&flavor="+$flavorval;
		});

		$specialIFrame.contents().find('#test #find').click(function(e) {
		
			zip = $specialIFrame.contents().find('#zipPara #zipCode').val();
			miles = $specialIFrame.contents().find('#milesPara #radius').val();
			
			if(zip == 'Enter Zip Code' && miles != '# Miles') {
				$specialIFrame.contents().find('#validationErrDiv').css('display','none');
				$specialIFrame.contents().find('#validationErrMsg').css('display','none');
				alert('Please enter a Zip Code');
			} else if(zip != 'Enter Zip Code' && miles == '# Miles') {
				$specialIFrame.contents().find('#validationErrDiv').css('display','none');
				$specialIFrame.contents().find('#validationErrMsg').css('display','none');
				alert('Please enter a Search Radius');
			} else if(zip == 'Enter Zip Code' || miles == '# Miles') {
				$specialIFrame.contents().find('#validationErrDiv').css('display','none');
				$specialIFrame.contents().find('#validationErrMsg').css('display','none');
				alert('Please enter a Zip Code and Search Radius');
			} else {
				var height	= 800;
				var width	= 650;
			
			
				$specialIFrame.contents().find('#winefinderForm').submit();
			
			}
		
		});
	
		//end footer special code
	
	
	
		}
		//end getFlavors()
		getFlavors();
} else {}
//
//newsletter main page code
	if ($('#innerIframe').length > 0) {
		function changemainnews() {
			var $mainnewsIFrame = $('#innerIframe');
			
			$mainnewsIFrame.contents().find('select.selectField').wrap('<span class="border"></span>');
			
			if ($mainnewsIFrame.contents().find('body ul#error-list').length == 0 ) { 
				
				$('#innerIframe').css('height','400px').css('margin-top','3px');
			}
		}
		
		var $mainnewsIFrame = $('#innerIframe');
		$mainnewsIFrame.contents().find('select.selectField').wrap('<span class="border"></span>');
		//$mainnewsIFrame.contents().find('select.selectField').wrap('<span class="border"></span>');
		//var $contactIFrame = $('#brandContactForm');
		//$contactIFrame.contents().find('select.selectField').wrap('<span class="border"></span>');
		var $newsSubmit = $mainnewsIFrame.contents().find('body p#submitWrapper input#submit');
		$($newsSubmit).click(function() {
			
			//re-wrap your selects
			$('#innerIframe').css('height','860px').css('margin-top','3px');
					setTimeout(changemainnews, 400);
		});	
	
	
	}
	//end if statement for main newsletter
	

       
//newsletter left column code
	if ($('#innerleftIframe').length > 0) {
		
		//css changes, gallo.newsletterForm is called from within the iframe so this is necessary
		//on submit this needs to be called again, hence the function
		function changenewsform() {
		
		var $leftIFrame = $(window.frames[ "leftnewsletter" ].document);
		$leftIFrame.contents().find("head").append('<link rel="stylesheet" href="/resources/css/newsleftside.css" type="text/css" />');
			
		var $leftnewsIFrame = $('#innerleftIframe');
		$leftnewsIFrame.contents().find('select.selectField').wrap('<span class="border"></span>');
		/* this is not needed if the header is appended with the other css
		$leftnewsIFrame.contents().find('label').css('float','none').css('text-align','left');
		$leftnewsIFrame.contents().find('#newsletterForm div#firstNameWrapper').css('padding','0px 0px 2px 0px');
		$leftnewsIFrame.contents().find('#newsletterForm div#lastNameWrapper').css('padding','0px 0px 2px 0px');
		$leftnewsIFrame.contents().find('#newsletterForm p#emailWrapper').css('padding','0px 0px 2px 0px');
		$leftnewsIFrame.contents().find('#newsletterForm p#DOBWrapper').css('width','270px');
		$leftnewsIFrame.contents().find('#newsletterForm input#dobYear').css('margin-left','15px');
		$leftnewsIFrame.contents().find('#newsletterForm span#dobYearFormat').css('display','block').css('float','right').css('padding','5px 25px 1px 0px');
		$leftnewsIFrame.contents().find('#newsletterForm p#zipCodeWrapper').css('padding','1px 0px');
		$leftnewsIFrame.contents().find('#newsletterForm p#DOBWrapper').css('padding','1px 0px');
		$leftnewsIFrame.contents().find('#newsletterForm p#moreInfoWrapper').css('padding','5px 0px 0px 0px');
		$leftnewsIFrame.contents().find('#newsletterForm p#submitWrapper').css('margin','0px');
		$leftnewsIFrame.contents().find('#newsletterForm p#formRequirements').css('margin','0px');
		*/
		}
		
		//end of changenewsform()
		
		changenewsform();
		var $leftnewsIFrame = $('#innerleftIframe');
		//when the error fields display the newsletter needs its height changed, main code doesn't resize iframe
		var $newsSubmit = $leftnewsIFrame.contents().find('body p#submitWrapper input#submit');
		
		//appending header to add css, because on reload only iframe reloads so this left side restyling fails in JS
		var $leftIFrame = $(window.frames[ "leftnewsletter" ].document);
		$leftIFrame.contents().find("head").append('<link rel="stylesheet" href="/resources/css/newsleftside.css" type="text/css" />');
		
		$($newsSubmit).click(function() {
			if ($leftnewsIFrame.contents().find('body ul#error-list').length > 0) {
			$('#innerleftIframe').css('height','840px').css('margin-top','3px');
			
			//changenewsform();
			//if there are still errors on a subsequent submit this will execute
			} else {
			
				if ( ($leftnewsIFrame.contents().find('body').filter("p:first").text()) === ('Thank you for signing up for the Ecco Domani newsletter.') ) { 
					$('#innerleftIframe').css('height','400px').css('margin-top','3px');
					
					//currently the response 'thank you' iframe has html#iframe, this error is preventing jquery access to elements
					} else {
					
					
					$('#innerleftIframe').css('height','860px').css('margin-top','3px');
					setTimeout(changenewsform, 400);
					
					//this executes on first submit, currently on thank you response 1.5.2011
					}
				
			}
			
			//changenewsform();
		});
		
		
	}



}


