// JavaScript Document

//the show/hide windows

var shown = [];

function showChecked(id){
	if(this.checked == true){
		var div = document.getElementById(id);
		div.style.display='block';
	} else {
		var div = document.getElementById(id);
		div.style.display='none';
	}
}

function showPointerDiv(type) {
	//make sure array key has been defined - if not = false
	if(window.shown[type] == undefined){
		shown[type] = false;	
	}
	if(!shown[type]){
		var div = document.getElementById(type);
		div.style.display='block';
		shown[type] = true
	} else {
		hidePointerDiv(type);
	}
}
function hidePointerDiv(type){
	var div = document.getElementById(type);
	div.style.display='none';
	shown[type] = false;
}

function popUp(theURL,winName,features) {
  window.open(theURL,winName,features);
}

/*links functions*/
function selectAll(id){
	document.getElementById(id).focus();
	document.getElementById(id).select();
}
function maxChars(m_chars, id){
	if(document.getElementById(id).length > m_chars){
		alert("shit!")
	}
}

/* forum functions*/
function setReadOnly(id){
	document.getElementById(id).readOnly=true;
}
function unsetReadOnly(id){
	document.getElementById(id).readOnly=false;
}
function addFrownie(post, key){
	document.forum_post.elements[post].value += " "+key+" ";
	/*document.forum_post.elements[post].focus();*/
	return;
}
function populateStates(countrylistId, statelistid, selectedState) {

	var objstate= document.getElementById(statelistid);
	var objregionlabel = document.getElementById('states_div');
	var objcountry = document.getElementById(countrylistId);
	objstate.options.length = 0;
	var statelist = j_statelist;	

	var countryId = objcountry[objcountry.selectedIndex].value;
	var counter = 0;
	var states = new Array();

	for ( i=0; i < statelist.length; i++ ){
		if ( statelist[i].c_id == countryId){
		    //copy matching states into an array
		    states[counter] = { optText:statelist[i].title, optValue:statelist[i].s_id };
		    counter++;
		}
	}	

	// sort the states array
	states.sort(sortAsc);

    // copy sorted options from array to states dropdown
    for (var j=0; j<states.length; j++) {
        var optObj = document.createElement('option');
        optObj.text = states[j].optText;
        optObj.value = states[j].optValue;
        objstate.options.add(optObj);

        if(states[j].optValue == selectedState){
            optObj.selected = true;
		}
    }

	if(counter > 0){	
	 	objstate.style.display='block';
	 	objregionlabel.style.display = 'block';
	} else {	
	    objregionlabel.style.display = 'none';
		objstate.style.display='none';
	}
}
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}
// sort function - ascending (case-insensitive)
function sortAsc(r1, r2) {
    var v1 = r1.optText.toLowerCase();
    var v2 = r2.optText.toLowerCase();
    if (v1 > v2) return(1);
    if (v1 < v2) return(-1);
    return(0);
}

function jumpToURL(surl) {
	window.location.href = surl;
}


