//***********************************************
//
//jQuery functions
//
//***********************************************

$(document).ready(function(){
	
	$('input[type*=checkbox],input[type*=radio]').css('border','none');
	$("#confirmDiv").css('display','none');
	
	$('input[name*="Offer Redemption"]').click(function(){

		if($(this).val()==="Registered Card"){
			$("#regCard input").attr('rel','required');
			$("#regCard").slideDown();
			}//end if
		
		else{
			$("#regCard").slideUp();
			$("#regCard input").attr('rel','');
			$('#regCard input[type*="text"]').val("");
			}//end else
		
		});
	
	$('input[name*="Project Printing"]').click(function(){
		
		if($(this).val()==="Amex" || $(this).val()==="Other"){
			$("#printerContact input").attr('rel','required');
			$("#printerContact").slideDown();
			}//end if
		
		else{
			$("#printerContact").slideUp();
			$("#printerContact input").attr('rel','');
			$('#printerContact input[type*="text"]').val("");
			}//end else
		
		});
	
	$('#sendcb').click(function(){
		
		//reset error list
		$('#errorDiv').css("display","none");
		//$('#errorList').empty();
		$("label").attr("class","");
		
		var arr = new Array();
		
		$('#creativeBrief').find("input,textarea,legend").each(function(i){
			
			var curr = $(this);
			var req = $(this).attr("rel");
			var value = curr.val();
			
			if($(this).attr('type')!="hidden"){
			
				if(req==="required"){
					req="true";
					}//end if
			
				else if(req==="title"){
					req="title";
					}//end if
			
				else{
					req="false";
					}//end if	
				
				arr.push(new Array(curr,req,value));
			
				}//end if
			
			});
		
		checkBrief(arr);
		
		return false;

		});
		
	});//end document.ready


function checkBrief(a){
	
	var currChecked="";
	var isChecked=false;
	var isErrors=false;
	var checkBoxErrors=false;
	
	var outputStr="";
	
	for(i=0;i<a.length;i++){
		
		if(a[i][1]==="true"){
			
			if(a[i][0].attr("type")==="checkbox" || a[i][0].attr("type")==="radio"){
				
				if(a[i][0].attr("name")!=currChecked){
					
					if(isChecked===false && currChecked!=""){
						isErrors=true;
						}//end if
					
					isChecked=false;
					currChecked=a[i][0].attr("name");
					}//end if
				
				if(a[i][0].attr("checked")==true){
					$("input[name*='"+a[i][0].attr("name")+"']").parent().attr("class","");
					isChecked=true;
					outputStr+="<strong>"+a[i][0].attr("name")+":</strong> "+a[i][2]+"<br />";
		
					}//end if
					
				else if(a[i][0].attr("checked")!=true && isChecked===false){
					$("input[name*='"+a[i][0].attr("name")+"']").parent().attr("class","error");
					}//end if
				
				}//end if
			
			else if(a[i][2].length<1 || a[i][2]==="mm/dd/yy"){
				isErrors=true;
				a[i][0].parent().attr("class","error");
				}//end else if
			
			else{
				outputStr+="<strong>"+a[i][0].attr("name")+":</strong> "+a[i][2]+"<br />";
				}//end else
			
			}//end if
		
		else if(a[i][1]==="title"){
			outputStr+="<br /><hr /><strong>"+a[i][0].html().toUpperCase()+"</strong><hr /><br />";
			}//end if
		
		else{
			
			if(a[i][0].attr("type")==="checkbox" || a[i][0].attr("type")==="radio"){
				
				if(a[i][0].attr("checked")===true && a[i][0].attr("name").length>0){
					outputStr+="<strong>"+a[i][0].attr("name")+":</strong> "+a[i][2]+"<br />";
					}//end if
					
				}//end if
			
			else{
				
				if(a[i][0].attr("name").length>0 && a[i][2].length>0){
					outputStr+="<strong>"+a[i][0].attr("name")+":</strong> "+a[i][2]+"<br />";
					}//end if
					
				else if(a[i][0].attr("name").length<1 && a[i][2].length>0){
					outputStr+="&mdash;"+a[i][2]+"<br />";
					}//end else
				
				}//end of else
			
			}//end else
		
		}//end for
	
	$('#briefContent').val(outputStr);
	sendBrief(isErrors,outputStr);
	}//end function checkBrief
	
function displayError(){
	
	if($('#errorDiv').css("display")==="none"){
		$('#errorDiv').empty().append("<strong>ALERT!</strong> There were errors while processing your request. Please complete all required fields marked in <strong>red</strong>").slideDown();
		scrollTo(0,200);
		}//end if
		
	}//end if
	
function sendBrief(x,str){
	
	if (x===false && $('#agreement').attr('checked')===true) {
		
		var r=confirm("Are you sure you would like to submit the creative brief?");
		
		if (r===true){
			$('body').append("<div id='processing'></div>").fadeIn();
			$.ajax({  
				type: "POST", 
	  			//url: "http://www.jakgraphicdesign.com/lib/core/sendcb.asp",
	  			url: "lib/core/sendcb.asp",  
	  			data: $('#creativeBrief').serialize(),  
	  			success: function(data) {
	    			if(data==="true"){
	    				
	    				$('#cbForm').slideUp('normal',function(){	
	    				
	    					$('#processing').fadeOut('normal',function(){	
	  							
	  							$("#confirmDiv").empty().append("<h2>Your creative brief has been successfully submitted!</h2><div><p>For your reference, below is the creative brief you've submitted. This copy has also been emailed to you at "+$('#receiptEmail').val()+".</p><p><a href='#' onClick='window.print()' >Print page</a> | <a href='creativebrief.html'> Fill out another creative brief</a><br /></p><p>"+str+"</p></div>").slideDown('normal',function(){
	  								$("input[type*=text],textarea").val("");
	    							$("input[type*=checkbox],input[type*=radio]").attr("checked",false);
	  								});
	  							$(this).remove();
	    						});
	    					
	    					});
	    					
	    				}//end if
	    		
	    			else if(data==="false"){
	    				$('#processing').fadeOut('fast',function(){
	    					alert("Sorry, please try again.");
	    					$(this).remove();
	    					});
	    				}
	    		
	    			//return false;
	    		
	    			},//end success
	  				
	  			error: function(e,r,t){
	  				$('#processing').fadeOut('fast',function(){
		  				alert("Sorry an error occured. Your message couldn't be delivered at this time. Please try again later.");
		  				$(this).remove();
		  				});
	  				//return false;
	  				}//end error  
			
				});
					
			}//end if
			
		}//end if
	
	else{
		displayError();
		}//end else
		
	}//end sendBrief