// StyleSwitcher functions written by Paul Sowden
// http://www.alistapart.com/stories/alternate/
function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName('link')[i]); i++) {
    if(a.getAttribute('rel').indexOf('style') != -1 && a.getAttribute('title')) {
      a.disabled = true;
      if(a.getAttribute('title') == title) { a.disabled = false; }
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName('link')[i]); i++) {
    if(a.getAttribute('rel').indexOf('style') != -1 && a.getAttribute('title') && !a.disabled) { return a.getAttribute('title'); }
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName('link')[i]); i++) {
    if(a.getAttribute('rel').indexOf('style') != -1 && a.getAttribute('rel').indexOf('alt') == -1 && a.getAttribute('title')) { return a.getAttribute('title'); }
  }
  return null;
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = '; expires='+date.toGMTString();
  }
  else { expires = ''; }
  document.cookie = name+'='+value+expires+'; path=/';
}

function readCookie(name) {
  var nameEQ = name + '=';
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') { c = c.substring(1,c.length); }
    if (c.indexOf(nameEQ) == 0) { return c.substring(nameEQ.length,c.length); }
  }
  return null;
}

// Copyright (c) 1996-1997 Athenia Associates.
// http://www.webreference.com/js/
// License is granted if and only if this entire
// copyright notice is included. By Tomer Shiran.

function setCookie (name, value, expires, path, domain, secure) {
    var curCookie;
    curCookie = name + '=' + escape(value);  
    if (expires) { curCookie += '; expires=' + expires.toGMTString(); } 
    else { curCookie += ''; }
    if (path){ curCookie +='; path=' + path; } else { curCookie += ''; }
    if (domain){ curCookie +='; domain=' + domain; } else { curCookie += ''; }
    if (secure){ curCookie +='; secure'; } else { curCookie += ''; }    
    document.cookie = curCookie;
}

function getCookie (name) {
    var prefix = name + '=';
    var c = document.cookie;
    var nullstring = '';
    var cookieStartIndex = c.indexOf(prefix);
    if (cookieStartIndex == -1){ return nullstring; }
    var cookieEndIndex = c.indexOf(';', cookieStartIndex + prefix.length);
    if (cookieEndIndex == -1){ cookieEndIndex = c.length; }
    return unescape(c.substring(cookieStartIndex + prefix.length, cookieEndIndex));
}

function deleteCookie (name, path, domain) {
    if (getCookie(name)) {
        document.cookie = name + '=';
        if (path){ document.cookie +='; path=' + path; } else { document.cookie += ''; }
        if (domain){ document.cookie +='; domain=' + domain; } else { document.cookie += ''; }
        document.cookie += '; expires=Thu, 01-Jan-70 00:00:01 GMT';
    }
}

function fixDate (date) {
    var base = new Date(0);
    var skew = base.getTime();
    if (skew > 0){ date.setTime(date.getTime() - skew); }
}

function rememberMe (f) {
    var now = new Date();
    fixDate(now);
    now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
    setCookie('mtcmtauth', f.author.value, now, '', HOST, '');
    setCookie('mtcmtmail', f.email.value, now, '', HOST, '');
    setCookie('mtcmthome', f.url.value, now, '', HOST, '');
}

function forgetMe (f) {
    deleteCookie('mtcmtmail', '', HOST);
    deleteCookie('mtcmthome', '', HOST);
    deleteCookie('mtcmtauth', '', HOST);
    f.email.value = '';
    f.author.value = '';
    f.url.value = '';
}

function addFav() {
	if (document.all) { window.external.AddFavorite('http://www.interfusions.com', 'Interfusions (if)'); 
    } else { alert('Your browser does not support this action.'); }
}

function chkAlphaNum (obj) {
    var alphanum = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    var checkstr = obj.value;
    for (i = 0;  i < checkstr.length; i++){
        ch = checkstr.charAt(i);
        for (j = 0;  j < alphanum.length; j++) {
            if (ch == alphanum.charAt(j)) { break; }
        }
        if (j == alphanum.length) { return false; }
    }
    return true;
}

function chkName (obj) {
    var alphanum = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
    var checkstr = obj.value;
    for (i = 0;  i < checkstr.length; i++){
        ch = checkstr.charAt(i);
        for (j = 0;  j < alphanum.length; j++) {
            if (ch == alphanum.charAt(j)) { break; }
        }
        if (j == alphanum.length){ return false; }
    }
    return true;
}

function chkEmail(obj) {
    //var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    //return re.test(obj.value);
    return (obj.value.indexOf(".") > 2) && (obj.value.indexOf("@") > 0);
}

// Alert and Help Boxes

function createMsgBx(initY){ 
    // winW = (ns4)? window.innerWidth-16 : document.body.offsetWidth-20;
    // winH = (ns4)? window.innerHeight : document.body.offsetHeight;
    if (ns4) { winW = window.innerWidth-16; } else { winW = document.body.offsetWidth-20; }
    if (ns4) { winH = window.innerHeight; } else { winH = document.body.offsetHeight; }
    winX = winW/2 -200; // 400 is width of box
    if (initY) { winY = initY; }
    else { winY = winH/2 -100; } // 200 is height of box
    
    style_str = '<style> #msgbx { position: absolute; top: ' + winY + '; left: ' + winX + '; } </style>';
    document.write(style_str);
}

// Alert text variables
var tconfirm = '';

// Help text variables 
var tcode = '<p>To join IF, you need an account creation code. There are two ways to get one: <ul><li>From an existing IF member -- many people hear about IF because their friends are using the site. If this is the case for you, go ask one of your friends for a code. They can get one here. This will get you a basic free account. <li>Purchasing one -- you can purchase a paid account and get access to all the features of IF. </ul></p>';
var tusn = '<p><b>Username Requirements:</b> <ul><li>No blank spaces</li><li>At least 3 characters</li><li>Only alphanumeric characters allowed</li></ul></p>';
    
function chgMsg(txt) {
    document.getElementById('msgbx').style.display = 'block'; // Display hidden div
    document.getElementById('msgbx').innerHTML = closex; // Empty div and add close button to top
    document.getElementById('msgbx').innerHTML += txt; // Append appropriate text
}

// Sniff browser type (Short version)
var ns4, ie4;
if (document.layers) { ns4 = true; } else { ns4 = false; }
if (document.all) { ie4 = true; } else { ie4 = false; }

// Login functions
var error_color = '#ddeeff';

function showError(obj, msg){
    hideSelect();
    msg = '<p><b>ERROR!</b></p><p>' + msg + '</p>';
    chgMsg(msg);
    if (obj) { obj.style.background = error_color; }
    //obj.focus();
}

function hideSelect(){ // This function hides all select boxes; z-index does not work with selects and divs (only IE)
    if (ie4) {
        var allsel = document.getElementsByTagName('select');
        for (i=0; i<allsel.length; i++){
            if (allsel[i].style.visibility=='hidden'){ allsel[i].style.visibility='visible'; }
            else { allsel[i].style.visibility='hidden'; }
        }
    }
}

function doUpload() {
    var f = document.frmUpload;
    //f.imgfile.value = f.imgfile.value.underscore();
    if (f.imgfile.value == ''){ showError(f.imgfile,'File needed.'); return; } 
    document.getElementById('btnUpload').style.background = '#ffffff';
    document.getElementById('btnUpload').style.border = 0;
    f.btnUpload.value = 'Processing...';
	f.submit();
}

// Text constants
//   Use text links
//   if (ie4) var closex = "<p align=\"right\"><a href=\"javascript:void(0)\" onclick=\"document.getElementById('msgbx').style.display='none'; hideSelect()\">close [x]</a></p>";
//   else var closex = "<p align=\"right\"><a href=\"javascript:void(0)\" onclick=\"document.getElementById('msgbx').style.display='none';\">close [x]</a></p>";
//   Use form buttons
if (ie4) var closex = "<p align=\"right\"><form><input type=\"button\" value=\"x\" class=\"btn\" style=\"float:right\" onclick=\"document.getElementById('msgbx').style.display='none'; hideSelect()\"></form></span>";
else var closex = "<p align=\"right\"><form><input type=\"button\" value=\"x\" class=\"btn\" style=\"float:right\" onclick=\"document.getElementById('msgbx').style.display='none';\"></form></p>";

// IFOrmation functions
function createPopup (width, height, href, name, scrollbars, resizable) {
    //if (width < 530) width = 530;
    var options = 'width=' + width + ',height=' + height;
    if (name != window.name){ options += ',screenX=20,screenY=20,left=20,top=20'; }
    if (scrollbars){ options += ',scrollbars=yes'; }
    else { options += ',scrollbars=no'; }
    if (resizable){ options += ',resizable=yes'; }
    else { options += ',resizable=no'; }
    var wnd = window.open(href, name, options);
    if (wnd && wnd.focus){ wnd.focus(); }
    return (wnd);
}
function popIFO (usid){
    //href = '/ifo/index.php/user/' + usid + '/';
    href = '/ifo/?uid=' + usid;
    createPopup(540,260,href,'IFO',0,0);
}

function popIFO_public (usid){
    //href = '/public/ifo/index.php/user/' + usid + '/';
    href = '/public/ifo/?uid=' + usid;
    createPopup(540,260,href,'IFO',0,0);
}

var cookie = readCookie('style');
var title;
if (cookie) { title = cookie; } else { getPreferredStyleSheet(); }
setActiveStyleSheet(title);

var HOST = 'www.interfusions.com';

// Common string functions
String.prototype.trim = function () { return this.replace(/^\s*|\s*$/,''); }
String.prototype.underscore = function () { return this.replace(/\s*/,'_'); }
function ss(txt){ window.status = txt; return true; }

window.onload = function(e) {
  var cookie = readCookie('style');
  var title;
  if (cookie) { title = cookie; } else { title = getPreferredStyleSheet(); }
  setActiveStyleSheet(title);
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie('style', title, 365);
}