document.onload = clearcookie();

var web_server_hostname = location.hostname;


function getEnvironment(hostname) {
    var env = '';
    env = hostname.split('.')[0];
    return env;
}


function clearcookie() {
    var AppID = GetCookieValue('vzAppID');
    /*  SetCookie('LOB_CATEGORY', '', '/', '.verizon.com');*/
    if (AppID != "") {
        SetCookie('vzAppID', '', '/', '.verizon.com');
    }
}

function DeleteCookies()
{
    var arrDeleteList =  new Array();
    var cookieList =sCookiesList;
    try
    {
        if(cookieList!= null && cookieList!=undefined && cookieList!="")
        {
            arrDeleteList=cookieList.split(',');
            for(i=0;i<arrDeleteList.length;i++)
            {
                if(GetCookieValue('dotcomsid') == null)
                {
                     var cookieValue = GetCookieValue(arrDeleteList[i]);
                     if(cookieValue !=null || cookieValue !="" )
                     {
                        Delete_SessionCookie(arrDeleteList[i],'/','.verizon.com'); 
                     }
                 }
             }
        }
     }
     catch(e){}
}

// this deletes the cookie when called
function Delete_SessionCookie( name, path, domain ) 
{
     document.cookie = name + "=" + ( ( path ) ? ";path=" + path : "") + ( ( domain ) ? ";domain=" + domain : "" ) + ";expires=Thu, 01-Jan-1970 00:00:01 GMT"; 
}

function URLEncode (clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') {
    	output += match[1];
      x += match[1].length;
    } else {
      if (clearString[x] == ' ')
        output += '+';
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}

function getQueryVariable(key, default_) {
    if (default_ == null) {
        default_ = "";
    }
    var search = location.search;
    if (search == "") {
        return default_;
    }
    search = search.substr(1);
    var params = search.split("&");
    for (var i = 0; i < params.length; i++) {
        var pairs = params[i].split("=");
        if (pairs[0] == key) {
            return pairs[1];
        }
    }
    return default_;
}


function signinloop(LOB) {
    var uidf = false;
    var pwdf = false;
    var SMUserID = "";
    var SMPassword = "";
    var dossopost = false;

    if (LOB != "") {
        SetCookie('LOB_CATEGORY', LOB, '/', '.verizon.com');
    }


    var ln = window.document.forms.length;
    for (var i = 0; i < ln; i++) {
        var lne = window.document.forms[i].length;
        for (var j = 0; j < lne; j++) {
            var elm = window.document.forms[i].elements[j].name;
            if (elm.toUpperCase() == "USERID") {
                SMUserID = window.document.forms[i].elements[j].value;
                if (window.document.forms[i].elements[j].value.indexOf('$') >= 0) uidf = true;
            }
            else if (elm.toUpperCase() == "PASSWORD") {
                SMPassword = window.document.forms[i].elements[j].value;
                if (window.document.forms[i].elements[j].value.indexOf('$') >= 0) pwdf = true;
            }

            if ((SMUserID != "") && (SMPassword != "") && (dossopost == false)) {
                if (uidf == true) {
                    window.document.forms[i].method = "post";
                    window.document.forms[i].action = "https://" + web_server_hostname + "/ForYourHome/registration/Mreg/MRBridge.aspx";
                    window.document.forms[i].submit();
                    dossopost = true;
                    break;
                }
                else {
                    window.document.forms[i].method = "post";
                    window.document.forms[i].action = "https://" + web_server_hostname + "/sso/common/retaillogin.fcc";
                    window.document.forms[i].submit();
                    dossopost = true;
                    break;
                }
            }
        }
    }

}

function signinloop3(LOB, RedirectTarget) {

    var uidf = false;
    var pwdf = false;
    var SMUserID = "";
    var SMPassword = "";
    var SMTarget = "";
    var dossopost = false;
    var gotquerystring='';
    var env = getEnvironment(web_server_hostname);
    var routername = "https%3A%2F%2Fsignin.verizon.com%2Fsso%2FProcessLoginServlet%3FloginType%3Dconsumer%26target%3Dhttps%3A%2F%2F" + env + ".verizon.com%2Fforyourhome%2FMyAccount%2FProtected%2FCommon%2Fergcon.aspx";
    
    if (LOB != "") {
        SetCookie('LOB_CATEGORY', LOB, '/', '.verizon.com');
    }

    if (RedirectTarget != "") {
        SetCookie('RedirectTarget', RedirectTarget, '/', '.verizon.com');
    }

    
    gotquerystring = unescape(getQueryVariable('goto'));
    if(gotquerystring!='' && gotquerystring.toUpperCase().indexOf('ERGCON.ASPX')==-1){
        gotquerystring = gotquerystring.replace(":443","");
        gotquerystring = "?Target=" +  gotquerystring;
        routername =  routername + URLEncode(URLEncode(gotquerystring));
    }
    else if(gotquerystring!='' && gotquerystring.toUpperCase().indexOf('SIGNIN.VERIZON.COM')> -1 && gotquerystring.toUpperCase().indexOf('ERGCON.ASPX')>-1){
        gotquerystring = gotquerystring.replace("%3A443","");
        routername =  URLEncode(gotquerystring);
    }
    
    var ln = window.document.forms.length;
    for (var i = 0; i < ln; i++) {
        var lne = window.document.forms[i].length;
        for (var j = 0; j < lne; j++) {
            var elm = window.document.forms[i].elements[j].name;
            if (elm.toUpperCase() == "USERID") {
                SMUserID = window.document.forms[i].elements[j].value;
            }
            else if (elm.toUpperCase() == "PASSWORD") {
                SMPassword = window.document.forms[i].elements[j].value;
            }
            else if (elm.toUpperCase() == "TARGET") {
                SMTarget = window.document.forms[i].elements[j].value;
                if (SMTarget != null && SMTarget.toUpperCase().indexOf('/SSO/') >= 0) {
                    SMTarget = SMTarget.replace('/sso/redirect/redirect.asp', 'https://' + web_server_hostname + '/foryourhome/MyAccount/Protected/Common/ergcon.aspx');
                }
                else
                    SMTarget = "https://" + web_server_hostname + "/foryourhome/MyAccount/Protected/Common/ergcon.aspx";
            }
            if(flagRemoveVerizonNet=="Y")
            {
                var tempUserId = SMUserID.toLowerCase();
                if(tempUserId.indexOf('@verizon.net')!=-1)
                    tempUserId = tempUserId.replace('@verizon.net','');
                SMUserID = tempUserId;
            }
            if ((SMUserID != "") && (SMPassword != "") && (SMTarget != "") && (dossopost == false)) {
                window.document.forms[i].elements["IDToken1"].value = SMUserID;
                window.document.forms[i].elements["IDToken2"].value = SMPassword;
                window.document.forms[i].elements["__VIEWSTATE"].value = "";

                dossopost = true;
                window.document.forms[i].method = "post";

                
                
                var RemOptValue = "N";
		var encRemFlag = "";
		if ((document.getElementById("chkRemopt") != null && document.getElementById("chkRemopt").checked == true)||(document.getElementById("chkRemoptPop") != null && document.getElementById("chkRemoptPop").checked == true))
		{
			RemOptValue = "Y";
			if (strKeepMeSignedIn == "Y")
			{
			encRemFlag = "%26RememberMe%3dtrue";
			}
			else
			{
			 encRemFlag = "";
			}
		}
                if (env && env == "www25") {

                    if (window.document.location.href.toLowerCase().indexOf("orlogin.aspx") > -1)
                        window.document.forms[i].action = "http://login.verizon.com:8081/amserver/UI/Login?realm=dotcom&module=LDAP&clientId=myvzorl&goto=" + SMTarget;
                    else if (window.document.location.href.toLowerCase().indexOf("bundleloginalone.aspx") > -1)
                        window.document.forms[i].action = "http://login.verizon.com:8081/amserver/UI/Login?realm=dotcom&module=LDAP&clientId=myvzbla&goto=" + SMTarget;
                    else
                        window.document.forms[i].action = "http://login.verizon.com:8081/amserver/UI/Login?realm=dotcom&module=AIAW&goto="+ routername + encRemFlag +"&clientId=central";

                }
                else if (env && env == "www26") {
                    if (window.document.location.href.toLowerCase().indexOf("orlogin.aspx") > -1)
                        window.document.forms[i].action = "http://login.verizon.com:8081/amserver/UI/Login?realm=dotcom&module=LDAP&clientId=myvzorl&goto=" + SMTarget;
                    else if (window.document.location.href.toLowerCase().indexOf("bundleloginalone.aspx") > -1)
                        window.document.forms[i].action = "http://login.verizon.com:8081/amserver/UI/Login?realm=dotcom&module=LDAP&clientId=myvzbla&goto=" + SMTarget;
                    else
                        window.document.forms[i].action = "http://login.verizon.com:8081/amserver/UI/Login?realm=dotcom&module=AIAW&goto=" + routername + encRemFlag +"&clientId=central";
                }
                else if (env && env == "www98") {
                    if (window.document.location.href.toLowerCase().indexOf("orlogin.aspx") > -1)
                        window.document.forms[i].action = "https://auth.verizon.com/amserver/UI/Login?realm=dotcom&module=LDAP&clientId=myvzorl&goto=" + SMTarget;
                    else if (window.document.location.href.toLowerCase().indexOf("bundleloginalone.aspx") > -1)
                        window.document.forms[i].action = "https://auth.verizon.com/amserver/UI/Login?realm=dotcom&module=LDAP&clientId=myvzbla&goto=" + SMTarget;
                    else
                        window.document.forms[i].action = "https://auth.verizon.com/amserver/UI/Login?realm=dotcom&module=AIAW&clientId=myvz&goto="+ routername + encRemFlag +"&clientId=central";

                }
                else {
                    if (window.document.location.href.toLowerCase().indexOf("orlogin.aspx") > -1)
                        window.document.forms[i].action = "https://auth.verizon.com/amserver/UI/Login?realm=dotcom&module=LDAP&clientId=myvzorl&goto=" + SMTarget;
                    else if (window.document.location.href.toLowerCase().indexOf("bundleloginalone.aspx") > -1)
                        window.document.forms[i].action = "https://auth.verizon.com/amserver/UI/Login?realm=dotcom&module=LDAP&clientId=myvzbla&goto=" + SMTarget;
                    else
                        window.document.forms[i].action = "https://auth.verizon.com/amserver/UI/Login?realm=dotcom&module=AIAW&clientId=myvz&goto="+ routername + encRemFlag +"&clientId=central";

                }

                window.document.forms[i].submit();
                break;
            }
            if (dossopost) break;
        }
    }

}

function signinpopup(LOB, RedirectTarget) {

    var uidf = false;
    var pwdf = false;
    var SMUserID = "";
    var SMPassword = "";
    var SMTarget = "";
    var dossopost = false;
    var gotquerystring = '';
    var env = getEnvironment(web_server_hostname);
    var routername = "https%3A%2F%2Fsignin.verizon.com%2Fsso%2FProcessLoginServlet%3FloginType%3Dconsumer%26target%3Dhttps%3A%2F%2F" + env + ".verizon.com%2Fforyourhome%2FMyAccount%2FProtected%2FCommon%2Fergcon.aspx";
    if (LOB != "") {
        SetCookie('LOB_CATEGORY', LOB, '/', '.verizon.com');
    }
    if (RedirectTarget != "") {
        SetCookie('RedirectTarget', RedirectTarget, '/', '.verizon.com');
    }
    gotquerystring = unescape(getQueryVariable('goto'));
    if (gotquerystring != '' && gotquerystring.toUpperCase().indexOf('ERGCON.ASPX') == -1) {
        gotquerystring = gotquerystring.replace(":443", "");
        gotquerystring = "?Target=" + gotquerystring;
        routername = routername + URLEncode(URLEncode(gotquerystring));
    }
    else if (gotquerystring != '' && gotquerystring.toUpperCase().indexOf('SIGNIN.VERIZON.COM') > -1 && gotquerystring.toUpperCase().indexOf('ERGCON.ASPX') > -1) {
        gotquerystring = gotquerystring.replace("%3A443", "");
        routername = URLEncode(gotquerystring);
    }

    var divElem = document.getElementById('LoginPopup');
    strUserID = document.getElementById('UsId');
    SMUserID = strUserID.value;
    strPassword = document.getElementById('Pswd');
    SMPassword = strPassword.value;
    var lne = window.document.forms["formLogin"].length;
    for (var j = 0; j < lne; j++) {
        var elm = window.document.forms["formLogin"].elements[j].name;
        if (elm.toUpperCase() == "TARGET") {
            SMTarget = window.document.forms["formLogin"].elements[j].value;
            if (SMTarget != null && SMTarget.toUpperCase().indexOf('/SSO/') >= 0)
                SMTarget = SMTarget.replace('/sso/redirect/redirect.asp', 'https://' + web_server_hostname + '/foryourhome/MyAccount/Protected/Common/ergcon.aspx');
            else
                SMTarget = "https://" + web_server_hostname + "/foryourhome/MyAccount/Protected/Common/ergcon.aspx";
        }
    }

    if ((SMUserID != "") && (SMPassword != "") && (SMTarget != "") && (dossopost == false)) {
        window.document.forms["formLogin"].elements["IDToken1"].value = SMUserID;
        window.document.forms["formLogin"].elements["IDToken2"].value = SMPassword;
        window.document.forms["formLogin"].elements["__VIEWSTATE"].value = "";
        dossopost = true;
        window.document.forms["formLogin"].method = "post";
        var RemOptValue = "N";
        var encRemFlag = "";
        if ((document.getElementById("chkRemopt") != null && document.getElementById("chkRemopt").checked == true)||(document.getElementById("chkRemoptPop") != null && document.getElementById("chkRemoptPop").checked == true))
         {
            RemOptValue = "Y";
            if (strKeepMeSignedIn == "Y") {
                encRemFlag = "%26RememberMe%3dtrue";
            }
            else {
                encRemFlag = "";
            }
        }
        if (env && env == "www25") {

            if (window.document.location.href.toLowerCase().indexOf("orlogin.aspx") > -1)
                window.document.forms["formLogin"].action = "http://login.verizon.com:8081/amserver/UI/Login?realm=dotcom&module=LDAP&clientId=myvzorl&goto=" + SMTarget;
            else if (window.document.location.href.toLowerCase().indexOf("bundleloginalone.aspx") > -1)
                window.document.forms["formLogin"].action = "http://login.verizon.com:8081/amserver/UI/Login?realm=dotcom&module=LDAP&clientId=myvzbla&goto=" + SMTarget;
            else
                window.document.forms["formLogin"].action = "http://login.verizon.com:8081/amserver/UI/Login?realm=dotcom&module=AIAW&goto=" + routername + encRemFlag + "&clientId=central";

        }
        else if (env && env == "www26") {
            if (window.document.location.href.toLowerCase().indexOf("orlogin.aspx") > -1)
                window.document.forms["formLogin"].action = "http://login.verizon.com:8081/amserver/UI/Login?realm=dotcom&module=LDAP&clientId=myvzorl&goto=" + SMTarget;
            else if (window.document.location.href.toLowerCase().indexOf("bundleloginalone.aspx") > -1)
                window.document.forms["formLogin"].action = "http://login.verizon.com:8081/amserver/UI/Login?realm=dotcom&module=LDAP&clientId=myvzbla&goto=" + SMTarget;
            else
                window.document.forms["formLogin"].action = "http://login.verizon.com:8081/amserver/UI/Login?realm=dotcom&module=AIAW&goto=" + routername + encRemFlag + "&clientId=central";
        }
        else if (env && env == "www98") {
            if (window.document.location.href.toLowerCase().indexOf("orlogin.aspx") > -1)
                window.document.forms["formLogin"].action = "https://auth.verizon.com/amserver/UI/Login?realm=dotcom&module=LDAP&clientId=myvzorl&goto=" + SMTarget;
            else if (window.document.location.href.toLowerCase().indexOf("bundleloginalone.aspx") > -1)
                window.document.forms["formLogin"].action = "https://auth.verizon.com/amserver/UI/Login?realm=dotcom&module=LDAP&clientId=myvzbla&goto=" + SMTarget;
            else
                window.document.forms["formLogin"].action = "https://auth.verizon.com/amserver/UI/Login?realm=dotcom&module=AIAW&clientId=myvz&goto=" + routername + encRemFlag + "&clientId=central";

        }
        else {
            if (window.document.location.href.toLowerCase().indexOf("orlogin.aspx") > -1)
                window.document.forms["formLogin"].action = "https://auth.verizon.com/amserver/UI/Login?realm=dotcom&module=LDAP&clientId=myvzorl&goto=" + SMTarget;
            else if (window.document.location.href.toLowerCase().indexOf("bundleloginalone.aspx") > -1)
                window.document.forms["formLogin"].action = "https://auth.verizon.com/amserver/UI/Login?realm=dotcom&module=LDAP&clientId=myvzbla&goto=" + SMTarget;
            else
                window.document.forms["formLogin"].action = "https://auth.verizon.com/amserver/UI/Login?realm=dotcom&module=AIAW&clientId=myvz&goto=" + routername + encRemFlag + "&clientId=central";

        }
        window.document.forms["formLogin"].submit();
    }
}

function signinloop4(LOB, RedirectTarget) {
    var uidf = false;
    var pwdf = false;
    var SMUserID = "";
    var SMPassword = "";
    var dossopost = false;

    if (LOB != "") {
        SetCookie('LOB_CATEGORY', LOB, '/', '.verizon.com');
    }

    if (RedirectTarget != "") {
        SetCookie('RedirectTarget', RedirectTarget, '/', '.verizon.com');
    }

    var ln = window.document.forms.length;
    for (var i = 0; i < ln; i++) {
        var lne = window.document.forms[i].length;
        for (var j = 0; j < lne; j++) {
            var elm = window.document.forms[i].elements[j].name;
            if (elm.toUpperCase().indexOf("USERID") > -1) {
                SMUserID = window.document.forms[i].elements[j].value;
            }
            else if (elm.toUpperCase() == "PASSWORD") {
                SMPassword = window.document.forms[i].elements[j].value;
            }

            if ((SMUserID != "") && (SMPassword != "") && (dossopost == false)) {
                window.document.forms[i].method = "post";
                window.document.forms[i].action = "https://" + web_server_hostname + "/sso/common/retaillogin.fcc";
                window.document.forms[i].submit();
                dossopost = true;
                break;
            }

        }
    }

}
function signinloop2(LOB, RedirectTarget) {
    var uidf = false;
    var pwdf = false;
    var SMUserID = "";
    var SMPassword = "";
    var dossopost = false;

    if (LOB != "") {
        SetCookie('LOB_CATEGORY', LOB, '/', '.verizon.com');
    }

    if (RedirectTarget != "") {
        SetCookie('RedirectTarget', RedirectTarget, '/', '.verizon.com');
    }

    var ln = window.document.forms.length;
    for (var i = 0; i < ln; i++) {
        var lne = window.document.forms[i].length;
        for (var j = 0; j < lne; j++) {
            var elm = window.document.forms[i].elements[j].name;
            if (elm.toUpperCase() == "USERID") {
                SMUserID = window.document.forms[i].elements[j].value;
                if (window.document.forms[i].elements[j].value.indexOf('$') >= 0) uidf = true;
            }
            else if (elm.toUpperCase() == "PASSWORD") {
                SMPassword = window.document.forms[i].elements[j].value;
                if (window.document.forms[i].elements[j].value.indexOf('$') >= 0) pwdf = true;
            }

            if ((SMUserID != "") && (SMPassword != "") && (dossopost == false)) {
                if (uidf == true) {
                    window.document.forms[i].method = "post";
                    window.document.forms[i].action = "https://" + web_server_hostname + "/ForYourHome/registration/Mreg/MRBridge.aspx";
                    window.document.forms[i].submit();
                    dossopost = true;
                    break;
                }
                else {
                    window.document.forms[i].method = "post";
                    window.document.forms[i].action = "https://" + web_server_hostname + "/sso/common/retaillogin.fcc";
                    window.document.forms[i].submit();
                    dossopost = true;
                    break;
                }
            }
        }
    }

}


function SetCookie(name, value, path, domain) {
    document.cookie = name + "=" + escape(value) +
        ((path) ? ";path=" + path : "") +
        ((domain) ? ";domain=" + domain : "");
}
function CreateMyVzComCookie(ctrl_txtuid) {
    //reading remember checbox user selected value
    var RemOptValue = "N";
    if ((document.getElementById("chkRemopt") != null && document.getElementById("chkRemopt").checked == true)||(document.getElementById("chkRemoptPop") != null && document.getElementById("chkRemoptPop").checked == true))
        RemOptValue = "Y";
    var uid = "";
    if (document.getElementById(ctrl_txtuid) != null)
        uid = document.getElementById(ctrl_txtuid).value;
    var MyVzIdValue = "";
    if (RemOptValue == "Y")
        MyVzIdValue = "uid=&remopt=" + RemOptValue;
	
    SetMyVzComCookie("MyVzCom", MyVzIdValue, 60, '/', '.verizon.com', false);
    return true;
}
function SetMyVzComCookie(name, value, expires, path, domain, secure) {
try
{
    var today = new Date();
    today.setTime(today.getTime());
    
    //expires = expires * 1000 * 60 * 60 * 24;
	var exdate=new Date();
//	expires=		exdate.setDate(exdate.getDate()+expiredays);
    exdate.setDate(exdate.getDate()+expires); //new Date(exdate.getDate() + expires);


    document.cookie = name + "=" + value +
		((expires != null) ? ";expires=" + exdate.toGMTString() : "") +
        ((path) ? ";path=" + path : "") +
        ((domain) ? ";domain=" + domain : "") +
        ((secure) ? ";secure=" + secure : "");

	
}
catch(e)
{
//alert("error setting MyvzCom cookie");
}

}

function GetCookieValue(name) {
    var start = document.cookie.indexOf(name + "=");
    var len = start + name.length + 1;
    if ((!start) && (name != document.cookie.substring(0, name.length))) return null;
    if (start == -1) return null;
    var end = document.cookie.indexOf(";", len);
    if (end == -1) end = document.cookie.length;
    return unescape(document.cookie.substring(len, end));
}

function forgotpass(target, LOB) {
    if (LOB != "") {
        SetCookie('LOB_CATEGORY', LOB, '/', '.verizon.com');
    }
    document.location.href = "https://" + web_server_hostname + "/sso/common/userid.asp?Target=" + target;
}

function newuserlogin(target, LOB) {
    if (LOB != "") {
        SetCookie('LOB_CATEGORY', LOB, '/', '.verizon.com');
    }
    document.location.href = "https://" + web_server_hostname + "/sso/common/userid.asp?AppID=" + LOB + "&Target=" + target;
}

function resetsmtryno() {
    SetCookie('SMTRYNO', '', '/', '.verizon.com');
}





	
