function ClientValidateOver18(source, arguments)
{
	arguments.IsValid = getElem("MyOver18").checked;
}

function CheckValidate()
{
	if (getElem("MyOver18").checked)
	{
		//window.open("../SessionTransfer/Sender.aspx?url=g_over18.asp",null,"height=220,width=560,toolbar=no,menubar=no,location=no");
		window.open("../../~ln~/asp/g_over18.asp",null,"height=220,width=560,toolbar=no,menubar=no,location=no");
	}
}
function ClientValidateRules(source, arguments)
{
	arguments.IsValid = getElem("Rules").checked;
}

function DisplayTerms()
{
	var iSelectedIndex=getElem("CountryDropDownList").selectedIndex;
	var selectedValue=getElem("CountryDropDownList").item(iSelectedIndex).value;	
	
	if (selectedValue > 0)
	{
		window.open("../Registration/RegistrationTC.aspx",null,"height=820,width=660,toolbar=no,menubar=no,location=no,scrollbars=yes");
		
	}
	else
	{
		alert("You must select your country of residence to view your Terms and Conditions!");
		
	}
	
	return false;
}

//function ClientValidateComboBox(source, arguments)
//{
//	var iSelectedIndex=getElem(source.controltovalidate).selectedIndex;
//	var selectedValue=getElem(source.controltovalidate).item(iSelectedIndex).value;	
//	arguments.IsValid =(selectedValue > 0);	
//}

//function ClientValidateAccount(source, arguments)
//{
//	var clientId = getElem("AccountId").value;
//	//if ((getElem("button").innerText == "false") &&(isNaN(clientId) && (clientId != "")) || ( && (clientId == "")))
//	if ((getElem("button").innerText == "false") && ((clientId == "") || (isNaN(clientId) && (clientId != ""))))
//	//if (getElem("button").innerText == "false")
//	//{
//	//	if ((clientId == "") || (isNaN(clientId) && (clientId != "")))
//		{
//			arguments.IsValid = false;
//		}
//	//}
//	
//}

//function SetButton(value)
//{
//	window.document.getElementById("button").innerText = value;
//}

//function getElem(sId)
//{	
//	return window.document.getElementById("RegistrationControl1_" + sId);
//}

//function isEmpty(elem)
//{
//	var str = elem.value;
//	if(str == null || str.length == 0)
//		return true;
//		
//	return false;
//}

//function isNumber(elem)
//{
//	var str = elem.value;
//	var re = /^[-]?\d*\.?\d*$/;
//	str = str.toString();
//	
//	if(!str.match(re))
//		return false;
//	
//	return true;
//}

//function ValidateForm()
//{
//var name = getElem("Name");
//var surname = getElem("Surname");
//var email = getElem("EmailName");
//var emailSuff = getElem("EmailSuffix");
//var emailConfirm = getElem("ConfirmEmail");
//var emailSuffConfirm = getElem("ConfirmEmailSuffix");
//var password = getElem("Password");
//var passwordConfirm = getElem("ConfirmPassword");
//var dobDD = getElem("DOB_dd");
//var dobMM = getElem("DOB_mm");
//var dobYYYY = getElem("DOB_yy");
//var Over18 = getElem("Over18");
//var addr1 = getElem("AddressLine1");
//var addr2 = getElem("AddressLine2");
//var city = getElem("City");
//var postcode = getElem("PostCode");
//var country = getElem("CountryDropDownList");
//var pob = getElem("POB");
//var homeNumber = getElem("HomeNumber");
//var currency = getElem("CurrencyDropDownList");
//var passReg = /(?=^.{6,10}$)(?=.*\d)(?=.*[a-zA-Z])(?!.*\s).*$/;


//if(isEmpty(name)){alert('You must enter your Name.'); name.focus(); return false;}
//if(isEmpty(surname)){alert('You must enter your Surname'); surname.focus(); return false;}
//if(isEmpty(email) || isEmpty(emailSuff) || isEmpty(emailConfirm) || isEmpty(emailSuffConfirm)){alert('You must enter your Email address.'); return false;}

//if(email.value != emailConfirm.value){alert(' The email names must match '); return false;}
//if(emailSuff.value != emailSuffConfirm.value){alert(' The email suffixes must match '); return false;}
//if(isEmpty(password)){alert('You must enter your Password.'); password.focus(); return false;}

//if(!password.value.toString().match(passReg)){alert('Your Password must be be 6 to 10 character in length and should contain at least a letter and a number.'); password.focus(); return false;}
//if(isEmpty(passwordConfirm)){alert('You must enter your Password.'); passwordConfirm.focus(); return false;}
//if(!passwordConfirm.value.toString().match(passReg)){alert('Your Password must be be 6 to 10 character in length and should contain at least a letter and a number.'); password.focus(); return false;}


//if(password.value != passwordConfirm.value){alert(' The passwords must match '); return false;}
//if(isEmpty(dobDD) || isEmpty(dobMM) || isEmpty(dobYYYY)){alert('You must enter your Date of Birth.'); return false;}
//if(!isNumber(dobDD)){alert('You must enter a valid Date of Birth.');dobDD.focus(); return false;}
//if(!isNumber(dobMM)){alert('You must enter a valid Date of Birth.');dobMM.focus(); return false;}
//if(!isNumber(dobYYYY)){alert('You must enter a valid Date of Birth.');dobYYYY.focus(); return false;}


//if(!Over18.checked){alert('You must check the Over 18 box to continue.'); Over18.focus(); return false;}
//if(isEmpty(addr1)){alert('You must enter the first line of your Address.'); addr1.focus(); return false;}
//if(isEmpty(addr2)){alert('You must enter the second line of your Address.'); addr2.focus(); return false;}
//if(isEmpty(city)){alert('You must enter your City of residence.'); city.focus(); return false;}
//if(isEmpty(postcode)){alert('You must enter your Post Code.'); postcode.focus(); return false;}

//if(country.selectedIndex == 0){alert(' You must select a country '); country.focus(); return false;}
//if(isEmpty(pob)){alert('You must enter your Place of Birth.'); pob.focus(); return false;}
//if(isEmpty(homeNumber)){alert('You must enter your Home Telephone Number.'); homeNumber.focus(); return false;}
//if(!isNumber(homeNumber)){alert('You must enter a numeric value as your Home Number.'); homeNumber.focus(); return false;}
//if(currency.selectedIndex == 0){alert(' You must select a currency '); currency.focus(); return false;}
//return true;

//}

//var valid = false;
//var _current = null;

   
(function($) {
    $(document).ready(function() {

        AttachInputTips();

        //attatch event handler
        $('#' + Register).bind("click", function(event) {
            return ValidateFormValues();
        });

        $("#" + txtFirstName)[0].focus();

    });
})(jQuery);

function ValidateFormValues() {


    if (isFirstNameValid == false) {
        $("#" + txtFirstName)[0].focus();
        return false;
    }
    else if (isSurnameValid == false) {
        $("#" + txtSurname)[0].focus();
        return false;
    }
    else if (isEmailValid == false) {
        $("#" + txtEmail)[0].focus();
        return false;
    }
    else if (isCondfirmEmailValid == false) {
        $("#" + txtConfirmEmail)[0].focus();
        return false;
    }
    else if (isPasswordValid == false) {
        $("#" + txtPassword)[0].focus();
        return false;
    }
    else if (isConfirmPasswordValid == false) {
        $("#" + txtConfirmPassword)[0].focus();
        return false;
    }
    else if (isDateOfBirthValid == false) {
        $("#" + txtDateOfBirth)[0].focus();
        return false;
    }
    else if (isOver18Valid == false) {
        $("#" + MyOver18)[0].focus();
        return false;
    }
    else if (isAddressLine1Valid == false) {
        $("#" + txtAddressLine1)[0].focus();
        return false;
    }
    else if (isCityValid == false) {
        $("#" + txtCity)[0].focus();
        return false;
    }
    else if (isPostCodeValid == false) {
        $("#" + txtPostCode)[0].focus();
        return false;
    }
    else if (isCountryValid == false) {
        $("#" + listCountry)[0].focus();
        return false;
    }
    else if (isPlaceOfBrithValid == false) {
        $("#" + txtPlaceOfBrith)[0].focus();
        return false;
    }
    else if (isHomePhoneValid == false) {
        $("#" + txtHomePhone)[0].focus();
        return false;
    }
    else if (isCurrencyValid == false) {
        $("#" + listCurrency)[0].focus();
        return false;
    }
    else if ($("#" + txtCodiceFiscale).Visible == true && isCodiceFiscale == false) {
        $("#" + txtCodiceFiscale)[0].focus();
        return false;
    }
    else if (isSecurityCodeValid == false) {
        $("#" + txtCap)[0].focus();
        return false;
    }
    else if (isPrivacyPolicySelected == false) {
        $("#" + MyTAndC)[0].focus();
        return false;
    }
    else if (isTAndCSelected == false) {
        $("#" + MyTAndC)[0].focus();
        return false;
    }

    return true;
}

function AttachInputTips() {

    //validate first name
    $("#" + txtFirstName).inputTip({
        // Text displayed when the input passes the validation
        goodText: "",
        // Text displayed when the input doesn't pass the validation
        badText: "",
        // Text displayed as a tip when the input field is focused
        tipText: "",
        /* Function called to validate the input. It should fire "callback" with the following parameters
        *  First parameter:
        *  - 0: show the tip text
        *  - 1: validation succeeded
        *  - 2: validation succeeded with no image
        *  - 3: validation failed
        *  - 4: validation failed
        *  - 5: validation failed
        *  - 6: validation failed
        * Second parameter: optional text to display instead of the standard text */
        validateText: function(inputValue, callback) {
            // Checking if the input field contains text.
            if (inputValue.length > 0) {
                callback(1);
                isFirstNameValid = true;
            }
            else {
                callback(3); 
                isFirstNameValid = false;
            }
        },
        // True if the validation should be performed on every key/up event (false by default)
        validateInRealTime: false
    });

    //validate surname
    $("#" + txtSurname).inputTip({
        goodText: "",
        badText: "",
        tipText: "",
        validateText: function(inputValue, callback) {
            // Checking if the input field contains text.
            if (inputValue.length > 0) {
                callback(1);
                isSurnameValid = true;
            }
            else {
                callback(3);
                isSurnameValid = false;
            }
        }
    });

    //validate email
    $("#" + txtEmail).inputTip({
        goodText: "",
        badText: "",
        badText1: $("#JS_EmailError1").val(),
        badText2: $("#JS_EmailError2").val(),
        tipText: "",
        validateText: function(inputValue, callback) {
            // Checking if the input field contains text.
            if (inputValue.length > 0) {
                //checking if email is valid
                var regex = /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/
                if (regex.test(inputValue)) {

                    //checking if email already registered
                    $.get('../HttpHandlers/Registration.ashx', { email: inputValue }, function(data) {
                        if (data == "false") {
                            callback(5);
                            isEmailValid = false;
                        }
                        else {
                            callback(1);
                            isEmailValid = true;
                        }
                    });
                }
                else {
                    callback(4);
                    isEmailValid = false;
                }
            }
            else {
                callback(3);
                isEmailValid = false;
            }
        }
    });


    //validate confirm email
    $("#" + txtConfirmEmail).inputTip({
        goodText: "",
        badText: "",
        badText1: $("#JS_EmailError1").val(),
        badText2: $("#JS_EmailError3").val(),
        tipText: "",
        validateText: function(inputValue, callback) {
            // Checking if the input field contains text.
            if (inputValue.length > 0) {
                //checking if email is valid
                var regex = /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/
                if (regex.test(inputValue)) {
                    callback(1);
                    isCondfirmEmailValid = true;

                    //comparing if email values
                    var email = $("#" + txtEmail).val();
                    if (inputValue != email) {
                        callback(5);
                        isCondfirmEmailValid = false;
                    }
                    else {
                        callback(1);
                        isCondfirmEmailValid = true;
                    }

                }
                else {
                    callback(4);
                    isCondfirmEmailValid = false;
                }
            }
            else {
                callback(3);
                isCondfirmEmailValid = false;
            }
        }
    });


    //validate password
    $("#" + txtPassword).inputTip({
        goodText: "",
        badText: "",
        badText1: $("#JS_PasswordError1").val(),
        tipText: "",
        validateText: function(inputValue, callback) {
            // Checking if the input field contains text.
            if (inputValue.length > 0) {
                //checking if email is valid
                var regex = /^\b[a-zA-Z0-9]{6,10}\b$/
                if (regex.test(inputValue)) {
                    callback(1);
                    isPasswordValid = true;

                }
                else {
                    callback(4);
                    isPasswordValid = false;
                }
            }
            else {
                callback(3);
                isPasswordValid = false;
            }
        }
    });

    //validate confirm password
    $("#" + txtConfirmPassword).inputTip({
        goodText: "",
        badText: "",
        badText1: $("#JS_PasswordError1").val(),
        badText2: $("#JS_PasswordError2").val(),
        tipText: "",
        validateText: function(inputValue, callback) {
            // Checking if the input field contains text.
            if (inputValue.length > 0) {
                //checking if email is valid
                var regex = /^\b[a-zA-Z0-9]{6,10}\b$/
                if (regex.test(inputValue)) {
                    callback(1);
                    isConfirmPasswordValid = true;

                    //comparing if email values
                    var password = $("#" + txtPassword).val();
                    if (inputValue != password) {
                        callback(5);
                        isCondfirmEmailValid = false;
                    }
                    else {
                        callback(1);
                        isCondfirmEmailValid = true;
                    }

                }
                else {
                    callback(4);
                    isConfirmPasswordValid = false;
                }
            }
            else {
                callback(3);
                isConfirmPasswordValid = false;
            }
        }
    });

    //validate date of birth
    $("#" + txtDateOfBirth).inputTip({
        goodText: "",
        badText: "",
        badText1: $("#JS_DOBError1").val(),
        badText2: $("#JS_Over18Error1").val(),
        tipText: "",
        validateText: function(inputValue, callback) {
            // Checking if the input field contains text.
            if (inputValue.length > 0) {
                //checking if dob is valid
                var regex = new RegExp("^[0-9]{2}/[0-9]{2}/[0-9]{4}$");
                if (regex.test(inputValue)) {

                    //check if date is valid
                    $.get('../HttpHandlers/Registration.ashx', { dob: inputValue }, function(data) {
                        if (data == "false") {
                            callback(3);
                            isDateOfBirthValid = false;
                        }
                        else {

                            //check if client is 18 years
                            $.get('../HttpHandlers/Registration.ashx', { agedob: inputValue }, function(data) {
                                if (data == "false") {
                                    callback(5);
                                    isDateOfBirthValid = false;
                                }
                                else {
                                    callback(1);
                                    isDateOfBirthValid = true;
                                }
                            });
                        
                        }
                    });
                }
                else {
                    callback(4);
                    isDateOfBirthValid = false;
                }
            }
            else {
                callback(3);
                isDateOfBirthValid = false;
            }
        }
    });


    //validate over 18 selected
    $("#" + MyOver18).inputTip({
        goodText: "",
        badText: "",
        tipText: "",
        validateText: function(inputValue, callback) {
            // Checking if the input field contains text.
            if ($("#" + MyOver18)[0].checked == true) {
                callback(1);
                isOver18Valid = true;
            }
            else {
                callback(3);
                isOver18Valid = false;
            }
        }
    });
    
    //validate bonuscode
    $("#" + txtBonusCode).inputTip({
        goodText: "",
        badText1: $("#JS_BonuscodeError1").val(),
        badText2: $("#JS_BonuscodeError2").val(),
        tipText: "",
        validateText: function(inputValue, callback) {
            // Checking if the bonus code is valid
            if (inputValue.length > 0) {

                //check if bonus code is valid
                $.get('../HttpHandlers/Registration.ashx', { bonuscode: inputValue }, function(data) {
                    if (data == "false") {
                        callback(4);
                        isBonusCodeValid = false;
                    }
                    else {
                        isBonusCodeValid = true;
                    }
                    
                    if (isBonusCodeValid == true)
                    {
                        //check if bonus code is expired
                        $.get('../HttpHandlers/Registration.ashx', { promotioncode: inputValue }, function(data) {
                            if (data == "false") {
                                callback(5);
                                isBonusCodeValid = false;
                            }
                            else {
                                callback(2);
                                isBonusCodeValid = true;
                            }
                        });
                    }
                });
            }
            else {
                callback(2);
                isBonusCodeValid = true;
            }
            
        }
    });
    
    //validate addressline1
    $("#" + txtAddressLine1).inputTip({
        goodText: "",
        badText: "",
        tipText: "",
        validateText: function(inputValue, callback) {
            // Checking if the input field contains text.
            if (inputValue.length > 0) {
                callback(1);
                isAddressLine1Valid = true;
            }
            else {
                callback(3);
                isAddressLine1Valid = false;
            }
        }
    });

    //validate town/city
    $("#" + txtCity).inputTip({
        goodText: "",
        badText: "",
        tipText: "",
        validateText: function(inputValue, callback) {
            // Checking if the input field contains text.
            if (inputValue.length > 0) {
                callback(1);
                isCityValid = true;
            }
            else {
                callback(3);
                isCityValid = false;
            }
        }
    });

    //validate postcode
    $("#" + txtPostCode).inputTip({
        goodText: "",
        badText: "",
        tipText: "",
        validateText: function(inputValue, callback) {
            // Checking if the input field contains text.
            if (inputValue.length > 0) {
                callback(1);
                isPostCodeValid = true;
            }
            else {
                callback(3);
                isPostCodeValid = false;
            }
        }
    });

    //validate country
    $("#" + listCountry).inputTip({
        goodText: "",
        badText: "",
        tipText: "",
        validateText: function(inputValue, callback) {
            // Checking if the input field contains text.
            if (inputValue > -1) {
                callback(1);
                isCountryValid = true;
            }
            else {
                callback(3);
                isCountryValid = false;
            }
        }
    });

    //validate placeofbirth
    $("#" + txtPlaceOfBrith).inputTip({
        goodText: "",
        badText: "",
        tipText: "",
        validateText: function(inputValue, callback) {
            // Checking if the input field contains text.
            if (inputValue.length > 0) {
                callback(1);
                isPlaceOfBrithValid = true;
            }
            else {
                callback(3);
                isPlaceOfBrithValid = false;
            }
        }
    });

    //validate homephone
    $("#" + txtHomePhone).inputTip({
        goodText: "",
        badText: "",
        tipText: "",
        validateText: function(inputValue, callback) {
            // Checking if the input field contains text.
            if (inputValue.length > 0) {
                callback(1);
                isHomePhoneValid = true;
            }
            else {
                callback(3);
                isHomePhoneValid = false;
            }
        }
    });

    //validate currency
    $("#" + listCurrency).inputTip({
        goodText: "",
        badText: "",
        tipText: "",
        validateText: function(inputValue, callback) {
            // Checking if the input field contains text.
            if (inputValue > -1) {
                callback(1);
                isCurrencyValid = true;
            }
            else {
                callback(3);
                isCurrencyValid = false;
            }
        }
    });

    //validate Codice Fiscale
    $("#" + txtCodiceFiscale).inputTip({
        goodText: "",
        badText: "",
        tipText: "",
        validateText: function(inputValue, callback) {
            // Checking if the input field contains text.
            if (inputValue.length > 0) {
                callback(1);
                isCodiceFiscale = true;
            }
            else {
                callback(3);
                isCodiceFiscale = false;
            }
        }
    });

    //validate securitycode
    $("#" + txtCap).inputTip({
        goodText: "",
        badText: "",
        tipText: "",
        validateText: function(inputValue, callback) {
            // Checking if the input field contains text.
            if (inputValue.length > 0) {

                callback(1);
                isSecurityCodeValid = true;
            }
            else {
                callback(3);
                isSecurityCodeValid = false;
            }
        }
    });

    //validate private policy selected
    $("#" + MyPrivacy).inputTip({
        goodText: "",
        badText: "",
        tipText: "",
        validateText: function(inputValue, callback) {
            // Checking if the input field contains text.
            if ($("#" + MyPrivacy)[0].checked == true) {
                callback(1);
                isPrivacyPolicySelected = true;
            }
            else {
                callback(3);
                isPrivacyPolicySelected = false;
            }
        }
    });

    //validate private policy selected
    $("#" + MyTAndC).inputTip({
        goodText: "",
        badText: "",
        tipText: "",
        validateText: function(inputValue, callback) {
            // Checking if the input field contains text.
            if ($("#" + MyTAndC)[0].checked == true) {
                callback(1);
                isTAndCSelected = true;
            }
            else {
                callback(3);
                isTAndCSelected = false;
            }
        }
    });
}
