
	var visitors, geo_target, category, visitor_sel, geo_sel, cat_sel;	// globals
	
	function showhide(e){
		var clkd = $(e);
		if(clkd.style.display == 'block')
			clkd.style.display = 'none';
		else clkd.style.display = 'block';
	}
	
	document.observe("dom:loaded", function() {
		// gather hidden fields
		visitors = $('visitors');
		geo_target = $('geo_target');
		category = $('category');
		
		// clear old values
		visitors.value = '';
		geo_target.value = '';
		category.value = '';
		$('url').value = 'Enter website URL to send traffic to here';
		
		// get text labels
		visitor_sel = $('visitor_sel');
		geo_sel = $('geo_sel');
		cat_sel = $('cat_sel');
		
		// get all li's
	  $$('#visitor_drop li').each(function(li){
	  	Event.observe(li, 'click', function(){
	  		visitors.value = ''+li.id+''; // force to string, leading 0's
	  		$('visitor_wrap').hide();
	  		visitor_sel.innerHTML = li.className;
	  		$('vis_ver').innerHTML = li.className;
	  		refresh_order();
			});
	  });
	  	  
	  $$('#geo_drop li').each(function(li){
	  	Event.observe(li, 'click', function(){
	  		if(li.className != 'nohov'){
		  		geo_target.value = ''+li.id+''; // force to string, leading 0's
		  		$('geo_wrap').hide();
		  		geo_sel.innerHTML = li.innerHTML;
	  			$('geo_ver').innerHTML = li.innerHTML;
	  			refresh_order();
	  		}
			});
	  });
	  
	  $$('#cat_drop li').each(function(li){
	  	Event.observe(li, 'click', function(){
	  		category.value = ''+li.id+''; // force to string, leading 0's
	  		$('cat_wrap').hide();
	  		cat_sel.innerHTML = li.className;
	  		$('cat_ver').innerHTML = li.className;
			});
	  });
	});
	
	function test_url(url){
		if(check_url(url)){
			if(url.substr(0,7) != 'http://')
				url = 'http://' + url;
			window.open(url, 'TestWindow');
		}
		else{
			if(url == '')
				alert('The URL field is empty. Please enter the Website Address (URL) you wish to send targeted traffic to.');
			else
				alert('The Website Address (URL) field is not in a valid format. Please add a valid website address. If the problem persists, please go to the website and copy the address from the address bar and paste it in the box below. ');
		}
	}
	
	function check_url(url){
		//if(url != 'Enter website URL to send traffic to here' && isUrl(url)){
		if(url != 'Enter website URL to send traffic to here' && url != ''){
			return true;
		}
		return false;
	}
	
	function isUrl(sText){
		// ex: http://www.doms.com, www.this.that.org, http://fun.com/folder/page.php?p=17
		var urlPattern = /^((http|ftp|https):\/\/)?(www\.)?[a-zA-Z0-9]+[a-zA-Z0-9.-]*\.\w{2,3}(\/[a-zA-Z0-9_.-]*)*(\/(\w))*(\/(\w)+\.\w{2,4}.*)?$/;
		return validateInput(sText, urlPattern);
	}
	
	function validateInput(sText, pattern){
		if(pattern.test(sText)){
			return true;
		} else return false;
	}
	
	function add_http(u){
		var url = $(u).value;
		if(url.substr(0,7) != 'http://')
			return 'http://';
		return '';
	}
	
	function refresh_order(){
		if(visitors.value && geo_target.value){
			update_order();
		}
	}
	
	function update_order(){
		new Ajax.Request('ajax/update.php', 
	  { 
			parameters: 'vis='+visitors.value+'&geo='+geo_target.value+'&cat='+category.value, 
			method:'get', 
			onSuccess: function(transport){
				
				if (transport.responseText != ''){
					var total = $('total_cost');
					replaceHTML(total, transport.responseText, 'span');
				}
				removeChildren($('ajload'));
			},
			onLoading: function(){
				var ajaxbar = document.createElement("img");
				ajaxbar.src = "images/ajax-loader.gif";
				
				removeChildren($('ajload'));
				$('ajload').appendChild(ajaxbar);
			}
	  });	
	}
	
	function replaceHTML(dest_element, destcontent, type){
		if(!type) 
			type = 'div';
		var new_element = document.createElement(type);
		if(dest_element.id)
			new_element.id = dest_element.id;
		if(dest_element.className)
			new_element.className = dest_element.className;
		removeChildren(dest_element);
		new_element.innerHTML = destcontent;
		dest_element.appendChild(new_element);
	}
	
	function removeChildren(node) {
		if(node.hasChildNodes()) {
			while(node.childNodes.length >= 1 ) {
				node.removeChild(node.firstChild);
			}
		}
	}
	
	function validate(){
		var errors = '';
		if(!visitors.value)
			errors += 'Please select number of guaranteed visitors.\n';
		if(!geo_target.value)
			errors += 'Please select a targeted region.\n';
		if(!category.value)
			errors += 'Please select a category of traffic.\n';
		if(!$('url').value || $('url').value == 'Enter website URL to send traffic to here')
			errors += 'Please type a website address (URL) to send traffic to.\n';
		
		if(errors.length > 0){
			return errors;
		}
		else{
			return null;
		}
	}
	
	function do_submit(){
		errors = validate(); 
		
		if(errors == null || errors == ''){ 
			var u = $('url');
			if(u.value)
				$('site_url').value = add_http(u) + u.value; 
				
			$('buy_form').submit(); 
		}
		else{
			alert(errors);
		}
	}
