// node ids
var _ID_ENTER = "_enterDiv";
var _ID_THANK = "_thankDiv";
var _ID_SORRY = "_errorDiv";
var _ID_EXCEED = "_exceedDiv";
var _ID_BTNSENDSMS = "_submitLink";
var _ID_TXTBXSENDSMS = "_phoneInput";
var _ID_THANKLINK = "_thankServiceLink";
var _ID_ERRORLINK = "_errorServiceLink";
// id's of pannels which we want to show one at a time
var _panelArray = [_ID_ENTER, _ID_THANK, _ID_SORRY, _ID_EXCEED];
var _serviceLinkArray = ["_thankServiceLink", "_errorServiceLink"];

// default gomobile delivery page
var _GOMOBILE_URL = "/Entry.aspx?phone=";
var _gomobileURL = _GOMOBILE_URL;

// regular expression for phone number
var _MOBILENO_REG = new RegExp(/^(0|(\+)?61)?4[0-9]{8}$/);

// initialise the events
jQuery(document).ready(function(){
    initialiseEvents();
});

function initialiseEvents()
{
    // add the onclick event of the button
    jQuery("#" + _ID_BTNSENDSMS).click(function(){
        sendSMS(jQuery("#" + _ID_TXTBXSENDSMS).val());
        return false;
    });    
    jQuery("#" + _ID_THANKLINK).click(function(){
        activateSmsPanel(_ID_ENTER);
        return false;
    });
    jQuery("#" + _ID_ERRORLINK).click(function(){
        activateSmsPanel(_ID_ENTER);
        return false;
    });
}

function sendSMS(phoneNo)
{
    if (!phoneNo || !phoneNo.match(_MOBILENO_REG))
    {
        activateSmsPanel(_ID_SORRY);     
    }
    else
    {
        // in future, if the advertisement media need to be tracked, assign
        // code to this variable
        var adParam = ""
        if (phoneNo.indexOf("04") == 0)
        {
            phoneNo = "61" + phoneNo.substring(1,10);
        }
        var goMobileUrl = _gomobileURL + phoneNo;
		
		// bust the cache
		goMobileUrl += "&cachebuster=" + new Date().getTime();
		
        
        jQuery.get(
			goMobileUrl, 
			function(data){
				
				if (data == "Valid")
				{
					activateSmsPanel(_ID_THANK);        
				} 
				else if (data == "ExceedMaxEntries")
				    activateSmsPanel(_ID_EXCEED);        
				else
				{
					activateSmsPanel(_ID_SORRY);
				}
			}, 
			"text"); 
    }

    // supress keypress event bubbling
    return false;
}

function activateSmsPanel(panelId)
{
    // show the pannel int he array that matches the provided pannel id
    for (var i = 0; i < _panelArray.length; i++)
    {
        if (_panelArray[i] != panelId)
        {
            jQuery("#" + _panelArray[i]).hide();
        }
        else
        {
            jQuery("#" + _panelArray[i]).show();
        }
    }
    return false;
}   

