﻿function Check()
{
    var username = $("loginname").value;
    var accountType = $("accountType").value;
    var country = $("countrySelector").value;
    var contactName = $("contactName").value;
    var address = $("street").value;
    var state = $("state").value;
    var zip = $("zip").value;
    var email = $("email").value;
    var companyName = $("companyName").value;
    var companyUrl = $("siteUrl").value;
    
    if (companyName.length > 500)
    {
        alert("Company Name is too long");
        $("siteUrl").focus();
    }
    if (TrimWhiteSpaces(companyName) != "")
    {
        if (!IsAccount(companyName))
        {
            alert("Invalid Name");
            $("companyName").focus();
            return false;
        }
    }
    
    if (companyUrl.length > 500)
    {
        alert("Company Url is too long");
        $("siteUrl").focus();
    }
        
    if (TrimWhiteSpaces(companyUrl) != "")
    {
        if (!isUrl("http://"+ companyUrl) && !isUrl(companyUrl))
        {
            alert("Invalid URL");
            $("siteUrl").focus();
            return false;
        }
    }
    
    if (username.length > 20)
    {
        alert("username should be less than 20");
        $("loginname").focus();
    }
    if (TrimWhiteSpaces(username) == "")
    {
        alert("LoginName should not be empty!");
        $("loginname").focus();
        return false;
    }
    if (TrimWhiteSpaces(username) != "")
    {
        if (!IsAccount(username))
        {
            alert("Invalid LoginName");
            $("loginname").focus();
            return false;
        }
    }
    
    if (TrimWhiteSpaces(country) == "")
    {
        alert("Please Choose your Country or territory!");
        $("countrySelector").focus();
        return false;
    }
    
    if (accountType == "")
    {
        alert("Please Choose your site accountType");
        $("accountType").focus();
        return false;
    }
    
    if (contactName.length > 50)
    {
        alert("ContactName is too long");
        $("contactName").focus();
    }
    if (TrimWhiteSpaces(contactName) == "")
    {
        alert("ContactName can not be empty!");
        $("contactName").focus();
        return false;
    }
    if (TrimWhiteSpaces(contactName) != "")
    {
        if (!IsAccount(contactName))
        {
            alert("Invalid ContactName");
            $("contactName").focus();
            return false;
        }
    }

    if (address.length > 200)
    {
        alert("Address is too long");
        $("street").focus();
    }
    if (TrimWhiteSpaces(address) != "")
    {
        if (!IsAccount(address))
        {
            alert("Invalid Address!");
            $("street").focus();
            return false;
        }
    }
    
    if (state.length > 100)
    {
        alert("State is too long");
        $("state").focus();
    } 
    if (TrimWhiteSpaces(state) != "")
    {
        if (!IsAccount(state))
        {
            alert("Invalid State Name");
            $("state").focus();
            return false;
        }
    }
    
    if (TrimWhiteSpaces(zip) == "")
    {
        alert("Zip code can not be empty");
        $("zip").focus;
        return false;
    }
    if (TrimWhiteSpaces(zip) != "")
    {
        if (!isZip(zip))
        {
            alert("Invalid Zip code");
            $("zip").focus;
            return false;
        }
    }
    
    if (email == "")
    {
        alert("Email should not be empty");
        $("email").focus();
        return false;
    }
    if (email != "")
    {
        if (!IsEmail(email))
        {
            alert("Invalid Email address!");
            $("email").focus();
            return false;
        }
    }
    return true;
}
function checkPwd()
{
    var pwd =  $("txtPwd").value;
    var conPwd = $("txtConfirmPwd").value;
    var pwdans = $("txtPAnswer").value;
    if (pwd == "" || TrimWhiteSpaces(pwd) == "")
    {
        alert("Password is empty");
        $("txtPwd").focus;
        return false;
    }
    if (conPwd == "" || TrimWhiteSpaces(conPwd) == "")
    {
        alert("ConfirmPassword is empty");
        $("txtConfirmPwd").focus;
        return false;
    }
    if (TrimWhiteSpaces(pwd) != TrimWhiteSpaces(conPwd))
    {
        alert("ConfirmPassword is defferent from Password");
        $("txtConfirmPwd").focus;
        return false;
    }
    
    if (TrimWhiteSpaces(pwdans) == "")
    {
        alert("Your answeris empty");
        $("txtPAnswer").focus();
        return false;
    }
    if (TrimWhiteSpaces(pwdans) != "")
    {
        if (!IsAccount(pwdans))
        {
            alert("Invalid Answer!");
            $("txtPAnswer").focus();
            return false;
        }
    }
    return true;
}

function fillData()
{
    if (!Check())
        return false;
    $("lblUrl").innerText = $("siteUrl").value;
    if ($("companyName") != null)
        $("lblCompanyName").innerText = $("companyName").value;
    $("lblAccType").innerText = $("accountType").value;
    $("lblCountry").innerText = $("countrySelector").value;
    $("lblContName").innerText = $("contactName").value;
    $("lblAddress").innerText = $("street").value;
    $("lblState").innerText = $("state").value;
    $("lblZip").innerText = $("zip").value;
    $("lblEmail").innerText = $("email").value;
    $("lblContactCountry").innerText = $("countrySelector").value;
    $("reg").style.display = "none";
    $("lblLoginName").innerText = $("loginname").value;
    $("confirm").style.display = "block";
}

function goBack()
{
    $("confirm").style.display = "none";
    $("pwd").style.display = "none";
    $("reg").style.display = "block";
    $("sure").style.display = "block";
    $("btnSubmit").style.display = "none";
}

function showPwd()
{
    $("sure").style.display = "none";
    $("btnSubmit").style.display = "block";
    $("pwd").style.display = "block";
    $("txtPwd").focus();
}
function IsEmail(obj)
{
    var temp = TrimWhiteSpaces(obj);
    //var patrn = /^[-!#$%&'*+\./0-9=?A-Z^_`a-z{|}~]+@[-!#$%&'*+\./0-9=?A-Z^_`a-z{|}~]+(.[-!#$%&'*+\./0-9=?A-Z^_`a-z{|}~]+)+$/;
    var patrn = /^[\w.-]+@([0-9a-z][\w-]+\.)+[a-z]{2,3}$/i;
    //var patrn = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
    return patrn.exec(temp);
    //return patrn.test(temp);
}

function isUrl(obj)
{
    var temp = TrimWhiteSpaces(obj);
    //var patrn = /^http[s]*:\/\/([\w-]+\.?)+[\w-]+(\/[\w- .\/?%&=]*)?$/;
    // var patrn = /^http[s]*:\/\/(([\w-]+\.?)+[\w-]+(\/[\w- .\/?%&=]*)?)(|:\d{1,5})$/;
    var patrn1 = /[a-zA-Z0-9]/;
    if (!patrn1.exec(temp.substring(temp.length - 1, temp.length)))
    return false;
    else
    {
    var patrn = /^http[s]*:\/\/(([\w-]+\.?)+[\w-]+(\/[\w- .\/?%&=]*)?)(|:(\d{1,5})+(([\w-]+\.?)+[\w-]+(\/[\w- .\/?%&=]*)?))$/;
  

    return patrn.exec(temp);
    }
}

function IsAccount(obj)
{
    var temp = TrimWhiteSpaces(obj);
    var patrn = /^[a-zA-Z][a-zA-Z0-9_]{0,19}$/;
    return patrn.exec(temp);
}

function isZip(obj)
{
    var temp = TrimWhiteSpaces(obj);
    var patrn = /^\d{5}(-\d{4})?$/;
    return patrn.exec(temp);
}

function isPhone(obj)
{
    var temp = TrimWhiteSpaces(obj);
    var patrn = /^((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}$/;
    return patrn.exec(temp);
}

function TrimWhiteSpaces(str) 
{
    var start;
    var end;
    str = str.toString();
    var len = str.length;
    for (start = 0; start < len; start ++)
    {
        ch = str.charAt(start);
        if (ch!=' ' && ch!='\t' && ch!='\n' && ch!='\r' && ch!='\f')
            break;
    }
    if (start == len)
        return "";
    for (end = len - 1; end > start; end --)
    {
        ch = str.charAt(end);
        if (ch!=' ' && ch!='\t' && ch!='\n' && ch!='\r' && ch!='\f')
            break;
    }
    end ++;
    return str.substring(start, end);
}