// ---------------------------
// Post A Job Page
// Form framework
// Created: 21 Feb, 2008
// Author: Hamish
// ---------------------------

// from global.js
function numbersOnly(e) {
	e.value = e.value.replace(/[^0-9]/g, "");
}

var reqFields = ""; // Must have GLOBAL scope

function loadQuestions(){
	var id = document.getElementById('hidCategoryID').value;
	strRequest = 'blah&id=' + id;
	ajaxrequest(strRequest, showQuestions, 'job_post_questionnaire', 0);
}

function showQuestions(strResponse){
	document.getElementById('questiontime').innerHTML=strResponse;
	showQuestionnaireArea();
	update_questions_visibility();
}

function update_questions_visibility() {
	var form = document.getElementById('frmPostAJob');
	var max = form.elements.length;
	
	for(var i = 0; i < max; i++) {
		var element_id = form.elements[i].id;
		if (element_id != null) {
			if (element_id.indexOf("jsq_a") == 0) {
				var el = document.getElementById(element_id);
				var div_id = "DIV" + element_id;
				var div = document.getElementById(div_id);
				if (div != null) {
					div.style.display = (el.checked == true) ? 'block' : 'none';
				}
			}
		}
	}

}

function displaySpecifiedArea(e) {
	Effect.toggle(e, "appear", {duration: 0.3});
}

function updateJobAddress() {
	var sa = $('txtStreetAddress');
	var jas = $('jobAddressSelection');
	
	if(jas && !sa.value.blank()) {
		jas.innerHTML = "At the " + sa.value + "... address";
	}
	else if(jas) {
		jas.innerHTML = "Address details required at the top of the page";
	}
}

function updateDescriptionStrengthMeter() {
	var smb = $('strengthMeterBar');
	var jd = $('taJobDescription');
	var ew = smb.getWidth();
	var size = 0;

	if(jd.value.length == 0) {
		smb.morph('width: 0px;', {duration: 0.3});
		size = 0;
		return;
	}
	if(jd.value.length >= 1 && jd.value.length < 50) {
		if(size != 25) {
			smb.morph('width: 72px;', {duration: 0.5});
		}
		size = 25;
		return;
	}
	if(jd.value.length >= 50 && jd.value.length < 100) {
		if(size != 50) {
			smb.morph('width: 144px;', {duration: 0.5});
		}
		size = 50;
		return;
	}
	if(jd.value.length >= 100 && jd.value.length < 150) {
		if(size != 75) {
			smb.morph('width: 216px;', {duration: 0.5});
		}
		size = 75;
		return;
	}
	if(jd.value.length >= 150) {
		if(size != 100) {
			smb.morph('width: 288px;', {duration: 0.5});
		}
		size = 100;
		return;
	}
}

function validateRadio(obj){ 
	for(var i=0; i<obj.length; i++){ 
		if(obj[i].checked==true){return true;}
	} 
	return false;
}

function validateForm() {
	var aaa = $('accountActionsArea');
	var tnuu = $('txtNewUserUsername');
	var tnupa = $('txtNewUserPasswordA');
	var tnupb = $('txtNewUserPasswordB');
	var fn = $('txtFirstName');
	var ln = $('txtLastName');
	var lp = $('txtLandlinePhone');
	var mp = $('txtMobilePhone');
	var em = $('txtEmail');
	var nm = $('ddbNotificationMethod');
	var sa = $('txtStreetAddress');
	var sp = $('txtSuburbPostcode');
	var l = $('ddbLocation');
	var mt = $('ddbMarketingType');
	var jt = $('txtJobTitle');
	var jc = $('txtJobCategory');
	var hcid = $('hidCategoryID');
	var hcn = $('hidCategoryName');
	var hcc = $('hidCapturedCategory');
	var jd = $('taJobDescription');
	var hp = $('ddbHireOrPlanning');
	var jb = $('ddbJobBudget');
	var cd = $('ddbCommercialOrDomestic');
	var q1 = $('radQuotes_1');
	var q2 = $('radQuotes_2');
	var q3 = $('radQuotes_3');
	var q4 = $('radQuotes_4');
	var tf = $('ddbTimeframe');
	var tfof = $$('.timeframefield');
	var rjls = $('radJobLocationSame');
	var rjlo = $('radJobLocationOther');
	var jsa = $('txtJobStreetAddress');
	var jsp = $('txtJobSuburbPostcode');
	var jl = $('ddbJobLocation');
	var tc = $('chkTermsAndConditions');
	var rgn = $('radGenerateDetailsNo');
	var rgy = $('radGenerateDetailsYes');
	var pc = $('txtPromotionalCode');
	var hhs = $('ddbHomeHardwareStores');
	var wi   = $('warningInformation');
	if(!tc.checked) {
		alert("Please read and tick the Terms and Conditions");
		tc.focus();
		return false;
	}

	if(!(/^[A-Za-z\'\,\-\s\(\)]{1,}$/.test(fn.value))) {
		alert("Please enter your first (given) name, using valid characters only");
		fn.focus();
		return false;
	}
	if(!(/^[A-Za-z\'\,\-\s\(\)]{1,}$/.test(ln.value))) {
		alert("Please enter your last name (surname), using valid characters only");
		ln.focus();
		return false;
	}
	if(lp.value.blank() && mp.value.blank()) {
		alert("Please enter either a landline or mobile phone number");
		lp.focus();
		return false;
	}
	if(!lp.value.blank() && !(/^[0-9]{10}$/.test(lp.value))) {
		alert("Please provide a valid 10-digit landline phone number, including the area code");
		lp.focus();
		return false;
	}
	if((mp.value.blank() && nm.value == "m") || (!mp.value.blank() && !(/^04[0-9]{8}$/.test(mp.value))) || (!(/^04[0-9]{8}$/.test(mp.value)) && nm.value == "m")) {
		alert("Please provide a valid 10-digit mobile phone number");
		mp.focus();
		return false;
	}
	if((em.value.blank() && nm.value == "e") || (!em.value.blank() && !(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(em.value))) || (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(em.value)) && nm.value == "e")) {
		alert("Please provide a valid email address");
		em.focus();
		return false;
	}
	if(em.value.startsWith("_") || em.value.endsWith("_")) {
		alert("Please provide a valid email address");
		em.focus();
		return false;
	}
	if(!(/^m|e|0$/.test(nm.value))) {
		alert("Please select a valid notification method from the list");
		nm.focus();
		return false;
	}
	if(sa.value.blank()) {
		alert("Please enter your street address");
		sa.focus();
		return false;
	}
	if(l.value == 0) {
		alert("Please search for and select a valid location from the drop-down list");
		sp.focus();
		return false;
	}
	if(aaa) {
		if(mt.value == 0) {
			alert("Please tell us how you heard about us!");
			mt.focus();
			return false;
		}
		else if(mt.value == 84) {
			if(hhs.value == 0) {
				alert("Please enter a valid home hardware promotional code and click the 'validate code' button,\nor select a store from the list of stores provided");
				pc.focus();
				return false;
			}
		}
	}

	if(jt.value.blank()) {
		alert("Please fill in the job title");
		jt.focus();
		return false;
	} else if((/04[0-9]{8}/.test(jt.value))) {
		alert("Please do not enter a mobile phone number in the job title");
		jt.focus();
		return false;
	} else if(((/\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+/.test(jt.value)))) {
		alert("Please do not enter a email address in the job title");
		jt.focus();
		return false;
	}	
	if(jc.value.blank()) {
		alert("Please search for and choose a category that best describes your job");
		jc.focus();
		return false;
	}
	if(jc.value != hcn.value) {
		var answer = confirm("You have changed the category to a custom one - it will be recorded as 'Other' - do you want to proceed?");
		if(answer) {
			hcc.value = jc.value;
			hcn.value = "Other : Other";
			//jc.value = hcn.value;
			hcid.value = 285;
		}
		else {
			return false;
		}
	}
	if(jd.value.blank()) {
		alert("Please enter a description of the job to be completed");
		jd.focus();
		return false;
	} else if((/04[0-9]{8}/.test(jd.value))) {  //no mobile phone number
		alert("Please do not enter a mobile phone number in the job description");
		jd.focus();
		return false;
	} else if(((/\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+/.test(jd.value)))) {
		alert("Please do not enter a email address in the job description");
		jd.focus();
		return false;
	}	
	if(hp.value == 0) {
		alert("Please check one option: Ready to Hire OR Planning and Budgeting");
		hp.focus();
		return false;
	}
	if(jb.value == 0) {
		alert("Please select your approximate budget");
		jb.focus();
		return false;
	}
	if(cd.value == 0) {
		alert("Please select a job type - Commercial or Domestic");
		cd.focus();
		return false;
	}
	if(q1.checked == false && q2.checked == false && q3.checked == false && q4.checked == false) {
		alert("Please select the number of quotes you would like");
		q1.focus();
		return false;
	}	
	if(tf.value == 0) {
		alert("Please select a time frame");
		tf.focus();
		return false;
	}
	
	if(wi.style.display == 'block' && tf.value =='emergency') {
		alert("Please confirm emergency time frame");
		tf.focus();
		return false;
	}
	
	var tfofe = tfof.find(function (x) { return x.visible() && x.down('input').value.search(/^\d{1,2}-\d{1,2}-\d{4}$/) == -1; });
	if (tfofe)
	{
		alert('Please enter a valid time frame date (DD-MM-YYYY).');
		tfofe.down('input').focus();
		return false;
	}
	//job location
	if(rjlo && rjlo.checked) {
		if(jsa.value.blank()) {
			alert("Please enter the address of where the job will be taking place");
			jsa.focus();
			return false;
		}
		if(jl.value == 0) {
			alert("Please search for and select a valid job location from the drop-down list");
			jsp.focus();
			return false;
		}
	}
	
	// Job specific questions
	if (!checkManditoryJsq()) {
		alert("Please answer all job specific questions");
		return false;
	}
	
	if(aaa) {
		if(rgn.checked) {
			//if(!(tnuu.value.blank())) {
				if(!(/^[0-9A-Za-z\_]{5,}$/.test(tnuu.value))) {
					alert("Username must be at least 5 characters in length and only consist of letters, numbers and underscores");
					tnuu.focus();
					return false;
				}
				if(tnupa.value.length < 6) {
					alert("Password must be at least 6 characters in length.");
					tnupa.focus();
					return false;
				}
				if(tnupb.value != tnupa.value) {
					alert("Passwords do not match.");
					tnupa.focus();
					return false;
				}
			//}
		}
	}
	
return true;
}

Event.observe(window, 'load', function() {
	var bpmj = $('btnPostMyJob');
	//var la = $('loginArea');
	//var fpa = $('forgottenPasswordArea');
	//var bpajl = $('btnPostAJobLogin');
	//var bfpa = $('btnForgotPasswordSubmit');
	//var bcu = $('btnCheckUsername');
	//var tnuu = $('txtNewUserUsername');
	//var tnupa = $('txtNewUserPasswordA');
	//var tnupb = $('txtNewUserPasswordB');
	var djb = $('ddbJobBudget');
	var tq = $('thirdQuote');
	var fq = $('fourthQuote');	
	var q2 = $('radQuotes_2');
	var q3 = $('radQuotes_3');
	//var rubn = $('radUsedBeforeNo');
	//var ruby = $('radUsedBeforeYes');
	//var rjls = $('radJobLocationSame');
	//var rjlo = $('radJobLocationOther');
	var jla = $('jobLocationArea');
	//var aaa = $('accountActionsArea');
	//var ada = $('accountDetailsArea');
	var hcc = $('hidCapturedCategory');
	var hcn = $('hidCategoryName');
	var hci = $('hidCategoryID');
	var tllp = $('txtLandlinePhone');
	var tmp = $('txtMobilePhone');
	var sa = $('txtStreetAddress');
	var jd = $('taJobDescription');
	var sp = $('txtSuburbPostcode');
	//var jsp = $('txtJobSuburbPostcode');
	var rgn = $('radGenerateDetailsNo');
	var rgy = $('radGenerateDetailsYes');
	var tf = $('ddbTimeframe');
	var tfof = $$('.timeframefield');
	var pc = $('txtPromotionalCode');
	var bvpc = $('btnValidatePromotionalCode');
	var mt = $('ddbMarketingType');
	var hhs = $('ddbHomeHardwareStores');
	var finance = $('financeOption');

	if (bpmj != undefined) {
		bpmj.enable();
		
		sp.observe('keyup', function(event) {
			if (sp.value.length >= 3) {
				getSuburb(sp.value);
			}
		});
		//jsp.observe('keyup', function(event) {
		//	lookupLocation(jsp.id, 'dynamicJobLocationArea', 'ddbJobLocation');
		//	if (sp.value.length >= 3) {
		//		getSuburb(sp.value);
		//	}
		//});
		jd.observe('keyup', function(event) {
			updateDescriptionStrengthMeter();
		});
		sa.observe('keyup', function(event) {
			updateJobAddress();
		});
		tllp.observe('keyup', function(event) {
			numbersOnly(tllp);
		});
		tmp.observe('keyup', function(event) {
			numbersOnly(tmp);
		});
		//rjls.observe('click', function(event) {
		//	if(jla.visible()) {
		//		displaySpecifiedArea(jla);
		//	}
		//});
		//rjlo.observe('click', function(event) {
		//	if(!jla.visible()) {
		//		displaySpecifiedArea(jla);
		//	}
		//});
		djb.observe('change', function(event) {

			// 6 ($0-$149),	1 ($150-$499), 8 ($500-$1,099), 9 ($1,100-$2,499), 10 ($2,500-$4,999), 11 ($5,000-$9,999)	
			// 3 ($10,000-$49,999), 4 ($50,000-$99,999), 5 ($100,000+)	
		
			if(djb.value == "selected" || djb.value == 0 || djb.value == 6 || djb.value == 1) {
				// If under $500
				tq.hide();
				fq.hide();
				q2.checked = true;
				finance.hide();
			}
			else if(djb.value == 8 || djb.value == 9) {
				// If $500 - $2500
				tq.show();
				fq.hide();
				q2.checked = true;
				if(djb.value == 8) {
					finance.hide();
				} else if (djb.value == 9) {
					finance.show();
				}
			}	
			else if(djb.value == 10 || djb.value == 11) {
				tq.show();
				fq.hide();
				q3.checked = true;
				finance.show();
			}    	
			else {
				tq.show();
				fq.show();
				q3.checked = true;
				finance.show();
			}
		});
		$('txtTimeframedate').observe('blur', function(event) {
			$('txtTimeframedate').value = $F('txtTimeframedate').replace(/\//g, '-');
		});
		tfof.invoke('hide');
		if($F('txtTimeframedate') != "") {
			tfof.invoke('show');
		}
		tf.observe('change', function (e) {
			tfof.findAll(function (x) {
				return x.visible();
			}).each(function (x) {
				new Effect.toggle(x, 'blind', { duration: 0.5 }); 
			});
			if ($('timeframefield' + tf.value) && !$('timeframefield' + tf.value).visible()) {
				new Effect.toggle($('timeframefield' + tf.value), 'blind', { duration: 0.5 });
			} else {
				$('timeframefielddate').hide();
			}
		});
		
		/*if($('hidLocation')) {
			reLookupLocation($F('hidLocation'), 'dynamicLocationArea', 'ddbLocation');
		}
		if($('hidJobLocation')) {
			reLookupLocation($F('hidJobLocation'), 'dynamicJobLocationArea', 'ddbJobLocation');
		}*/
		
		help_array = new Array();
		title_array = new Array();
		title_array['txtJobTitleHelp']	= "Help: What would you like done?";
		title_array['txtJobCategoryHelp']	= "Help: What category?";
		title_array['ddbJobBudgetHelp']	= "Help: Approximate budget?";
		title_array['radQuotesHelp']	= "Help: Number of businesses?";
		title_array['ddbTimeframeHelp']	= "Help: Timeframe?";
		title_array['txtNotificationMethodHelp'] = "Help: Notification Method?";
		title_array['contactDetails']	= "Why do we require your contact details?";
		help_array['txtJobTitleHelp']	= "<p>Please be brief but detailed. This information will help the Provider determine if they are able to provide the requested service or not.</p><p>Include dimensions, access, brands, and any specific information related to the service you require.</p><p>The work category (eg, Plumbing) is not necessary in the summary, as that will be selected next.</p>";
		help_array['txtJobCategoryHelp']		= "<p>Type in a few letters of the work category, eg, \"plumb\" or \"elect\", then select the actual category from the drop down list.</p><p>Choose the closest matching category and sub category to the service you require.</p>";
		help_array['ddbJobBudgetHelp']			= "<p>Approximate budgets are used to select appropriate providers for your request. For example, some providers only do small jobs, and some providers only do large jobs.</p><p>Amounts are not quotations or budgets and are not binding in any way for you or the provider.</p>";
		help_array['radQuotesHelp']				= "<p>We will aim to arrange the specified number of businesses to call you back.</p><p>Please note that businesses that contact you are independent businesses and are not directly, or in any way, employed by us.</p>";
		help_array['ddbTimeframeHelp']			= "<p>Generally, longer time frame requests receive a better response as this allows the Provider to accurately schedule time for your request.</p><p>Whichever timeframe you specify, available businesses should contact you within 1-2 business days.</p><p>Quotation times and dates (if required) can then be discussed directly with the Provider.</p>";
		help_array['txtNotificationMethodHelp']	= "<p>This is how we will keep you updated regarding your job's progress.</p>";
		help_array['contactDetails']	= "<p>We require your contact details so that our providers can contact you! As soon as you place your quote request we contact our providers and supply those interested with your contact details. It is that simple.</p><p>You can see how important it is that we have accurate contact information so our providers can get in touch with you.</p><p>We also respect your privacy. Your contact information will be made available only to those providers interested in quoting for your work and will not be sold or rented out to other parties without your permission.</p>";
		tsfi = document.getElementsByClassName("extraFieldInformation");
		for(i=0; i<tsfi.length; i++) {
			if(tsfi[i].id != "") {
				var thid = tsfi[i].id;
				new Tip(tsfi[i].id, help_array[thid], {title: title_array[thid], className: "fixed", effect: "appear", hook:{target: 'topRight', tip: 'topLeft'}});
			}
		}
	}
});

function show_calendar_popup (e) {
	var c = new calendar1($(e));
	c.year_scroll = true;
	c.time_comp = false;
	return c.popup();
}

function show_terms (e) {

}

function showEmergencyWarning() {
	document.getElementById('timeframeSection').className = 'timeframeSection';
	/*if(document.getElementById('emergency').selected) {
		document.getElementById('warningInformation').style.display = 'block';
	} else {
		document.getElementById('warningInformation').style.display = 'none';
	}*/
	document.getElementById('radQuotes_2').disabled = false;
	if(document.getElementById("radQuotes_3")) {
			document.getElementById('radQuotes_3').disabled = false;
	}


}

function okEmergencyTimeFrame() {
	document.getElementById('warningInformation').style.display = 'none';
  	document.getElementById('timeframeSection').className = 'timeframeWarningSection';
  	
	document.getElementById('radQuotes_2').disabled = true;

	if(document.getElementById("radQuotes_3")) {
			document.getElementById('radQuotes_3').disabled = true;
	}

	document.getElementById('radQuotes_1').checked = true;
}

function cancelEmergencyTimeFrame() {
	document.getElementById('warningInformation').style.display = 'none';
	document.getElementById('ddbTimeframe').options[0].selected = true;
}
 

