/*
 *
 * commonjoe.js 1.0 - 30 May 2002
 *
 * Common JavaScript functions for the joe collective
 *
 * Copyright (c) 2002 Scott D. Ross - All Rights Reserved.
 *
 * joe@jetropolis.com
 *
 */

function is_mobile()
{
    var mobile;
    var browserName = navigator.appName;
    var browserVersion = parseInt(navigator.appVersion);

    if (browserName == "BlackBerry") { mobile = 1; }
    else { mobile = 0; }

    return mobile;
}

function break_out()
{
    if (window.top != window.self)
        window.top.location.href = window.self.location.href;
    return;
}

function sniff_browser()
{
    var browser;
    var shitty;
    var browserName = navigator.appName;
    var browserVersion = parseInt(navigator.appVersion);
    
    if (browserName == "Netscape" && browserVersion >= 6) { browser = "NN6"; shitty = 0; }
    else if (browserName == "Netscape" && browserVersion == 5) { browser = "NN5"; shitty = 0; }
    else if (browserName == "Netscape" && browserVersion == 4) { browser = "NN4"; shitty = 0; }
    else if (browserName == "Netscape" && browserVersion == 3) { browser = "NN3"; shitty = 0; }
    else if (browserName == "Microsoft Internet Explorer" && browserVersion >= 6) { browser = "IE6"; shitty = 0; }
    else if (browserName == "Microsoft Internet Explorer" && browserVersion == 5) { browser = "IE5"; shitty = 0; }
    else if (browserName == "Microsoft Internet Explorer" && browserVersion == 4) { browser = "IE4"; shitty = 0; }
    else if (browserName == "Microsoft Internet Explorer" && browserVersion == 3) { browser = "IE3"; shitty = 1; }
    else if (browserName == "Opera" && browserVersion >= 6) { browser = "OB6"; shitty = 0; }
    else if (browserName == "Opera" && browserVersion == 5) { browser = "OB5"; shitty = 0; }
    else if (browserName == "BlackBerry") { browser = "BBY"; shitty = 0; }
    else if (browserName == "iPhone" || browserName == "iPod") { browser = "iPX"; shitty = 0; }
    else { browser = "XXX"; shitty = 1; }
    
    alert("Browser identified as " + browser);
}

function advise_you()
{
    window.open("/advisory.html","advisory","width=225,height=125,menubar=0,toolbar=0,location=0,directories=0,status=0,scrollbars=0,resizable=0,copyhistory=0");
    return;
}

function auto_focus()
{
    if (document.forms[0])
        document.forms[0].elements[0].focus();
    return;
}

function count_chars()
{
    var from_val = document.forms[0].from.value;
    var body_val = document.forms[0].body.value;
    
    var from_len = from_val.length;
    var body_len = body_val.length;
    var delimiter_len = 30;
    
    var len = from_len + body_len + delimiter_len;
    
    var validity = false;
    
    if (len <= delimiter_len)
    {
        alert("There is nothing to count. Please enter your message.");
        document.forms[0].from.focus();
    }
    else if (len <= 160)
    {
        alert("Message is currently "
        + len
        + " characters. Message length is okay.");
        validity = true;
    }
    else
    {
        alert("Message length is "
        + len
        + " characters. Please shorten the message.");
        document.forms[0].body.focus();
    }
    return validity;
}

function validate_form()
{
    validity = true;
    if (!check_empty(document.forms[0].elements[0].value))
    {
        validity = false;
        alert('Please select a file to upload.');
        document.forms[0].elements[0].focus();
    }
    if (validity)
    {
        document.forms[0].submit();
        return validity;
    }
}

function check_email(str) {

    var at="@"
    var dot="."
    var lat=str.indexOf(at)
    var lstr=str.length
    var ldot=str.indexOf(dot)
    
    if (str.indexOf(at)==-1){
       alert("Invalid email address")
       return false;
    }
    if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
       alert("Invalid email address")
       return false;
    }
    if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
        alert("Invalid email address")
        return false;
    }
     if (str.indexOf(at,(lat+1))!=-1){
        alert("Invalid email address")
        return false;
     }
     if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
        alert("Invalid email address")
        return false;
     }
     if (str.indexOf(dot,(lat+2))==-1){
        alert("Invalid email address")
        return false;
     }
     if (str.indexOf(" ")!=-1){
        alert("Invalid email address")
        return false;
     }
     return true;                  
}

function check_registration()
{
    validity = true;
    if (!check_empty(document.forms[0].elements[2].value))
    {
        validity = false;
        alert('Please enter your email address');
        document.forms[0].elements[2].focus();
        return validity;
    }
    if (check_email(document.forms[0].elements[2].value)==false)
    {
        validity = false;
        alert('Please enter a valid email address');
        document.forms[0].elements[2].focus();
        return validity;
    }
    if (!check_empty(document.forms[0].elements[4].value))
    {
        validity = false;
        alert('Please enter a username');
        document.forms[0].elements[4].focus();
        return validity;
    }
    if (!check_empty(document.forms[0].elements[5].value))
    {
        validity = false;
        alert('Please enter a password');
        document.forms[0].elements[5].focus();
        return validity;
    }
    if (!check_empty(document.forms[0].elements[6].value))
    {
        validity = false;
        alert('Please confirm password');
        document.forms[0].elements[6].focus();
        return validity;
    }
    if ((document.forms[0].elements[5].value) != (document.forms[0].elements[6].value))
    {
        validity = false;
        alert('Passwords do not match');
        document.forms[0].elements[6].focus();
        return validity;
    }
    if (validity)
    {
        // document.forms[0].submit();
        document.forms[0].submit.focus();
        return validity;
    }
}

function check_update()
{
    validity = true;
    if (!check_empty(document.forms[0].elements[2].value))
    {
        validity = false;
        alert('Please enter your email address');
        document.forms[0].elements[2].focus();
        return validity;
    }
    if (check_email(document.forms[0].elements[2].value)==false)
    {
        validity = false;
        alert('Please enter a valid email address');
        document.forms[0].elements[2].focus();
        return validity;
    }
    if (!check_empty(document.forms[0].elements[4].value))
    {
        validity = false;
        alert('Please enter a password');
        document.forms[0].elements[4].focus();
        return validity;
    }
    if (!check_empty(document.forms[0].elements[5].value))
    {
        validity = false;
        alert('Please confirm password');
        document.forms[0].elements[5].focus();
        return validity;
    }
    if ((document.forms[0].elements[4].value) != (document.forms[0].elements[5].value))
    {
        validity = false;
        alert('Passwords do not match');
        document.forms[0].elements[5].focus();
        return validity;
    }
    if (validity)
    {
        // document.forms[0].submit();
        document.forms[0].submit.focus();
        return validity;
    }
}

function check_empty(text)
{
      return (text.length > 0);
}

function format_date()
{
    currentDate = new Date();

    var thisDay = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
    var thisMonth = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
    var thisDate = currentDate.getDate();
    var thisYear = currentDate.getYear();
    
    if (thisYear < 1000) thisYear = thisYear + 1900;

   if (thisDate > 9)
      return(thisDay[currentDate.getDay()] +" " +thisDate +" " +thisMonth[currentDate.getMonth()] +" " +thisYear);
   else
      return(thisDay[currentDate.getDay()] +" 0" +thisDate +" " +thisMonth[currentDate.getMonth()] +" " +thisYear);
}

function format_text()
{
    choice = document.forms[0].format.selectedIndex;
    
    if (choice == 1)
        document.forms[0].comment.value = document.forms[0].comment.value + "<p>";
    if (choice == 2)
        document.forms[0].comment.value = document.forms[0].comment.value + "<br>";
    if (choice == 3)
        document.forms[0].comment.value = document.forms[0].comment.value + "<big></big>";
    if (choice == 4)
        document.forms[0].comment.value = document.forms[0].comment.value + "<b></b>";
    if (choice == 5)
        document.forms[0].comment.value = document.forms[0].comment.value + "<i></i>";
    if (choice == 6)
        document.forms[0].comment.value = document.forms[0].comment.value + "<tt></tt>";
    if (choice == 7)
        document.forms[0].comment.value = document.forms[0].comment.value + "<pre></pre>";
    if (choice == 8)
        document.forms[0].comment.value = document.forms[0].comment.value + "<blockquote></blockquote>";
    if (choice == 9)
        document.forms[0].comment.value = document.forms[0].comment.value + "<a href=\"http://\" target=\"nob\"></a>";
    if (choice == 10)
        document.forms[0].comment.value = document.forms[0].comment.value + "&rsquo;";
    if (choice == 11)
        document.forms[0].comment.value = document.forms[0].comment.value + "&ldquo;&rdquo;";
    if (choice == 12)
        document.forms[0].comment.value = document.forms[0].comment.value + "<img src=\"http://www.bitterjoe.com/photos/\" width=\"\" height=\"\" border=0 alt=\"\">";
    if (choice == 13)
        document.forms[0].comment.value = document.forms[0].comment.value + "<small></small>";
    
    return;
}

function open_window(url)
{
    popUp = window.open(url, 'pops', 'width=800,height=625,menubar=1,toolbar=1,location=1,directories=0,status=1,scrollbars=1,resizable=1,copyhistory=0');
    popUp.focus();
    return;
}

function print_window(url)
{
    printMe = window.open(url, 'prints', 'width=800,height=625,menubar=0,toolbar=0,location=0,directories=0,status=0,scrollbars=1,resizable=1,copyhistory=0');
    printMe.focus();
    return;
}
