

function changeInputType(input) {
    var n = document.createElement('INPUT');
    n.type = 'password';
    n.className = input.className;
    n.value = input.value;
    n.name = input.name;
    n.onblur = input.onblur;
    var parent = input.parentNode;
    parent.removeChild(input);
    parent.appendChild(n);
    n.focus();
}

String.prototype.stripHtml = function() {
    val = this;
    val = val.replace(/<[^>]+>/gim, "");
    val = val.replace(/<\/[^>]+>/gim, "");
    val = val.replace(/&nbsp;/gim, "");
    return val;
}

String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
    return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
    return this.replace(/\s+$/,"");
}

function makeArray(IntarrSize) {

  for (var n = 0; n < IntarrSize; n++)
    this[n] = "";

  return this;

}

function customSplit(strvalue, separator, arrayName) {
  var n = 0;

  if (separator.length != 0) {
    while (strvalue.indexOf(separator) != -1) {
      eval("arr"+n+" = strvalue.substring(0, strvalue.indexOf(separator));");
      strvalue = strvalue.substring(strvalue.indexOf(separator)+separator.length,
          strvalue.length+1);
      n++;
    }
    eval("arr" + n + " = strvalue;");
    arraySize = n+1;
  }
  else {
    for (var x = 0; x < strvalue.length; x++) {
      eval("arr"+n+" = \"" + strvalue.substring(x, x+1) + "\";");
      n++;
    }
    arraySize = n;
  }

  eval(arrayName + " = new makeArray(arraySize);");

  for (var i = 0; i < arraySize; i++)
    eval(arrayName + "[" + i + "] = arr" + i + ";");

  return arraySize;
}

function isEmail(argvalue) {

  if (argvalue.indexOf(" ") != -1)
    return false;
  else if (argvalue.indexOf("@") == -1)
    return false;
  else if (argvalue.indexOf("@") == 0)
    return false;
  else if (argvalue.indexOf("@") == (argvalue.length-1))
    return false;

  // arrayString = argvalue.split("@"); (works only in netscape3 and above.)
  var retSize = customSplit(argvalue, "@", "arrayString");

  if (arrayString[1].indexOf(".") == -1)
    return false;
  else if (arrayString[1].indexOf(".") == 0)
    return false;
  else if (arrayString[1].charAt(arrayString[1].length-1) == ".") {
    return false;
  }

  return true;

}

/* form checking */

var ERROR = 'error';
var OK = 'ok';

function FormObject(form, okButton, notValidMessage) {
    this.errors = new Object();
    this.form = form;
    this.okButton = okButton;
    this.validateFormMessage = notValidMessage != undefined ? notValidMessage : 'Some fields are not correct, please retype and click the button again.';

    this.event = function(e) { if(!e) e = window.event; return e; }
    this.el = function(name) { return this.form.elements[name]; }
    
    this.validateElement = function(el, e) { return true; }

    this.validateForm = function(e) {
        e = this.event(e);
        if(this.validateElement) {
            for(el in this.form.elements) {
                var ell = this.form.elements[el];
                if(ell && ell.onblur != null) {
                    try { this.validateElement(ell, e); } catch(ex) { }
                }
            }
        }

        try { this.validateCustomElements(e); } catch(ex) {}

        var isok = this.isValid();
        if(!isok)
            alert(this.validateFormMessage);

        return isok;
        
    }
    
    this.disable = function() {
        for(key in this.form.elements) {
            this.form.elements[key].disabled=true;
        }
        this.okButton.disabled = true;
    }

    this.enable = function() {
        for(key in this.form.elements) {
            this.form.elements[key].disabled=false;
        }
        this.okButton.disabled = false;
    }
    
    this.isValid = function() {  for(k in this.errors) if(this.errors[k] == 1) return false; return true; }
    
    this.setState = function(el, state) { 
        var elo = _(el.name+'_message');

        el.className = el.className.replace(/[^_|\s]*_state/, '');
        elo.className = elo.className.replace(/[^_|\s]*_mstate/, '');
        
        el.setAttribute('state', state); 
        el.className += state == ERROR ? ' error_state' : ' ok_state';
        elo.className += state == ERROR ? ' error_mstate' : ' ok_mstate';
        
        // set the class to the message
        
        
    }
    this.getState = function(el) { return el.getAttribute('state'); }
    
    this.checkState = function(el, state) { return this.getState(el) == state; }
    
    this.setMessage = function(el, message) { 
        el = _(el);
        if(!el)
            el = this.el(el);
        if(el.value)
            el.value = message;
        else
            el.innerHTML = message; 
    }
    
    this.setError = function(el, message){ 

        this.setState(el, ERROR);
        this.setMessage(el.name+'_message', message);
        
        this.errors[el.name] = 1;
    }
    
    this.clearError = function(el, message) {
        this.setState(el, OK);
        this.setMessage(el.name+'_message', message);
        
        this.errors[el.name] = 0;
    }
    
    this.enablePost = function() {
        this.okButton.disabled = !this.isValid();
    }
    

}


/* form checking */

function _(id) {
    return document.getElementById(id);
}

function findChild(parent, request) {
    for(obj in parent.childNodes) {
        obj = parent.childNodes[obj];
        if(obj.nodeType == 1) {
            if(eval(request))
                return obj;
        }
    }
    return null;
}

function bin2hex(bin){
    var result = "";
    var temp = "";
    for(var i=0;i<bin.length;i++){
        var chr = bin.charCodeAt(i);
        if(chr>127){
            chr = encodeURIComponent(bin.charAt(i));
        }else{  
            chr = chr.toString(16); 
        }
        result += chr;
    } 

    for(var i=0;i<result.length;i++) {
        var chr = result.charAt(i);
        if(chr!='%'){
            temp+=chr;
        }
    } 
    return temp.toLowerCase();
} 

function win(src, w, h, scrollBars, resizable) { //location , menubar , toolbar , titlebar , fullscreen
    var top = Math.round(window.screen.height/2 - h/2);
    var left = Math.round(window.screen.width/2 - w/2);
    var wnd = window.open(src, "untitled_" + Math.ceil(Math.random()*100), "status=no,help=no,resizable="+resizable+",scrollbars="+scrollBars+",width=" + w + ",height=" + h + ",top=" + top + ",left=" + left);
    if (wnd){
        wnd.opener = window;
        wnd.focus();
    }
    return false;
}

function op(src, w, h) {
    /*var s = src;

    s = /url\((.*)\)/.exec(s);
    
    if(s) {
        if(s.length > 1)
            s = src[1];
    }
    else        */
        s = src;
    
    /*var img = new Image();
    img.src = s;*/
    win(src, w, h, false, false);
}

function showPopup(text) {
    
    var shadow = document.createElement('DIV');
    shadow.id = "popup_shadow";
    shadow.style.width = '100%';
    shadow.style.height = document.body.offsetHeight + 'px';
    shadow.style.backgroundColor = '#000000';
    shadow.style.position = "absolute";
    shadow.style.left = "0px";
    shadow.style.top = "0px";
    try { shadow.style.opacity = "0.8";} catch(e) {}
    try {shadow.style.filter = "alpha(opacity=80)";} catch(e) {}
    document.body.appendChild(shadow);
    
    var div = document.createElement('DIV');
    div.id = "popup_div";
    div.className = 'popup';
    div.innerHTML = text + '<div class="close"><a href="javascript: closePopup();">Close</a></div>';
    document.body.appendChild(div);

}

function closePopup() {
    document.getElementById('popup_shadow').style.display='none';
    document.getElementById('popup_div').style.display='none';
    
    setCookie("popup_shown", "1", "Mon, 01-Jan-2010 00:00:00 GMT", "/");

}

function checkShown() {
    if(getCookie('popup_shown') == '1')
        return true;
    return false;
}

function setCookie (name, value, expires, path, domain, secure) {
      document.cookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

function getCookie(name) {
    var cookie = " " + document.cookie;
    var search = " " + name + "=";
    var setStr = null;
    var offset = 0;
    var end = 0;
    if (cookie.length > 0) {
        offset = cookie.indexOf(search);
        if (offset != -1) {
            offset += search.length;
            end = cookie.indexOf(";", offset)
            if (end == -1) {
                end = cookie.length;
            }
            setStr = unescape(cookie.substring(offset, end));
        }
    }
    return(setStr);
}

