// Home page Flash
 $(document).ready(function(){
 
	//Footer Copyright
	var current_date = new Date();
	year_value = current_date.getFullYear();
	$('#CopyRight').html("&copy; " + year_value + " Associated Press. All Right Reserved." );
	
	//Form Email Text Function
	$('input:text[name="text_Email"]').click(function(){
		if (this.value == "Your Email Address"){ this.value = ""; }
			}).blur(function(){
		if ($.trim(this.value) == ""){ this.value = "Your Email Address"; }
	});
	
	
	var phoneName="";
	// Select phone options depending on phone selected from dropdown menu
	$('select[name="select_Phone"]').change(function(){	
		phoneName = $(this).val();
		sendDataToFlash1(phoneName);
		$('#PhoneList li a').removeClass('On');
		$('#PhoneList li a').each(function(i){
			if ($(this).text() == phoneName){
				$(this).addClass('On');
			}
		});
	});
	
	// Change dropdown menu depending on phone option selected
	$('#PhoneList li a').click(function(){
		$('#PhoneList li a').removeClass('On');
		$(this).addClass('On');
		phoneName = $(this).text();
		
		var selElem = document.formPhone.select_Phone;
		for (i=0; i<selElem.length; i++){			
			if (selElem[i].value == phoneName ){
				selElem[i].selected = true;
			}
		}
		sendDataToFlash1(phoneName);
	});
	
	// Functions to feed flash with data
	
	// end of flash function
	
	//Form Validation
	$("#formPhone").submit(function(){
		var strData = "";
		var email = $.trim($('input:text[name="text_Email"]').val());
		phoneName = $('select[name="select_Phone"]').val();
		phoneName = $.trim(phoneName);
		
		if (phoneName == ""){ 
			if (!$('select[name="select_Phone"]').hasClass('error')){
				$('select[name="select_Phone"]').addClass('error');
			}			
		}else{
			$('select[name="select_Phone"]').removeClass('error');
		}
		if (email =="" || email == "Your Email Address"){			
			if (!$('#EmailWrap').hasClass('error')){
				$('#EmailWrap').addClass('error');
			}
		}else{
			$('#EmailWrap').removeClass('error');
		}
		
		if (phoneName!= "" && (email !="" && email != "Your Email Address")){
			strData = "email=" + email + "&phDevice="+phoneName
			sendData(strData);
		}
		return false;
	});
	
	function sendData(strData){
	    var errMsg = "";		
	    $.ajax({
	        url: "worldcupprocess.php",
	        type:"POST",
	        dataType:"application/x-www-form-urlencoded",
	        data:strData,
	        success: function(msg){			        
				msg = $.trim(msg);
				if(msg=="1"){
					//alert("Success");
					alert("Thank you. An email has been sent to you using the provided email address. If you don't receive it in a few minutes, please check your spam filter.");
				}else{
					alert("Sorry but an error occured while processing your request. Please enter your e-mail address and try again.");
				}
				
	        }
	    });
	} 
	
	// Helper function
	function GetSiteUrl(){
	    var siteUrl = location.href;
	    var arrSiteUrl = siteUrl.split("/");
	    var siteDom = arrSiteUrl[0] + "//" + arrSiteUrl[2] + "/";
	    return siteDom;
	}
});

