﻿var img_browse = new Image(24, 24);
var img_browse_on = new Image(24, 24);
var img_add = new Image(24, 24);
var img_add_on = new Image(24, 24);
var img_delete = new Image(24, 24);
var img_delete_on = new Image(24, 24);
var img_request_on = new Image(115, 24);
var img_request = new Image(115, 24);
var img_nav_on = new Image(16, 16);
var img_nav = new Image(16, 16);

img_browse.src = "images/button_addfile.png";
img_browse_on.src = "images/button_addfile_on.png";
img_add.src = "images/img_add.png";
img_add_on.src = "images/img_add_on.png";
img_delete.src = "images/img_delete.png";
img_delete_on.src = "images/img_delete_on.png";
img_request_on.src = "images/button_requestquote_ON.png"
img_request.src = "images/button_requestquote_OFF.png"
img_nav_on.src = "images/nav_on.png"
img_nav.src = "images/nav.png"

var nfiles = 1;
var id = 1;
var fileList = [];
var mh;

MM_preloadImages('images/file.png','images/uploading.png','images/success.png','images/failed.png');

function checkEmail(email)
{
    var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
    //if (email != "")
    //{
        if (!email.match(emailExp)) return false;
    //}
    return true;
}

jQuery.noConflict();

function doDeleteFile(num) {

    var fn = jQuery("#file_" + num).val();
    //document.getElementById("file_"+num).value;
    for (var i = 0; i < fileList.length; i++)
        if (fn == fileList[i]) { fileList.splice(i, 1); break; }
    jQuery("#attach_" + num).remove();
    jQuery("#img_delete_" + num).unbind();
    nfiles--;
    //if (nfiles == 0) jQuery("#Attachment1").css("left",'auto');
    //else 
    jQuery("#file_" + num).remove();
    mh.expand();
}

function doAddFile(e) {
    if (nfiles > 5) return false;
    for (var i = 0; i < fileList.length; i++)
        if (jQuery(e).val() == fileList[i]) return false;

    var fn = getFilename(e);
    //alert(e.name);
    //e.parentNode;
    //e.style.position = 'absolute';
    jQuery(e).css("left", "-1000px");
    //e.style.left = '-1000px';

    // jQuery("#img_browse").
    //<!--<input style="cursor: pointer" type="button" value="Delete" onclick="doDeleteFile(' + nfiles + ')"/>-->
    if (nfiles == 1)
        var html = '<div id="attach_' + id + '" style="border-style: dotted none dotted none;"><span>' + fn + '</span><img id="img_delete_' + id + '" alt="" src="images/img_delete.png" onclick="doDeleteFile(' + id + ')"/></div>';

    else
        var html = '<div id="attach_' + id + '" ><span >' + fn + '</span><img id="img_delete_' + id + '" alt="" src="images/img_delete.png" onclick="doDeleteFile(' + id + ')"/></div>';
    jQuery(html).appendTo(jQuery("#files_list"));
    bindDelete(id);
    //jQuery("#file_" + nfiles).val(jQuery(e).val());
    jQuery("#latest_file").val(fn);
    fileList[fileList.length] = jQuery(e).val();
    jQuery("#file_" + id).unbind();
    nfiles++;
    id++;
    var newfile = '<input type="file" onchange="doAddFile(this)" size="27" class="file" name="userfiles[]" id="file_' + id + '" />';
    jQuery(newfile).insertAfter(jQuery(e));

    bindAttach(id);

    mh.expand();
}

function doExpand() {
    nfiles++;

    var adh; // = '<BR><b><font face="Verdana" color="#000000">File attachment ' + nfiles + ' :</font></b><BR><input type="file" size="16"  name="imageFile' + nfiles + '">';

    //adh = '<br />';
    //adh = 'File attachment ' + nfiles + ':<br />'
    adh = '<div class="fileinputs"><div class="fileinput" >';
    adh += '<input type="file" onchange="setFilename(' + nfiles + ')" class="file" size="27" id="Attachment' + nfiles + '"/></div>';
    adh += '<div class="fakefile">';
    adh += 'Choose a file: <br />';
    adh += '<input type="text" size="36" id="attach_name_' + nfiles + '" style="margin-right: 4px" readonly="readonly" />';
    adh += '<img alt="" id="img_browse_' + nfiles + '" src="images/img_browse.png" height="20" width="20" style="cursor: pointer; margin-right: 4px" />';
    adh += '<img alt="" id="img_add_' + nfiles + '" src="images/img_add.png" height="20" width="20" style="cursor: pointer; margin-right: 4px"  />';
    adh += '<img alt="" id="img_delete_' + nfiles + '" src="images/img_delete.png" height="20" width="20" style="cursor: pointer; display: none; margin-right: 4px" />';
    adh += '</div></div>';
    jQuery(adh).appendTo(jQuery("#files"));
    //jQuery("#files").append(jQuery(adh));
    bindButton("browse_" + nfiles, doBrowse);
    bindButton("add_" + nfiles, doExpand);
    //bindButton("delete_" + nfiles, null);
    bindAttach("browse_" + nfiles);
    //files.insertAdjacentHTML('BeforeEnd', adh);
    //document.form1.nf.value = nfiles;
    mh.expand();

    return false;
}
function bindButton(name, clickfn) {
    var lname = name.split("_")[0];

    jQuery("#img_" + name).mouseup(function() {

        jQuery("#img_" + name).attr("src", eval("img_" + lname + "_on.src"));
    }).mouseover(function() {
        jQuery("#img_" + name).attr("src", eval("img_" + lname + "_on.src"));
    }).mouseout(function() {
        jQuery("#img_" + name).attr("src", eval("img_" + lname + ".src"));
    }).mousedown(function() {
        jQuery("#img_" + name).attr("src", eval("img_" + lname + ".src"));
    }).click(clickfn);
}



//jQuery("#subnav ul").mouseover(function() { jQuery(this).css("background-color", "#9f3b0f"); }).mouseout(function() { jQuery(this).css("background-color", ""); });

function getFilename(e) {
    var value = jQuery(e).val();
    if (!value) return '';
    value = value.split("\\");
    return value[value.length - 1];

}

function doBrowse() {
    //jQuery("#Attachment1").click();


    jQuery("#latest_file").val(getFilename());

}

function bindDelete(id) {


    jQuery("#img_delete_" + id).mouseup(function() {

        jQuery("#img_delete_" + id).attr("src", eval("img_delete_on.src"));
    }).mouseover(function() {
        jQuery("#img_delete_" + id).attr("src", eval("img_delete_on.src"));
    }).mouseout(function() {
        jQuery("#img_delete_" + id).attr("src", eval("img_delete.src"));
    }).mousedown(function() {
        jQuery("#img_delete_" + id).attr("src", eval("img_delete.src"));
    });
}

function bindAttach(id) {


    jQuery("#file_" + id).mouseup(function() {

        jQuery("#img_browse").attr("src", eval("img_browse_on.src"));
    }).mouseover(function() {
        jQuery("#img_browse").attr("src", eval("img_browse_on.src"));
    }).mouseout(function() {
        jQuery("#img_browse").attr("src", eval("img_browse.src"));
    }).mousedown(function() {
        jQuery("#img_browse").attr("src", eval("img_browse.src"));
    });
}

function bindBrowse() {


    jQuery("#img_browse").mouseup(function() {

        jQuery("#img_browse").attr("src", eval("img_browse_on.src"));
    }).mouseover(function() {
        jQuery("#img_browse").attr("src", eval("img_browse_on.src"));
    }).mouseout(function() {
        jQuery("#img_browse").attr("src", eval("img_browse.src"));
    }).mousedown(function() {
        jQuery("#img_browse").attr("src", eval("img_browse.src"));
    });
}

function bindSend() {


    jQuery("#send").mouseup(function() {

        jQuery("#send").attr("src", eval("img_request_on.src"));
    }).mouseover(function() {
        jQuery("#send").attr("src", eval("img_request_on.src"));
    }).mouseout(function() {
        jQuery("#send").attr("src", eval("img_request.src"));
    }).mousedown(function() {
        jQuery("#send").attr("src", eval("img_request.src"));
    });
}

jQuery(document).ready(function() {
    //bindButton("browse_1", null);
    //bindButton("add_1", doExpand);
    //bindButton("delete_1", null);
    //bindAttach(1);
	bindBrowse();
	bindSend();
    jQuery("#subnav li").click(function() {
        location.href = jQuery(this).children("a").attr("href");

    });

    jQuery("#subnav li").hover(
      function() {
          var path = jQuery(this).children("a").attr("pathname");
          if (path.charAt(0) != "/") path = '/' + path;
          if (path != location.pathname) {
              jQuery(this).css({ backgroundColor: "#8db9cc", borderStyle: "", borderWidth: "", borderColor: "" });
              jQuery(this).children("a").css({ color: "white", backgroundImage: "url('"+img_nav_on.src+"')" });
          }
      },
      function() {
          var path = jQuery(this).children("a").attr("pathname");
          if (path.charAt(0) != "/") path = '/' + path;
          if (path != location.pathname) {
              var cssObj = {
                  backgroundColor: "",
                  borderStyle: "", 
                  borderWidth: "",
                  borderColor: ""
              }
              jQuery(this).css(cssObj);
              jQuery(this).children("a").css({ color: "#9f3b0f", backgroundImage: "url('"+img_nav.src+"')" });
          }
      });
    jQuery("#subnav li").each(
        function() {
            var path = jQuery(this).children("a").attr("pathname");
            if (path.charAt(0) != "/") path = '/' + path;
            if (path == location.pathname) {
                jQuery(this).children("a").css({ textDecoration: "underline", color: "white", backgroundImage: "url('"+img_nav_on.src+"')" });
                jQuery(this).css({ backgroundColor: "#6e8f9e", borderStyle: "inset", borderWidth: "2px", borderColor: "#C0C0C0" });
            }
        });
    // jQuery("#subnav a").hover(function() { jQuery(this).fadeOut(100); jQuery(this).fadeIn(500); });
});