// JavaScript Document
//alert('test');

var checkTimer = setInterval("check()",500);

$(document).ready(function(){ 
//alert('ready');
	$('#PDFormNew_MyPDForm_email').addClass('required email');
	$('#PDFormNew_MyPDForm_name').addClass('required');
	$('#PDFormNew_MyPDForm_phone').addClass('required digits');
	$('#PDFormNew_MyPDForm_school').addClass('required');
	$('#PDFormNew_MyPDForm_address').addClass('required');
	$('#PDFormNew_MyPDForm_suburb').addClass('required');
	$('#PDFormNew_MyPDForm_postcode').addClass('required digits');
	$('#PDFormNew_MyPDForm_state').addClass('required');
	$('#PDFormNew_MyPDForm_card_number').addClass('creditcard');
	$('#PDFormNew_MyPDForm_card_check_value').addClass('digits');
	
	$('#PDFormNew_MyPDForm_name').blur(check);
	$('#PDFormNew_MyPDForm_name01').blur(check);
	$('#PDFormNew_MyPDForm_name02').blur(check);
	$('#PDFormNew_MyPDForm_name03').blur(check);
	$('#PDFormNew_MyPDForm_name04').blur(check);
	if ($('#PDFormNew_MyPDForm_gm_school')){
	$('#PDFormNew_MyPDForm_gm_school').click(check);
	}
	check();
	//if ($("#PDFormNew_MyPDForm_formtype").val() == 'GWIForm'){		
	//	$("#PDFormNew_MyPDForm_payment option[value='invoice']").remove();		
	//}
	if ($("#PDFormNew_MyPDForm_formtype").val() == 'PDForm'){	
		$("#PDFormNew_MyPDForm_ref option[value='A Previous ORIGO Conference']").remove();
		$("#PDFormNew_MyPDForm_ref option[value='ORIGO Mailout']").remove();
		$("#PDFormNew_MyPDForm_ref option[value='ORIGO Professional Development']").remove();
	}
	if($("#PDFormNew_MyPDForm_session_name").text()!='Groundworks Gold Coast') {
		$("#PDFormNew_MyPDForm_ref option[value='Education Views QLD']").remove();
	}
	$("#PDFormNew_MyPDForm_payment").change(onSelectChange);
	
	//$('#PDFormNew_MyPDForm').validate();
	onSelectChange()
	//buildprefs();
	
		
});

function check() {
	//alert("test");
	var count = 1;
	var timeObj = new Date();
	var base = $('#baseprice').val();
	if (base!=""){
		var val = base;
		$(".addname").each(
			function(){
				count++;				
			}
		);
		val*=count;
		// Discount taken out by BT - 15/04/2011
		/*
		 $(".discount").each(
			function(){
				//alert($(".discount").val());
				var darray = $(".discount").val().split('|');
				var expiry = darray[2].split('-');				
				if(darray[2]==''||(expiry[0]>=timeObj.getFullYear()&&parseInt(expiry[1])>=(timeObj.getMonth()+1)&&parseInt(expiry[2])>=timeObj.getDate())){					
					if (darray[3]==0||count>=darray[3]){
						if (darray[0]==0){
							val	= val-darray[1];
						}else{
							val	= val-(val*(1/100*darray[1]));
						}
					}
				}
				
			}						 
		);
		*/	
		var newval = 'AU$'+val.toFixed(2);
		if($('#PDFormNew_MyPDForm_loc').val()=="US"){
			newval = 'US$'+val.toFixed(2);	
		}
		$('#PDFormNew_MyPDForm_session_price').text(newval);
		$("input[name='session_price']").val(newval);
	}
		
/*		for (i=1; i<5; i++) {
			if ($('#PDFormNew_MyPDForm_name0'+i).html()!=null){
			if ($('#PDFormNew_MyPDForm_name0'+i).val()!=("")){count++;}
			}
		}
		val = val * count;
		if ($('#PDFormNew_MyPDForm_gm_school').length>0) {
			if ($("input[@name=gm_school]:checked").val()=="yes"){
				val = val*.80;		
			}
		} 
		
		if ($("#PDFormNew_MyPDForm_formtype").val() != 'pd'){		
			if ((timeObj.getMonth()+1) < 3) {
				val = val-(20*count);		
			}
		}
		
		if ((count>=4)&&($("#PDFormNew_MyPDForm_formtype").val() != 'pd')) {
			//$('#PDFormNew_MyPDForm_session_price').text('$'+val-10);
			val = val*.95;
		}
		var newval = 'AU$'+val.toFixed(2);
		$('#PDFormNew_MyPDForm_session_price').text(newval);
		//PDForm.elements["session_price"] = '$'+val.toFixed(2);
		$("input[name='session_price']").val(newval);
		//alert('name: '+$('#PDFormNew_MyPDForm_name0'+i).val());
	}*/
}

function buildprefs() {
	/*$('.prefselect').each(
		function(){
			var opts=0;
			$(this).children().each(
				function(){opts++;}					
			);
			for (i=1;i<opts;i++){
				clone = $(this).parent().after($(this).parent().clone());				
			}
			//$(this).attr("size", opts);
		}
	);*/
}
/*
function prefchange() {
	$('.prefselect').each(
		$(this).children().each(
			$(this).				
		);
	);
}*/


function onSelectChange(){  
     var selected = $("#PDFormNew_MyPDForm_payment option:selected");            
     if(selected.val() != 'invoice'){           
		 $("#PD_CC_details").css("display","block");
		 $('#PDFormNew_MyPDForm_card_number').addClass('required');
		 $('#PDFormNew_MyPDForm_card_expiry').addClass('required');
		 $('#PDFormNew_MyPDForm_card_name').addClass('required');
		 $('#PDFormNew_MyPDForm_card_check_value').addClass('required');
		 $('#PDFormNew_MyPDForm').validate();
     }  else {
		 $("#PD_CC_details").css("display","none");
		 $('#PDFormNew_MyPDForm_card_number').removeClass('required');
		 $('#PDFormNew_MyPDForm_card_expiry').removeClass('required');
		 $('#PDFormNew_MyPDForm_card_name').removeClass('required');
		 $('#PDFormNew_MyPDForm_card_check_value').removeClass('required');
		 $('#PDFormNew_MyPDForm').validate();
	 }
}  

function attendee(action){
	var id = $("#id").val();
	var rList = $("#PDFormNew_MyPDForm_resource").html();
	//alert(rlist);
	var rSelect="";
	if(rList!=null) rSelect = '<div class="pdForm"><label class="left">Resource Item</label><select id="PDFormNew_MyPDForm_resource'+id+'" style="width:200px" name="resource'+id+'">'+rList+'</select></div>';
	if (action=='add')	{
		$('#additional').after('<div id="att'+id+'" class="attholder"><div class="pdForm"><label class="left addname">Full Name</label><input class="required" type="text" name="name'+id+'" /> <span class="required">*</span><a onclick="remove(\'#att'+id+'\')"> &nbsp; Remove</a></div><div class="pdForm"><label class="left">Email Address</label><input class="email" type="text" name="email'+id+'" /></div>'+rSelect+'</div>');
		id=(id - 1) + 2;
		$("#id").val(id);
		$('#PDFormNew_MyPDForm').validate();
		check();
	}
}

function remove(el){	
	$(el).remove();
	check();
}

function printme(){
	var pw = window.open("about:blank", "_new");
	pw.document.open();
	pw.document.write("<html><head><title>Temporary Print Window</title>"+"<script>\n" +
    "function step1() {\n" +
    "  setTimeout('step2()', 10);\n" +
    "}\n" +
    "function step2() {\n" +
    "  window.print();\n" +
    "  window.close();\n" +
    "}\n" +
    "</scr" + "ipt>\n" +"<style>.pdform {padding:5px 0;}</style><link media='print' type='text/css' href='themes/origo/css/blueprint/print.css' rel='stylesheet'/></head><body onLoad='step1()'>"+$('#print').html()+"</body></html>");
	pw.document.close();	
	pw.print();
}
