function GetXmlHttpObject()
{
    var xmlHttp=null;

    try
    {
        xmlHttp=new XMLHttpRequest();
    }
    catch (e)
    {
        try
        {
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            try
            {
                xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(e)
            {
                alert("Your browser does not support AJAX!");
            }
        }
    }

    return xmlHttp;
}

function set_values(xml_attribute,tag_name)
{
    //alert(xml_attribute.responseText);
    var return_value = new Object();
    var xmldoc = xml_attribute.responseXML;
    var root = xmldoc.getElementsByTagName(tag_name).item(0);
    var index = 0;

    for( i=0; i<root.childNodes.length; i++ )
    {
         var node = root.childNodes.item(i);

         if( node.childNodes.length > 0 )
         {
             for( j=0; j < node.childNodes.length; j++ )
             {
                // Reading values inside tags (ie <aaaa>value</aaa>)
                var childNode = node.childNodes.item(j);

                return_value[childNode.parentNode.nodeName] = childNode.data;
             }
         }
         else
         if( node.attributes.length > 0 )
         {
            if( !return_value[node.nodeName] )
            {
                return_value[node.nodeName] = new Object();
            }

            var temp_arr = new Object();

            for( j=0; j < node.attributes.length; j++ )
            {
                // Reading attribute's values (ie <aaa id='xxxx' value='xxx'>)
                var attribute = node.attributes[j];
                temp_arr[attribute.nodeName] =attribute.nodeValue;
            }

            return_value[node.nodeName][index] = temp_arr;

            index++;
         }
    }
    return return_value;
}

function set_state_change()
{
    if (xmlHttp.readyState==4)
    {
        var message = new Array();
        message = set_values(xmlHttp,'root');
        alert(message['message']);
    }
}

function xml_user_key()
{
    if (xmlHttp.readyState==4)
    {
        xmlResult = set_values(xmlHttp,'root');
        xGetElementById('access-number-td').innerHTML = xmlResult['HTML'];
    }
}

function xml_area()
{
    if( xmlHttp.readyState == 4 )
    {
        xmlResult = set_values(xmlHttp,'root');
        xGetElementById('area').parentNode.innerHTML = xmlResult['HTML'];
    }
}

function submit_subscription()
{
    var passed = check_subscription_details();

    if( passed == true )
    {
        xmlHttp=GetXmlHttpObject();
        if (xmlHttp==null)
        {
          alert ("Your browser does not support AJAX!");
          return;
        }

        url = xGetElementById('server-path').value + "/include/xml/subscription-functions.php";

        post_variables = "function=CreateMember";
        post_variables += "&friendly-name="+escape(xGetElementById('friendly-name').value);
        post_variables += "&phone-number="+escape(xGetElementById('phone-number').value);
        post_variables += "&mobile-number="+xGetElementById('mobile-number').value;
        post_variables += "&email-address="+xGetElementById('email-address').value;
        post_variables += "&address-1="+xGetElementById('address-1').value;
        post_variables += "&address-2="+xGetElementById('address-2').value;
        post_variables += "&address-3="+xGetElementById('address-3').value;
        post_variables += "&address-4="+xGetElementById('address-4').value;
        post_variables += "&address-5="+xGetElementById('address-5').value;

        xGetElementById('submit-button').disabled = true;
        xGetElementById('account-td').style.display = 'none';
        xGetElementById('terms-td').style.display = 'none';
        xGetElementById('wait-td').style.display = 'block';
       
        xmlHttp.open("POST",url,true);
        xmlHttp.onreadystatechange=xml_subscription;
        xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        xmlHttp.send(post_variables);
    }
}

function xml_subscription()
{
    if (xmlHttp.readyState==4)
    {
        //alert(xmlHttp.responseText);
        xmlResult = set_values(xmlHttp,'root');

        if( xmlResult['ErrorMessage'] )
        {
            alert(xmlResult['ErrorMessage']);

            xGetElementById('submit-button').disabled = false;
            xGetElementById('account-td').style.display = 'block';
            xGetElementById('terms-td').style.display = 'block';
            xGetElementById('wait-td').style.display = 'none';
        }
        else
        {
            alert(xmlResult['Message']);
            window.location.href = xGetElementById('server-path').value;
        }
    }
}


function check_subscription_details()
{
    var elems = xGetElementsByClassName('input-missed');

    for( var prop in elems )
    {
        elems[prop].className = 'input-mandatory';
    }
    
    if( xGetElementById('friendly-name').value == "" )
    {
        alert("Please specify name / company name");
        xGetElementById('friendly-name').focus();
        xGetElementById('friendly-name').className = 'input-missed';
        return false;
    }
    
    if( xGetElementById('phone-number').value == "" )
    {
        alert("Please specify phone number");
        xGetElementById('phone-number').focus();
        xGetElementById('phone-number').className = 'input-missed';
        return false;
    }
    
    if( xGetElementById('email-address').value == "" )
    {
        alert("Please specify email address");
        xGetElementById('email-address').focus();
        xGetElementById('email-address').className = 'input-missed';
        return false;
    }
    else
    if( !isEmail(xGetElementById('email-address').value) )
    {
        alert("Invalid email address format");
        xGetElementById('email-address').focus();
        xGetElementById('email-address').className = 'input-missed';
        return false;
    }
    
    if( xGetElementById('email-address-confirm').value == "" )
    {
        alert("Please specify confirm email address");
        xGetElementById('email-address-confirm').focus();
        xGetElementById('email-address-confirm').className = 'input-missed';
        return false;
    }
    else
    if( xGetElementById('email-address').value != xGetElementById('email-address-confirm').value )
    {
        alert("Email addresses do not match!");
        xGetElementById('email-address-confirm').focus();
        xGetElementById('email-address-confirm').className = 'input-missed';
        return false;
    }

    if( xGetElementById('address-1').value == "" )
    {
        alert("Please specify address");
        xGetElementById('address-1').focus();
        xGetElementById('address-1').className = 'input-missed';
        return false;
    }

    if( xGetElementById('authorise-signup').checked == false )
    {
        alert("Please check the details and tick confirmation check box");
        xGetElementById('authorise-signup').focus();
        xGetElementById('authorise-signup').className = 'input-missed';
        return false;
    }

    if( xGetElementById('seen-terms-and-conditions').checked == false )
    {
        alert("Please read our Terms and Conditions and tick confirmation check box");
        xGetElementById('seen-terms-and-conditions').focus();
        xGetElementById('seen-terms-and-conditions').className = 'input-missed';
        return false;
    }

    return true;
}

function check_terms_and_conditions()
{
    if( xGetElementById('seen-terms-and-conditions').checked == true )
    {
        if( !confirm("Do you confirm that you have read Terms & Conditions for TNZ Group Services?") )
        {
            xGetElementById('seen-terms-and-conditions').checked = false;
            window.open(xGetElementById('server-path').value+'/About/TermsAndConditions/','TermsAndConditions','toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,copyhistory=yes,resizable=yes');
        }
    }
}

function check_email_format(textfield)
{
    if( xGetElementById(textfield).value != "" && !isEmail(xGetElementById(textfield).value) )
    {
        alert("Invalid email address format!");
        xGetElementById(textfield).focus();
        return;
    }
}

function copy_company_email_confirm()
{
    xGetElementById('rxemail-address-1').value = xGetElementById('company-email').value;
    xGetElementById('txemail-address-1').value = xGetElementById('company-email').value;
}

function init_subscription()
{
    xAddEventListener('company-email-confirm','change',copy_company_email_confirm,false);
}

xAddEventListener(window,'load',init_subscription,false);
