
var browserVersion = parseInt(navigator.appVersion);
var isNetscape = navigator.appName.indexOf("Netscape") != -1;
var isIE = navigator.appName.indexOf("Microsoft") != -1;
var agent = navigator.userAgent.toLowerCase();
var isWindows = agent.indexOf("win") != -1;
var isMac = agent.indexOf("mac") != -1;
var isNix = agent.indexOf("X11") != -1;

/**
 * A wrapper to the window.open function
 */
function OpenWindow(url, width, height, left, top, name, scroll, resize) {
        if (width==null) width = 500;
        if (height==null) height = 500;
        if (left == null) left = 10;
        if (top == null) top = 10;
        if (name == null) name = 'usps_intranet_popup';
        if (scroll == null) scroll = 'no';
        if (resize == null) resize = 'no';

        window.open(url, name, 'toolbar=no,location=no,status=no,directories=no,menubar=no,scrollbars='+scroll+',resizable='+resize+',width='+width+',height='+height+',left='+left+',top='+top, 'replace=false');
}


function openWindow(url, width, height, left, top, name, scroll, resize) {
	if (width == null) width = 500;
	if (height == null) height = 400;
	if (left == null) left = 10;
	if (top == null) top = 10;
	if (name == null) name = 'tjd_popup';
	if (scroll == null) scroll = 'no';
	if (resize == null) resize = 'no';

	window.open(url, name, 'toolbar=no,location=no,status=no,directories=no,menubar=no,scrollbars='+scroll+',resizable='+resize+',width='+width+',height='+height+',left='+left+',top='+top, 'replace=false');
}


/**
 * A function to throw a js error
 */
function ThrowError (errors) {
	if (!errors) return false;

	var msg_str = "The following errors occured:\n\n";
	for(var i=0; i < errors.length; i++) {
		msg_str += "  "+(i+1)+". "+errors[i]+"\n";
	}

	alert (msg_str);
	return true;
}

function ChangeBgColor (obj, color) {
	obj.style.backgroundColor = color;
}

function ChangePointer (obj, pointer) {
	obj.style.cursor = pointer;
}

function SetStatus (st) {
	window.status = st;
}

function GoToPage (page)
{
    window.location = page;
}

var preloadedimages = new Array();
function PreloadImages ()
{
    for (i=0;i<PreloadImages.arguments.length;i++)
    {
        var nimages = preloadedimages.length;
        preloadedimages[nimages]=new Image();
        preloadedimages[nimages].src=PreloadImages.arguments[i];
    }
}


function delete_directory_entry(entry_id) {
   
   if (confirm('Are you sure you want to delete this entry?\nPlease Note: It will be permantently deleted.'))
    {
   document.directoryEntryDelete.entry_id.value=entry_id;
   document.directoryEntryDelete.submit();
   return false;
   } else {
   return false;
   }
   

 }


function doConfirm(text)
{
    val=confirm(text);
    return (!val) ? false : true;
}


function change_inner_text ( targetId, text1, text2 ) {
	if (document.getElementById) {
		if ( innerTxt =  document.getElementById( targetId )) {
				if (innerTxt.innerHTML==text1) { 
			    innerTxt.innerHTML = text2;
			  } else {
				innerTxt.innerHTML = text1;
			  } 	
		   }
	}	
}

function toggle( targetId ){
  if (document.getElementById){
  		target = document.getElementById( targetId );
  			if (target.style.display == "none"){
  				target.style.display = "";
  			} else {
  				target.style.display = "none";
  			}
  	}
} 


function dropdownSubmit(select, formName)
{
    selectedIndex = select.options.selectedIndex;
    selectedValue = select.options[selectedIndex].value;
    if (selectedValue != null && selectedValue != '') {
        document.forms[formName].submit();
    }
}

function newdropdownSubmit(select, formName, inputname, formToSend)
{
    selectedIndex = select.options.selectedIndex;
    selectedValue = select.options[selectedIndex].value;
    if (selectedValue != null && selectedValue != '') {
        document.forms[formToSend][inputname].value = selectedValue;
		document.forms[formToSend].submit();
    }
}

/**
* clear a form default value
*/

function clearText(thefield){
	if (thefield.defaultValue==thefield.value) {
	 thefield.value = "";
	} else if (thefield.value=='') {
	 thefield.value = thefield.defaultValue;
	} 
} 

/**
* x-marketing select a forum
*/

function selectForum()
{
	 var forumlocation = document.selectforum.forum.value;
	 window.location=forumlocation;
}

function selectCMSArchive()
{
	var cmslocation = document.selectarchive.content.value;
	window.location=cmslocation;
	
}

/**
* Find a property.com on classifieds
*/

function checkfindaprop()
{

		var errFlag = new Array();
		_errMsg = '';
		var frm = document.forms['findAPropCom'];
		
		var value = frm.elements['salerent'].value;
		var field = frm.elements['salerent'];
		var regex = /(\s|\S)/;
		if (!regex.test(value) && !errFlag['salerent']) {
			errFlag['salerent'] = true;
			_errMsg = _errMsg + '\n - Please select whether you would like to buy or rent';
		}
		
		if (_errMsg != '') {
			_errMsg = _errMsg + '\nPlease correct these fields.';
			alert(_errMsg);
			return false;
		}
		
		var value = frm.elements['minprice'].value;
		var field = frm.elements['minprice'];
		if (field.defaultValue==value) {
	 		field.value = "";
		}
		
		var value = frm.elements['maxprice'].value;
		var field = frm.elements['maxprice'];
		if (field.defaultValue==value) {
	 		field.value = "";
		}
		
		return true;
 }

/**
* Find a property.com on classifieds
*/

function toggle_rent_time(){
  
    if (document.getElementById){
	target = document.getElementById('weekly_monthly');
	var frm = document.forms['findAPropCom'];
	var value = frm.elements['salerent'].value;
	var field = frm.elements['salerent'];
  		
		if (value==1) {
			target.style.display = "";
		} else {
			target.style.display = "none";
		}

  	}
} 


function windowPopSetup() {
	var a;
	for (var i = 0; (a = document.links[i]); i++) {
		if (a.target && a.target.indexOf("_windowPop") == 0) {
			a.onclick = windowPop;
		}
	}
	
	var form;
	for (var i = 0; (form = document.forms[i]); i++) {
	   if (form.target && form.target.indexOf("_windowPop") == 0) {
		 form.onsubmit = windowPopForm;   
	   }
	}
}

function windowPop() {
	var a = this.target.split(":");
	var sFeatures = a[1];
	window.open(this.href, a.length > 2 ? a[2] : String((new Date()).getTime()), sFeatures);
	return false;
}

function windowPopForm() {
	
	var a = this.target.split(":");
	var sFeatures = a[1];
	var sendurl = this.action;
	var windowname = a.length > 2 ? a[2] : 'winPop'
	window.open('about:blank', windowname, sFeatures);
	this.target = windowname;
	this.submit();
	return false;
}



function word_count_init(max_words) {
    setTimeout('calculate_word_count('+max_words+')', 250);
}

function calculate_word_count(max_words) {
    var content_id = document.getElementById('textarea_id').value;
    var content    = document.getElementById(content_id);
    var num_words  = document.getElementById('num_words');
    
    var words_arr  = content.value.split(/\s+/g); // split the sentence into an array of words
    var word_count = words_arr.length;
    if (words_arr[words_arr.length - 1] == '' || words_arr[words_arr.length - 1] == ' ') word_count--;
    
    if (word_count > max_words) {
        var newstr = '';
        for (var i=0; i < max_words; i++) {
            if (i>0) newstr += " ";
            newstr += words_arr[i];
        }
        content.value = newstr+" ";
    }
    
    num_words.value = word_count;  
    
    setTimeout('calculate_word_count('+max_words+')', 250);
}

function pageNav(increment) {
  f=document.pageNavForm;
  if (increment == "next") {
    f.page_num.value++;
  } else {
    f.page_num.value--;
  }
  f.submit();
  return false;
}

function gotoPage(pageNo) {
  f=document.pageNavForm;
  f.page_num.value=pageNo;
  f.submit();
  return false;
}


function clearSearchText(id1, id2) {

  if (!document.getElementById) return false;
	  if (id1) {
		if (!document.getElementById(id1)) return false;
			document.getElementById(id1).value = '';
	  }
	  if (id2) {
		if (!document.getElementById(id2)) return false;
			document.getElementById(id2).value = '';
	  }
}


function setUpEntQuiz () {
   if (document.getElementById) {
        if (document.getElementById('quiz_submit_button')) {
                document.quiz_questions_form.quiz_submit_button.disabled = true	
		}
   }
}

function makeLiveSubmitButtonNow() {
	if (document.getElementById) {
        if (document.getElementById('quiz_submit_button') && document.getElementById('quizConfirmTerms')) {
                var ourform = document.quiz_questions_form;
				if (ourform.quizConfirmTerms.checked) {
						ourform.quiz_submit_button.disabled = false;
				} else {
						ourform.quiz_submit_button.disabled = true;
				}
		}
   }	
		
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

addLoadEvent(windowPopSetup);
addLoadEvent(setUpEntQuiz);



