function getArgs(query) {
    var args = new Object(  );
    // Get query string
    var pairs = query.split("&");
     // Break at comma
    for(var i = 0; i < pairs.length; i++) {
        var pos = pairs[i].indexOf('=');
          // Look for "name=value"
        if (pos == -1) continue;
          // If not found, skip
        var argname = pairs[i].substring(0,pos);
          // Extract the name
        var value = pairs[i].substring(pos+1);
          // Extract the value
        args[argname] = unescape(value);
         // Store as a property
       // In JavaScript 1.5, use decodeURIComponent(  ) 
       // instead of escape(  )
    }
    return args;     // Return the object
}

function isEmpty(string) {
    if (string == null || string == "") 
	return true;
    else 
	return false;
}

function contains(items, value) {
    for(var i = 0 ; i < items.length ; i++) {
        if(value == items[i]) {
	    return true;

	}
    }
    return false;
}
	
function detectCookie(urlToRedirectTo) {
    var tmpcookie = new Date();
    chkcookie = (tmpcookie.getTime() + '');
    document.cookie = "chkcookie=" + chkcookie + "; path=/";
    if (document.cookie.indexOf(chkcookie,0) < 0) {
	window.location.href = urlToRedirectTo;
    }
}

function setAdultPrefFromCookie(aTag) {
    if (readCookie('adultPrefCookie') != null) 
	aTag.href = aTag.href + '&adultPref='+readCookie('adultPrefCookie');
}

function showPreview(clicked) {
    if ($("#anonymity_not_logged" ).length) return;
    jqueryClicked = $(clicked);
    var indexes = [1,2,3];
    jQuery.each(indexes, function() {
	if (this == jqueryClicked.attr("value")) {
	    $("#anonymity_div_" + this).fadeIn(500);
	} else {
	    $("#anonymity_div_" + this).hide();
	}
    });

}

function checkMinLenght() {
    var textfield = $("#short_question_text").val();

    if (textfield.length < 20 || textfield == "In a nutshell, what's your question?") {
	$("#exclamation").show();
	return false;
    }
    document.entry.submit();
}

function checkAliasAvailability(aliasInputElement, firstNameElement, statusElement, indicatorElement, button) {
    $(button).hide();
    $(indicatorElement).show();
    $(statusElement).text("");
    $.getJSON("/index.aspx?template=check_alias_availability.ascx", { alias: $(aliasInputElement).val(),
								      first_name: $(firstNameElement).val()}, 
	      function(json) {
		  $(indicatorElement).hide();
		  $(button).show();
		  if(json.result) {
		      $(statusElement).css("color", "green")
		      $(statusElement).text("Available");
		  } else {
		      $(statusElement).css("color", "red")
		      $(statusElement).text("Not available");
		      $(aliasInputElement).val(json.alias);
		  }
	      });
}

function createScreenName(firstNameElement, screenNameElement, indicatorElement, button) {
    $(button).hide();
    $(indicatorElement).show();
    $.getJSON("/index.aspx?template=create_alias.ascx", { first_name: $(firstNameElement).val() }, 
	      function(json) {
		  $(indicatorElement).hide();
		  $(button).show();
		  if (json.error == "") {
		      $(screenNameElement).val(json.result);
		  } else {
		      alert(json.error);
		  }
	      });
}

function targetingSelectAll(element) {
    $(element).find('tbody > tr > td :input').each(function() {
	this.checked = true;
    });
}

function targetingAdvancedFormCheck() {
   var chosen = false;
   $("#gender_woman").find('tbody > tr > td :input').each(function() {
	if(this.checked == true) {
	  chosen = true;
	}
   });
   $("#gender_guy").find('tbody > tr > td :input').each(function() {
	if(this.checked == true) {
	  chosen = true;
	}
   });
   if(chosen != true) {
     $("#gender_woman").find('tbody > tr > td :input').each(function() {
	this.checked = true;
     });
     $("#gender_guy").find('tbody > tr > td :input').each(function() {
	this.checked = true;
     });
   }
}

function countCharsAndHideExclamation(element) {
    countChars(element);
    if ($(element).val().length > 19) $("#exclamation").hide();
}

function showHideCities(element) {
    showHideCitiesWithoutStates(element);
    if($(element).val() == "219") {
	$("#219").hide();
	$("#219_state").show();
    }
}

function showHideCitiesWithoutStates(element) {
    $("#" + last).hide();
    $("#" + last +"_state").hide();
    $("#widget" + last +"_state").val(0);
    $("#widget"+last).val(0);
    $("#" + $(element).val()).show();
    last=$(element).val();
}

function showCitySelect(element) {
    // only for US, id = 219
    if (cityStateMapping[$(element).val()] && cityStateMapping[$(element).val()][0]) {
	$("#widget219").removeOption(/./);
	$("#widget219").addOption("","Please Select", true);
	jQuery.each(cityStateMapping[$(element).val()], function() {
	    $("#widget219").addOption(this.city_id,this.city_name, false);
	});
	$("#219").show();
	return true;
    } 
    $("#219").hide();
}


function setGuyTypes(containing_div, new_value) {
	$(containing_div).find('tbody > tr > td :input').each(function() {
		$(this).attr("disabled", new_value);
	});

	$(containing_div).find('input:hidden').each(function() {
		$(this).attr("disabled", new_value);
	});
}

function disableGuyTypes(containing_div) {
	setGuyTypes(containing_div, true);
	$(containing_div).hide();

}

function enableGuyTypes(containing_div) {
	$(containing_div).show();
	setGuyTypes(containing_div, false);
}

function toggleGuyTypes(show_guytypes, hide_guytypes) {
	disableGuyTypes(hide_guytypes);
	enableGuyTypes(show_guytypes);
}
