Array.prototype.in_array = function (value){ 
	for(var i=0; i < this.length; i++){
		if(this[i] === value){ 
			return true; 
		} 
	} 
	return false; 
};


function is_valid_email(str){
	if (!trim(str)){
  	return false;
  }
  
  if (str.indexOf("@")==-1 || str.indexOf(".")==-1 || str.indexOf(" ")!=-1 || str.length<6){
  	return false;
  }
  
  return true;
}

function trim(str){
	if(!str.length)
  	return str;

  while(str.charAt(0) == ' ')
  	str = str.substring(1, str.length);

  if(!str.length)
  	return str;

  while(str.charAt(str.length - 1) == ' ')
  	str = str.substring(0, str.length - 1);

  return str;
}

function show(div, val){
	if(document.getElementById(div)){
		document.getElementById(div).style.display = '';
		document.getElementById(div).innerHTML = val;
	}
}

function hide(div){
	if(document.getElementById(div)){
		document.getElementById(div).style.display = 'none';
	}

}

function hit_enter(field, event) {
  var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
  if (keyCode == 13){
    return true;
  }
  return false;
}

function get_checkbox(frm, FieldName){
	var answer = '';
	for(var i=0; i<frm.elements.length; i++){
		if(frm.elements[i].type == 'checkbox' && frm.elements[i].name == FieldName)
			if(frm.elements[i].checked){
				answer += (answer ? ',' : '') + frm.elements[i].value;
			}
	}
	return answer;
}

function set_checkbox(frm, FieldName){
  var args = set_checkbox.arguments;
  var val = [];
  
	for(var i=2;i<args.length;i++){
		val.push(args[i]);	
	}
	
	for (var i=0; i<frm.elements.length; i++){
		if(frm.elements[i].type == 'checkbox' && frm.elements[i].name == FieldName){
			if(val.in_array(frm.elements[i].value)){
				frm.elements[i].checked = true;
			}else{
				frm.elements[i].checked = false;
			}
		}
	}
}

function check_checkbox(frm, FieldName){
  for(var i=0;i<frm.elements.length;i++)
    if(frm.elements[i].type == 'checkbox' && frm.elements[i].name == FieldName)
      if(frm.elements[i].checked)
        return true;

  return false;
}

function get_select(Field){		
	var answer = '';
	if(Field != undefined){
		for(var i=0; i<Field.options.length; i++){
			if(Field.options[i].selected)
				answer += (answer ? ',' : '') + Field.options[i].value;
		}
	}
	return answer;
}

function set_select(Field){
  var args = set_select.arguments;
	var val = [];
	
	for(var i=1;i<args.length;i++){
		val.push(args[i]);	
	}
		
	for(var i=0; i<Field.options.length; i++){
		if(val.in_array(Field.options[i].value)){
			Field.options[i].selected = true;
		}else{
			Field.options[i].selected = false;
		}
	}	
}

function check_listing_form(frm){
	hide('div_listing');
	
	if(!is_valid_email(frm.contact_email.value)){
		show('div_listing', 'Please enter a valid email.');
		frm.contact_email.focus();
		return false;
	}
	
	if(!trim(frm.name.value)){
		show('div_listing', 'Please enter name of listing.');
		frm.name.focus();
		return false;
	}
	
	var country_id = get_select(frm.country_id);
	var province_id = get_select(frm.province_id);
	var location_id = get_select(frm.location_id);
	
	if(!country_id || (country_id > 0 && !province_id) || (country_id > 0 && province_id > 0 && !location_id)){
		show('div_listing', 'Please select location.');
		return false;
	}
	
	if(!get_select(frm.category_id)){		
		show('div_listing', 'Please select category.');
		return false;
	}
	
	if(!trim(frm.code.value)){
		show('div_listing', 'Please enter authorization code.');
		frm.code.focus();
		return false;
	}
	
	return true;
}

function check_verify_form(frm){
	hide('div_verify');
	
	if(!trim(frm.listing_id.value)){
		show('div_verify', 'Please enter id of listing.');
		frm.listing_id.focus();
		return false;
	}
	
	if(!trim(frm.passw.value)){
		show('div_verify', 'Please enter password.');
		frm.passw.focus();
		return false;
	}
	
	return true;
}

function check_forgot_info_form(frm){
	hide('div_forgot_info');
	
	if(!is_valid_email(frm.contact_email.value)){
		show('div_forgot_info', 'Please enter a valid email.');
		frm.contact_email.focus();
		return false;
	}
	
	return true;
}

function check_category_form(frm){
	hide('div_category');
	
	if(!is_valid_email(frm.contact_email.value)){
		show('div_category', 'Please enter a valid email.');
		frm.contact_email.focus();
		return false;
	}
	
	if(!trim(frm.category_name.value)){
		show('div_category', 'Please enter name of category.');
		frm.category_name.focus();
		return false;
	}

	var country_id = get_select(frm.country_id);
	var province_id = get_select(frm.province_id);
	var location_id = get_select(frm.location_id);

	if(!country_id || (country_id > 0 && !province_id) || (country_id > 0 && province_id > 0 && !location_id)){
		show('div_category', 'Please select location.');
		return false;
	}
	
	if(!trim(frm.code.value)){
		show('div_category', 'Please enter authorization code.');
		frm.code.focus();
		return false;
	}
	
	return true;
}

function popUp(theurl,thewindow,thewidth,theheight,thescroll,theResize) {
  var iMyWidth;
  var iMyHeight;
  var scroll;
  var win2;
  var features = '';
  var isResizable;

	if (theResize || theResize == null)
		isResizable = 'yes';
	else
		isResizable = 'no';

  if (thescroll)
   scroll = 'yes';
  else
   scroll = 'no';
   
  //gets top and left positions based on user's resolution so hint window is centered.
  iMyWidth = (window.screen.width/2) - ((thewidth/2) + 10);
  //half the screen width minus half the new window width (plus 5 pixel borders).
  iMyHeight = (window.screen.height/2) - ((theheight/2) + 50);
  //half the screen height minus half the new window height (plus title and status bars).

  features = "status=yes,height="+theheight+",width="+thewidth+",titlebar=no"+",resizable="+isResizable+",left="+iMyWidth+",top="+
    iMyHeight+",screenX="+iMyWidth+",screenY="+iMyHeight+",scrollbars="+scroll;

  win2 = window.open(theurl,thewindow,features);
  win2.focus();

  return win2;
}

function GMapSearch2(div_map, list){	
	if(GBrowserIsCompatible()){
		var map = new GMap2(document.getElementById(div_map));
    var locationList = [list];
    
    init_lat = locationList[0].lat;
    init_lng = locationList[0].lon;
   	
    map.setCenter(new GLatLng(init_lat, init_lng), 25);
    //map.addControl(new GLargeMapControl());
    //map.addControl(new GMapTypeControl());
		
		function createMarker(point, text) {
      var marker = new GMarker(point,{text:text});
      GEvent.addListener(marker, "click", 
      	function() {
        	marker.openInfoWindowHtml(text);
      	}
      );
      return marker;
    }
        
    //alert(locationList[0].name+", "+locationList[0].lat+","+locationList[0].lon);
		
    var site = 0;
    var point = null;
    //Loops which adds markers to the map
    while (site < locationList.length){
      var point = new GLatLng(locationList[site].lat, locationList[site].lon);
      var details = "<strong>"+locationList[site].business_name+"<strong><br/>"+locationList[site].address;
      if(locationList[site].id){
      	var marker =  createMarker(point, details);
      	map.addOverlay(marker);
    	}
    	site++;
    }
  }
}