﻿// Browser Detection Javascript
/*function getBrowser() 
{
    var agent = navigator.userAgent.toLowerCase();
    if (agent.indexOf("opera") != -1) 
    {
        return 'Opera';
    }
    if (agent.indexOf("staroffice") != -1) 
    {
        return 'Star Office';
    }
    if (agent.indexOf("webtv") != -1) 
    {
        return 'WebTV';
    }
    if (agent.indexOf("beonex") != -1) 
    {
        return 'Beonex';
    }
    if (agent.indexOf("chimera") != -1) 
    {
        return 'Chimera';
    }
    if (agent.indexOf("netpositive") != -1) 
    {
        return 'NetPositive';
    }
    if (agent.indexOf("phoenix") != -1) 
    {
        return 'Phoenix';
    }
    if (agent.indexOf("firefox") != -1) 
    {
        return 'Firefox';
    }
    if (agent.indexOf("safari") != -1) 
    {
        return 'Safari';
    }
    if (agent.indexOf("skipstone") != -1) 
    {
        return 'SkipStone';
    }
    if (agent.indexOf("msie") != -1) 
    {
        return 'Internet Explorer';
    }
    if (agent.indexOf("netscape") != -1) 
    {
        return 'Netscape';
    }
    if (agent.indexOf("mozilla/5.0") != -1) 
    {
        return 'Mozilla';
    }
    if (agent.indexOf('\/') != -1) 
    {
        if (agent.substr(0,agent.indexOf('\/')) != 'mozilla') 
        {
            return navigator.userAgent.substr(0,agent.indexOf('\/'));
        }
        else 
        {
            return 'Netscape';
        } 
    else if (agent.indexOf(' ') != -1)
    {
        return navigator.userAgent.substr(0,agent.indexOf(' '));
    }
    else 
    {
        return navigator.userAgent;
    }
}*/

function printMe()
// Simple Function to print the current Page as is
{
	var a = window.open('','','scrollbars=yes,width=800,height=400');
	
	a.document.open("text/html");
	a.document.write('<html><head><link type="text/css" rel="Stylesheet" media="all" href="website.css" /><style type="text/css">div{background-image:none;background-color:#FFFFFF;} </style></head><body style="padding-left:20px;background-image:none;background-color:#FFFFFF;">');
	a.document.write(document.getElementById('printMe').innerHTML);
	a.document.write('<div align="center" style="margin-top: 30px; margin-bottom: 30px;">');
	a.document.write('<a href="#" onclick="javascript:window.close();" style="font: normal 10px Verdana;">Close Window</a></div>');
	a.document.write('</body></html>');
	a.document.close();
	a.print();
}

function postForm(formId)
{   
    //var target = 'menuForm';
    //var myform = document.getElementById(target);
    document.getElementById(formId).submit();
}

function noPostBack(sNewFormAction)
{
    //document.forms[0].action = sNewFormAction;
    document.forms[0].action = 'default.aspx';
    document.forms[0].__VIEWSTATE.name = 'NOVIEWSTATE';
    document.forms[0].__VIEWSTATE.value = '';
    document.forms[0].method = 'post';
    document.forms[0].submit();

}

function LeaveDaysTip(text)
{
    Tip(text, WIDTH, 200, PADDING, 5, BGCOLOR, '#fff', FONTCOLOR, '#404040', TITLE, 'Days Taken', TITLEFONTCOLOR, '#fff', TITLEBGCOLOR, '#404040', BORDERCOLOR, '#404040', BORDERSTYLE, 'solid', BORDERWIDTH, 1, FADEIN, 200, FADEOUT, 200, CLOSEBTN, true, CLOSEBTNTEXT, 'Close', STICKY, true);
}

function Bullet(picId, state, target, OU)
{
    var html;
    if(state == 'open')
    (
        html = '<a style="background: transparent;" href="javascript:' + target + '.slidedown();Bullet(\'' + picId + '\', \'close\', \'' + target + '\', \'' + OU + '\');"><span><IMG SRC="newIcons/Icons/bullet_open.png" border="0"/> <img src="newIcons/Icons/bullet_users.png" border="0"/></span> ' + OU + '</a>'
    )
    else
    (
        html = '<a style="background: #DCE1E8;" href="javascript:' + target + '.slideup();Bullet(\'' + picId + '\', \'open\', \'' + target + '\', \'' + OU + '\');"><span><IMG SRC="newIcons/Icons/bullet_close.png" border="0"/> <img src="newIcons/Icons/bullet_users.png" border="0"/></span> ' + OU + '</a>'
    )
    document.getElementById(picId).innerHTML = html;
}

function jsCheckBox(instanceName, Title, inputID)
{
    this.Render = Render;
    this.Click = Click;
    this.Add = Add;
    this.toString = toString;
    
    if(Title == null)
    {
        Title = 'Select an option:';
    }
    
    var valueArray = new Array();
    var index = valueArray.length;
    
    function toString()
    {
        return instanceName;
    }
    
    function Items(id, Itemvalue, Itemlabel)
    {
        this.id = id;
        this.Itemvalue = Itemvalue;
        this.Itemlabel = Itemlabel;
    }
    
    function Add(Itemvalue, Itemlabel)
    {
        index++;
        var id = instanceName + index;
        valueArray[id] = new Items(id, Itemvalue, Itemlabel);
    }
    
    function Render()
    {
        var checkbox_html = '<input id="' + inputID + '" name="' + inputID + '" type="hidden" value="" />';
        checkbox_html += '<fieldset style="border:1px solid #DCE1E8;"><legend>' + Title + '</legend><table >';
    
        for (key in valueArray)
        {
            checkbox_html += '<tr><td width="30" align="center" valign="middle">';
            checkbox_html += '<div class="jsCheckBox" id="' + valueArray[key].id + '"><a href="#" onclick="javascript: '+ this.toString() +'.Click(\'' + valueArray[key].id + '\', \'unchecked\');">';
            checkbox_html += '<img src="media/checkbox_unchecked.png" border="0" /></a></div></td>';
            checkbox_html += '<td valign="middle" align="left">' + valueArray[key].Itemlabel + '</td>';
            checkbox_html += '</tr>';
        }
        checkbox_html += '</table></fieldset>';
        document.write(checkbox_html);     
    }
    
    function Click(divId, state)
    {
        for (key in valueArray)
        {          
            if(divId == valueArray[key].id)
            {
                if(state == 'unchecked')
                {  
                    var icon = 'checkbox_checked';
                    var status = 'checked';
                    document.getElementById(inputID).value = valueArray[key].Itemvalue  
                }
                else
                {
                    var icon = 'checkbox_unchecked';
                    var status = 'unchecked';
                    document.getElementById(inputID).value = ''
                }
            }
            else
            {
                var icon = 'checkbox_unchecked';
                var status = 'unchecked';
            }
            var html = '<a href="#" onclick="javascript: '+ this.toString() +'.Click(\'' + valueArray[key].id + '\', \'' + status + '\');"><img src="media/' + icon + '.png" border="0" /></a>'
            document.getElementById(valueArray[key].id).innerHTML = html;
            
        }
    }
}

function printLeaveForm()
{
    var type = document.getElementById('LeaveType').value;
    
    switch(type)
    {
        case 'Sick':
        var heading = '<span style="font-size:1.5em;">SICK LEAVE FORM</span>';
        heading += '<br>DELFT ADMINISTRATION (PTY) LTD';
        heading += '<br><span style="text-align: left; display:block; width: 80%; padding: 5px; background: #DCE1E8; font-size:0.8em;">PLEASE NOTE: a Doctor\'s Certificate is required in the following instances:<br>';
        heading += 'i.) you take more than 2 consecutive days off sick<br>ii.) you are sick on a Monday or a Friday<br>iii.) you are sick the day before or the day after a Public Holiday</span>';
        break;
        
        case 'Annual':
        var heading = '<span style="font-size:1.5em;">ANNUAL LEAVE FORM</span>';
        heading += '<br>DELFT ADMINISTRATION (PTY) LTD';
        break;
        
        case 'Maternity':
        var heading = '<span style="font-size:1.5em;">MATERNITY LEAVE FORM</span>';
        heading += '<br>DELFT ADMINISTRATION (PTY) LTD';
        break;
        
        default:
        var heading = '<span style="font-size:1.5em;">SICK LEAVE FORM</span>';
        heading += '<br>DELFT ADMINISTRATION (PTY) LTD';
        heading += '<br><span style="text-align: left; display:block; width: 80%; padding: 5px; background: #DCE1E8; font-size:0.8em;">PLEASE NOTE: a Doctor\'s Certificate is required in the following instances:<br>';
        heading += 'i.) you take more than 2 consecutive days off sick<br>ii.) you are sick on a Monday or a Friday<br>iii.) you are sick the day before or the day after a Public Holiday</span>';
    }
    
    var awayFor = document.getElementById('dateInput').value;
    var newDates = awayFor.replace(/\n/g,', ');
    
    document.getElementById('printFormHeading').innerHTML = heading;
    document.getElementById('periodAway').innerHTML = newDates;
    
    printMe();
}

function Title(title)
{
    document.getElementById('Title').innerHTML = title;
}

function makeTitle(id, title)
{
    var html = '<div class="rounded">';
    html += '<b class="rtop"><b class="rb1"></b><b class="rb2 r_c">';
    html += '</b><b class="rb3 r_b"></b><b class="rb4 r_b"></b></b>';
    html += '<div class="rcontent">';
    html += '<h1 class="r_a">' + title + '</h1></div>';
    document.getElementById(id).innerHTML = html;
}

function openLock()
{
    var icon = '<img src="icons/lock_open.png">';
    document.getElementById('lockIcon').innerHTML = icon;
}

function closeLock()
{
    var icon = '<img src="icons/lock.png">';
    document.getElementById('lockIcon').innerHTML = icon;
}

function checkPW(target)
{
    if(document.getElementById('pass1').value == '')
    {
        alert('WARNING:\nThe passwords do not match.\n\nPlease try again...');
        document.getElementById('pass1').value = '';
        document.getElementById('pass2').value = '';
        document.getElementById('pass1').focus();
    }
    else if(document.getElementById('pass2').value == '')
    {
        alert('WARNING:\nThe passwords do not match.\n\nPlease try again...');
        document.getElementById('pass1').value = '';
        document.getElementById('pass2').value = '';
        document.getElementById('pass1').focus();
    }
    else if(document.getElementById('pass1').value != document.getElementById('pass2').value)
    {
        alert('WARNING:\nThe passwords do not match.\n\nPlease try again...');
        document.getElementById('pass1').value = '';
        document.getElementById('pass2').value = '';
        document.getElementById('pass1').focus();
    }
    else
    {
        if(document.getElementById('pass1').value.length < 5)
        {
            alert('WARNING:\nThe password must be at least 5 characters long.\n\nPlease try again...');
            document.getElementById('pass1').value = '';
            document.getElementById('pass2').value = '';
            document.getElementById('pass1').focus();
        }
        else
        {
            document.getElementById('resetPW').action = 'resetPassword.aspx?ResetUser=' + target; 
            postForm('resetPW');
        }
    } 
}

function jsButton(divID, icon, label)
{
    var html = '<div class="btnHolder">';
    html += '<b class="btntop"><b class="btnb1"></b><b class="btnb2 btn_c">';
    html += '</b><b class="btnb3 btn_b"></b><b class="btnb4 btn_b"></b></b>';
    html += '<div class="btncontent">';
    html += '<h1 class="btn_a"><img src="icons/' + icon + '" border="0"><br />' + label + '</h1></div>';
    html += '<b class="btnbottom"><b class="btnb4"></b><b class="btnb3 btn_c"></b>';
    html += '<b class="btnb2 btn_b"></b><b class="btnb1 btn_b"></b></b></div>';
    document.getElementById(divID).innerHTML = html;
}

function jsDropDown(instanceName, inputID, width, onChange)
{
    this.Render = Render;
    this.Return = Return;
    this.Open = Open;
    this.Select = Select;
    this.Add = Add;
    this.toString = toString;
    this.Activate = Activate;
    this.OutputCode = OutputCode;
    
    if(width == null)
    {
        width = 300;
    }
    
    var change = null;
    var liChange = null;
    if(onChange != null)
    {
        change = 'onChange=\"javascript:' + onChange + ';\" ';
        liChange = ' ' + onChange + ';';
    }
    else
    {
        change = '';
        liChange = '';
    }
    
    
    var emwidth = width - 42;
    var parentId = inputID + '_' + instanceName + '_parent';
    var childId = inputID + '_' + instanceName + '_child';
    var titleId = inputID + '_' + instanceName + '_title';
    var ddLabel = '';
      
    var valueArray = new Array();
    var index = valueArray.length -1;
    var jsParent = null;
    var jsChild = null;
    var dropdown_html = null;
    //var jsTitle = null;
    
    function toString()
    {
        return instanceName;
    }
    
    function Items(id, Itemvalue, Itemlabel, selected, Itemhref)
    {
        this.id = id;
        this.Itemvalue = Itemvalue;
        this.Itemlabel = Itemlabel;
        if(selected)
        {
            this.selected = true;
        }
        else
        {
            this.selected = false;
        }
        if(Itemhref != null)
        {
            this.Itemhref = Itemhref;
        }
        else
        {
            this.Itemhref = 'javascript: void(0);';
        }        
    }
    
    function Add(Itemvalue, Itemlabel, selected, Itemhref)
    {
        index++;
        var id = index;
        valueArray[id] = new Items(id, Itemvalue, Itemlabel, selected, Itemhref);
    }
    
    function Return()
    {
        dropdown_html = '<input id="' + inputID + '" name="' + inputID + '" ' + change + 'type="hidden" value="" />\n';
        dropdown_html += '<ul id="' + parentId + '" class="jsdropdown" style="width : ' + width + 'px;">\n';
        dropdown_html += '<li><a href="javascript: void(0);" onclick="javascript:' + this.toString() + '.Open();">\n';
        dropdown_html += '<b></b><span style="width: ' + emwidth + 'px;" id="' + titleId + '">Select</span></a></li>\n';
        dropdown_html += '</ul>\n';
    
        dropdown_html += '<div id="' + childId + '" class="jsdropdownbox">\n';
        dropdown_html += '<ul>\n';
    
        for (key in valueArray)
        {
            dropdown_html += '<li><a href="' + valueArray[key].Itemhref + '" onclick="javascript:' + this.toString() + '.Select(' + valueArray[key].id + ');'+liChange+'">' + valueArray[key].Itemlabel + '</a></li>\n';
            if(valueArray[key].selected == true)
            {
                ddLabel = valueArray[key].Itemlabel;
            }
        }
        dropdown_html += '</ul></div>\n';
        dropdown_html += '<script type="text/javascript">\n';
        dropdown_html += 'ADD_DHTML("' + parentId + '" + NO_DRAG);\n';
        dropdown_html += 'ADD_DHTML("' + childId + '" + NO_DRAG);\n';
        //dropdown_html += 'ADD_DHTML("' + titleId + '" + NO_DRAG);';
        //dropdown_html += 'var ddChild = dd.elements["' + childId + '"];';
        //dropdown_html += 'ddChild.hide(true);';
        dropdown_html += this.toString() + '.Activate();\n';
        dropdown_html += '</script>\n';
      
        return dropdown_html;  
    }
    
    function Render()
    {
        var html = this.Return();
        //alert(html);
        document.write(html);
    }
    
    function OutputCode()
    {
        var output = '<script type="text/javascript">var '+ this.toString() + ' = new jsDropDown(\'' + this.toString() + '\', \'' + inputID + '\', ' + width + ', \'' + onChange + '\');\n';
        for (key in valueArray)
        {
            output += this.toString() + '.Add("'+ valueArray[key].Itemvalue + '", "' + valueArray[key].Itemlabel + '", ' + valueArray[key].selected + ', "' + valueArray[key].Itemhref + '");\n';
        }
        output += this.toString()+'.Render();\n</script>\n';
        //output += this.Return();
        
        return output;
    }
    
    function Select(id)
    {
       // var jsChild = dd.elements[childId];
        document.getElementById(inputID).value = valueArray[id].Itemvalue;
        document.getElementById(titleId).innerHTML = valueArray[id].Itemlabel;  
        
        jsChild.hide(true);      
    }
    
    function Open()
    {  
        jsChild.show();
        
        //alert('Details: \nxPos: ' + xPos + '\nyPos: ' + yPos + '\nWidth: ' + pwidth);
    }
    
    function Activate()
    {
        jsParent = dd.elements[parentId];
        //jsTitle = dd.element[titleId];
        //jsTitle.resizeTo(emwidth, 22);
        document.getElementById(titleId).innerHTML = ddLabel;
                
        var yPos = jsParent.y;
        
        if(dd.n6)
        {
            var pwidth = width-2;
            var xPos = jsParent.x-2;
        }
        else
        {
            var pwidth = width-2;
            var xPos = jsParent.x-2;
        }
        var height = 22;
        jsChild = dd.elements[childId];
        var newHeight = yPos + height;
        
        childHeight = 18 * (index +1);
        jsChild.resizeTo(pwidth, childHeight);
        jsChild.moveTo(xPos, newHeight);
        jsChild.hide(true);   
    }
}

function WhichBrowser()
{
  if (navigator.userAgent.indexOf("MSIE") != -1) {
    return "IE"; }
  else if (navigator.userAgent.indexOf("Firefox") != -1) {
    return "FF"; }
  else if (navigator.userAgent.indexOf("Navigator") != -1) {
    return "NN"; }
  else if (navigator.userAgent.indexOf("Opera") != -1 ) {
    return "O"; }
  else {
    return "?";
  }
}

function findPos(obj) 
{
	var curleft = 0;
	var curtop = 0;
    if (obj.offsetParent) 
    {
        do 
        {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
	    } 
	    while (obj = obj.offsetParent);
	}
	    
	var pos = new Array();
	pos[0] = curleft;
	pos[1] = curtop;
	
	return pos;
}

function getOffset(parent, child)
{
    var p = document.getElementById(parent);
    var c = document.getElementById(child );

    var top  = p.offsetHeight;
    var left = p.offsetWidth;

    for (; p; p = p.offsetParent)
    {
        top  += p.offsetTop;
        left += p.offsetLeft;
    }
    
    var pos = new Array(2);
	pos[0] = left;
	pos[1] = top;
	
	return pos;
}

function opendd(parentId, childId)
{
    var browser = WhichBrowser();
    var parent = document.getElementById(parentId);
    var child = document.getElementById(childId);
    var parentWidth = parent.offsetWidth;
     
    var jsParent = dd.elements[parentId];
        
    var xPos = jsParent.x - 2;
    var yPos = jsParent.y;
    var width = jsParent.w - 20;
    var height = jsParent.h;
    
    var jsChild = dd.elements[childId];
    
    childHeight = jsChild.h;
    jsChild.resizeTo(width, childHeight);
    jsChild.moveTo(xPos, (yPos + height));
    jsChild.show();
    
    alert(jsContent.name + ':\nx: ' + xStart + '\ny: ' + yStart);
    alert(jsParent.name + ':\nx: ' + xPos + '\ny: ' + yPos);
    
    
    
    /*child.style.left = xPos + 'px';
    child.style.top = yPos + 'px';
    child.style.display = 'block';
    child.style.width = parentWidth + 'px';  */
    
        //var position = findPos(parentId);
       /* 
    var innerOffset = getOffset(parentId, childId);
    var outerOffset = getOffset('content', childId);
    
    alert('Content Div :\n' + outerOffset[0] + 'px\n' + outerOffset[1] + 'px');
    alert('Dropdown :\n' + innerOffset[0] + 'px\n' + innerOffset[1] + 'px');
    
    child.style.left = (-271 + innerOffset[0]) + 'px';
    child.style.top = (-100 + innerOffset[1]) + 'px';
    
    //child.style.left = xPos + 'px';
    child.style.width = parentWidth + 'px';
    child.style.display = 'block';
    
    
    //alert(position[0] + '\n' + position[0]);*/
}

function closedd(childId)
{
    var jsChild = dd.elements[childId];
    
    jsChild.hide(true);
}

function checkAD(user, OU, Tvid)
{
    document.getElementById('changeProfile').action = 'editEmployee.aspx?Employee=' + user + '&viewOU=' + OU + '&Tvid=' + Tvid; 
    document.getElementById('new_DateEmployed').value = document.getElementById('new_Date').value;
    document.getElementById('old_DateEmployed').value = document.getElementById('old_Date').value;
    
    document.getElementById('Submitted').value = 'true';
    postForm('changeProfile'); 
}

function submitLogin()
{
    if(document.getElementById('Uname').value == "")
    {
        document.getElementById('Uname').value = "";
        document.getElementById('Upass').value = "";
        document.getElementById('Uname').focus();
        
        alert('Please try again...');
    }
    else if(document.getElementById('Upass').value == "")
    {
        document.getElementById('Uname').value = "";
        document.getElementById('Upass').value = "";
        document.getElementById('Uname').focus();
        
        alert('Please try again...'); 
    }
    else
    {
       document.getElementById('ok').value = 'true';
       postForm('login');
    }  
}

function submitSearch()
{
    if(document.getElementById('SearchOption').value == "")
    {
        document.getElementById('SearchOption').value = "1";
        postForm('searchForm');
    }
    else
    {
       postForm('searchForm');
    }  
}

function submitUpload(input)
{
    if(document.getElementById('FileUpload').value == "")
    {
        document.getElementById('FileUpload').focus();
        alert('Please select a file to upload');
    }
    else
    {
       document.getElementById('hideFile').value = document.getElementById('FileUpload').value; 
       postForm('UploadForm');
    }  
}

function clearInput(id)
{
    document.getElementById(id).value = "";
}

function makeCaption(file, id)
{
    var caption = document.getElementById(id).value;
    document.getElementById('updateMe').value = caption;
    document.getElementById('updateFile').value = file;
    document.getElementById('updateImg').method = 'post';
    postForm('updateImg');
}

function submitMe(formId, inputId2Validate)
{
    if(inputId2Validate != null)
    {
        alert('You need to complete this field\nbefore you may proceed...');
        document.getElementById(inputId2Validate).focus();
    }
    else
    {
        postForm(formId);
    }
}

function newUser()
{
    if(document.getElementById('Pass1').value != document.getElementById('Pass2').value)
    {
        alert('The passwords do not match...');
        document.getElementById('Pass1').value = "";
        document.getElementById('Pass2').value = "";
        document.getElementById('Pass1').focus();
    }
    else if(document.getElementById('UserName').value == "")
    {
        alert('You need to supply a UserName...');
        document.getElementById('UserName').value = "";
        document.getElementById('UserName').focus();
    }
    else if(document.getElementById('Pass1').value == "")
    {
        alert('You need to supply a Password...');
        document.getElementById('Pass1').value = "";
        document.getElementById('Pass1').focus();
    }
    else if(document.getElementById('FullName').value == "")
    {
        alert('You need to supply the User\'s\nfull name and surname...');
        document.getElementById('FullName').value = "";
        document.getElementById('FullName').focus();
    }
    else
    {
        postForm('newUserForm');
    }
}

function makeContent(cat, id)
{
    document.getElementById('cat').value = cat;
    document.getElementById('cat_events').className = "cat";
    document.getElementById('cat_teambuilding').className = "cat";
    document.getElementById('cat_community').className = "cat";
    document.getElementById('cat_riverrafting').className = "cat";
    document.getElementById(id).className = "isCat";
}

function swapPic(id, imgID)
{
    document.getElementById(id).innerHTML = '<img src="' + imgID + '">'
}


//eg: toppreview, picname, toppic, h - int, countpics
function selectPic(id, imgID, idprefix, no, totalno)
{
    abc = 0
    
    while (abc < (totalno + 1))
    {
        document.getElementById(idprefix + '_' + abc).className = 'picUnselected'
        abc++  
    }
    document.getElementById(id).innerHTML = '<img src="' + imgID + '">'
    document.getElementById(idprefix + '_' + no).className = 'picSelected'
    
    var name = imgID.replace('../imageLibrary/thumbs/', '')
    
    //alert(name)
    
    if(id == "TopPreview")
    {
        document.getElementById('himg1').value = name
    }
    else if (id == "MidPreview")
    {
        document.getElementById('himg2').value = name
    }
    else
    {
        document.getElementById('himg3').value = name
    }
    
}
