﻿function refreshCityList(countryName) {
    CoinBox.GetCities(countryName, refreshCityList_callback);
}

function refreshCityList_callback(res) {
    if (res.error != null) {
        alert(res.error);
    }
    else {
        ClearOptionsFast('SelectCity');
        for (i = 0; i < res.value.length; i++) {
            appendCity(res.value[i]);
        }
    }
}

function appendCity(cityName) {
    var elOptNew = document.createElement('option');
    elOptNew.text = cityName;
    elOptNew.value = cityName
    var elSel = document.getElementById('SelectCity');

    try {
        elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
    }
    catch (ex) {
        elSel.add(elOptNew); // IE only
    }
}

function ClearOptionsFast(id) {
    var selectObj = document.getElementById(id);
    var selectParentNode = selectObj.parentNode;
    var newSelectObj = selectObj.cloneNode(false); // Make a shallow copy
    selectParentNode.replaceChild(newSelectObj, selectObj);
    return newSelectObj;
}

setTimeout('refreshCityList("TÜRKİYE")', 2000);

function SaveCoinBox() {
    if (!checkValid(document.getElementById('TextBoxName'))) {
        alert(alertMsg);
        return;
    }
    if (!checkValid(document.getElementById('TextBoxSurname'))) {
        alert(alertMsg);
        return;
    }
    if (!checkValid(document.getElementById('txtEmail'))) {
        alert(alertMsg);
        return;
    }
    if (!checkValid(document.getElementById('txtPhone'))) {
        alert(alertMsg);
        return;
    }
    if (!checkValid(document.getElementById('txtCaptcha'))) {
        alert(alertMsg);
        return;
    }


    var captchaText = document.getElementById('txtCaptcha').value;
    CoinBox.CheckCaptcha(captchaText,
            document.getElementById('TextBoxName').value,
            document.getElementById('TextBoxSurname').value,
            document.getElementById(selectCountyClient).value,
            document.getElementById('SelectCity').value,
            document.getElementById('txtEmail').value,
            document.getElementById('txtPhone').value,
            document.getElementById('txtOccupation').value,
            document.getElementById('txtWorkPlace').value
                                    , SaveCoinBox_callback);


}

function SaveCoinBox_callback(res) {
    if (res.value) {
        alert(successMsg);
    }
    else {
        var randomnumber = Math.floor(Math.random() * 10000);
        document.getElementById('txtCaptcha').value = '';
        document.getElementById('txtCaptcha').style.backgroundColor = '#a1b0e7';
        if (!checkValid(document.getElementById('txtCaptcha'))) {
            alert(alertMsg);
        }
        document.getElementById('captcha').src = '#';
        document.getElementById('captcha').src = 'captcha.aspx?' + randomnumber;
    }
}

function checkValid(elm) {
    if ((elm.style.backgroundColor == '#ffffff') || (elm.style.backgroundColor == 'rgb(255, 255, 255)')) {
        return true;
    }

    elm.focus();
    return false;
}


function eMailCheck(str) {

    var at = "@"
    var dot = "."
    var lat = str.indexOf(at)
    var lstr = str.length
    var ldot = str.indexOf(dot)
    if (str.indexOf(at) == -1) {
        return false;
    }

    if (str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr) {
        return false;
    }

    if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr) {
        return false;
    }

    if (str.indexOf(at, (lat + 1)) != -1) {
        return false;
    }

    if (str.substring(lat - 1, lat) == dot || str.substring(lat + 1, lat + 2) == dot) {
        return false;
    }

    if (str.indexOf(dot, (lat + 2)) == -1) {
        return false;
    }

    if (str.indexOf(" ") != -1) {
        return false;
    }
    return true;
}

function validateRequired(element) {
    if (element.value == '') {
        element.style.backgroundColor = '#a1b0e7';
    }
    else {
        element.style.backgroundColor = '#ffffff';
    }
}

function numbersonly(e) {
    var unicode = e.charCode ? e.charCode : e.keyCode;
    if (unicode != 8) { //if the key isn't the backspace key (which we should allow)
        if (unicode < 48 || unicode > 57) return false; //if not a number return false //disable key press
    }
    else return true;
}

function validateMail(element) {
    var emailText = element.value;
    var isMailValid = eMailCheck(emailText);
    if (!isMailValid) {
        element.style.backgroundColor = '#a1b0e7';
    }
    else {
        element.style.backgroundColor = '#ffffff';
    }
}

             