
$(document).ready(function() {
	$("#newsletter_name").DefaultValue("Enter Name");
	$("#newsletter_email").DefaultValue("Enter Address");
});


$("#submit_newsletter").live("click", function() {
	var email = $("#newsletter_email").val();
    var name = $("#newsletter_name").val();
	$.ajax({
		url: BASEURL + 'include/ajax.php',
		type: "post",
		data: "type=newsletter&name="+name+"&email="+email,
		success: function(result) {
			if(result.match("SUCCESS!")) {
				var msg = result.split("SUCCESS!");
				alert_msg('<p>'+msg[1]+'</p>');
				$("#newsletter_name").val("Enter Name");
				$("#newsletter_email").val("Enter Address");				
			} else
				alert_msg('<p>'+result+'</p>');		
			sURL = unescape(window.location.pathname);
			//window.location.href = sURL;
		}
	});
});

$("#submit_contact").live("click", function() {
	$.ajax({
		url: BASEURL + 'include/ajax.php',
		type: "post",
		data: $("#contact_form").serialize()+"&type=contact",
		success: function(result) {
			if(result.match("SUCCESS!")) {
				var msg = result.split("SUCCESS!");
				alert_msg('<p>'+msg[1]+'</p>');
				clearForm("#contact_form");			
			} else
				alert_msg('<p>'+result+'</p>');			
		}
	});
});

$("#submit_mailer_pack").live("click", function() {
	$.ajax({
		url: BASEURL + 'include/ajax.php',
		type: "post",
		data: $("#mailer_pack_form").serialize()+"&type=mailer_pack",
		success: function(result) {
			if(result.match("SUCCESS!")) {
				var msg = result.split("SUCCESS!");
				alert_msg('<p>'+msg[1]+'</p>');
				clearForm("#mailer_pack_form");			
			} else
				alert_msg('<p>'+result+'</p>');				
			//sURL = unescape(window.location.pathname);
			//window.location.href = sURL;
		}
	});
});


$("#submit_proposal").live("click", function() {
	//$('#uploadify').uploadifyUpload();	
	$.ajax({
		url: BASEURL + 'include/ajax.php',
		type: "post",
		data: $("#proposal_form").serialize()+"&type=proposal",
		success: function(result) {
			if(result.match("SUCCESS!")) {
				var msg = result.split("SUCCESS!");
				alert_msg('<p>'+msg[1]+'</p>');
				clearForm("#proposal_form");
				$("#files").html('');
				$("#pfiles").html('');
			} else
				alert_msg('<p>'+result+'</p>');				
			//sURL = unescape(window.location.pathname);
			//window.location.href = sURL;
		}
	});
});

$(".cancel").live("click", function() {
	rel = $(this).attr("rel");
	var id = $(this);
	$(".pd").each(function() {
		if($(this).val() == rel)
			$(this).remove();
    });
	$("span.fileName").each(function() {
		var name = $(this).html();
		if(name.match(rel))
			$(this).parent().remove();													
	});									 
});


$(".pager").live("click", function() {
	ids = $(this).attr("rel");
	ids_array = ids.split("-");
	pid = ids_array[0];
	id = ids_array[1];
	
	if(id > 0) {
		$.ajax({
			url: BASEURL + 'include/ajax.php',
			type: "post",
			data: "type=pager&id="+id+"&pid="+pid,
			success: function(result) {
				var data = result.split("&amp;");
				var current = data[1].split("::");
				$("#image-"+pid).html('<img src="'+current[0]+'" alt="" width="625" height="'+current[1]+'"/>');
				
				
				if(data[0] == "") {
					$("#prev-"+pid).attr("rel",pid+"-0");
					$("#prev-"+pid).addClass("curd");
				} else {
					$("#prev-"+pid).attr("rel",pid+"-"+data[0]);	
					$("#prev-"+pid).removeClass("curd");
				}
				if(data[2] == "") {
					$("#next-"+pid).attr("rel",pid+"-0");
					$("#next-"+pid).addClass("curd");					
				} else {
					$("#next-"+pid).attr("rel",pid+"-"+data[2]);
					$("#next-"+pid).removeClass("curd");
				}
				//sURL = unescape(window.location.pathname);
				//window.location.href = sURL;
			}
		});	
	}
});


function clearList() {
	$("#files").html('');
	$("#pfiles").html('');	
}

function clearForm(form) {
	// iterate over all of the inputs for the form
    // element that was passed in
    $(':input', form).each(function() {
    	var type = this.type;
		var tag = this.tagName.toLowerCase(); // normalize case
		// it's ok to reset the value attr of text inputs,
		// password inputs, and textareas
		if (type == 'text' || type == 'password' || tag == 'textarea')
   			this.value = "";
		// checkboxes and radios need to have their checked state cleared
		// but should *not* have their 'value' changed
		else if (type == 'checkbox' || type == 'radio')
		   this.checked = false;
 		// select elements need to have their 'selectedIndex' property set to -1
		// (this works for both single and multiple select elements)
		else if (tag == 'select')
			this.selectedIndex = -1;
 	});
}


jQuery.fn.DefaultValue = function(text){
    return this.each(function(){
		//Make sure we're dealing with text-based form fields
		if(this.type != 'text' && this.type != 'password' && this.type != 'textarea')
			return;
		
		//Store field reference
		var fld_current=this;
		
		//Set value initially if none are specified
        if(this.value=='') {
			this.value=text;
		} else {
			//Other value exists - ignore
			return;
		}
		
		//Remove values on focus
		$(this).focus(function() {
			if(this.value==text || this.value=='')
				this.value='';
		});
		
		//Place values back on blur
		$(this).blur(function() {
			if(this.value==text || this.value=='')
				this.value=text;
		});
		
		//Capture parent form submission
		//Remove field values that are still default
		$(this).parents("form").each(function() {
			//Bind parent form submit
			$(this).submit(function() {
				if(fld_current.value==text) {
					fld_current.value='';
				}
			});
		});
    });
};


