function dump_props(obj, obj_name)
{
   var result = "";
   for (var i in obj)
   {
      result += obj_name + "." + i + " = " + obj[i] + "<BR>";
   }
   result += "<HR>";
   return result
}

function e(id)
{
   return document.getElementById(id);
}

function gv(id)
{
   if (e(id)) return e(id).value;
}

function sv(id, value)
{
   if (e(id)) e(id).value = value;
}

function gc(id)
{
   if (e(id)) return e(id).className;
}

function sc(id, cl)
{
   if (e(id)) e(id).className = cl;
}

function gvradio(radioobj)
{
   for (var i = 0; i<radioobj.length; i++)
     if (radioobj[i].checked) return radioobj[i].value;
}

function getOption(id)
{
   var ops = e(id).options;
   var value = gv(id);
   for (i=0; i<ops.length; i++)
     if (ops[i].value == value) return ops[i].innerHTML;
}

function getOptionObject(id)
{
   var ops = e(id).options;
   var value = gv(id);
   for (i=0; i<ops.length; i++)
     if (ops[i].value == value) return ops[i];
}

function setOption(id, val)
{
   var ops = e(id).options;
   var value = gv(id);
   for (i=0; i<ops.length; i++)
     if (ops[i].value == value)
       {ops[i].innerHTML = val; break;}
}

function delOption(id)
{
   var el = e(id);
   el.removeChild(el.childNodes[el.selectedIndex]);
}

function addOption(id, option)
{
   if (e(id)) e(id).appendChild(option);
}

function moveOptions(id1, id2)
{
   var options = e(id1).childNodes;
   var i = 0;
   while(i < options.length)
   {
      if (options[i].selected) e(id2).appendChild(options[i]);
      else i++;
   }
}

function selOptions(id)
{
   var ops = e(id).options;
   for (i=0; i<ops.length; i++)
     ops[i].selected = true;
}

function getDisplay(id)
{
   if (e(id)) return e(id).style.display;
}

function isShow(id)
{
   return (getDisplay(id) != 'none');
}

function hide(id)
{
   if (e(id)) e(id).style.display = "none";
}

function show(id)
{
   if (e(id)) e(id).style.display = "block";
}

function ShowHide(id, display)
{
   var obj = e(id);
   if (!display) display = "block"
   if (obj.style.display == "none") obj.style.display = display;
   else obj.style.display = "none";
}

var currentBodyCursor;

function setCursor(obj, cursor)
{
   if (obj) obj.style.cursor = cursor;
}

function getCursor(obj)
{
   if (obj) return obj.style.cursor;
}

function pushCursor()
{
   currentBodyCursor = getCursor();
}

function popCursor()
{
   setCursor(currentBodyCursor);
}

function checkNumberInt(str)
{
   var re = /^[\d]*$/;
   return re.test(str);
}

function checkNumberFloat(str)
{
   var re = /^[\d]*[\.,]?[\d]*$/;
   return re.test(str);
}

function checkEmail(str)
{
   var re = /[a-z\d-_\.]+@[a-z\d-_]+(\.[a-z\d_-]+)/i;
   return re.test(str);
}

function checkDate(str)
{
   var re = /^[\d]{1,2}\/[\d]{1,2}\/[\d]{4}$/;
   return re.test(str);
}

function ins(id, data)
{
   if (e(id)) e(id).innerHTML = data;
}

function isEmptyDate(id)
{
   return (gv('year_'+id) == 0 || gv('month_'+id) == 0  || gv('day_'+id) == 0);
}

function isEmptyTime(id)
{
   return (gv('hour_'+id) == 0 || gv('minute_'+id) == 0)
}

function validLocation(countryid, stateid, cityid)
{
   if (gv(countryid) == 0)
   {
      alert("Select country...");
      e(countryid).focus();
      return false;
   }
   if (gv(countryid) == 204)
   {
      if (gv(stateid) == 0)
      {
         alert("Select State...");
         e(stateid).focus();
         return false;
      }
      if(gv(cityid) == 0 || gv(cityid) == -1)
      {
         alert("Select City...");
         e(cityid).focus();
         return false;
      }
   }
   else if(gv(cityid) == 0 || gv(cityid) == -1)
   {
      alert("Select City...");
      e(cityid).focus();
      return false;
   }
   return true;
}

function validDateFromTo(fromid, toid)
{
   if (!checkDate(gv(fromid)))
   {
      alert("Date 'from' is invalid");
      sv(fromid, '');
      e(fromid).focus();
      return false;
   }
   if (!checkDate(gv(toid)))
   {
      alert("Date 'to' is invalid");
      sv(toid, '');
      e(toid).focus();
      return false;
   }
   return true;
}

function validFind(id)
    {

   var from = gv("from_find");
   var to = gv("to_find");

   if (!validLocation('sel_countryid_'+id, 'sel_stateid_'+id, 'sel_cityid_'+id) || !validDateFromTo('from_'+id, 'to_'+id)){
    return false;
        }


   if (cmpDate(from, to, 'd/m/y') == -1){
    alert("The start date of trip cannot be after the end date!")
         return false;
        }

   return true;
}

function formFindSubmit()
{

   if (validFind('find')){
	   
	   var form = e('form_find');
var num_of_elements = form.length;
var out="";
for (var i=0; i<num_of_elements; i++) {
		var theElement = form.elements[i];
		var element_name = theElement.name;
		var element_value = theElement.value;
		if(theElement.type=='checkbox'){
		    if(theElement.checked) out += (element_name + "=" + element_value + "&");
			
		}else if(theElement.type=='radio'){
		
				if(theElement.checked) out += (element_name + "=" + element_value + "&");
		}else{
	
			if((element_value == '') ||(element_value == 0) || (element_name == 'submit')){
	
				}else{
				out += (element_name + "=" + element_value + "&");
			}
		}
	}
	
	var url=out;
	//alert(out);
	document.location="/find_people.php?"+url;
	
	   
   }
}

function validImageFile(id)
{
   var path = e(id).value;
   if (!path) return true;
   var ext = path.lastIndexOf(".");
   if (ext == -1) return false;
   ext = path.substring(ext+1).toLowerCase();
   var arr = ['jpeg', 'jpg', 'gif', 'png'];
   for (var i=0; i<arr.length; i++)
     if (ext == arr[i]) return true;
   return false;
}

function isEmpty(id)
{
   if (!id || !e(id)) return true;
   var type = e(id).type;
   switch (type)
   {
      case 'text':
      case 'select-multiple':
        return (gv(id) == '');
      case 'select-one':
        return (gv(id) == '' || gv(id) == 0)
      case 'radio':
      case 'checkbox':
        return !e(id).checked;
      case 'hidden':
        if (id.substring(0, 9) == 'countryid' || id.substring(0, 7) == 'stateid' || id.substring(0, 6) == 'cityid')
        return (gv(id) == '0');
      default:
        return true;
   }
}

function isEmptyForm(formid)
{
   var flg = true;
   var elements = document.forms[formid];
   for (i=0; i<elements.length; i++)
     flg &= isEmpty(elements[i].id);
   return flg;
}

function setValidDate(obj, min, max, format)
{
   if (!obj.value) return;
   var farr = format.split("/");
   var d = obj.value.split("/");
   for (var i=0; i<3; i++) d[farr[i]] = d[i];
   dg = new Date(d['y'], d['m'], d['d']);
   if (min)
   {
      d = min.split("/");
      for (var i=0; i<3; i++) d[farr[i]] = d[i];
      dmin = new Date(d['y'], d['m'], d['d']);
   }
   if (max)
   {
      d = max.split("/");
      for (var i=0; i<3; i++) d[farr[i]] = d[i];
      dmax = new Date(d['y'], d['m'], d['d']);
   }
   if (dmin.valueOf() > dmax.valueOf()) return;
   if (dmax) if (dg.valueOf() > dm.valueOf()) obj.value = max;
   if (dmin) if (dg.valueOf() < dm.valueOf()) obj.value = min;
}

function formatDate(date, informat, outformat)
{
   if (!date) return;
   var inarr = informat.split("/");
   var outarr = outformat.split("/");
   var datearr = date.toString().split("/");
   var d = "";
   for (var i=0; i<3; i++) datearr[inarr[i]] = datearr[i];
   for (var i=0; i<3; i++) d += datearr[outarr[i]]+"/";
   return d.substr(0, d.length-1);
}

function cmpDate(from, to, format)
{
   var dfrom = new Date(formatDate(from, format, 'm/d/y'));
   var dto = new Date(formatDate(to, format, 'm/d/y'));
   if (dfrom.valueOf() > dto.valueOf()) return -1;
   else if (dfrom.valueOf() < dto.valueOf()) return 1;
   else return 0;
}
function setToDate(from, where)
{
	alert(gv(from));
   sv(where, gv(from));
}

function selectWord(id, word)
{
   if (e(id))
   {
      var val = gv(id);
      var s = val.indexOf(word);
      if (s != -1)
      {
         e(id).selectionStart = s;
         e(id).selectionEnd = s + word.length;
         e(id).focus();
      }
   }
}

// ---------------------- Widget functions -------------------------
function widget(wid, page, per)
{
   if (!page)
   {
      page = gv('page_'+wid);
      if (!page) page = 0;
   }
   if (!per) per = gv('per_'+wid);
   sv('page_'+wid, page);
   sv('per_'+wid, per);
   ajax.call(gv('widgetclass_'+wid)+'::update', getWidgetParams(wid), '');
}

function getWidgetParams(wid)
{
   var arr = Array();
   arr['widgetid'] = wid;
   arr['class'] = gv('widgetclass_'+wid);
   arr['count'] = gv('count_'+wid);
   arr['page'] = gv('page_'+wid);
   arr['per'] = gv('per_'+wid);
   arr['checkbox'] = gv('ischk_'+wid);
   arr['sort'] = gv('sort_'+wid);
   return arr;
}

function checkAllItems(wid, checked)
{
   var elements = document.forms['form_'+wid];
   for (var i=0; i<elements.length; i++)
   {
      var pref = elements[i].id.substr(0, 4);
      if (pref == 'chk_')
      {
         var id = elements[i].id;
         if (e(id)) e(id).checked = checked;
      }
   }
}

function getItems(wid)
{
   var arr = new Array();
   var elements = document.forms['form_'+wid];
   for (var i=0; i<elements.length; i++)
   {
      var pref = elements[i].id.substr(0, 4);
      if (pref == 'chk_')
      {
         var id = elements[i].id;
         if (e(id) && e(id).checked) arr[arr.length] = gv(id);
      }
   }
   return arr;
}

function sort(id, type)
{
   var x = gv('sort_'+id);
   if (Math.abs(x) == type) x = -x; else x = type;
   sv('sort_'+id, x);
   widget(id);
}

// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone){
s=stripCharsInBag(strPhone,validWorldPhoneChars);
return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

function ValidatePhone(str){
        var Phone=str;

        if ((Phone==null)||(Phone=="")){
                return false;
        }
        if (checkInternationalPhone(Phone)==false){
                return false;
        }
        return true
 }

// -------------------- End widget function -------------------------