var a = Array();
var frmadd = document.getElementById('frmaddlink');

function getvalues(){
        if (frmadd.Email.value == ''){
                alert('Please enter email');
                return;
        }
        a = [];
        frmedit = document.getElementById('frmeditlink');
        frmedit._sql.value = "select Partner_ID,FirstName,Url,Title,OptIn from partners where Email='"+frmadd.Email.value+"'";
        frmedit.submit(); // calls link_view.php which calls aftergetvalues()
}
function aftergetvalues(){
        // called by getvalues()
        if (!a[0]){
                frmadd.FirstName.value = 'NOT FOUND';
                frmadd.Partner_ID.value = 0;
                frmadd.Url.value = '';
                frmadd.Title.value = '';
                document.getElementById('btnSubmit').value = " Submit New Link ";
                document.getElementById('btnRemove').style.display = 'none';
                return;
        }
        frmadd.Partner_ID.value = a[0][0];
        frmadd.FirstName.value = a[0][1];
        frmadd.Url.value = a[0][2];
        frmadd.Title.value = a[0][3];
        frmadd.OptIn.value = a[0][4];
        if (frmadd.OptIn.value == 1){
                frmadd.OptIn.checked = true;
        } else {
                frmadd.OptIn.checked = false;
        }

        document.getElementById('btnSubmit').value = " Update Link ";
        document.getElementById('btnRemove').style.display = '';
}
function afteraddsite(){
        if (document.getElementById('btnSubmit').value == " Submit New Link "){
                document.getElementById('btnSubmit').value = " Update Link ";
                alert('Link submitted. Now look for an email from us, and click on the link to activate your website link.');
        } else {
                alert('Thank you. Link updated.');
        }
}
function remove_click(){
        frmadd._action.value = 'd';
        frmadd.action = 'link_data.php'; // avoid robot submission by leaving this blank at first
        frmadd.submit();
        frmadd.Email.value = 'REMOVED';
        frmadd.FirstName.value = '';
        frmadd.Url.value = '';
        frmadd.Title.value = '';
        frmadd.Partner_ID.value = 0;
        document.getElementById('btnEdit').disabled = true;
        document.getElementById('btnSubmit').value = " Submit Link ";
        document.getElementById('btnRemove').style.display = 'none';
}
function optin_click(v){
        if (v.checked){v.value=1;}else{v.value=0;}
}
function verifyfields(){
        if (frmadd.Email.value == ''){
                frmadd.Email.focus();
                alert('Please enter email.');
                return false;
        }
        if (!verifyemail(frmadd.Email.value)){
                alert('Please enter a valid email');
                return false;
        }
        if (frmadd.FirstName.value == ''){
                frmadd.FirstName.focus();
                alert('Please enter your first name.');
                return false;
        }
        if (frmadd.Title.value == ''){
                frmadd.Title.focus();
                alert('Please enter your website title.');
                return false;
        }
        if ((frmadd.Url.value).length < 10){
                frmadd.Url.focus();
                alert('Please enter your website address.');
                return false;
        }
        if ((frmadd.FirstName.value).length > 30){
                frmadd.FirstName.focus();
                alert('First Name is limited to 30 characters. You now have '+(frmadd.FirstName.value).length);
                return false;
        }
        if ((frmadd.Title.value).length > 100){
                frmadd.Title.focus();
                alert('Title is limited to 100 characters. You now have '+(frmadd.Title.value).length);
                return false;
        }
        if ((frmadd.Url.value).length > 100){
                frmadd.Url.focus();
                alert('Website address is limited to 100 characters. You now have '+(frmadd.Url.value).length);
                return false;
        }
        
        frmadd.action = 'link_data.php'; // avoid robot submission by leaving this blank at first
        return true;
}
function verifyurl(){
        u = frmadd.Url.value;
        p = u.indexOf('www.');
        if (p > -1){
                u = 'http://' + u.substring(p);
        } else {
                p = u.indexOf('//');
                if (p == -1) p = u.indexOf(':/');
                if (p == -1) p = -2;
                u = 'http://' + u.substring(p+2);
        }

        p = u.indexOf('/',9);
        if (p>0 && u.length > p){
                if (u.length > p+1){
                        alert('Primary domain addresses only are accepted in order to better track return clicks.')
                }
                u = u.substring(0,p);
        }

        frmadd.Url.value = u;
}
function verifyemail(v){
        p = v.indexOf('@');
        if (p > 0) p = v.indexOf('.', p);
        if (p < 2){
                return false;
        }
        document.getElementById('btnEdit').disabled = false;
        return true;
}
frmadd.Email.focus();
document.getElementById('frmhits').submit();

