/**
 * version 1.0
 * lk, zs
 * @2006 - Intermax Group
 **/
var validation_isStateAvailable = false;
var validation_jsEnabled = false;
//
function detectJS()
{
	validation_jsEnabled = true;
	lgth = document.forms[0].customerState.options.length - 1;
	document.forms[0].customerState.options[lgth] = null;
	if (document.forms[0].customerState.options[lgth]) validation_jsEnabled = false;
	if (validation_jsEnabled) swapStateRow(false);
}

function swapStateRow(bool) {
    stateArea = document.getElementById('stateDiv');
    stateArea.style.display = (bool) ? 'block' : 'none';
    stateNo_txt = document.getElementById('stateNoText');
    stateNo_txt.style.display = (!bool) ? 'block' : 'none';
}

function onChangeCountry()
{
    var frm = document.frm;
    var country = frm.customerCountryCode.value;
    var state_cb = frm.customerState;

    if (!validation_jsEnabled) return;

    showJustStates()
}

function onChangePayoutOption(obj)
{
	var val = obj.value;

	if(val == 'epassport')
	{
		divArea1 = document.getElementById('payoutOptionDiv3');
    	divArea1.style.display = 'block';
    	
    	divArea1 = document.getElementById('payoutOptionDiv1');
    	divArea1.style.display = 'none';
    	divArea1 = document.getElementById('payoutOptionDiv2');
    	divArea1.style.display = 'none';
	}
	if(val == 'bank')
	{
		divArea1 = document.getElementById('payoutOptionDiv1');
    	divArea1.style.display = 'block';
    	
    	divArea1 = document.getElementById('payoutOptionDiv2');
    	divArea1.style.display = 'none';
    	divArea1 = document.getElementById('payoutOptionDiv3');
    	divArea1.style.display = 'none';
	}
	if(val == 'epassportnew')
	{
		divArea1 = document.getElementById('payoutOptionDiv2');
    	divArea1.style.display = 'block';
    	
    	divArea1 = document.getElementById('payoutOptionDiv1');
    	divArea1.style.display = 'none';
    	divArea1 = document.getElementById('payoutOptionDiv3');
    	divArea1.style.display = 'none';
	}
	
}

function showJustStates(selectedState)
{
    var state_cb = document.frm.customerState;
    state_cb.options.length = 0;    //hide all options
    var selectedStateCode = false;
    if (selectedState)
    {
        selectedStateCode = selectedState;
    }

    var country_cb = document.frm.customerCountryCode;
    var countryCodeValue = country_cb.options[country_cb.selectedIndex].value;
	if (!countryCodeValue) return;
    var j = 0;
    var isZeroOption = false;
    var isSelected = false;
	for (var i in customerStates)
	{
	    if (!customerStates[i].v)
	    {
	        continue;
	    }
	    var countryCode_dp = customerStates[i].v.split('.')[0];
	    //alert( customerStates[i].v)
	    if (countryCodeValue == countryCode_dp)
	    {
	        if (!isZeroOption)
	        {
                state_cb.options[j] = new Option(' ', '');
                j ++;
                isZeroOption = true;
	        }
	        state_cb.options[j] = new Option(customerStates[i].l, customerStates[i].v);
	        if (selectedStateCode == customerStates[i].v)
	        {
	            state_cb.options[j].selected = "selected";
	        }
	        j ++;
	    }
	}

	validation_isStateAvailable = checkIsStateAvailable();
	swapStateRow(validation_isStateAvailable);
	//alert(validation_isStateAvailable)
}

function checkIsStateAvailable()
{
    var countryCodeValue = document.frm.customerCountryCode.value;
    for (var i in customerStates)
	{
	    if (!customerStates[i].v)
	    {
	        continue;
	    }

	    var checkCountryCode = customerStates[i].v.split('.')[0];
        //alert(checkCountryCode); return;
	    if (countryCodeValue == checkCountryCode)
	    {
	        return true;
	    }
	}
	return false;
}


function focusFirstTextField(formName){
    return;
    if (formName != null){
        for (var i=0; i < formName.length; i++){
            var element = formName.elements[i];
            if (element.type == 'text'){
                var elementName = element.name;
                formName[elementName].focus();
                return true;
            }
        }
        return false;
    }
    else {
        return false;
    }
}

function checkForm(methodType, programType)
{

    validation_isStateAvailable = checkIsStateAvailable();
    if (validation_isStateAvailable && !document.frm.customerState.value)
    {
        swapStateRow(validation_isStateAvailable);
        showJustStates()
    }
    var errorFields = [];
    var message = '';
    var errorMessages = [];
    var missingFields = [];

    var frm = document.frm;
    var pattern;
    var isCC = (methodType == 'credit_card');
    var isDD = (methodType == 'direct_debit');

    var focusDone = false;
    for(var x in mandatoryFields)
    {
        var field = mandatoryFields[x];
        var fieldName = field.n;
        var fieldLabel = field.l;
        var fieldRexExp = field.r;

        if (!frm[fieldName])
        {
            missingFields.push(fieldLabel);
            errorFields.push(fieldName);
            continue;
        }
        if(frm[fieldName].value == '')
        {
            if (fieldName == 'customerState' && !validation_isStateAvailable)
            {
                continue;
            }
            missingFields.push(fieldLabel);
            errorFields.push(fieldName);
        }
    }
    if (missingFields.length > 0)
    {
        message = pgMsg_VALIDATION_THE_FOLLOWING_FORM_FIELDS_ARE_MISSING + ':\n\n';
        message += '... ' + missingFields.join('\n... ');
        displayErrorIcos(errorFields);
        alert(message)
        return false;
    }

    for(var x in mandatoryFields)
    {
        var field = mandatoryFields[x];
        var fieldName = field.n;
        var fieldLabel = field.l;
        var fieldRexExp = field.r;
        var fieldInvMsg = field.invm;
        fieldLabel = fieldLabel.replace(":", "");

        if (!frm[fieldName])
        {
            continue;
        }
        // state: special
        if(fieldName == 'customerState' && !validation_isStateAvailable)
        {
            continue;
        }
        if(!checkFieldValRegExp(fieldName))
        {
            if (fieldInvMsg)
            {
                var msg = fieldInvMsg;
            }
            else
            {
                var msg = fieldLabel + ' ' + pgMsg_VALIDATION_IS_INVALID +'!';
            }

            errorMessages.push(msg);
            errorFields.push(fieldName);
        }
    }

    if (errorMessages.length > 0)
    {
        message = pgMsg_VALIDATION_DATA_NOT_VALID + ':\n\n';
        message += '... ' + errorMessages.join('\n... ');
        displayErrorIcos(errorFields);
        alert(message)
        return false;
    }

    frm.jsControl.value = '1';

    return true;
}


function pendingBarShow()
{
    var e = document.getElementById('pendingBar');
    if (!e) return;
    e.style.display = 'block';
}

function pendingBarHide()
{
    var e = document.getElementById('pendingBar');
    if (!e) return;
    e.style.display = 'none';

}


function pgResponseHandler(http_request)
{
    if (http_request.readyState != 4) return;

    pendingBarHide();
    if (http_request.status == 200)
    {
        var response = http_request.responseText;
        //alert(response);return;
        if (response.length > 0)
        {
            var url = './result.php?result=' + response;
            document.location = document.href = url;
            return;
        }
        pgResponseHandlerError();
    }
    else
    {
        pgResponseHandlerError();
    }

}
function pgResponseHandlerError()
{
    //document.location = document.href = url;
    var msg = pgMsg_AJAX_UNDEFINED_ERROR_OCCURED;
    msg += '\n' + pgMsg_AJAX_PLEASE_TRY_AGAIN_LATER;
    alert(msg)
}


function finishPayment(url)
{
    var parentOpener = window.opener;
    if (parentOpener) {
        parentOpener.location = url;
        window.close();
        return false;
    }
    //location.href = url;
    document.location = document.href = url;
    return false;
}


function checkChatUsername(button, inputField, remoteUrl)
{
    
    var username = inputField.value;
    if (!username)
    {
        alert(pgMsg_VALIDATION_NO_VALUE_TO_CHECK);
        return false;
    }
    var url = remoteUrl + '?username=' + escape(username) + '&' + new Date().getTime();
    function checkChatUsername_onLoad(v)
    {
        checkChatUsername_onResponse();
        if(v == 1)
        {
            alert(signupMsg_THIS_USERNAME_IS_AVAILABLE);
        }
        else
        {
            alert(signupMsg_THIS_USERNAME_IS_RESERVED);
        }
    }
    function checkChatUsername_onError(v)
    {
        alert('Error');
    }   
    
    function checkChatUsername_onResponse()
    {
        button.disabled = false;
        inputField.disabled = false;        
        inputField.value = username;
    }
     
    var i = new Image();
    i.onload =  function() { checkChatUsername_onLoad(this.width) };
    i.onerror = function() { checkChatUsername_onError () };
    
    button.disabled = true;
    inputField.disabled = true;
    inputField.value = signupMsg_CHECKING_IN_PROGRESS

    
    i.src = url;
    return false;
    
}