// JavaScript Document

$(document).ready(function() {
						   
	$('#slider').nivoSlider({controlNav:false});
	
	$("input").focus(function() {
		$(this).addClass("focus");						  
	});
	
	$("input").focusout(function() {
		$(this).removeClass("focus");					 
	});
	
	$("button").mouseover(function() {
		$(this).addClass("ui-state-hover");
	});
	
	$("button").mouseout(function() {
		$(this).removeClass("ui-state-hover");
	});
	
	$("input[type=submit]").mouseover(function() {
		$(this).addClass("ui-state-hover");
	});
	
	$("input[type=submit]").addClass("ui-state-default").css("padding","5px 10px").css("cursor","pointer");
	$("input[type=reset]").addClass("ui-state-default").css("padding","5px 10px").css("cursor","pointer");
	
	$("input[type=submit]").mouseout(function() {
		$(this).removeClass("ui-state-hover");
	});
	
	
	$("input[type=reset]").mouseover(function() {
		$(this).addClass("ui-state-hover");
	});
	
	$("input[type=reset]").mouseout(function() {
		$(this).removeClass("ui-state-hover");
	});
	
	$("select").focus(function() {
		$(this).addClass("focus");						  
	});
	
	$("select").focusout(function() {
		$(this).removeClass("focus");					 
	});
	
	$("textarea").focus(function() {
		$(this).addClass("focus");						  
	});
	
	$("textarea").focusout(function() {
		$(this).removeClass("focus");					 
	});
	
	$("#edmonton").click(function() {
		$(this).addClass("edmonton");							   
	});

	
	$("#uploadForm").submit(function() {
									 
		//var validate = $("#uploadForm").validate({});
		$("#txtFileNames").val();
		$("#txtFileCount").val("0");
		
		// Check if files are added
		
		var files = $("#hasFiles").val();
		
		var erros = $(".error").is(":visible");
		
		if(errors = true) {
			//alert("ERRORS");	
		}
		
		if(files != 1) {
				alert("You Must Select Files to Upload");
				$("#hasFiles").val("");
			} else {
				$(".loading").show();
				$(".loading").focus();
				
				// Define Uploadify ScriptData for Upload
				$("#file_upload").uploadifySettings('scriptData', {
					'location'	:	$("#ddlLocations").val(),
					'timestamp'	:	$("#txtTimestamp").val()
				});
			
				// Begin File Uploads
				$("#file_upload").uploadifyUpload();
			}
		
		/*
		if(files == "") {
			alert("No Files Selected");	
		} else {
		
			$(".loading").show();
			$(".loading").focus();
			
			// Define Uploadify ScriptData for Upload
			$("#file_upload").uploadifySettings('scriptData', {
				'location'	:	$("#ddlLocations").val(),
				'timestamp'	:	$("#txtTimestamp").val()
			});
		
			// Begin File Uploads
			$("#file_upload").uploadifyUpload();
			
		//}*/
		//send_email(timestamp);
		return false;
		
	});
	
	$(".page-id-607").ready(function() {
									 
		$("#field_260").change(function() {
			//$(this).append('<img src="../images/loading.gif" width="16px" height="16px" />');
			if($("#frm_data_field_261_container").length > 0) {
				$(this).parent().append('<img id="spinner" src="http://carboncopydigital.com/wp-content/themes/ccd/images/LoadingSpinner.gif" />Loading Account Managers');
			}
		});
		
		var attr = $("#field_selectfiles").attr('name');
	});

});

function getReps(branch) {
	$.ajax({
            url: "../app/ajax-functions.php",              
            type: "GET",        
            data: { 'do' : 'the_reps_ddl', 'location' : branch },     
            cache: false,
             
            //success
            success: function (msg) {              
				$("#ddlReps").html(msg);                     
            }       
	});
}

function save_html(timestamp) {
	// Get Field Data
	var branch = $("#ddlLocations").val();
	var ts = timestamp; 
	var name = $("#txtName").val();
	var company = $("#txtCompany").val();
	var email = $("#txtEmail").val();
	var phone =  $("#txtPhone").val();
	var acct = $("#ddlReps").val();
	var jobDesc =  $("#txtJobDesc").val();
	var paper =  $("#txtPaper").val();
	var originals = $("#txtOriginals").val();
	var copies = $("#txtCopies").val();
	var date = $("#txtDateTime").val();
	var po = $("#txtPO").val();
	var jobNo = $("#txtJobNo").val();
	var jobInst = encodeURIComponent($("#txtJobInst").val());
	var proof = "";
	if($("#cbProof").is(":checked") == true) {
		proof += "Yes";
	} else {
		proof += "No";
	}
	//var invoice = $("#input:checkbox[name=cbInvoice]:checked").val();
	var invoice = "";
	if($("#cbInvoice").is(":checked") == true) {
		invoice += "Yes";
	} else {
		invoice += "No";
	}
	var fileCount =  encodeURIComponent($("#txtFileCount").val());
	var fileNames = encodeURIComponent($("#txtFileNames").val());
	
	// Structure Field Data for AJAX Call
	var data = 'location=' + branch + '&timestamp=' + ts + '&name=' + name + '&company=' + company + '&email=' + email + '&phone=' + phone + "&acct=" + acct + '&jobdesc=' + jobDesc + '&paper=' + paper + '&originals=' + originals + '&copies=' + copies + '&date=' + date + '&po=' + po + '&jobno=' + jobNo + '&inst=' + jobInst + '&proof=' + proof + '&invoice=' + invoice + '&filecount=' + fileCount + '&filenames=' + fileNames;
	
	// Disable Form Input
	//$('input').attr('disabled','true');
	//$('textarea').attr('disabled','true');
	//$('select').attr('disabled','true');
	
	$('.loading').show();
	
	// Submit Data to Email Form
	$.ajax({
		url: "../app/uploadHTML.php",
		type: "GET",
		data: data,
		cache: false,
		success: function(html) {
			if(html==1) {
				$('#uploadForm').fadeOut('slow');
				
				$('.done').fadeIn('slow');
			} else {
				alert("Sorry, an unexpexted error occured. Please try again, if the problem persits contact your sales representative.");
			}
		}
	});
}

function send_email(timestamp) {
	// Get Field Data
	var branch = $("#ddlLocations").val();
	var ts = timestamp; 
	var name = $("#txtName").val();
	var company = $("#txtCompany").val();
	var email = $("#txtEmail").val();
	var phone =  $("#txtPhone").val();
	var acct = $("#ddlReps").val();
	var jobDesc =  $("#txtJobDesc").val();
	var paper =  $("#txtPaper").val();
	var originals = $("#txtOriginals").val();
	var copies = $("#txtCopies").val();
	var date = $("#txtDateTime").val();
	var po = $("#txtPO").val();
	var jobNo = $("#txtJobNo").val();
	var jobInst = encodeURIComponent($("#txtJobInst").val());
	var proof = "";
	if($("#cbProof").is(":checked") == true) {
		proof += "Yes";
	} else {
		proof += "No";
	}
	//var invoice = $("#input:checkbox[name=cbInvoice]:checked").val();
	var invoice = "";
	if($("#cbInvoice").is(":checked") == true) {
		invoice += "Yes";
	} else {
		invoice += "No";
	}
	var fileCount =  encodeURIComponent($("#txtFileCount").val());
	var fileNames = encodeURIComponent($("#txtFileNames").val());
	
	// Structure Field Data for AJAX Call
	var data = 'location=' + branch + '&timestamp=' + ts + '&name=' + name + '&company=' + company + '&email=' + email + '&phone=' + phone + "&acct=" + acct + '&jobdesc=' + jobDesc + '&paper=' + paper + '&originals=' + originals + '&copies=' + copies + '&date=' + date + '&po=' + po + '&jobno=' + jobNo + '&inst=' + jobInst + '&proof=' + proof + '&invoice=' + invoice + '&filecount=' + fileCount + '&filenames=' + fileNames;
	
	// Disable Form Input
	//$('input').attr('disabled','true');
	//$('textarea').attr('disabled','true');
	//$('select').attr('disabled','true');
	
	$('.loading').show();
	
	// Submit Data to Email Form
	$.ajax({
		url: "../app/uploadEmail.php",
		type: "GET",
		data: data,
		cache: false,
		success: function(html) {
			if(html==1) {
				$('#uploadForm').fadeOut('slow');
				
				$('.done').fadeIn('slow');
			} else {
				alert("Sorry, an unexpexted error occured. Please try again, if the problem persits contact your sales representative.");
			}
		}
	});
}
