// onload

////////////////////
// addLoadEvent() //
////////////////////

function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function () {
oldonload();
func();
}
}
}

// Ajax Functions

///////////////////
// getCalendar() //
///////////////////

function getCalendar(y,m) {
var request = createXMLHttpRequest();
ajaxRequest(request,'/ajax/openhouse_calendar.php',function () { returnTextData(request,'calendar_container'); },'y',y,'m',m);
}

// General Functions

////////////////
// in_array() //
////////////////

function in_array(needle,haystack) {
var found = false;
for (var i = 0; i < haystack.length; i++) {
if (haystack[i] == needle) {
found = true;
break;
} else {
found = false;
}
}
return found;
}

//////////////////
// getFileExt() //
//////////////////

function getFileExt(thisFileName) {
if (!thisFileName) {
return false;
}
if (thisFileName.length == 0) {
return false;
}
var dot = thisFileName.lastIndexOf('.');
if (dot == -1) {
return false;
}
var ext = thisFileName.substr(dot,thisFileName.length);
ext = ext.replace('.','');
ext = ext.toLowerCase();
if (ext.length == 0) {
return false;
}
return ext;
}

////////////////////////
// getClassElements() //
////////////////////////

function getClassElements(className, tag, elm) {
var testClass = new RegExp("(^|\\\\s)" + className + "(\\\\s|$)");
var tag = tag || "*";
var elm = elm || document;
var elements = (tag == "*" && elm.all) ? elm.all : elm.getElementsByTagName(tag);
var returnElements = [];
var current;
var elementLength = elements.length;
for (var i = 0; i < elementLength; i++) {
current = elements[i];
if (testClass.test(current.className)) {
returnElements.push(current);
}
}
return returnElements;
}

function viewMap(url,urlwidth,urlheight){
var url = url || 'map_print.htm';
var winheight = urlheight || 450;
var winwidth = urlwidth || 800;
var winMap=window.open(url,'new','toolbar=no,status=no,scrollbars=no,location=no,menubar=no,directories=no,resizable=yes,width='+winwidth+',height='+winheight+'');
}

function viewImage(imagelink,folder,urlwidth,urlheight,url) {
var url = url || '';
var imagelink = imagelink;
var folder = folder || '';
var imagesrc = folder+'/'+imagelink;
var urlwidth = urlwidth || 640;
var urlheight = urlheight || 480;
var screenwidth = screen.width;
var screenheight = screen.height;

if (urlwidth > screenwidth || urlheight > screenheight){ // url is larger
var showscroll = 'yes';
} else {
var showscroll = 'no';
}

if (urlwidth > screenwidth){ // width is larger
var leftpos = 0;
var winwidth = screenwidth;
} else {
var leftpos = (screenwidth - urlwidth)/2;
var winwidth = urlwidth;
}

if (urlheight > screenheight){ // height is larger
var toppos = 0;
var winheight = screenheight;
} else {
var toppos = (screenheight - urlheight)/2;
var winheight = urlheight;
}

var win=window.open(url+'?image='+imagesrc,'new','toolbar=no,status=no,scrollbars='+ showscroll +',location=no,menubar=no,directories=no,resizable=yes,left=' + leftpos + ',top=' + toppos + ',width=' + winwidth + ',height=' + winheight + '');
//var win=window.open(url+'?'+imagesrc,'new','toolbar=no,status=no,scrollbars='+ showscroll +',location=no,menubar=no,directories=no,resizable=no,left=' + leftpos + ',top=' + toppos + '');
win.focus()
}

function windowSizer(){
var imgWidth =  document.images[0].width;
var imgHeight =  document.images[0].height;
window.resizeTo(imgWidth, imgHeight);
var left = (screen.width - imgWidth) / 2;
var top = (screen.height - imgHeight) / 2;
self.moveTo(left, top);
//alert(imgWidth+'x'+imgHeight);
var clientWidth = document.body.parentNode.clientWidth;
var clientHeight = document.body.parentNode.clientHeight;

var wDif = clientWidth-imgWidth;
var hDif = clientHeight-imgHeight;

if( wDif != 0 || hDif != 0 ) {
//alert(wDif+' x '+hDif);
if(wDif > 0 || wDif < 0){
wDif = wDif*(-1);
};
if(hDif > 0 || hDif < 0){
hDif = hDif*(-1);
};
window.resizeBy(wDif, hDif);
var left = (screen.width - imgWidth) / 2;
var top = (screen.height - imgHeight) / 2;
self.moveTo(left, top);
//alert('client: '+clientWidth+'x'+clientHeight+'\nimg: '+imgWidth+'x'+imgHeight+'\ndif: '+wDif+'x'+hDif);
}	
self.focus();
}

function encrypt(username,hostname,linktext,subject,message){
var username = username || 'nina';
var hostname = hostname || 'ninaruberto.com';
var address = username + '&#64;' + hostname;
var linktext = linktext || address;
var subject = subject || '';
if (subject != '') {
subject = '?subject=' + subject;
}
var message = message || '';
if (message != '') {
message = '?body=' + message;
}
var output = '<a href="&#109;&#97;&#105;&#108;' + '&#116;&#111;&#58;'+ address + subject + message +'" class="email">'+ linktext +'</a>';
document.write(output);
}
