/*---------------------------------------*/
/*Document Ready*/
$(document).ready(function() {
        document.title = "CenturyLink Business Partner Program";
});

$(document).ready(function()
    {
        $("#details").tablesorter();
    }
  );


/*---------------------------------------*/
/*Logon Page Submit*/
function checkLogin(){
        var valid = 1;

        if ( document.login.userid.length = 0) {
            window.alert('here');
            document.getElementById('reqUser').innerHTML='<font color=red><-- Required field</font>';
            document.login.userid.focus();
            valid = 0;
        }else{
            document.getElementById('reqUser').innerHTML='&nbsp;';
        }

        if ( document.login.password.length = 0) {
            document.getElementById('reqPass').innerHTML='<font color=red><-- Required field</font>';
            document.login.password.focus();
            valid = 0;
        }else{
            document.getElementById('reqPass').innerHTML='&nbsp;';
        }

        if (valid == 1){
                document.login.source_id.value=1;
                document.login.program_id.value='107';
                document.login.staging.value=1;
                document.login.action="/cgi-bin/lp-validate.cgi";
                document.login.submit();
                return true;
        }else{
                return false;
        }

}

/*---------------------------------------*/
/*Change the frame source in the primary space*/

    function changeIframeSrc(id, url, divID) {
        x=document.getElementById(divID);
        x.style.display = "block";
        if (!document.getElementById) return;
            var el = document.getElementById(id);
         if (el && el.src) {
            el.src = url;
            return false;
        }
    return true;
    }

/*---------------------------------------*/
/*Open small window for excel file to download to*/

        function openExcel(URL) {
                        window.open(URL, "option_win","width=550, height=300, left=200, top=120, scrollbars=auto, toolbar=no, menubar=no, status=yes, resizable=yes");
        }

/*---------------------------------------*/
/*toggle vis on a div with single click option*/

function toggleVisibility(item){
    if (document.getElementById(item).style.display=='none'){
        document.getElementById(item).style.display='';
    }
    else{
         document.getElementById(item).style.display='none';
    }
}

/*---------------------------------------*/
/*toggle vis main filter band div in console*/

function filtershow(i) {
	if ( i == 1 ) {
	    document.getElementById('filterband').style.display="block";
	}else if (i == 0){
	    document.getElementById('filterband').style.display="none";
	}
return false;

}

/*---------------------------------------*/
/*check for a radio selected*/

function valButton(btn) {
        var cnt = -1;
        for (var i=btn.length-1; i > -1; i--) {
                if (btn[i].checked) {cnt = i; i = -1;}
        }
        if (cnt > -1) return btn[cnt].value;
        else return null;
}

/*---------------------------------------*/
/*validate basic date format*/

function isDate(s) {
        if (s.search(/^\d{1,2}[\/|\-|\.|_]\d{1,2}[\/|\-|\.|_]\d{2}/g) != 0)
        return 0;

        s = s.replace(/[\-|\.|_]/g, "/");

        var dt = new Date(Date.parse(s));
		var curYear = dt.getFullYear();
 
        var arrDateParts = s.split("/");
        return (
                dt.getMonth() == arrDateParts[0]-1 &&
                dt.getDate() == arrDateParts[1] &&
                (curYear.toString().slice(2)  == arrDateParts[2]  || dt.getFullYear() == arrDateParts[2])

                );

}

/*---------------------------------------*/
/*auto tab for phone fields in any form*/
        var isNN = (navigator.appName.indexOf("Netscape")!=-1);

        function autoTab(input, len, e) {
                var keyCode = (isNN) ? e.which : e.keyCode;
                var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
                if(input.value.length >= len && !containsElement(filter,keyCode)) {
                input.value = input.value.slice(0, len);
                input.form[(getIndex(input)+1) % input.form.length].focus();
        }

        function containsElement(arr, ele) {
                var found = false, index = 0;
                while(!found && index < arr.length)
                if(arr[index] == ele)
                found = true;
                else
                index++;
                return found;
        }

        function getIndex(input) {
                var index = -1, i = 0, found = false;
                while (i < input.form.length && index == -1)
                if (input.form[i] == input)index = i;
                else i++;
                return index;
                }
                return true;
        }


/*---------------------------------------*/
/*Open small light-box style window*/

function openModalSmall(src) {

        $.modal('<iframe src="' + src + '" height="300" width="350" style="border:0">');

                $.modal({

                        autoResize: true

                });

 }
/*---------------------------------------*/
 /*Open medium light-box style window*/

function openModalMedium(src) {

        $.modal('<iframe src="' + src + '" height="450" width="650" style="border:0">');

                $.modal({

                        autoResize: true

                });

 }


/*---------------------------------------*/
/*Open large light-box style window*/

function openModalLarge(src) {

        $.modal('<iframe src="' + src + '" height="600" width="800" style="border:0">');

                $.modal({

                        autoResize: true

                });

 }


/*---------------------------------------*/
/*Forgot Password Submit*/

 function forgotPassword(){
   document.forgot.task.value=1;
   document.forgot.submit();
   return true;

}

/*---------------------------------------*/
/*Validate form for changing logon from profile screen*/

 function checkLogonChange(){
    document.frm.redir.value = 1;
    var pattern=/[0-9]/i; // must contain at least one number
    var illegalChars = /[\W]/; // allow only letters and numbers

	function acceptDigits(objtextbox)
	{	var exp = /[^\\d]/g;
		objtextbox.value = objtextbox.value.replace(exp,'');
	}

                if (pattern.test(document.frm.newpid.value) == false && document.frm.randomized.value != 1){ //returns true
                    document.frm.redir.value = 0;
                    window.alert('The password you entered does not contain at least one number.');
                    document.frm.newpid.focus();
                }else if (document.frm.newpid.value.length < 5) {
                    document.frm.redir.value = 0;
                    window.alert('The password must be at least 5 characters long and contain at least 1 number.');
                    document.frm.newpid.focus();
                //}else if (illegalChars.test(document.frm.newpid.value)) {
                //     document.frm.redir.value = 0;
                //    window.alert('The password contains illegal characters.');
                //    document.frm.newpid.focus();
                }else if ( document.frm.oldpid.value == "" ) {
                    document.frm.redir.value = 0;
                    window.alert('Old password is required.');
                    document.frm.oldpid.focus();
                }else if ( document.frm.randomized.value == "" && document.frm.newpid.value == "" ) {
                    document.frm.redir.value = 0;
                    window.alert('Please enter your new password');
                    document.frm.newpid.focus();
                }else if ( document.frm.randomized.value == "" && document.frm.newpid_confirm.value == "" ) {
                    document.frm.redir.value = 0;
                    window.alert('You must retype your new password for confirmation.');
                    document.frm.newpid_confirm.focus();
                }else if ( document.frm.randomized.value == "" && document.frm.newpid_confirm.value != document.frm.newpid.value ) {
                    document.frm.redir.value = 0;
                    window.alert('The value you entered as your new password does not match the confirmation.  This step is required to ensure you did not make a typo on your initial entry.');
                    document.frm.newpid_confirm.focus();
                }

                if ( document.frm.redir.value == 0 ) {
                    return false;
                }
                else  {
                    document.frm.submit();
                   // window.alert('This submission does not do anything yet');
                    return true;
                }
 }

/*---------------------------------------*/
 /*Validate form for updating profile from profile screen*/

 function checkProfile(){
    document.frm.redir.value = 2;
    if ( document.frm.fn.value == "" ) {
                    document.frm.redir.value = 0;
                    window.alert('A full name is required.');
                    document.frm.fn.focus();
    }else if( document.frm.ln.value == "" )  {
                    document.frm.redir.value = 0;
                    window.alert('A full name is required.');
                    document.frm.ln.focus();
    }else if( document.frm.em.value == "" )  {
                    document.frm.redir.value = 0;
                    window.alert('An email is required as your username to logon, and for contact.');
                    document.frm.em.focus();
    }else if( document.frm.ad1.value == "" )  {
                    document.frm.redir.value = 0;
                    window.alert('A complete mailing address is required for AMEX awards.');
                    document.frm.ad1.focus();
    }else if( document.frm.ct.value == "" )  {
                    document.frm.redir.value = 0;
                    window.alert('A complete mailing address is required for AMEX awards.');
                    document.frm.ct.focus();
    }else if( document.frm.st.value == "" )  {
                    document.frm.redir.value = 0;
                    window.alert('A complete mailing address is required for AMEX awards.');
                    document.frm.st.focus();
    }else if( document.frm.zp.value == "" )  {
                    document.frm.redir.value = 0;
                    window.alert('A complete mailing address is required for AMEX awards.');
                    document.frm.zp.focus();
    } else if(  document.frm.zp.value.length < 5 || document.frm.zp.value.length > 9 )  {
                    document.frm.redir.value = 0;
                    window.alert('Please enter a 5 to 9 digit zip code.');
                    document.frm.zp.focus();
    }
    if ( document.frm.redir.value == 0 ) {
                    return false;
                }
                else  {
                    document.frm.submit();
                //    window.alert('This submission does not do anything yet');
                    return true;
                }
 }


/*---------------------------------------*/
/*----LEAD FORM SUBMISSION FUNCTIONS-----*/


     function smallBizShow(i) {
        var val = i.options[i.selectedIndex].value;
        if ( val == "1499-" ) {
            document.getElementById('sbg_div').style.display="block";
        }else{
            document.getElementById('sbg_div').style.display="none";
            document.isrdetail.cust_budget.value="1500+";
            document.isrdetail.winback_credit.value=0;
        }
        return false;

      }

     function smallBizShowVAR(i, p) {
        var val = i.options[i.selectedIndex].value;

        if ( val == "1499-" ) {
            if ( p < 30 ) {
                if ( document.isrdetail.var_eligible.value > 0.0 ) {
                    document.getElementById('sbg_var_div').style.display="block";
                }
            }else{
                document.getElementById('sbg_var_div').style.display="block";
            }

        }else{
            document.getElementById('sbg_var_div').style.display="none";
            document.isrdetail.cust_budget.value="1500+";
            document.isrdetail.winback_credit.value=0;
            document.isrdetail.bcustom1.value=0;
        }
        return false;

      }

        function updateprods(i) {
                if ( i > 0 ) {
                document.isrdetail.prodvalidate.value = (document.isrdetail.prodvalidate.value)*1 + 1;
                }else{
                document.isrdetail.prodvalidate.value = (document.isrdetail.prodvalidate.value)*1 - 1;
                }
        }

     function checkform() {
                document.isrdetail.redir.value = 1;
                if ( document.isrdetail.cust_budget.value == "0" ) {
                    document.isrdetail.redir.value = 0;
                    window.alert('Please answer the question on the customer Telecom Spending Budget');
                    document.isrdetail.cust_budget.focus();

                }else if ( document.isrdetail.contact_name.value == "" ) {
                    document.isrdetail.redir.value = 0;
                    window.alert('Please enter the name of the person to contact');
                    document.isrdetail.contact_name.focus();

                }else if (document.isrdetail.contact_phone.value == "" ){
                    document.isrdetail.redir.value = 0;
                    window.alert('Please provide the direct phone number of the person to contact');
                    document.isrdetail.contact_phone.focus();

                }else if (document.isrdetail.company_name.value == "" ) {
                    document.isrdetail.redir.value = 0;
                    window.alert('Please enter a customer business name name');
                    document.isrdetail.company_name.focus();

                }else if (document.isrdetail.main_btn1.value == "" || document.isrdetail.main_btn2.value == "" || document.isrdetail.main_btn3.value == "" ){
                    document.isrdetail.redir.value = 0;
                    window.alert('Please enter main BTN (phone number)');
                    document.isrdetail.main_btn1.focus();

                }else if (document.isrdetail.service_address1.value == "" ){
                    document.isrdetail.redir.value = 0;
                    window.alert('Please provide the service address');
                    document.isrdetail.service_address1.focus();

                }else if (document.isrdetail.service_city.value == "" ){
                    document.isrdetail.redir.value = 0;
                    window.alert('Please city of the service');
                    document.isrdetail.service_city.focus();

                }else if (document.isrdetail.service_state.value == "0" ){
                    document.isrdetail.redir.value = 0;
                    window.alert('Please select the appropriate state for the service');

                }else if (document.isrdetail.service_zip.value == "" ){
                    document.isrdetail.redir.value = 0;
                    window.alert('Please a zip/postal code for the service address');
                    document.isrdetail.service_zip.focus();

                }else if (document.isrdetail.best_time.value == "" ){
                    document.isrdetail.redir.value = 0;
                    window.alert('Please provide the best time to call the customer');
                    document.isrdetail.best_time.focus();

                }else if (document.isrdetail.prodvalidate.value == 0 ){
                    document.isrdetail.redir.value = 0;
                    window.alert('Please select at least one product');
               }
               var win_back = -1;
                for (var i=document.isrdetail.winback.length-1; i > -1; i--) {
                    if (document.isrdetail.winback[i].checked) {
                    win_back = i;
                    }
                }
                if (win_back == -1) {
                    window.alert("You must select the winback option.");
                    document.isrdetail.redir.value = 0;
                }

               var new_svc = -1;
                for (var i=document.isrdetail.new_service.length-1; i > -1; i--) {
                    if (document.isrdetail.new_service[i].checked) {
                    new_svc = i;
                    }
                }
                if (new_svc == -1) {
                    window.alert("You must indicate if this is new Qwest service for this customer.");
                    document.isrdetail.redir.value = 0;
                }



                if ( document.isrdetail.redir.value == 0 ) {
                    return false;
                }
                else  {
                    document.isrdetail.submit();
                    return true;
                }
    }

/*-------------------------------------------*/
/*----QWIK CASH FORM SUBMISSION FUNCTIONS-----*/


    function saveclaim() {
       document.claim.redir.value = 1;
       var accountTypeButton = valButton(document.claim.account_type);
       var channelButton = valButton(document.claim.channel);
       var serviceButton = valButton(document.claim.service_type);

       if ( document.claim.customer_name.value == "" ) {
        document.claim.redir.value=0;
        window.alert('Please enter the name of the customer.');
        document.claim.customer_name.focus();
       }
       else if (document.claim.account_number.value == "") {
        document.claim.redir.value=0;
        window.alert('Please enter the account number.');
        document.claim.account_number.focus();
       }
       else if (document.claim.account_type.value == "") {
        document.claim.redir.value=0;
        window.alert('Please enter the account number.');
        document.claim.account_number.focus();
       }
       else if (accountTypeButton == null) {
        document.claim.redir.value=0;
        window.alert('Please select the account type.');
        document.claim.account_number.focus();
       }
       else if (document.claim.invoice_number.value == "") {
        document.claim.redir.value=0;
        window.alert('Please enter the invoice number.');
        document.claim.invoice_number.focus();
       }
       else if (document.claim.order_date.value == "" || isDate(document.claim.order_date.value) == false) {
        document.claim.redir.value=0;
        window.alert('Please enter the order date in mm/dd/yy format.');
        document.claim.order_date.focus();
       }
       else if (document.claim.mrc_value.value == "" || isNaN(document.claim.mrc_value.value)) {
        document.claim.redir.value=0;
        window.alert('Please enter the mrc value with numbers only.');
        document.claim.mrc_value.focus();
       }
       else if (document.claim.term_legnth.value == ""|| isNaN(document.claim.term_legnth.value)) {
        document.claim.redir.value=0;
        window.alert('Please enter the term length of the sale in the number of months.');
        document.claim.term_legnth.focus();
       }
       else if (channelButton == null) {
        document.claim.redir.value=0;
        window.alert('Please indicate if this is channel integration.');
        document.claim.term_legnth.focus();
       }
       else if (serviceButton == null) {
        document.claim.redir.value=0;
        window.alert('Please choose the service type.');
        document.claim.locations.focus();
       }
       else if (document.claim.locations.value=="" || isNaN(document.claim.locations.value)){
          document.claim.redir.value=0;
          window.alert('Please enter a number only in the locations field.');
       }
       else if (document.claim.sale_date.value == "" && isDate(document.claim.sale_date.value) == false) {
        document.claim.redir.value=0;
        window.alert('Please enter the sale date in mm/dd/yy format.');
        document.claim.sale_date.focus();
       }
       else if (document.claim.install_date.value == "" && isDate(document.claim.install_date.value) == false ) {
        document.claim.redir.value=0;
        window.alert('Please enter the estimated install date in mm/dd/yy format.');
        document.claim.install_date.focus();
       }
       else if (document.claim.salesrep_name.value == "") {
        document.claim.redir.value=0;
        window.alert('Please the name of the sales rep on the sale.');
        document.claim.salesrep_name.focus();
       }
       if ( document.claim.redir.value == 0 ) {
            return false;
       }else {
            document.claim.submit();
            return true;
       }

    }

/*-------------------------------------------*/
/*----QWIK PAY FORM SUBMISSION FUNCTIONS-----*/


     function updateSBGprods(){
        var qty = 0;
        var pay = 0;
        for ( t=0;t<document.claim.products.value;t++ ) {
            x = eval("document.claim.comm"+t);
            y = eval("document.claim.qty"+t);
            pay = parseFloat((pay) + (x.value*y.value));

        }
        x = eval("pay");
        document.claim.comm.value = x;

        return true;

    }

    function saveSBGclaim() {
       document.claim.redir.value = 1;

	   if (document.claim.comm.value == 0){
		   document.claim.redir.value=0;
		   window.alert('Make sure you have the qty of products so your commission can be properly calculated.');
	   }
       else if ( document.claim.customer_name.value == "" ) {
        document.claim.redir.value=0;
        window.alert('Please enter the name of the customer.');
        document.claim.customer_name.focus();
       }
       else if (document.claim.btn.value == "") {
        document.claim.redir.value=0;
        window.alert('Please enter the BTN.');
        document.claim.btn.focus();
       }
       else if (document.claim.account_number.value == "") {
        document.claim.redir.value=0;
        window.alert('Please enter the account number.');
        document.claim.account_number.focus();
       }
       else if (document.claim.invoice_number.value == "") {
        document.claim.redir.value=0;
        window.alert('Please enter the invoice number.');
        document.claim.invoice_number.focus();
       }
       else if (document.claim.oms.value == "") {
        document.claim.redir.value=0;
        window.alert('Please enter the OMS number.');
        document.claim.oms.focus();
       }
       else if (document.claim.order_date.value == "" || isDate(document.claim.order_date.value) == 0) {
        document.claim.redir.value=0;
        window.alert('Please enter the order date in mm/dd/yy format.');
        document.claim.order_date.focus();
       }
       else if (document.claim.mrc_value.value == "" || isNaN(document.claim.mrc_value.value)) {
        document.claim.redir.value=0;
        window.alert('Please enter the mrc value with numbers only.');
        document.claim.mrc_value.focus();
       }
       else if (document.claim.nrc_value.value == "" || isNaN(document.claim.nrc_value.value)) {
        document.claim.redir.value=0;
        window.alert('Please enter the NRC value with numbers only.');
        document.claim.mrc_value.focus();
       }
       else if (document.claim.sale_date.value == "" || isDate(document.claim.sale_date.value) == 0) {
        document.claim.redir.value=0;
        window.alert('Please enter the sale date in mm/dd/yy format.');
        document.claim.sale_date.focus();
       }
       else if (document.claim.install_date.value == "" || isDate(document.claim.install_date.value) == 0 ) {
        document.claim.redir.value=0;
        window.alert('Please enter the estimated install date in mm/dd/yy format.');
        document.claim.install_date.focus();
       }
       else if (document.claim.salesrep_name.value == "") {
        document.claim.redir.value=0;
        window.alert('Please the name of the sales rep on the sale.');
        document.claim.salesrep_name.focus();
       }
       if ( document.claim.redir.value == 0 ) {
            return false;
       }else {
            document.claim.submit();
            return true;
       }

    }


