// JavaScript Document

var ajax = new sack();
var ajax_busy=false;
var selected = new Array();
var turingValue = null;
var formScript = {};
var rightArrow = document.createElement('img');
rightArrow.src = SITE_URL+"/images/right_arrow.gif";
rightArrow.alt = "&gt;";

var city_name='';
var state_name='';
var state_level = 2;
var country_data_obj;
var default_zip_label = "ZIP (Postal) Code";
var currentCatDailyPromoteTariff;
var currentLocationDetails;
var city_level;

var defaultAreaLevelName = Array();
defaultAreaLevelName[0] = 'continent';
defaultAreaLevelName[1] = 'country';
defaultAreaLevelName[2] = 'state/province';
defaultAreaLevelName[3] = 'county/district';
defaultAreaLevelName[4] = 'city/village';

function changeArea(){
	get_location_details(this.value);
}

function changeCategory(){
	get_category_details(this.value);
}

function get_location_details(area_id, not_select){
	if(ajax_busy){
		setTimeout("get_location_details("+area_id+")",100);
		return;
	}
	
	var location_select = document.getElementById('ad_location');
	if (location_select && location_select.options[location_select.selectedIndex].disabled && !not_select) {
		location_select.selectedIndex = selected[1];
		return;
	}
	
	displayWaitMessage();
	
	var details_page=site_root_stripped+'/index.php?module=ads&op=gac&';
	ajax_busy=true;
	ajax.requestFile = details_page+"ad_location="+area_id;
	ajax.onCompletion = function(){ showLocationDetails(area_id); };	// Specify function that will be executed after file has been found
	ajax.runAJAX();
}

function get_category_details(cat_id, not_select){
	var temp = document.getElementById('page_displayed_title');
	temp.innerHTML = "New Post";

	if(ajax_busy){
		setTimeout("get_category_details('"+cat_id+"')",100);
		return;
	}
	
	var category_select = document.getElementById('new_ad_category');
	if (category_select && category_select.options[category_select.selectedIndex].disabled && !not_select) {
		category_select.selectedIndex = selected[2];
		return;
	}
	
	displayWaitMessage();
	
	var details_page=site_root_stripped+'/index.php?module=ads&op=gcc&';
	ajax_busy=true;
	ajax.requestFile = details_page+"cat_id="+cat_id;
	ajax.onCompletion = function(){ showCategoryDetails(cat_id); };	// Specify function that will be executed after file has been found
	ajax.runAJAX();
}

function getForm(ad_type){
	if(ajax_busy){
		setTimeout("get_category_details('"+cat_id+"')",100);
		return;
	}
	
	var details_page=site_root_stripped+'/index.php?module=ads&op=gf&';
	ajax_busy=true;
	ajax.requestFile = details_page+"type="+ad_type;
	ajax.onCompletion = function(){ showForm(); };	// Specify function that will be executed after file has been found
	ajax.runAJAX();
}

function showForm(){
	eval(ajax.response);
	ajax_busy=false;
	requiredFields = false;
	details = details.replace(/<email>/, userMail);
	details = details.replace(/<first_name>/, userFirstName);
	details = details.replace(/<last_name>/, userLastName);
	
	form_placeholder.innerHTML = details;
	
	modifyFormBylocation();
	
	if(formScriptEncoded){
		eval(formScriptEncoded);
	}
	hideWaitMessage();
}

function modifyFormBylocation(){
	if(country_data_obj){
		var temp_zip_label = document.getElementById('zip_label');
		if(temp_zip_label){
			if(country_data_obj.zip_label){
				temp_zip_label.innerHTML = country_data_obj.zip_label;
			}
			else{
				temp_zip_label.innerHTML = default_zip_label;
			}
		}
		
		var temp_zip2_label = document.getElementById('zip2_label');
		if(temp_zip2_label){
			if(country_data_obj.zip2_label){
				temp_zip2_label.innerHTML = country_data_obj.zip2_label;
			}
			else{
				temp_zip2_label.innerHTML = '';
			}
		}
		var temp_zip2_field = document.getElementById('zip2_field');
		if(temp_zip2_field){
			if(country_data_obj.zip2_label){
				temp_zip2_field.style.display = 'inline';
			}
			else{
				temp_zip2_field.style.display = 'none';
			}
		}
	}
	var temp_city_field = document.getElementById('city_name');
	if(temp_city_field){
		if(currentLocationDetails && currentLocationDetails.selected && country_data_obj){
			for(var i in country_data_obj.area_labels){
				if(country_data_obj.area_labels[i] == 'City'){
					city_level = parseInt(i)+1; // this lavels are relative to country (level 1)
				}
			}
			
			if(currentLocationDetails.selected.level == city_level){
				city_name = currentLocationDetails.selected.name;
			}
			else if(currentLocationDetails.selected.level > city_level){
				for(var i=0;i<currentLocationDetails.forefathers.length;i++){
					if(currentLocationDetails.forefathers[i].level == city_level){
						city_name = currentLocationDetails.forefathers[i].name;
					}
				}
			}
			else{
				city_name = '';
			}
		}
		
		temp_city_field.value = city_name;
	}
	
	
	var temp_state_field = document.getElementById('state_name');
	if(temp_state_field){
		if(currentLocationDetails.selected.level == state_level){
			state_name = currentLocationDetails.selected.name;
		}
		else if(currentLocationDetails.selected.level > state_level){
			for(var i=0;i<currentLocationDetails.forefathers.length;i++){
				if(currentLocationDetails.forefathers[i].level == state_level){
					state_name = currentLocationDetails.forefathers[i].name;
				}
			}
		}
		else{
			state_name = '';
		}
		
		temp_state_field.value = state_name;
	}
}

function showLocationDetails(url){
	eval(ajax.response);
	ajax_busy=false;
	
	country_data_obj = details.country_data;
	currentLocationDetails = details;
	
	//forefathers
	ad_loc_placeholder.innerHTML = '';
	if(details.forefathers){
		var alink = document.createElement('a');
		alink.innerHTML = 'root';
		alink.href = "javascript: get_location_details('', true)";
		ad_loc_placeholder.appendChild(alink);
		ad_loc_placeholder.innerHTML += '&nbsp;';
		ad_loc_placeholder.appendChild(rightArrow);
		ad_loc_placeholder.innerHTML += '&nbsp;';
			
		for(var i=0;i<details.forefathers.length;i++){
			var alink = document.createElement('a');
			alink.innerHTML = details.forefathers[i].name;
			alink.href = "javascript: get_location_details("+details.forefathers[i].area_id+", true)";
			ad_loc_placeholder.appendChild(alink);
			ad_loc_placeholder.innerHTML += '&nbsp;';
			ad_loc_placeholder.appendChild(rightArrow);
			ad_loc_placeholder.innerHTML += '&nbsp;';
		}
	}
	if(details.selected){
		var alink = document.createElement('a');
		alink.innerHTML = details.selected.name;
		ad_loc_placeholder.appendChild(alink);
		current_loc=details.selected.area_id;
		
		modifyFormBylocation();
	}
	else{
		current_loc=0;
	}
		
	
	//childs
	ad_loc_select_placeholder.innerHTML = '';
	if(details.childs){
		current_loc=false;
		var location_select = document.createElement('select');
		location_select.setAttribute('id','ad_location');
		location_select.onchange = changeArea;
	
		var selected_opt = document.createElement('option');
		
		var select_caption;
		
		if(country_data_obj && country_data_obj.area_labels[details.childs[0].level-1]){
			select_caption = 'select '+country_data_obj.area_labels[details.childs[0].level-1]; // -1 because the label is relative to the country
		}
		else{
			select_caption = 'select '+defaultAreaLevelName[details.childs[0].level];
		}
		selected_opt.innerHTML=select_caption;
		selected_opt.setAttribute('disabled', true);
		selected_opt.setAttribute('selected', true);
		
		location_select.appendChild(selected_opt);
	
		for(var i=0; i<details.childs.length; i++){
			var child_opt = document.createElement('option');
			child_opt.innerHTML=details.childs[i].name;
			child_opt.value=details.childs[i].area_id;
			location_select.appendChild(child_opt);
		}
		ad_loc_select_placeholder.appendChild(location_select);
		selected[1] = location_select.selectedIndex;
		emulate_disabled(location_select);
	}
	
	
	hideWaitMessage();
}

function showCategoryDetails(url){
	
	eval(ajax.response);
	ajax_busy=false;
	
	form_placeholder.innerHTML = '';
	
	//forefathers
	ad_cat_placeholder.innerHTML = '';
	if(details.forefathers){
		var alink = document.createElement('a');
		alink.innerHTML = 'root';
		alink.href = "javascript: get_category_details('', true)";
		ad_cat_placeholder.appendChild(alink);
		ad_cat_placeholder.innerHTML += '&nbsp;';
		ad_cat_placeholder.appendChild(rightArrow);
		ad_cat_placeholder.innerHTML += '&nbsp;';
			
		for(var i=0;i<details.forefathers.length;i++){
			var alink = document.createElement('a');
			alink.innerHTML = details.forefathers[i].name;
			alink.href = "javascript: get_category_details("+details.forefathers[i].cat_id+", true)";
			ad_cat_placeholder.appendChild(alink);
			ad_cat_placeholder.innerHTML += '&nbsp;';
			ad_cat_placeholder.appendChild(rightArrow);
			ad_cat_placeholder.innerHTML += '&nbsp;';
		}
	}
	if(details.selected){
		var alink = document.createElement('a');
		alink.innerHTML = details.selected.name;
		ad_cat_placeholder.appendChild(alink);
		if(details.selected.table){
			getForm(details.selected.ad_type);
		}
		current_cat=details.selected.cat_id;
		currentCatDailyPromoteTariff = details.selected.promote_tariff;
	}
	else{
		current_cat=0;
	}
		
	//childs
	ad_cat_select_placeholder.innerHTML = '';
	if(details.childs){
		var category_select = document.createElement('select');
		category_select.setAttribute('id','ad_category');
		category_select.onchange = changeCategory;
	
		var selected_opt = document.createElement('option');
		selected_opt.innerHTML='select category';
		selected_opt.setAttribute('disabled', true);
		selected_opt.setAttribute('selected', true);
		
		category_select.appendChild(selected_opt);
	
		for(var i=0; i<details.childs.length; i++){
			var child_opt = document.createElement('option');
			child_opt.innerHTML=details.childs[i].name;
			child_opt.value=details.childs[i].cat_id;
			category_select.appendChild(child_opt);
		}
		ad_cat_select_placeholder.appendChild(category_select);
		selected[1] = category_select.selectedIndex;
		emulate_disabled(category_select);
	}
	
	if(!(details.selected && details.selected.table)){
		hideWaitMessage();
	}
}

if(newAd){
	var ad_loc_placeholder = document.getElementById('ad_loc_placeholder');
	var ad_loc_select_placeholder = document.getElementById('ad_loc_select_placeholder');
	var ad_cat_placeholder = document.getElementById('ad_cat_placeholder');
	var ad_cat_select_placeholder = document.getElementById('ad_cat_select_placeholder');
	var form_placeholder = document.getElementById('form_placeholder');
	
	get_location_details(current_loc);
	get_category_details(current_cat);
}

function processForm(thisform){
	if(!current_loc){
		alert("Please select the location!");
		return false;
	}
	if(!checkEmail(thisform.email.value)){
		alert("Please enter a valid email address!");
		return false;
	}
	if(!thisform.first_name.value){
		alert("Please fill in your first name!");
		return false;
	}
	if(!thisform.last_name.value){
		alert("Please fill in your last name!");
		return false;
	}
	if(!thisform.ad_title.value){
		alert("Please enter a title for your ad!");
		return false;
	}
	if(!thisform.description.value){
		alert("Please enter a description for your ad!");
		return false;
	}
	
	if(!thisform.user_phone.value){
		alert("Please enter your phone number!");
		return false;
	}
	
	if(!thisform.user_phone_type.value){
		alert("Please select your phone number type!");
		return false;
	}
	
	if(thisform.is_paid && thisform.is_paid.checked && !IsInt(thisform.paid_period.value)){
		alert("Please enter the promotion period!");
		return false;
	}
	
	if(thisform.state_name && !thisform.state_name.value){
		alert("Please enter the state / province!");
		return false;
	}
	
	if(!thisform.terms_of_service.checked){
		alert("You must agree with our Terms of Service!");
		return false;
	}

	if(newAd){
		if(!turingValue){
			remoteVerifyTuring(thisform.code.value, thisform.entered.value);
			return false;
		}
		if(turingValue!='good'){
			alert("Type in the numbers / letters you see in the box below!");
			return false;
		}
		var hidden_loc = document.getElementById('area_id');
		hidden_loc.setAttribute('value', current_loc);
		
		var hidden_cat = document.getElementById('cat_id');
		hidden_cat.setAttribute('value', current_cat);
	}
	
	if(formScript && formScript.customProcessForm){
		if(!formScript.customProcessForm(thisform)){
			return false;
		}
	}
	return true;
}

function createEmailAccount(createEmail){
	var new_ad_form = document.getElementById('new_ad_form');

	if(createEmail){
		if(ajax_busy){
			setTimeout("createEmailAccount("+(createEmail?'1':'0')+")",100);
			return;
		}
		if(new_ad_form.name.value.length<3){
			alert("Entered name must have at least 3 characters!");
			new_ad_form.create_user_mail.checked = false;
			return false;
		}
		
		var js_request = "action=register&fullname="+new_ad_form.name.value;
		var details_page=site_root_stripped+'/index.php?module=js_mail_proxy&js_url='+escape(js_request);
		details_page += "&code="+escape(new_ad_form.code.value);
		details_page += "&entered="+escape(new_ad_form.entered.value);
	
		ajax_busy=true;
		ajax.requestFile = details_page;
		ajax.onCompletion = function(){ createEmailAccountCallback();};	// Specify function that will be executed after file has been found
		ajax.runAJAX();
		return;
	}
	else{
		new_ad_form.email.readonly = true;
		new_ad_form.email.disbled = true;
	}
}

function createEmailAccountCallback(){
	eval(ajax.response);
	ajax_busy=false;

	var new_ad_form = document.getElementById('new_ad_form');
	if(details.successfull){
		new_ad_form.email.readonly = true;
		new_ad_form.email.disbled = true;
		new_ad_form.email.value = details.email;
		
		var alert_text = "Your account has been created";
		alert_text += "\nEmail address: "+details.email;
		alert_text += "\nPassword: "+details.pass;
		alert_text += "\n\nPlease remember the email address and password: ";
		alert(alert_text);
	}
	else{
		if(details.error){
			alert(details.error);
		}
		else{
			alert("Email account creation failed!");
		}
		new_ad_form.create_user_mail.checked = false;
	}
}

function remoteVerifyTuring(code, entered){
	if(ajax_busy){
		setTimeout("remoteVerifyTuring('"+code+"', '"+entered+"')",100);
		return;
	}
	displayWaitMessage();
	
	var details_page=site_root_stripped+'/index.php?module=ads&op=tv&';
	ajax_busy=true;
	ajax.requestFile = details_page+"code="+code+"&entered="+entered;
	ajax.onCompletion = function(){ setTuring();};	// Specify function that will be executed after file has been found
	ajax.runAJAX();
	return;
}

function setTuring(){
	eval(ajax.response);
	ajax_busy=false;
	turingValue = details;
	hideWaitMessage();
	var new_ad_form = document.getElementById('new_ad_form');
	var temp = processForm(new_ad_form);
	if(temp){
		var submittedField = document.createElement('input');
		submittedField.setAttribute('type', 'hidden');
		submittedField.setAttribute('name', 'submitted');
		submittedField.setAttribute('value', 'Save');
		new_ad_form.appendChild(submittedField);
		new_ad_form.submit();
	}
}


function checkEmail(emailString) {
	splitVal = emailString.split('@');
	if(splitVal.length <= 1) {
		return false;
	}
	if(splitVal[0].length <= 0 || splitVal[1].length <= 0) {
		return false;
	}
	
	splitDomain = splitVal[1].split('.');
	if(splitDomain.length <= 1) {
		return false;
	}
	if(splitDomain[0].length <= 0 || splitDomain[1].length <= 1) {
		return false;
	}
	return true;
}


function alertPromoteTariff(isChecked){
	if(isChecked){
		alert('This requires for you to pay a promotion fee.\nFor the selected category the tariff is:\n'+currentCatDailyPromoteTariff+' USD per day');
	}
}