﻿// JScript File
        
        function BuildHorizontalLoginForm(formName, actionURL, LoginFormPlaceholderElementID, TargetLocationElementID,  CSSClass, SubmitButtonImageURL, registerURL)
        {
            //Look up the location Placeholder
            var LoginFormPlaceholderElement = document.getElementById(LoginFormPlaceholderElementID);
            var formcount = document.forms.length;
            var useStandardSubmitButton;
            useStandardSubmitButton = (SubmitButtonImageURL == "");
                                  
            //Check for IE
            
            /*@cc_on @if (@_jscript)
                //Is IE - Use JScript Compliance

                if (formcount == 1)
                {
                    //Form Definition
                    var newForm = document.createElement("<form name='" + formName + "'>");
                        newForm.setAttribute("action",actionURL);
                        newForm.setAttribute("method","POST");
                    
                    //Table Definitions            
                    var newTable = document.createElement("table");
                    
                    var newTableRow_All = document.createElement("tr");                                            
                               
                    var newTableCell_UsernameLabel = document.createElement("td");
                        newTableCell_UsernameLabel.setAttribute("class","table_label");
                    var newTableCell_UsernameInput = document.createElement("td");
                        newTableCell_UsernameInput.setAttribute("class","table_input");                                 
                    var newTableCell_PasswordLabel = document.createElement("td");
                        newTableCell_PasswordLabel.setAttribute("class","table_label");
                    var newTableCell_PasswordInput = document.createElement("td");
                        newTableCell_PasswordInput.setAttribute("class","table_input");       
                    var newTableCell_Submit = document.createElement("td");
                        newTableCell_Submit.setAttribute("class","table_button");
                    var newTableCell_Register = document.createElement("td");
                        newTableCell_Register.setAttribute("class","table_register");
                    
                    //Control Definitions
                    var newUsernameLabel = document.createTextNode("Username:");
                    var newPasswordLabel = document.createTextNode("Password:");
                    
                    var newUsernameInput = document.createElement("<input name='txtUsername' id='txtUsername' type='text' onfocus='usernameOnFocus(this)' onblur='usernameOnBlur(this)' value='username' style='color:gray'></input>");
                    var newPasswordInput = document.createElement("<input name='txtPassword' id='txtPassword' type='password' onfocus='passwordOnFocus(this)' onblur='passwordOnBlur()' style='color:gray;display:none'></input>");                    
                    var newPasswordInputDummy = document.createElement("<input name='txtPasswordDummy' type='text' onfocus='passworddummyOnFocus()' onblur='' value='password' style='color:gray;display:block'></input>");                    
                    
                    var newSubmitButton;
                    if (useStandardSubmitButton)
                    {
                        newSubmitButton =  document.createElement("<input name='btnSubmit' type='submit' value='Login to MAIA Online'></input>");
                    }
                    else
                    {
                        newSubmitButton =  document.createElement("<input name='btnSubmit' type='image' src='" + SubmitButtonImageURL + "' alt='Login to MAIA Online'></input>");
                    }    
                    var newRegisterButton = document.createElement("<a href='" + registerURL + "' title='Register Now'></a>");
                    newRegisterButton.appendChild(document.createTextNode("Register Now"));
                                       
                    //Build up table contents
                    newTableCell_UsernameLabel.appendChild(newUsernameLabel);
                        //newTableRow_All.appendChild(newTableCell_UsernameLabel);
                    newTableCell_UsernameInput.appendChild(newUsernameInput);    
                        newTableRow_All.appendChild(newTableCell_UsernameInput);                                                         
                    newTableCell_PasswordLabel.appendChild(newPasswordLabel);
                        //newTableRow_All.appendChild(newTableCell_PasswordLabel);
                    newTableCell_PasswordInput.appendChild(newPasswordInput);
                    newTableCell_PasswordInput.appendChild(newPasswordInputDummy);
                        newTableRow_All.appendChild(newTableCell_PasswordInput);                    
                    newTableCell_Submit.appendChild(newSubmitButton); 
                        newTableRow_All.appendChild(newTableCell_Submit);                                             
                    newTableCell_Register.appendChild(newRegisterButton);
                        newTableRow_All.appendChild(newTableCell_Register);
                            newTable.appendChild(newTableRow_All);
                                                                                                      
                    newForm.appendChild(newTable);
                    LoginFormPlaceholderElement.appendChild(newForm);
                    LoginFormPlaceholderElement.innerHTML = newForm.outerHTML;

                    alignToControl(LoginFormPlaceholderElementID, TargetLocationElementID);
                }
                else
                {
                    if (formcount == 2)
                    {
                        var myform = document.getElementById(formName);
                        LoginFormPlaceholderElement.removeChild(myform);
                    }
                }
            @else */
                //Not I.E. use standard Javascript compliance
                if (formcount == 1)
                {
                    //Form Definition
                    var newForm = document.createElement("form");
                        newForm.setAttribute("name",formName);
                        newForm.setAttribute("id",formName);
                        newForm.setAttribute("action",actionURL);
                        newForm.setAttribute("method","POST");                  
                    
                    //Table Definitions            
                    var newTable = document.createElement("table");
                    
                    var newTableRow_All = document.createElement("tr");                                            
                                
                    var newTableCell_UsernameLabel = document.createElement("td");
                        newTableCell_UsernameLabel.setAttribute("class","table_label");
                    var newTableCell_UsernameInput = document.createElement("td");
                        newTableCell_UsernameInput.setAttribute("class","table_input");                                 
                    var newTableCell_PasswordLabel = document.createElement("td");
                        newTableCell_PasswordLabel.setAttribute("class","table_label");
                    var newTableCell_PasswordInput = document.createElement("td");
                        newTableCell_PasswordInput.setAttribute("class","table_input");       
                    var newTableCell_Submit = document.createElement("td");
                        newTableCell_Submit.setAttribute("class","table_button");
                    var newTableCell_Register = document.createElement("td");
                        newTableCell_Register.setAttribute("class","table_register");

                                            
                    //Control Definitions
                    var newUsernameLabel = document.createTextNode("Username:");
                    var newPasswordLabel = document.createTextNode("Password:");
                    
                    var newUsernameInput = document.createElement("input");
                        newUsernameInput.setAttribute("type", "text");
                        newUsernameInput.setAttribute("name", "txtUsername");
                        newUsernameInput.setAttribute("onfocus", "usernameOnFocus(this)");
                        newUsernameInput.setAttribute("onblur","usernameOnBlur(this)");
                        newUsernameInput.setAttribute("value","username");
                        newUsernameInput.setAttribute("style","color:gray");                        
                    var newPasswordInput = document.createElement("input");
                        newPasswordInput.setAttribute("type", "password");
                        newPasswordInput.setAttribute("name", "txtPassword");
                        newPasswordInput.setAttribute("id", "txtPassword");
                        newPasswordInput.setAttribute("onfocus", "passwordOnFocus(this)");
                        newPasswordInput.setAttribute("onblur","passwordOnBlur()");
                        newPasswordInput.setAttribute("style","display:none");
                    var newPasswordInputDummy = document.createElement("input");
                        newPasswordInputDummy.setAttribute("type", "text");
                        newPasswordInputDummy.setAttribute("name", "txtPasswordDummy");
                        newPasswordInputDummy.setAttribute("id", "txtPasswordDummy");
                        newPasswordInputDummy.setAttribute("onfocus", "passworddummyOnFocus()");
                        newPasswordInputDummy.setAttribute("value","password");
                        newPasswordInputDummy.setAttribute("style","color:gray;display:block");                        
                                                                      
                    var newSubmitButton;
                    if (useStandardSubmitButton)
                    {
                        newSubmitButton = document.createElement("input");
                        newSubmitButton.setAttribute("type","submit");
                        newSubmitButton.setAttribute("value","Login to MAIA Online");
                    }
                    else
                    {
                        newSubmitButton = document.createElement("input");
                        newSubmitButton.setAttribute("type","image");
                        newSubmitButton.setAttribute("src",SubmitButtonImageURL);
                        newSubmitButton.setAttribute("alt","Login to MAIA Online");
                    }
                    var newRegisterButton = document.createElement("a");
                        newRegisterButton.setAttribute("href", registerURL);
                        newRegisterButton.setAttribute("title","Register Now");
                        newRegisterButton.appendChild(document.createTextNode("Register Now"));                
                                        
                    //Build up table contents
                    newTableCell_UsernameLabel.appendChild(newUsernameLabel);
                        //newTableRow_All.appendChild(newTableCell_UsernameLabel);
                    newTableCell_UsernameInput.appendChild(newUsernameInput);    
                        newTableRow_All.appendChild(newTableCell_UsernameInput);                                                             
                    newTableCell_PasswordLabel.appendChild(newPasswordLabel);
                        //newTableRow_All.appendChild(newTableCell_PasswordLabel);
                    newTableCell_PasswordInput.appendChild(newPasswordInput);
                    newTableCell_PasswordInput.appendChild(newPasswordInputDummy);
                        newTableRow_All.appendChild(newTableCell_PasswordInput);                    
                    newTableCell_Submit.appendChild(newSubmitButton); 
                        newTableRow_All.appendChild(newTableCell_Submit);                 
                    newTableCell_Register.appendChild(newRegisterButton);
                        newTableRow_All.appendChild(newTableCell_Register);
                            newTable.appendChild(newTableRow_All);                                                                                                   
                    
                    newForm.appendChild(newTable);
                    
                    LoginFormPlaceholderElement.appendChild(newForm);

                    alignToControl(LoginFormPlaceholderElementID, TargetLocationElementID);
                }
                else
                {
                    if (formcount == 2)
                    {
                        var myform = document.getElementById(formName);
                        LoginFormPlaceholderElementID.removeChild(myform);
                    }
                }
                
            /* @end @*/
                             
        }

    function alignToControl(FormPlaceholderID, TargetControlID)
    {
        //Sets thisForm's CSS Postion to that of TargetControl
        //assumes parameters are actual element objects
        var FormPlaceholder = document.getElementById(FormPlaceholderID);
        var TargetControl = document.getElementById(TargetControlID);
        var TargetControlPos = findPos(TargetControl);
        var leftPos = TargetControlPos[0] + "px";
        var topPos = TargetControlPos[1] + "px";
        FormPlaceholder.style.left = leftPos;
        FormPlaceholder.style.top = topPos; 
        FormPlaceholder.style.display = 'block';    
        
    }
    
    function findPos(obj) 
    {
    
	    var curleft = curtop = 0;
	    if (obj.offsetParent) 
	    {
		    curleft = obj.offsetLeft;
		    curtop = obj.offsetTop;
		    while (obj = obj.offsetParent) 
		    {
			    curleft += obj.offsetLeft;
			    curtop += obj.offsetTop
		    }
	    }
	    return [curleft,curtop];
    }
    
    function usernameOnFocus(ctrl)
    {
        if (ctrl.value == 'username')
        {
            ctrl.value = '';
            ctrl.style.color = 'black'; 
        }
    }
    function usernameOnBlur(ctrl)
    {
        if (ctrl.value == '')
        {
            //Is Empty - Change Text Colour to Grey Italics to 
            ctrl.style.color = 'gray';  
            ctrl.value = 'username';         
        }
    }
    
    
    function passwordOnFocus(ctrl)
    {
            ctrl.style.color = 'black'; 
    }
    
    function passwordOnBlur()
    {
        var ctrlpassword = document.getElementById('txtPassword');
        var ctrldummy = document.getElementById('txtPasswordDummy');
        
        if (ctrlpassword.value == '')
        {            
            ctrldummy.style.display = 'block';
            ctrlpassword.style.display = 'none'; 
        }
    }
    
    function passworddummyOnFocus()
    {
        var ctrlpassword = document.getElementById('txtPassword');
        var ctrldummy = document.getElementById('txtPasswordDummy');
                
        ctrldummy.style.display = 'none';
        ctrlpassword.style.display = 'block';
        ctrlpassword.focus();
    }
