/* ----------------------------------
		Registrations JavaScript
   ---------------------------------- */
vSystem = "register";

function fChangeRegSelect(oForm) {
	if ( $("#package_code").val() != "" ) {
		$("#reg_type_submit").click();
	} 
}


function validateDateOfBirth (themessage, yyyy, mm, dd, minDate, maxDate, ifError ) {
	if ( !isDate(mm, dd, yyyy) ) themessage += " - " + ifError + "\n";
	else
	{
		var dTmp = mm + "/" + dd + "/" + yyyy + " 00:00:00";
		var d = new Date(dTmp);
		var maxD = new Date(maxDate + " 00:00:00");
		var minD = new Date(minDate + " 00:00:00");
		var addMessage = "";
		if ( maxDate && ( Date.UTC(d.getFullYear(), d.getMonth(), d.getDate()) > Date.UTC(maxD.getFullYear(), maxD.getMonth(), maxD.getDate() ) ) ) addMessage += " max: " + maxD.getDate() + "/" + (parseInt(maxD.getMonth())+1) + "/" + maxD.getFullYear() + ",";
		if ( minDate && ( Date.UTC(d.getFullYear(), d.getMonth(), d.getDate()) < Date.UTC(minD.getFullYear(), minD.getMonth(), minD.getDate() ) ) ) addMessage += " min: " + minD.getDate() + "/" + (parseInt(minD.getMonth())+1) + "/" + minD.getFullYear() + ",";
		if ( addMessage != "" ) {
			themessage += " - " + ifError + "\n"
			window["vFieldList"] = window["vFieldList"] + "date_of_birth,";
		}
		//if ( addMessage != "" ) themessage += " - " + ifError + " -" + addMessage + "\n"
	}
	return themessage;
}


function fJobRoleChange(oField) {
	if(oField.value=="100"){
		document.getElementById("div_job_role_other").style.display="block";
	}else{
		document.getElementById("div_job_role_other").style.display="none";
	}
}


function showRegistrationDiv(divname){
	$("#"+divname+"_registrations").slideToggle();
}

function showRegTypeDiv(divid){
	$("#RegTypeHolder_"+divid).slideToggle()
}

/* Checks to see if the registration type selected in the drop down isn't the label. */
function fRegCheckSubmit(oField) {
	if (oField.value != "") {
		oField.form.submit()
	}
}

function fPopSelectWithTwoLevelArray(elName, arr) {
	var currVal = $("select#"+elName).val();
	var options = '<option value="0">Please Choose...</option>';
	for (var i = 0; i < arr.length; i++) {
		options += '<option value="' + arr[i][0] + '">' + arr[i][1] + '</option>';
	}
	$("select#"+elName).html(options);
	$("select#"+elName).val(currVal);
}

function fJournoTerms(oBox) {
	$(oBox).attr("checked") ? $("#journoRegSelect").css("display","block") : $("#journoRegSelect").css("display","none")
}
function populateSelect(el, items) {
    el.options.length = 0;
    if (items.length > 0)
        el.options[0] = new Option('please select', '');

    $.each(items, function () {
        el.options[el.options.length] = new Option(this.name, this.value);
    });
}

function fChangeRegType() {
	if ($("#package_code").val() == "") { return false; }
	// Changing the registration type
	var bShowDoB = 0;
	var bShowStu = 0;
	var bShowSS = 0;
	var bShowGuest = 0;
	var bShowYLComp = 0;
	var bShowYC = 0;
	var bShowYM = 0;
	var bShowJobRole = 1;
	var nMinDoBYear = 1980;
	var sCurrRegType = $("#package_code").val(); // Format is 1:FD so need to get first digit
	aCurrRegType = sCurrRegType.split(":");
	sCurrRegTypeID = aCurrRegType[0];
	sCurrPackage = aCurrRegType[1];

	//alert(sCurrRegTypeID+" "+sCurrPackage)
	//alert(aPCodesDoB+" "+sCurrPackage)
	$.each(aPCodesDoB, function() {
		if(sCurrPackage == this) {
			bShowDoB = true;
			$.each(aPCodesYLCreatives, function() { if(sCurrPackage == this) {
				// Creative
				bShowYC = 1;
				$("#formEleDoBHolder div").hide();
				$("#formEleDoBCreatives").show();
				sDoBType = sDoBTextCreative;
				sDoBUSA = sDoBUSACreative;
				sDoBUK = sDoBUKCreative;
				nMinDoBYear =  sDoBUKCreative.substr(sDoBUKCreative.length-4,4);
			}	});
			$.each(aPCodesYLMarketers, function() { if(sCurrPackage == this) {
				// Marketer
				bShowYM = 1;
				$("#formEleDoBHolder div").hide();
				$("#formEleDoBMarketers").show();
				sDoBType = sDoBTextMarketer;
				sDoBUSA = sDoBUSAMarketer;
				sDoBUK = sDoBUKMarketer;
				nMinDoBYear =  sDoBUKMarketer.substr(sDoBUKMarketer.length-4,4);
			}	});
		}
	});
	// Job Role List
	if ( bShowYC == 1 ) {
		fPopSelectWithTwoLevelArray('job_role_id',arrJobRolesYC);
		$("#job_role_note_standard").hide();
		$("#job_role_note_yc").show();
		$("#job_role_container").show();
	} else if ( bShowYM ==1 ) {
		fPopSelectWithTwoLevelArray('job_role_id',arrJobRolesYClient);
		$("#job_role_note_standard").hide();
		$("#job_role_note_yc").show();
		$("#job_role_container").show();
	} else {
		fPopSelectWithTwoLevelArray('job_role_id',arrJobRoles);
		$("#job_role_note_standard").show();
		$("#job_role_note_yc").hide();
		$("#job_role_container").show();
	}
	//Job Role Hide
	$.each(arrPackageCodesNoJobRole, function() { if(sCurrPackage == this) {
		bShowJobRole = 0;
	} });
	if ( bShowJobRole == 1 ) { $("#job_role_container").show(); } else { $("#job_role_container").hide(); }
	$.each(aPCodesStu, function() {
		if(sCurrPackage == this) {
			// Student
			bShowStu = true;
			$("#formEleDoBHolder div").hide();
			$("#formEleDoBStudents").show();
			sDoBType = sDoBTextStudent;
			sDoBUSA = sDoBUSAStudent;
			sDoBUK = sDoBUKStudent;
			nMinDoBYear =  sDoBUKStudent.substr(sDoBUKStudent.length-4,4);
		}
	});
	$.each(aPCodesSS, function() {
		if(sCurrPackage == this) { bShowSS = true; }
	});
	$.each(arrGuestCodes, function() {
		if(sCurrPackage == this) {  bShowGuest = true; }
	});
	$.each(aPCodesYLComp, function() {
		if(sCurrPackage == this) { bShowYLComp = true; }
	});
	// Photo text
	if ( jQuery.inArray(sCurrPackage, aPCodesNoPhoto) == -1 ) { $("#formEleBadgeNote").show(); } else { $("#formEleBadgeNote").hide(); }

	//Guest
	if (bShowGuest) {
		$("#guest_section").show();
		//fGuestYes();
	} else {
		$("#guest_section").hide();
		fGuestNo();
	}
	// YC DoB
	if (bShowDoB) {
		bValidateDoB = true;
		$(".formEleDoB").show();
		// Populate DoB Year dropdown with relevatn values
		var t = $("#year_of_birth").val(); // Current value
		$("#year_of_birth").html("");
		$("<option value='0'>Year</option>").appendTo("#year_of_birth");
		for ( i=nMaxDoBYear ; i >= nMinDoBYear ; i--) {
			$("<option value='"+i+"'>"+i+"</option>").appendTo("#year_of_birth");
		}
		$("#year_of_birth").val(t);
	} else {
		bValidateDoB = false;
		$(".formEleDoB").hide();
	}
	// Young Lions Comp
	if (bShowYLComp) {
		$(".formEleYLComp").show();
	} else {
		$(".formEleYLComp").hide();
	}
	// Students
	if (bShowStu) {
		bValidateStudent = true;
		$("#for_company_name").html("College/University");
		$("#company_agency_group").hide();
		$("#form_company_type").hide();
		$("#form_job_role").hide();
		$("#form_degree_faculty").show();
	} else {
		bValidateStudent = false;
		$("#for_company_name").html("Company Name");
		$("#company_agency_group").show();
		$("#form_company_type").show();
		$("#form_job_role").show();
		$("#form_degree_faculty").hide();
	}
	// Seminar Speaker
	if (bShowSS) {
		bValidateSeminar = true;
		$("#form_seminar_date").show();
	} else {
		bValidateSeminar = false;
		$("#form_seminar_date").hide();
	}
	
	// Different reg type
	$(".person_label").html(eval("label_name_"+sCurrRegTypeID));
	if(nRegTypeCurrent != sCurrRegTypeID) {
		nRegTypeCurrent = sCurrRegTypeID; // Set global var
	}
	
	// Set hidden form values
	$("#del_form_registration_type_id").val(sCurrRegTypeID)
	$("#del_form_package_code").val(sCurrPackage)
	
}


// ---------------------------------------------
// 		ERROR DISPLAYS : REGISTRATIONS
// ---------------------------------------------
//  These are section spcific error display functions for the registrtion system
//  Requires:  ErrorDisplay.js, jquery.js

function fPressSubmitForm(vFormID) { AllowSubmit = 1; document.getElementById(vFormID).submit(); }
function fPressYes() { AllowSubmit = 1; document.getElementById('button_yes').click(); }

function fErrorLowAccomPref(vNumber) {
	if (typeof vFunction == 'undefined' ) vFunction = "fPressSubmit()";
	var vS = "";
	if (parseInt(vNumber) > 1) vS = "s";
	vMsg = "<h3>! Accommodation Preferences</h3>";
	vMsg += "You have only chosen "+vNumber+" accommodation preference"+vS+". Click 'Confirm' to continue or 'Cancel' to choose  more accommodation preferences."
	vMsg += '<div id="TBAlertButtons" class="center clearfix"><a href="javascript:tb_alert_close()" class="button_style icon_cross'+IEstyles+'" style="margin-right:50px;">Cancel</a><a href="javascript:fPressSubmit()" class="button_style icon_tick'+IEstyles+'">Confirm</a></div>';
	tb_alertbox(vMsg);
	return false;
}

function fYCStudentAccomNote() {
		if (AllowSubmit == 1) { return true; }
		vMsg = "<h3>Accommodation for Young Creatives &amp; Students</h3>";
		vMsg += "<p>Discounted hotel rates are limited and subject to availability.</p><p>Accommodation will be reserved in 2 and 3 star hotels in Cannes on the basis of two participants sharing a twin room.</p><p>The Festival office will randomly allocate two people of the same gender to share a room unless sharers are clearly specified at the time of registration.</p><p>If you would like to share your hotel room with another young creative/young marketer or student, please specify this at the time of registering otherwise we will assume you require a sharer.</p><p>Special room rates cannot be guaranteed if no sharer can be found.</p>"
		vMsg += '<div id="TBAlertButtons" class="center clearfix"><a href="javascript:fPressYes()" style="margin-right:50px;" class="button_style icon_tick'+IEstyles+'">Confirm</a></div>';
		tb_alertbox(vMsg);
		return false;
}

function fAccomYes() {
	// New Method to integrate
	$(".accom_switch_no").hide();
	$(".accom_switch_share").hide();
	$(".accom_switch_resident").hide();
	$(".accom_switch_yes").show();
	
	$("#accom_all").show();
	$(".accom_button").addClass("lesser_button");
	$(".accom_hdr").hide();
	$("#accom_hdr_yes").show();
	$("#accom_button_yes").removeClass("lesser_button");
	$("#accom_dates").show();
	$("#accom_yes").show();
	$("#accom_no").hide();
	$("#accom_sharing").hide();
	$("#accom_local_resident").hide();		
	$("#hotel_needed").val("1");
}

function fAccomNo() {
	// New Method to integrate
	$(".accom_switch_share").hide();
	$(".accom_switch_resident").hide();
	$(".accom_switch_yes").hide();
	$(".accom_switch_no").show();
	
	$("#accom_all").show();
	$(".accom_button").addClass("lesser_button");
	$(".accom_hdr").hide();
	$("#accom_hdr_no").show();
	$("#accom_button_no").removeClass("lesser_button");
	$("#accom_dates").show();
	$("#accom_yes").hide();
	$("#accom_sharing").hide();
	$("#accom_no").show();
	$("#accom_local_resident").hide();		
	$("#hotel_needed").val("0");
}

function fAccomShare() {
	// New Method to integrate
	$(".accom_switch_resident").hide();
	$(".accom_switch_yes").hide();
	$(".accom_switch_no").hide();
	$(".accom_switch_share").show();
	
	$("#accom_all").show();
	$(".accom_button").addClass("lesser_button");
	$(".accom_hdr").hide();
	$("#accom_hdr_share").show();
	$("#accom_button_share").removeClass("lesser_button");
	$("#accom_dates").hide();
	$("#accom_sharing").show();
	$("#accom_yes").hide();
	$("#accom_no").hide();
	$("#accom_local_resident").hide();	
	$("#hotel_needed").val("2");
}

function fAccomLocalResident() {
	// New Method to integrate
	$(".accom_switch_yes").hide();
	$(".accom_switch_no").hide();
	$(".accom_switch_share").hide();
	$(".accom_switch_resident").show();
	
	$("#accom_all").show();
	$(".accom_button").addClass("lesser_button");
	$(".accom_hdr").hide();
	$("#accom_hdr_local_resident").show();
	$("#accom_button_local_resident").removeClass("lesser_button");
	$("#accom_dates").hide();
	$("#accom_sharing").hide();
	$("#accom_yes").hide();
	$("#accom_no").hide();
	$("#accom_local_resident").show();	
	$("#hotel_needed").val("4");
}

// Accommodation  Choice Method
function fAccomChoice(sCh) {
	if (sCh == "name") {
		$("#accom_hotel_choices").show();
		$("#accom_rating_choice").hide();
		$("#for_hotel_special_requests").html("Special Requests");
	} else if (sCh == "rating") {
		$("#accom_hotel_choices").hide();
		$("#accom_rating_choice").show();
		$("#for_hotel_special_requests").html("	Additional Information");
	}
}

// GUEST

function fGuestYes() {
	$("#guest_yes").show();
	$("#guest_added").val("1");
	$("#guest_button_yes").removeClass("lesser_button");
	$("#guest_button_no").addClass("lesser_button");
}
function fGuestNo() {
	$("#guest_yes").hide();
	$("#guest_added").val("0");
	$("#guest_button_no").removeClass("lesser_button");
	$("#guest_button_yes").addClass("lesser_button");
}

// AMEND FUNCTIONS
var bFormSubmitted = false;

function fSubmitForm() {
	if (bFormSubmitted == false) {
		bFormSubmitted = true
		return true
	} else {
		return false;
	}
}

function fShowEdit(sID) {
	$("#div_"+sID).show();
	$("#divEdit_"+sID).hide();
	$("#divEditValue_"+sID).hide();
	$("#divClose_"+sID).show();
	$(sID).focus()
}
function fCloseEdit(sID) {
	$(sID).value="";
	$("#div_"+sID).hide();
	$("#divEdit_"+sID).show();
	$("#divEditValue_"+sID).show();
	$("#divClose_"+sID).hide();
}
function fCheckEmpty(sID) {
	//if( document.getElementById(sID).value == "" ) {
		//$("div_"+sID).style.display = "none"
		//$("divEdit_"+sID).style.display = "block"
	//}
}

function fCConnectText() {
	fErrorAlert("<h3>Cannes Connect</h3><p>Cannes Connect is an online networking tool for Cannes Lions delegates. It allows you to look up fellow attendees and send them a message or meeting request through our website. A delegate's email address will NEVER be disclosed to other delegates.</p><p>By opting out of Cannes Connect, you will not be able to look up other attendees, receive any messages or have access to the Delegate List.</p>");
}

function fUseUserCompanyClearBox() {
	$('input#use_company_details')[0].checked = false;
}

function fUseUserCompanyReg() {
	if ( $('input#use_company_details')[0].checked == true ) {
		// Use Details
		$("#company_name").val(arrUSerCompanyDetails[0]);
		$("#agency_network").val(arrUSerCompanyDetails[1]);
		$("#UltimateGroupCode").val(arrUSerCompanyDetails[2]);
		$("#address_1").val(arrUSerCompanyDetails[3]);
		$("#address_2").val(arrUSerCompanyDetails[4]);
		$("#address_3").val(arrUSerCompanyDetails[5]);
		$("#city").val(arrUSerCompanyDetails[6]);
		$("#postcode").val(arrUSerCompanyDetails[7]);
		$("#country_code").val(arrUSerCompanyDetails[8]);
		if (bValidateSplitTel == true) {
			arrTel = arrUSerCompanyDetails[9].split("|");
			$("#telephone_country").val(arrTel[0]);
			$("#telephone_area").val(arrTel[1]);
			$("#telephone_number").val(arrTel[2]);
			arrMob = arrUSerCompanyDetails[11].split("|");
			$("#mobile_country").val(arrMob[0]);
			$("#mobile_area").val(arrMob[1]);
			$("#mobile_number").val(arrMob[2]);
			arrFax = arrUSerCompanyDetails[12].split("|");
			$("#fax_country").val(arrFax[0]);
			$("#fax_area").val(arrFax[1]);
			$("#fax_number").val(arrFax[2]);
		} else {
			$("#telephone").val(arrUSerCompanyDetails[9]);
			$("#mobile").val(arrUSerCompanyDetails[11]);
			$("#fax").val(arrUSerCompanyDetails[12]);
		}
		$('input#telephone_type_'+arrUSerCompanyDetails[10])[0].checked = true;
		$("#web_address").val(arrUSerCompanyDetails[13]);
		$("#company_type").val(arrUSerCompanyDetails[14]);
		$("#company_type_other").val(arrUSerCompanyDetails[15]);
		if (arrUSerCompanyDetails[14] == "OTH") { $("#div_company_type_other").show(); }
	} else {
		// Don't use details
		$("#company_name").val("");
		$("#agency_network").val("");
		$("#UltimateGroupCode").val("");
		$("#address_1").val("");
		$("#address_2").val("");
		$("#address_3").val("");
		$("#city").val("");
		$("#postcode").val("");
		$("#country_code").val("");
		if (bValidateSplitTel == true) {
			$("#telephone_country").val("");
			$("#telephone_area").val("");
			$("#telephone_number").val("");
			$("#mobile_country").val("");
			$("#mobile_area").val("");
			$("#mobile_number").val("");
			$("#fax_country").val("");
			$("#fax_area").val("");
			$("#fax_number").val("");
		} else {
			$("#telephone").val("");
			$("#mobile").val("");
			$("#fax").val("");
		}
		$('input#telephone_type_general')[0].checked = false;
		$('input#telephone_type_direct')[0].checked = false;
		$("#web_address").val("");
		$("#company_type").val("");
		$("#company_type_other").val("");
		$("#div_company_type_other").hide(); 
	}
}

function fValUploadPhotoForm() {
	if ( $("#photo_file").val() == "" ) {
		fErrorAlert("<h3>Select Photo To Upload</h3><p>Please choose a photo to upload by clicking the Browse button.</p>");
		return false;
	} else {
		return true;
	}
}

//		CANCELLATION PAGE
function fValCancel() {
	form = document.form_cancel_delegate
	resetFormStyles("form_cancel_delegate")	
	var themessage = "";
	window["vFieldList"] = "";
	
	
	if ( bCancelRegIsFree == 0 ) {
		
		if ( cCancelPaymentTypeID == 2 ) {
			// Bank Transfer
			themessage = validateText(themessage, form, "cancellation_reason", "Reason for cancelling.");
			if ( $('input#has_paid_yes')[0].checked == true ) {
				//Paid Already
				themessage = validateSelect(themessage, form, "cancellation_refund_action_id", "Choose a refund option.");
				if ( $("#cancellation_refund_action_id").val() == 1 ) {
					themessage = validateText(themessage, form, "cancellation_bt_account_name", "Account Name");
					themessage = validateText(themessage, form, "cancellation_bt_account_number", "Account Number");
					themessage = validateText(themessage, form, "cancellation_bt_sort_code", "Sort Code");
					//themessage = validateText(themessage, form, "cancellation_bt_iban_number", "IBAN Number");
					//themessage = validateText(themessage, form, "cancellation_bt_bic_swift_number", "BIC/SWIFT");
					themessage = validateText(themessage, form, "cancellation_bt_bank_name", "Bank Name");
					themessage = validateText(themessage, form, "cancellation_bt_bank_address", "Bank Address");
				}
			}
		} else {
			// CC options
			themessage = validateText(themessage, form, "cancellation_reason", "Reason for cancelling.");
			themessage = validateSelect(themessage, form, "cancellation_refund_action_id", "Choose a refund option.");
		}
	} else {
		// Free
		themessage = validateText(themessage, form, "cancellation_reason", "Reason for cancelling.");
	}
	
	if (themessage == "") {
		return true;
	} else {
		showMissingFields(window["vFieldList"]);
		fErrorValidation(themessage);
		return false;
	}
}

function fCancelPaidCheck() {
	if ( $('input#has_paid_yes')[0].checked ) {
		$(".cancel_paid_no").hide();
		$(".cancel_paid_yes").show();
	} else if ( $('input#has_paid_no')[0].checked ) {
		$(".cancel_paid_yes").hide();
		$(".cancel_paid_no").show();
	}
}

function fValidateUploadForm() {
	var bHasValue = false;
	$.each( $('input[type="file"]') , function() {
		if ( this.value != "" ) { bHasValue = true; }
	});
	if (bHasValue) { return true; } else {
		fErrorAlert("<h3>Select File to Upload</h3><p>Please choose a file to upload by clicking the Browse button.</p>");
		return false;
	}
}


