// JavaScript Document

function form_submit(step, next, meth, c_iso) {
	var target = '';
	if (step > 1 && !next) {
		target = step - 1;
	} else if (step < 3 && next) {
		target = step + 1;
	} else if (step == 3 && next) {
		target = 4;
	}
	document.share_form.cf_prev.value = step;
		
	if (step == 1) {
		
		
		
		if (meth == 'upl') {
			
			document.share_form.cf_yt_link.value = ''; // empty YouTube link input
			document.share_form.cf_method.value = 'upl';
			// check if the TOU was agreed to ELSE return message AND return false
			if (!document.getElementById("agree").checked) {
				error_output('agree','100px','45px');
				error_output('embed','0','0');
				error_output('upload','0','0');
				return false;
			}
			if (!document.share_form.upload.value) {
				error_output('upload','100px','45px');
				error_output('embed','0','0');
				error_output('agree','0','0');
				return false;	
			}
			document.getElementById("prog_mess").style.height = '16px';
			document.getElementById("prog_mess").innerHTML = 'Uploading...';
			
			setTimeout("progress(100); upload_check();", 500); // start checking the upload progress
			document.getElementById("prog_bar").style.height = '25px';
		} else if (meth == 'emb') {
			
			if (!document.share_form.embed.value) {
				error_output('embed','100px','45px');
				error_output('upload','0','0');
				error_output('agree','0','0');
				return false;	
			}
			document.getElementById("prog_bar").style.height = '25px';
			document.share_form.cf_yt_link.value = document.share_form.embed.value;
			document.share_form.cf_method.value = 'emb';
			document.share_form.cf_tf_code.value = ''; // empty Togfrog code input
		}
		
		document.share_form.cf_step.value = target;
		document.share_form.submit();
		
	} else if (step == 2) {
		if (next) {
			if (!document.share_form.price_out.value && document.share_form.price_out.value != '0') {
				error_output('price','100px','45px');
				return false;	
			} else {
				error_output('price','0','0');
			}
			if (!document.share_form.title.value) {
				error_output('title','100px','45px');
				return false;	
			} else {
				error_output('title','0','0');	
			}
		}
		if (document.share_form.price_out.value == '0') {
			document.share_form.cf_price.value = '0.00';
		} else {
			document.share_form.cf_price.value = document.share_form.price_out.value;
		}
		if (meth == 'gmaps') {
			document.share_form.cf_map.value = 'gmaps';
		}
		document.share_form.cf_title.value = document.share_form.title.value;
		document.share_form.cf_tags.value = document.share_form.tags.value;
		document.share_form.cf_desc.value = document.share_form.description.value;
		
		document.share_form.cf_step.value = target;
		document.share_form.submit();
		
	} else if (step == 3) {
		
		if (!meth && next) {
			if (!document.share_form2.add_loc.value || document.share_form2.add_loc.value == 'false') { // only check coords, country and city if NO add location
				if (!document.share_form2.country.value) {
					error_output('country','100px','45px');
					if (!document.share_form2.city.value) {
						error_output('city','100px','45px');
					} else {
						error_output('city','0','0');	
					}
					if (!document.share_form.cf_lat.value || !document.share_form.cf_lng.value) {
						error_output('coords','100px','10px');
					} else {
						error_output('coords','0','0');	
					}
					return false;	
				} else {
					error_output('country','0','0');	
				}
				
				if (!document.share_form2.city.value) {
					error_output('city','100px','45px');
					if (!document.share_form.cf_lat.value || !document.share_form.cf_lng.value) {
						error_output('coords','100px','10px');
					} else {
						error_output('coords','0','0');	
					}
					return false;	
				} else {
					error_output('city','0','0');	
				}
				
				if (!document.share_form.cf_lat.value || !document.share_form.cf_lng.value) {
					error_output('coords','100px','10px');
					return false;	
				} else {
					error_output('coords','0','0');	
				}
			}
			
		} else if (meth == 'gmaps') {
			document.share_form.cf_map.value = 'gmaps';
			
			var iso_code = c_iso;
			var pattern = /^[A-Z]{1,3}$/;
							
			if (iso_code.match(pattern)) {
				document.share_form.cf_co_iso.value = iso_code;
			} else {
				document.share_form.cf_co_iso.value = 'none';
			}
		}
		
		// In case of back click, reset the map type
		if (!next) {
			document.share_form.cf_map.value = 'ammap';
		}
		
		if (!meth) {
			// Add to existing
			
			document.share_form.cf_count_code.value = document.share_form2.country.value;
			document.share_form.cf_city.value = document.share_form2.city.value;
			if (document.share_form2.add_loc.value) {
				document.share_form.cf_loc_exist.value = document.share_form2.add_loc.value;
			} else {
				document.share_form.cf_loc_exist.value = 'FALSE';
			}	
		}
		
		document.share_form.cf_step.value = target;
		document.share_form.submit();
	}
}


function convert_currency (rate) {	
	var us = document.getElementById("us_rate").value;
	var input = document.getElementById("price_input").value;
	var conv = input * (us / rate);
	var result = conv.toFixed(0);
	
	if (result > 999999.99) {
		document.getElementById("output").innerHTML = ' - the price can not be more than $US 999999.99';
	} else if (result >= 0 || result == false) {
		document.getElementById("price_out").value = result;
		// document.getElementById("price_out_shown").innerHTML = result;
	} else {
		document.getElementById("price_out").value = '';
		// document.getElementById("price_out_shown").innerHTML = '-.--';
	}
	if (input == '') {
		document.getElementById("price_out").value = '';
		// document.getElementById("price_out_shown").innerHTML = '-.--';
	}
}

function tou_check () {
	if (document.getElementById("agree").checked) {
		document.getElementById('cf_agree').value = 'TRUE';
		document.getElementById('form_fade').style.width = '0';
		document.getElementById('form_fade').style.height = '0';
		document.getElementById('share_error').innerHTML = '';
		error_output('agree','0','0');
	} else if (!document.getElementById("agree").checked) {
		document.getElementById('cf_agree').value = 'FALSE';
		document.getElementById('form_fade').style.width = '490px';
		document.getElementById('form_fade').style.height = '175px';
	}
}

function error_output (id, w, h) {
	if (id == 'agree') {
		//document.getElementById('agree_err').style.width = w;
		document.getElementById('agree_err').style.height = h;
		if (h == 0) {
			document.getElementById('agree_err').innerHTML = '';
			//document.getElementById('agree_err').style.display = 'none';
		} else {
			document.getElementById('agree_err').innerHTML = '<p>Required</p>';
			//document.getElementById('agree_err').style.display = 'block';
		}
	} else if (id == 'upload') { 
		//document.getElementById('upl_err').style.width = w;
		document.getElementById('upl_err').style.height = h;
		
		if (h == 0) {
			document.getElementById('upl_err').innerHTML = '';
			//document.getElementById('upl_err').style.display = 'none';
		} else {
			document.getElementById('upl_err').innerHTML = '<p>Choose a vieo file</p>';
			//document.getElementById('upl_err').style.display = 'block';
		}
	} else if (id == 'embed') { 
		//document.getElementById('emb_err').style.width = w;
		document.getElementById('emb_err').style.height = h;
		if (h == 0) {
			document.getElementById('emb_err').innerHTML = '';
			//document.getElementById('emb_err').style.display = 'none';
		} else {
			document.getElementById('emb_err').innerHTML = '<p>Remember to put a link</p>';
			//document.getElementById('emb_err').style.display = 'block';
		}
	} else if (id == 'price') {
		document.getElementById('pri_err').style.height = h;
		if (h == 0) {
			document.getElementById('pri_err').innerHTML = '';
		} else {
			document.getElementById('pri_err').innerHTML = "<p>Please estimate the price - 0 if it's free.</p>";
		}
	} else if (id == 'title') {
		document.getElementById('tit_err').style.height = h;
		if (h == 0) {
			document.getElementById('tit_err').innerHTML = '';
		} else {
			document.getElementById('tit_err').innerHTML = '<p>Please enter a title</p>';
		}
	} else if (id == 'description') {
		document.getElementById('desc_err').style.height = h;
		if (h == 0) {
			document.getElementById('desc_err').innerHTML = '';
		}
	} else if (id == 'country') {
		document.getElementById('country_err').style.height = h;
		if (h == 0) {
			document.getElementById('country_err').innerHTML = '';
		} else {
			document.getElementById('country_err').innerHTML = '<p>Pick a country!</p>';
		}
	} else if (id == 'city') {
		document.getElementById('city_err').style.height = h;
		if (h == 0) {
			document.getElementById('city_err').innerHTML = '';
		} else {
			document.getElementById('city_err').innerHTML = '<p>City name is required!</p>';
		}
	} else if (id == 'coords') {
		document.getElementById('map_err').style.height = h;
		if (h == 0) {
			document.getElementById('map_err').innerHTML = '';
		} else {
			document.getElementById('map_err').innerHTML = '<p><a style="padding: 2px 2px 3px; float: right; width: 14px; height: 13px;" onclick="error_output(\'coords\',\'0\',\'0\');return false;" href="#"><img src="styles/icons/close.gif" alt="close box" title="close box"></a>Click the map to indicate the activity location</p>';
		}
	} else {
		return false;	
	}
}

function focus_form (id) {
	if (id == 'embed') {
		document.getElementById('share_guide_dynamic').innerHTML = '';
	} else if (id == 'upload') {
		document.getElementById('share_guide_dynamic').innerHTML = '';
	} else {
		document.getElementById('share_guide_dynamic').innerHTML = '';
	}
}

function clear_loc_add () {
	document.getElementById('count_cit_hide').style.display = 'block';
	document.getElementById('form_loc_prev').innerHTML = '<p style="color: #FFF;">NB: A new activity will be created!</p><input type="hidden" name="add_loc" id="add_loc" value="false" />';
	document.getElementById('req_star1').innerHTML = '*';
	document.getElementById('req_star2').innerHTML = '*';
}

// function used to set focus in a form field
function form_focus (form, field) {
	//alert(field);
	document.forms[form].elements[field].focus();
}


/**** AJAX ****/

function GetXmlHttpObject() {
	var xmlHttp = null;
	try {	// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
		
	} catch (e) {	// Internet Explorer
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

function get_country_list (inp) {
	
	if (inp == 'all') { // Get all countries
		xmlHttp = GetXmlHttpObject();
		
		if (xmlHttp == null) {
			alert ("Your browser does not support AJAX!");
			return;
		}
		
		var url = "ajax/ajax_get_countries.php";
		url = url + "?sid=" + Math.random();
		xmlHttp.onreadystatechange = stateChangedCountryList;
		xmlHttp.open("GET", url, true);
		xmlHttp.send(null);
	}
}

function stateChangedCountryList() {
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
		document.getElementById("country_container").innerHTML = xmlHttp.responseText;
	}
}

function add_to_existing (lid) {
	xmlHttp = GetXmlHttpObject();
		
	if (xmlHttp == null) {
		alert ("Your browser does not support AJAX!");
		return;
	}
	
	var url = "ajax/ajax_share_add_prev.php";
	url = url + "?loc=" + lid;
	url = url + "&sid=" + Math.random();
	xmlHttp.onreadystatechange = stateChangedAddLoc;
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
		
}

function stateChangedAddLoc() {
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
		document.getElementById('form_loc_prev').innerHTML = xmlHttp.responseText;
	}
	document.getElementById('req_star1').innerHTML = '';
	document.getElementById('req_star2').innerHTML = '';
	document.getElementById('count_cit_hide').style.display = 'none';
	
	error_output('country','0','0');
	error_output('city','0','0');
	error_output('coords','0','0');
}
	
	
	function progress_begin () {
	//alert(ext); //extension = ext;
	//document.getElementById("ajax_return").innerHTML = '0%';
	setTimeout("upload_check()", 750);
}


function progress (percent) {
	
	var width = 200;
	var move_pix = width - ((percent*width)/100);
	
	//document.getElementById("ajax_return").innerHTML = percent + '%';
	document.getElementById('prog_bar').style.backgroundPosition = '-' + move_pix + 'px';
}

function guide_focus_change (map) {
	document.getElementById('share_guide_point1').style.fontWeight = 'normal';
	document.getElementById('share_guide_point2').style.fontWeight = 'bold';
	
	// load new map
	if (map == 'afr') {
		document.getElementById('ammap').reloadData('ammap/ammap_data_africa.xml');
	} else if (map == 'aus') {
		document.getElementById('ammap').reloadData('ammap/ammap_data_australia.xml');
	} else if (map == 'eur') {
		document.getElementById('ammap').reloadData('ammap/ammap_data_europe.xml');
	} else if (map == 'asi') {
		document.getElementById('ammap').reloadData('ammap/ammap_data_asia.xml');
	} else if (map == 'nam') {
		document.getElementById('ammap').reloadData('ammap/ammap_data_namerica.xml');
	} else if (map == 'sam') {
		document.getElementById('ammap').reloadData('ammap/ammap_data_samerica.xml');
	} else if (map == 'wor') {
		document.getElementById('ammap').reloadData('ammap/ammap_data.xml');
		document.getElementById('share_guide_point1').style.fontWeight = 'bold';
		document.getElementById('share_guide_point2').style.fontWeight = 'normal';
	}
}