// Open HTML Editor
var editor_win = null ;
function OpenEditor(url){
    if (editor_win==null || editor_win.closed) {
        // open window and assign to global variable
        editor_win = window.open(url ,'editor','width=710,height=520,menubar=0,resizable=1,status=0,titlebar=0,toolbar=0');
    }
    else {
        // if window already open, load url again
        editor_win.location.href = url;
    }
    // give window the focus
    if (editor_win.focus) editor_win.focus()
    return false;
}



//------------
// standard popup ( href, width, height, features )
// args are optional
//------------
var popup_win ;

function popup( ) {

    var features ;

    // set default width and height
    // Note: comparison of undefined element to null will fail in Netscape

    if ( !arguments[0] )  url="";  else url = arguments[0]
    if ( !arguments[1] )  width = 710 ;  else width = arguments[1]
    if ( !arguments[2]  ) height = 560 ; else height = arguments[2]
    if ( !arguments[3]  )
    	features = "location=0,menubar=0,resizable=1,scrollbars=0,status=0,titlebar=0,toolbar=0";
	else
		features = arguments[3]

    features += ",width=" + width + ",height=" + height ;

    if (popup_win==null || popup_win.closed) {
        // open window and assign to global variable
        // NOTE : help pages target this window by name
        popup_win = window.open( url , "window", features );
    }
    else {
        // if window already open, load new url
        popup_win.location.href = url;
        // when resizing, have to take titlebar into consideration
        popup_win.resizeTo ( width, height+30 );
    }

    // give popup the focus
    if (popup_win.focus) popup_win.focus()

    return false;
}

var imgWin = null ;
function imgPopup ( imgUrl ) {

    // preload the image to get height and width
    var imgPreload = new Image();
    imgPreload.src = imgUrl ;

    /*
    var features = "location=0,menubar=0,resizable=1,scrollbars=0,status=0,titlebar=0,toolbar=0";
    features += ",width=" + w + ",height=" + h ;
	*/
    var features = "location=0,menubar=0,resizable=1,scrollbars=0,status=0,statusbar=0,titlebar=0,toolbar=0,width=600,height=600";
    if (imgWin==null || imgWin.closed) {
        // open window and assign to global variable
        // NOTE : help pages target this window by name
        // w & h exclude chrome
        imgWin = window.open( imgUrl , 'image', features );
    }
    /*else {
        // if window already open, load new url
        imgWin.location.href = imgUrl;
        // when resizing, have to take titlebar into consideration
        h += 30;
        w += 12 ;
        imgWin.resizeTo ( w, h );
    }*/
    with (imgWin.document){
        writeln('<html><head>');
        writeln('<meta http-equiv="imagetoolbar" content="false">');
        writeln('<title>Image</title>');
        writeln('<style type="text/css">');
        writeln('body { padding:0;margin:5px;text-align:center;font-family:verdana;font-size:11px; }');
        writeln('</style>');
		writeln('<script language="JavaScript" type="text/javascript">');
		writeln('function resizewin() {');
		writeln('w = document.myimage.width + 30;');
		// windows - depending on user's security settings cannot turn off status bar
		writeln('h = document.myimage.height + 100;');
		writeln('focus();');
		// writeln('alert(w+" "+h);');
		writeln('window.resizeTo(w,h);');
		writeln('}');
		writeln('window.onload = resizewin;');
		writeln('</script>');
		writeln('</head>');
		writeln('<body>');
        writeln('<img name="myimage" src="'+imgUrl+'" style="border:1px solid #333">' );
        
        writeln('<div style="top:5px;right:5px;position:absolute"><a href="javascript:self.close()"><img src="images/fam_cancel.gif" alt="Close" border="0"></a></div>');
        writeln('</body></html>' );
        close () ;
    }
    return false ;
}


function resizePopop () {
	w = imgWin.document.myimage.width  - imgWin.document.body.clientWidth + 30;
	h = imgWin.document.myimage.height - imgWin.document.body.clientHeight + 30;
	imgWin.resizeBy(w,h);
    imgWin.focus ();
}

function FitPic() {

       iWidth = (NS)?window.innerWidth:document.body.clientWidth;

       iHeight = (NS)?window.innerHeight:document.body.clientHeight;

       iWidth = document.images[0].width - iWidth;

       iHeight = document.images[0].height - iHeight;

       window.resizeBy(iWidth, iHeight);

       self.focus();

     };
//-----------
// confirm delete
//-----------
function ConfirmDelete( title ) {
    var msg = "Do you really want to delete  '" + title + "' ?"
    if ( confirm(msg) )
        return true;
    else
        return false ;
}


//-----------
// EmptyField - form validation
// if a field is empty, output warning and return true
//-----------
function EmptyField( input ) {
    if ( isEmpty( input.value ) ) {
        input.focus() ;
        alert ("You did not enter a value for '" + input.name  +
            "'\n\nThis is a required field. Please enter it now.");
        return true ;
    }
    return false ;
}

function EmptySelect ( sel, fieldname ) {
	if ( isEmpty(fieldname) ) fieldname = sel.name ;
	if ( isEmpty(sel.selectedIndex) || isEmpty(sel.options[sel.selectedIndex].value) ) {
        sel.focus() ;
        alert ("You did not enter a value for '" + fieldname  +
            "'\n\nThis is a required field. Please enter it now.");
        return true ;
    }
    return false ;
}

// return true if the string is null or its length = 0
function isEmpty(str) {
    return ((str == null) || (str.length == 0))
}

//------------
// isValidNumber - true if number between 0...999
function isValidNumber( input ) {
  var val = parseInt(input);
  if (isNaN(val) || val < 0 || val > 999) { return false; }
  return true;
}


function find_obj(name) {

    if (document.getElementById) {
        if (document.getElementById(name) ) return document.getElementById(name);

    } else if (document.all) {
        if ( document.all[name] ) return document.all[name];

    } else if (document.layers) {
        if (document.layers) return document.layers[name];
    }
    return null;
}


//-----------------
// suggestLogin - called from user forms
// checks for user_firstname and user_lastname
function suggestlogin () {
  var firstname = document.editform.user_firstname ;
  var lastname  = document.editform.user_lastname

    if (isEmpty(firstname) || isEmpty(lastname)) {
        alert ( "You need to enter the user's firstname and lastname.");
        return;
    }
    var url = "suggestlogin.php?f="+firstname.value+"&l="+lastname.value ;
    popup(url,160,140);
}



function toggle(id) {
    if (!document.images) return ;

    var img = document.images['infoImg'+id]
    if (!img) return ;
    var div = document.getElementById('info'+id);
    if (!div) return ;

    if (div.style.display=='none') {
        div.style.display = 'block' ;
        div.style.visibility = 'visible' ;
        img.src = 'images/up.gif' ;
    }
    else {
        div.style.display = 'none' ;
        div.style.visibility = 'hidden' ;
        img.src = 'images/down.gif' ;
    }
}

function winHeight () {
	var myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myHeight = window.innerHeight;
	} else if( document.documentElement &&
	( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myHeight = document.body.clientHeight;
	}

	return myHeight ;
}


function winsize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement &&
      ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }

  window.alert( 'Width = ' + myWidth );
  window.alert( 'Height = ' + myHeight );
}
