/* Updates domain price */
function updateDomainPrice(periodList){
    var orderBundle = document.getElementById("postForm:hiddenOrderBundle").getAttribute("value");
    var options = periodList.options;
    var selectedIndex = options.selectedIndex;
    var period = options[selectedIndex].value;
    var domain = document.getElementById(periodList.id.substring(0, periodList.id.indexOf(":period")) + ":domain").firstChild.nodeValue;

    var ajaxRequestUrl = document.getElementById("headerForm:webAppContext").getAttribute("value") + "/servlet/AjaxResponseServlet";
    var postBody = "command=domainPeriodChanged&orderBundle=" + orderBundle +
                   "&period=" + period + "&domain=" + domain + "&formId=" + periodList.id;

    //alert("postBody: " + postBody);
    //alert("AjaxUrl: " + ajaxRequestUrl);
    
    /*
	 * AjaxObject is a hypothetical object that encapsulates the transaction
	 *     request and callback logic.
	 *
	 * handleSuccess( ) provides success case logic
	 * handleFailure( ) provides failure case logic
	 * processResult( ) displays the results of the response from both the
	 * success and failure handlers
	 * call( ) calling this member starts the transaction request.
	 */

	var AjaxObject = {

	    handleSuccess:function(o){
	        // This member handles the success response
	        // and passes the response object o to AjaxObject's
	        // processResult member.
	        this.processResult(o);
	    },

	    handleFailure:function(o){
	        alert('Ajax request to determine price failed! - ' + o.status);
	    },

	    processResult:function(o){
	        // This member is called by handleSuccess
                var response = o.responseXML;
                var priceId = (response.getElementsByTagName("price")[0]).getAttribute("id");
                var priceValue = (response.getElementsByTagName("price")[0]).getAttribute("value");

                if(document.getElementById(priceId) != null){
                    document.getElementById(priceId).firstChild.nodeValue = priceValue;
                }
	    },

	    startRequest:function() {
	       YAHOO.util.Connect.asyncRequest('POST', ajaxRequestUrl, callback, postBody);
	    }
	};

	/*
	 * Define the callback object for success and failure
	 * handlers as well as object scope.
	 */
	var callback =
	{
	    success: AjaxObject.handleSuccess,
	    failure: AjaxObject.handleFailure,
            timeout: 6000,
	    scope: AjaxObject
	};

	// Start the transaction.
	AjaxObject.startRequest(); 
}

/* Updates Certificate price */
function updateCertPrice(periodList){
    var orderBundle = document.getElementById("postForm:hiddenOrderBundle").getAttribute("value");
    var options = periodList.options;
    var selectedIndex = options.selectedIndex;
    var period = options[selectedIndex].value;

    var ajaxRequestUrl = document.getElementById("headerForm:webAppContext").getAttribute("value") + "/servlet/AjaxResponseServlet";
    var postBody = "command=certPeriodChanged&orderBundle=" + orderBundle +
                   "&period=" + period + "&formId=" + periodList.id;

    //alert("postBody: " + postBody);
    //alert("AjaxUrl: " + ajaxRequestUrl);

    /*
	 * AjaxObject is a hypothetical object that encapsulates the transaction
	 *     request and callback logic.
	 *
	 * handleSuccess( ) provides success case logic
	 * handleFailure( ) provides failure case logic
	 * processResult( ) displays the results of the response from both the
	 * success and failure handlers
	 * call( ) calling this member starts the transaction request.
	 */

	var AjaxObject = {

	    handleSuccess:function(o){
	        // This member handles the success response
	        // and passes the response object o to AjaxObject's
	        // processResult member.
	        this.processResult(o);
	    },

	    handleFailure:function(o){
	        alert('Ajax request to determine price failed! - ' + o.status);
	    },

	    processResult:function(o){
	        // This member is called by handleSuccess
                var response = o.responseXML;
                var priceId = (response.getElementsByTagName("price")[0]).getAttribute("id");
                var priceValue = (response.getElementsByTagName("price")[0]).getAttribute("value");

                if(document.getElementById(priceId) != null){
                    document.getElementById(priceId).firstChild.nodeValue = priceValue;
                }
	    },

	    startRequest:function() {
	       YAHOO.util.Connect.asyncRequest('POST', ajaxRequestUrl, callback, postBody);
	    }
	};

	/*
	 * Define the callback object for success and failure
	 * handlers as well as object scope.
	 */
	var callback =
	{
	    success: AjaxObject.handleSuccess,
	    failure: AjaxObject.handleFailure,
            timeout: 6000,
	    scope: AjaxObject
	};

	// Start the transaction.
	AjaxObject.startRequest();
}

/* Check if Username is available */
function checkUserAvailability(username){
    var usernameValue = document.getElementById(username.id).value;
    
    var ajaxRequestUrl = document.getElementById("headerForm:webAppContext").getAttribute("value") + "/servlet/AjaxResponseServlet";
    var postBody = "command=checkUser&username=" + usernameValue + "&formId=" + username.id;

    //alert("postBody: " + postBody);
    //alert("AjaxUrl: " + ajaxRequestUrl);

    /*
	 * AjaxObject is a hypothetical object that encapsulates the transaction
	 *     request and callback logic.
	 *
	 * handleSuccess( ) provides success case logic
	 * handleFailure( ) provides failure case logic
	 * processResult( ) displays the results of the response from both the
	 * success and failure handlers
	 * call( ) calling this member starts the transaction request.
	 */

	var AjaxObject = {

	    handleSuccess:function(o){
	        // This member handles the success response
	        // and passes the response object o to AjaxObject's
	        // processResult member.
	        this.processResult(o);
	    },

	    handleFailure:function(o){
	        alert('Ajax request to determine username availability Failed! - ' + o.status);
	    },

	    processResult:function(o){
	        // This member is called by handleSuccess
                var response = o.responseXML;
                var usernameAttributeValue = (response.getElementsByTagName("username")[0]).getAttribute("value");

                if(document.getElementById(username.id) != null){
                    if(usernameAttributeValue == "1"){
                        // available
                       document.getElementById(username.id + "Img").src = document.getElementById("headerForm:webAppContext").getAttribute("value") + '/images/yes.png';
                       document.getElementById(username.id + "Img").onmouseover = function(){showhint('Username is available.', this, event, '150px');};
                       document.getElementById("postForm:newUserAvailable").value = "1"
                    }else{
                       // not available
                       document.getElementById(username.id + "Img").src = document.getElementById("headerForm:webAppContext").getAttribute("value") + '/images/no.gif';
                       document.getElementById(username.id + "Img").width = "16";
                       document.getElementById(username.id + "Img").height = "16";
                       document.getElementById(username.id + "Img").onmouseover = function(){showhint('Username is not available.', this, event, '150px');};
                       document.getElementById("postForm:newUserAvailable").value = "0";
                    }
                }
	    },

	    startRequest:function() {
	       YAHOO.util.Connect.asyncRequest('POST', ajaxRequestUrl, callback, postBody);
	    }
	};

	/*
	 * Define the callback object for success and failure
	 * handlers as well as object scope.
	 */
	var callback =
	{
	    success: AjaxObject.handleSuccess,
	    failure: AjaxObject.handleFailure,
            timeout: 6000,
	    scope: AjaxObject
	};

	// Start the transaction.
	AjaxObject.startRequest();
}

/* validateState */
function validateState(countryList, stateId){
    var options = countryList.options;
    var selectedIndex = options.selectedIndex;
    var countryCode = options[selectedIndex].value;
    var stateCode = document.getElementById(stateId).value;

    if(countryCode == "CA" || countryCode == "US"){
        var ajaxRequestUrl = document.getElementById("headerForm:webAppContext").getAttribute("value") + "/servlet/AjaxResponseServlet";
        var postBody = "command=validateState&stateCode=" + stateCode + "&countryCode=" + countryCode + "&formId=" + stateId;

        //alert("postBody: " + postBody);
        //alert("AjaxUrl: " + ajaxRequestUrl);

        /*
             * AjaxObject is a hypothetical object that encapsulates the transaction
             *     request and callback logic.
             *
             * handleSuccess( ) provides success case logic
             * handleFailure( ) provides failure case logic
             * processResult( ) displays the results of the response from both the
             * success and failure handlers
             * call( ) calling this member starts the transaction request.
             */

            var AjaxObject = {

                handleSuccess:function(o){
                    // This member handles the success response
                    // and passes the response object o to AjaxObject's
                    // processResult member.
                    this.processResult(o);
                },

                handleFailure:function(o){
                    alert('Ajax request to validate State Failed! - ' + o.status);
                },

                processResult:function(o){
                    // This member is called by handleSuccess
                    var response = o.responseXML;
                    var stateProvinceArray = response.getElementsByTagName("state");

                    // remove current options
                    var stateOptions = document.getElementById(stateId);

                    // remove current options
                    stateOptions.length = 0;

                    // add new options
                    for(i = 0; i < stateProvinceArray.length; i++){
                        stateOptions.options[i] = new Option(stateProvinceArray[i].getAttribute("desc"), stateProvinceArray[i].getAttribute("value"));
                    }
                },

                startRequest:function() {
                   YAHOO.util.Connect.asyncRequest('POST', ajaxRequestUrl, callback, postBody);
                }
            };

            /*
             * Define the callback object for success and failure
             * handlers as well as object scope.
             */
            var callback =
            {
                success: AjaxObject.handleSuccess,
                failure: AjaxObject.handleFailure,
                timeout: 6000,
                scope: AjaxObject
            };

            // Start the transaction.
            AjaxObject.startRequest();
    }else{
        // remove current options
        var stateOptions = document.getElementById(stateId);
        if(options.length > 1){
            stateOptions.length = 0;

            // add new option
            stateOptions.options[0] = new Option("N/A", "-1");
        }
    }
}