function GetTradeTypes(mtcId)
{
	qryStr	=	'?mtc_cate_id='+(0 < parseInt(mtcId)	?	mtcId	:	-999);

	$('#DIV_TRADE_TYPES').html('<div style="padding-top:8px;width:210px;">'+ShowLoading('')+'</div>');
	$('#DIV_TRADE_TYPES').load('global_trade_types_AJAX.php'+qryStr);

	$('#auto_suggest_txt1').val('');
}

function ShowLocationSelection(locationType)
{
	if(0 == locationType)
		$('#DIV_SERVICE_LOCATION').load('job_process_location_details_AJAX.php?cm_cust_id='+$('#cm_cust_id').val());
	else
		$('#DIV_SERVICE_LOCATION').load('job_process_location_details_AJAX.php?cm_cust_id=-999');
	$('#DIV_SERVICE_LOCATION').fadeIn('slow');
}

function HideLocationSelection()
{
//	$("#DIV_SERVICE_LOCATION").css('display','none');
	$("#DIV_SERVICE_LOCATION").fadeOut('normal');
	$('#country').val('');
	$('#post_code').val('');
	$('#t_address').val('');
	$('#address').val('');
	$('#area').val('');
	$('#city').val('');
	$('#region').val('');
}

function SearchCustomerSuggest(strSearch, strElSuggestId, strElSuggestTxtId)
{
	AutoSuggest(strSearch, 'global_customer_SUGGEST.php', 'str='+strSearch, strElSuggestId, strElSuggestTxtId);
	$('#cm_cust_id').val('');
	$('#DIV_SERVICE_LOCATION').load('job_process_location_details_AJAX.php');
}

function SetCustomerSuggestValue(suggestVal)
{
	suggestValArray	=	('' != suggestVal)	?	suggestVal.split(globalSeparatorString)	:	new Array('', '');
	$('#cm_cust_id').val(suggestValArray[0]);
	if('' != gStrElSuggestTxtId)
		$('#'+gStrElSuggestTxtId).val(trim(suggestValArray[1]));
	else
		$('#auto_suggest_txt').val(trim(suggestValArray[1]));
	CloseSuggest();

	$('#DIV_SERVICE_LOCATION').load('job_process_location_details_AJAX.php?cm_cust_id='+suggestValArray[0]);
	$('#DIV_SERVICE_LOCATION').fadeIn('slow');
	if('undefined' != $("input[name='job_location']:checked").val())
		$("input[name='job_location']:checked").attr('checked', '');
}

function SearchJobSuggest(strSearch, strElSuggestId, strElSuggestTxtId)
{
	AutoSuggest(strSearch, 'global_job_SUGGEST.php', 'str='+strSearch, strElSuggestId, strElSuggestTxtId);
	$('#mtc_cate_id').val('');
	$('#mtt_type_id').val('');
}

function SetJobSuggestValue(suggestVal)
{
	suggestValArray	=	('' != suggestVal)	?	suggestVal.split(globalSeparatorString)	:	new Array('', '', '');
	$('#mtc_cate_id').val(suggestValArray[0]);
	$('#DIV_TRADE_TYPES').load('global_trade_types_AJAX.php?mtc_cate_id='+(0 < parseInt(suggestValArray[0])	?	suggestValArray[0]	:	-999));
	setTimeout("$('#mtt_type_id').val(suggestValArray[1])",300);
	$('#'+gStrElSuggestTxtId).val(trim(suggestValArray[2]));
	CloseSuggest();
}

function MyPreferredDate(selDate, selSession, thisObj)
{
	var imgSrc			=	$(thisObj).attr('src');
	var imgOldName	=	imgSrc.substring(imgSrc.lastIndexOf('/')+1, imgSrc.length);

	if($('#quote_pref_date1').val() == selDate && $('#quote_pref_time1').val() == selSession) {
		$(thisObj).attr('src', imgSrc.replace(imgOldName, (('AM' == selSession)	?	'am_green.png'	:	'pm_green.png')));	// code to deselect
		$('#quote_pref_date1').val('');
		$('#quote_pref_time1').val('');
		return;
	}
	else if($('#quote_pref_date2').val() == selDate && $('#quote_pref_time2').val() == selSession) {
		$(thisObj).attr('src', imgSrc.replace(imgOldName, (('AM' == selSession)	?	'am_green.png'	:	'pm_green.png')));	// code to deselect
		$('#quote_pref_date2').val('');
		$('#quote_pref_time2').val('');
		return;
	}

	if('' != $('#quote_pref_date1').val() && '' != $('#quote_pref_date2').val()) {
		alert('Please de-select an already selected date in-order to select another date!');
		return false;
	}

	if('' == $('#quote_pref_date1').val()) {
		$(thisObj).attr('src', imgSrc.replace(imgOldName, (('AM' == selSession)	?	'am_select.png'	:	'pm_select.png')));	// code to select
		$('#quote_pref_date1').val(selDate);
		$('#quote_pref_time1').val(selSession);
	}
	else {
		$(thisObj).attr('src', imgSrc.replace(imgOldName, (('AM' == selSession)	?	'am_select.png'	:	'pm_select.png')));	// code to select
		$('#quote_pref_date2').val(selDate);
		$('#quote_pref_time2').val(selSession);
	}

	return;
}

var oldVal	=	1;
function NavigateJobQuoteCalendar(val)
{
	if($('#JOB_QUOTE_CALENDAR_'+oldVal)) $('#JOB_QUOTE_CALENDAR_'+oldVal).fadeOut(0);

	$('#JOB_QUOTE_CALENDAR_'+val).fadeIn(1000);
	oldVal	=	val;
}

function AddCustomer()
{
	$('#DIV_POPUP').load('global_customer_add.php');
	LoadPopup('DIV_POPUP', '');
}

function CMFormValidate(frmName)
{
	if(CMFormValidator.exec()) {

		$.post(
						'customers_script.php', 
						$('#'+frmName).serialize(), 
						function(status) {
							if(0 >= status) {
								$('#DIV_POPUP').load('global_customer_add.php?err=1');
								HideLoading();
							}
							else {
								alert('Registration successfull! Thankyou for registering with Better Local Services.');
								$('#mobile').val($('#pop_mobile').val());
								$('#email').val($('#pop_email').val());

								if(BookAppointment(status)) {
									setTimeout("HideLoading()", 300);
									ClosePopup('DIV_POPUP');
								}
							}
						}
					);

	}

	return false;
}

function ViewJobTermsAndConditions()
{
	$('#DIV_POPUP').load('global_job_terms_conditions.php');
	LoadPopup('DIV_POPUP', '');
}

function GoToStep1()
{
	ShowLoading('formsubmit');
	$('#DIV_JOB_STEP_1').fadeIn('speed');
	$('#DIV_JOB_STEP_2').fadeOut('speed');
	HideLoading();
}

function GoToStep2()
{
	if(FormValidator.exec()) {
		$('#DIV_JOB_STEP_1').fadeOut('speed');
		$('#DIV_JOB_STEP_2').fadeIn('speed');
		$('#DIV_JOB_STEP_3').fadeOut('speed');
		HideLoading();
	}

	return false;
}

function GoToStep3()
{
	ShowLoading('formsubmit');
	$('#DIV_JOB_STEP_3').load('find_local_suppliers_step3_INC.php?'+$('#frmJobProcess').serialize());
	$('#DIV_JOB_STEP_3').fadeIn('speed');
	$('#DIV_JOB_STEP_4').fadeOut('speed');
	HideLoading();
}

function FindSupplier()
{
	if(FormValidatorStep2.exec(true)) {
		if(!$('#post_code').val()) {
			alert('"Post code" is a required field');
			$('#post_code').focus();
			return false;
		}
		else if(!$('#address').val()) {
			alert('"Address" is a required field');
			$('#address').focus();
			return false;
		}
		else if(!$('#mobile').val()) {
			alert('"Mobile" is a required field');
			$('#mobile').focus();
			return false;
		}
		else if(!$('#email').val()) {
			alert('"Email address" is a required field');
			$('#email').focus();
			return false;
		}
		else if(1 == $('#c_exists').val() && undefined == $("input[name='job_location']:checked").val()) {
			alert('"Job location" is a required field');
			$('#job_location:eq(0)').focus();
			return false;
		}

		ShowLoading('formsubmit');

//		if(0 == $('#c_exists').val()) {
//			$.post(
//							'find_local_suppliers_error_check.php', 
//							$('#frmJobProcess').serialize(), 
//							function(status) {
//								if(0 >= status)
//									$('#DIV_JOB_STEP_2').load('find_local_suppliers_step2_INC.php');
//								else {
//									$('#DIV_JOB_STEP_3').load('find_local_suppliers_step3_INC.php?'+$('#frmJobProcess').serialize());
//									$('#DIV_JOB_STEP_2').fadeOut('speed');
//									$('#DIV_JOB_STEP_3').fadeIn('speed');
//								}
//	
////								HideLoading();
//							}
//						);
//		}
//		else {
			$('#DIV_JOB_STEP_3').load('find_local_suppliers_step3_INC.php?'+$('#frmJobProcess').serialize());
			$('#DIV_JOB_STEP_2').fadeOut('speed');
			$('#DIV_JOB_STEP_3').fadeIn('speed');
//		}

		HideLoading();
	}
}

function CreateJob()
{
	if(0 == $('#supplier_none').val() && !$('#quote_pref_date1').val() && !$('#quote_pref_date2').val() && !$('#quote_pref_time1').val() && !$('#quote_pref_time2').val()) {
		alert('Please select atleast one preferred date');
		return false;
	}
	else if(1 == $('#is_ssa').val()) {
		var blnAlert	=	true;
		$("input[name='ssa_sm_supp_id[]']").each( function() {
				if(true === blnAlert && !$('#quote_pref_date1_'+this.value).val() && !$('#quote_pref_date2_'+this.value).val() && !$('#quote_pref_time1_'+this.value).val() && !$('#quote_pref_time2_'+this.value).val())
					blnAlert	=	true;
				else
					blnAlert	=	false;
			}
		)

		if(true == blnAlert) {
//			alert('Please select atleast one preferred date for each supplier');
			alert('Please click either "Get Quote" or "Order Now" to select your preferred date and time for appointment');
			return false;
		}
	}

	if(0 == $('#c_exists').val()) {
		ShowLoading('formsubmit');
		$('#DIV_JOB_STEP_4').load('find_local_suppliers_step4_INC.php?'+$('#frmJobProcess').serialize());
		$('#DIV_JOB_STEP_3').fadeOut('speed');
		$('#DIV_JOB_STEP_4').fadeIn('speed');
		HideLoading();
		return false;
	}

	return true;
}

function BookAppointment(cmId)
{
	$('#action').val('JOB_ADD');
	$('#cm_cust_id').val(cmId);
	$('#frmJobProcess').submit();
	return true;
}

function CustomerLogin(frmName)
{
	if(CLFormValidator.exec()) {
		$('#action').val('CUSTOMER_LOGIN');
		$.post(
						'customers_script.php', 
						$('#'+frmName).serialize(), 
						function(status) {
							if(0 >= status)
								alert('Un-expected Error has been occured while trying to Login!\r\nPlease check the password you entered is correct and try again...');
							else {
								alert('Login successfull!');
//								SetCustomerSuggestValue(status);
								BookAppointment(status);
							}

							HideLoading();
							ClosePopup('DIV_POPUP');
						}
					);
	}

	return false;
}

function ShowAvailabilityCalendar(smSuppId)
{
	$('#DIV_POPUP').load('global_job_appointment_calendar.php?sm_supp_id='+smSuppId+'&pref_date1='+$('#quote_pref_date1_'+smSuppId).val()+'&pref_date2='+$('#quote_pref_date2_'+smSuppId).val()+'&pref_time1='+$('#quote_pref_time1_'+smSuppId).val()+'&pref_time2='+$('#quote_pref_time2_'+smSuppId).val());
	LoadPopup('DIV_POPUP', '');
}

function MySSAPreferredDate(selDate, selSession, thisObj, smSuppId)
{
	var imgSrc			=	$(thisObj).attr('src');
	var imgOldName	=	imgSrc.substring(imgSrc.lastIndexOf('/')+1, imgSrc.length);

	if($('#quote_pref_date1_'+smSuppId).val() == selDate && $('#quote_pref_time1_'+smSuppId).val() == selSession) {
		$(thisObj).attr('src', imgSrc.replace(imgOldName, (('AM' == selSession)	?	'am_green.png'	:	'pm_green.png')));	// code to deselect
		$('#quote_pref_date1_'+smSuppId).val('');
		$('#quote_pref_time1_'+smSuppId).val('');
		$('#SPN_PREF_DATE1_'+smSuppId).html('');
		return;
	}
	else if($('#quote_pref_date2_'+smSuppId).val() == selDate && $('#quote_pref_time2_'+smSuppId).val() == selSession) {
		$(thisObj).attr('src', imgSrc.replace(imgOldName, (('AM' == selSession)	?	'am_green.png'	:	'pm_green.png')));	// code to deselect
		$('#quote_pref_date2_'+smSuppId).val('');
		$('#quote_pref_time2_'+smSuppId).val('');
		$('#SPN_PREF_DATE2_'+smSuppId).html('');
		return;
	}

	if('' != $('#quote_pref_date1_'+smSuppId).val() && '' != $('#quote_pref_date2_'+smSuppId).val()) {
		alert('Please de-select an already selected date in-order to select another date!');
		return false;
	}

	if('' == $('#quote_pref_date1_'+smSuppId).val()) {
		$(thisObj).attr('src', imgSrc.replace(imgOldName, (('AM' == selSession)	?	'am_select.png'	:	'pm_select.png')));	// code to select
		$('#quote_pref_date1_'+smSuppId).val(selDate);
		$('#quote_pref_time1_'+smSuppId).val(selSession);
		$('#SPN_PREF_DATE1_'+smSuppId).html(selDate+' ('+selSession+')');
	}
	else {
		$(thisObj).attr('src', imgSrc.replace(imgOldName, (('AM' == selSession)	?	'am_select.png'	:	'pm_select.png')));	// code to select
		$('#quote_pref_date2_'+smSuppId).val(selDate);
		$('#quote_pref_time2_'+smSuppId).val(selSession);
		$('#SPN_PREF_DATE2_'+smSuppId).html(selDate+' ('+selSession+')');
	}

	return;
}

function DonePreferredDateSelection(smSuppId, intClose, orderNow)
{
	if(1 != intClose) {

		if(0 < smSuppId && !$('#quote_pref_date1_'+smSuppId).val() && !$('#quote_pref_date2_'+smSuppId).val() && !$('#quote_pref_time1_'+smSuppId).val() && !$('#quote_pref_time2_'+smSuppId).val()) {
			$('#DIV_BTN_SELECT'+smSuppId).css('display', '');
			$('#DIV_BTN_DESELECT'+smSuppId).css('display', 'none');
			$('#TR_'+smSuppId).removeClass('tr_bgcolor');

			alert('Please select atleast one preferred date');
			return false;
		}
		else if(0 < smSuppId) {
			if(1 == orderNow) {
				$('#DIV_BTN_SELECT'+smSuppId).css('display', '');
				$('#DIV_BTN_DESELECT'+smSuppId).css('display', 'none');
				$('#TR_'+smSuppId).removeClass('tr_bgcolor');

				$('#sm_supp_id').val(smSuppId);
				$('#supplier_none').val(0);
				$('#is_ssa').val(0);
				$('#quote_pref_date1').val($('#quote_pref_date1_'+smSuppId).val());
				$('#quote_pref_time1').val($('#quote_pref_time1_'+smSuppId).val());
				$('#quote_pref_date2').val($('#quote_pref_date2_'+smSuppId).val());
				$('#quote_pref_time2').val($('#quote_pref_time2_'+smSuppId).val());

				if(0 == $('#c_exists').val())
					CreateJob();
				else
					$('#btn_save').click();	//	triggering book appoinment onclick event
			} else {
				$('#DIV_BTN_SELECT'+smSuppId).css('display', 'none');
				$('#DIV_BTN_DESELECT'+smSuppId).css('display', '');
				$('#TR_'+smSuppId).addClass('tr_bgcolor');
			}
		}

	}

	ClosePopup('DIV_POPUP');
}

function ClearAvailabilityCalendar(smSuppId)
{
	if(true == confirm('Are you sure you want to de-select the selected preferred dates?')) {
		$('#quote_pref_date1_'+smSuppId).val('');
		$('#quote_pref_date2_'+smSuppId).val('');
		$('#quote_pref_time1_'+smSuppId).val('');
		$('#quote_pref_time2_'+smSuppId).val('');
		
		$('#SPN_PREF_DATE1_'+smSuppId).html('');
		$('#SPN_PREF_DATE2_'+smSuppId).html('');
	
		$('#DIV_BTN_SELECT'+smSuppId).css('display', '');
		$('#DIV_BTN_DESELECT'+smSuppId).css('display', 'none');
	}
}

function ShowSupplierRecentJobs(smSuppId)
{
	alert('Coming Soon');
	return false;
}

function OrderThisJob(smSuppId)
{
	if(true == confirm('Are you sure you want to Book this Supplier?')) {
		$('#DIV_POPUP').load('global_job_appointment_calendar.php?order_now=1&sm_supp_id='+smSuppId+'&pref_date1='+$('#quote_pref_date1_'+smSuppId).val()+'&pref_date2='+$('#quote_pref_date2_'+smSuppId).val()+'&pref_time1='+$('#quote_pref_time1_'+smSuppId).val()+'&pref_time2='+$('#quote_pref_time2_'+smSuppId).val());
		LoadPopup('DIV_POPUP', '');
	}

	return false;
}

function ForgotPassword(secId, forLogin)
{
	if(0 == secId)	//	customer forgot password
	{
		$('#DIV_POPUP').load('global_customer_forgot_password.php?for_login='+forLogin);
		LoadPopup('DIV_POPUP', '');
	}
	else if(1 == secId)	//	supplier forgot password
	{
		$('#DIV_POPUP').load('global_supplier_forgot_password.php');
		LoadPopup('DIV_POPUP', '');
	}

	return false;
}

function ResetForgottenPassword(secId, frmName)
{
	if(0 == secId && CFPFormValidator.exec())
	{
		$.post(
						'customers_script.php',
						$('#'+frmName).serialize(),
						function(msg) {
							if(0 == msg) {
								$('#DIV_POPUP').load('global_customer_forgot_password.php?err=1');
								HideLoading();
							}
							else {
								$('#DIV_C_MSG_FORGOT_PWD').html(msg);
								$('#DIV_C_MAIN_MSG_FORGOT_PWD').slideDown();

								HideLoading();
								ClosePopup('DIV_POPUP');
							}
						}
					);
	}
	else if(1 == secId && SFPFormValidator.exec())
	{
		ShowLoading('formsubmit');
		$.post(
						'suppliers_script.php',
						$('#'+frmName).serialize(),
						function(msg) {
							if(0 == msg) {
								$('#DIV_POPUP').load('global_supplier_forgot_password.php?err=1');
								HideLoading();
							}
							else {
								$('#DIV_S_MSG_FORGOT_PWD').html(msg);
								$('#DIV_S_MAIN_MSG_FORGOT_PWD').slideDown();

								HideLoading();
								ClosePopup('DIV_POPUP');
							}
						}
					);
	}

	return false;
}

function SetMobileCode(countryVal)
{
	if(-1 != countryVal.indexOf(globalSeparatorString)) {
		spltArray		=	countryVal.split(globalSeparatorString);
		$('#mobile_prefix').val(spltArray[1]);
	} else {
		$('#mobile_prefix').val(globalMobilePrefixDefaultText);
	}
}

function SetMobileCodePopup(countryVal)
{
	if(-1 != countryVal.indexOf(globalSeparatorString)) {
		spltArray		=	countryVal.split(globalSeparatorString);
		$('#pop_mobile_prefix').val(spltArray[1]);
	} else {
		$('#pop_mobile_prefix').val(globalMobilePrefixDefaultText);
	}
}
