String.prototype.isWithin = function(min, max) { return (this != null && this != undefined && this.length >= min && this.length <= max); };

var prefix = 'ctl00_ctl00_Main_SubMain_';

// used in Registration3.aspx
var wasCleared = false;

function disableEnterKey(e)
{
     var key;

     if(window.event)
          key = window.event.keyCode;     //IE
     else
          key = e.which;     //firefox

     if(key == 13)
          return false;
     else
          return true;
}

function validate(page)
{
    switch (page)
    {
        case 1:
            valReg1();
            break;
        case 2:
            valReg2();
            break;
        case 3:
            valReg3();
            break;
        case 4:
            valReg4();
            break;
        default:
            break;
    }    
}


function valReg1()
{
    var allElementList = document.forms[0].elements;//If you have more than one form element you must iterate it also
    var l = allElementList.length;
    for( var i = 0; i < l; i++){
        var element = allElementList[i];//element is taken
    }

    var email = document.getElementById(prefix + 'email').value;
    var password1 = document.getElementById('password1').value;
    var password2 = document.getElementById('password2').value;
    var first = document.getElementById(prefix + 'first').value;
    var last = document.getElementById(prefix + 'last').value;

    var results = new Array();

    
    isEmail(email) ? results['email'] = '' : results['email'] = 'Invalid e-mail address';
    
    if (password1 != password2)
    {
        results['password1'] = 'Passwords must match';
        results['password2']  = ' '; // this makes things work, not sure why but it does.
    }
    else 
    {
        password1.isWithin(6, 16) && password2.isWithin(6, 16) ? results['password1'] = results['password2'] = '' : results['password1'] =  'Password must be at least 6 characters and less than 16'; 
        results['password2']  = ' '; // this makes things work, not sure why but it does.
        
        if (results['password1'].length === 0)
        {
            var regex = new RegExp(/(\s)/);
            if (regex.test(password1) || regex.test(password2))
            {
                results['password1'] = 'Passwords cannot contain spaces';
            }
            else
            {
                results['password1'] = results['password2']  = '' 
            }
        }
    }
    first.isWithin(1, 32) ? results['first'] = '' : results['first'] = 'Must not be blank';
    last.isWithin(1, 32) ? results['last'] = '' : results['last'] = 'Must not be blank';
    submit_purple(results);
}


function valReg2()
{
    var submit = true;

    var noInstitutionIsChecked = document.getElementById(prefix + 'noInstitution').checked;
    var institutionWasSelected = document.getElementById(prefix + 'universityDisplay').innerHTML.trim().match("No school") == null;


    // "no institute" checked, but school selected
    var both = noInstitutionIsChecked && institutionWasSelected;
    // "no insitute" not checked, but no school selected
    var  neither = !noInstitutionIsChecked && !institutionWasSelected;

//    alert(neither + ' ' + both);
    if (neither)
    {
        alert('Please select a school/institution or check "I am not affiliated with an institution" to continue.');
        submit = false;
    }
    if (both)
    {
        alert('Please clear the school/institution or uncheck "I am not affiliated with an institution" to continue.');
        submit = false;
    }

    if (submit)
    {
        document.forms[0].submit();
    }
}


function valReg3()
{
    //var prefix = 'ctl00_ctl00_Main_SubMain_';
    
    var billToInstitution = document.getElementById(prefix + 'billToInstitution').checked;
    var billingName = document.getElementById(prefix + 'billingName').value;
    var billingAddress1 = document.getElementById(prefix + 'billingAddress1').value;
    var billingAddress2 = document.getElementById(prefix + 'billingAddress2').value;
    var billingCity = document.getElementById(prefix + 'billingCity').value;
    var billingState = document.getElementById(prefix + 'billingState').value;
    var billingRegion = document.getElementById(prefix + 'billingRegion').value;
    var billingZip = document.getElementById(prefix + 'billingZip').value;
    var billingCountry = document.getElementById(prefix + 'billingCountry').value;
    var phone = document.getElementById(prefix + 'phone').value;
  
    var hiddenInstitutionID = document.getElementById('hiddenInstitutionID').value;
    
    if (hiddenInstitutionID === '-1' && (document.getElementById(prefix + 'billingName').disabled))
    {
        alert('Please enter an address. You previously removed your institution.');
        document.getElementById(prefix + 'billToInstitution').disabled = false;
        document.getElementById(prefix + 'billToInstitution').checked = false;
        document.getElementById(prefix + 'billToInstitution').disabled = true;
        
        document.getElementById(prefix + 'billingName').disabled = false;
        document.getElementById(prefix + 'billingAddress1').disabled = false;
        document.getElementById(prefix + 'billingAddress2').disabled = false;
        document.getElementById(prefix + 'billingCity').disabled = false;
        document.getElementById(prefix + 'billingState').disabled = false;
        document.getElementById(prefix + 'billingZip').disabled = false;
        document.getElementById(prefix + 'billingCountry').disabled = false;

        return;
    }

    var results = new Array();
    var currDate = new Date();

    if (!billToInstitution)
    {
        billingName.isWithin(1, 64) ? results['billingName'] = '' : results['billingName'] = 'Must not be blank';
        billingAddress1.isWithin(1, 64) ? results['billingAddress1'] = '' : results['billingAddress1'] = 'Must not be blank';
        billingCity.isWithin(1, 64) ? results['billingCity'] = '' : results['billingCity'] = 'Must not be blank';
        
        if ( billingCountry == 'USA' ) {
			billingState.isWithin(1, 64) ? results['billingState'] = '' : results['billingState'] = 'Must not be blank';
        } else {
			billingRegion.isWithin(1, 64) ? results['billingState'] = '' : results['billingState'] = 'Must not be blank';
        }
        
        
        !(billingCountry === 'USA' && !isZipCode(billingZip)) ? results['billingZip'] = '' : results['billingZip'] = 'Must be a valid zip code';
    }
 
    isPhone(phone) ? results['phone'] = '' : results['phone'] = 'Must not be blank and include area code';
 
    submit_purple(results);
}

function valReg4()
{
    //var prefix = 'ctl00_ctl00_Main_SubMain_';

    var shipToInstitution = document.getElementById(prefix + 'shipToInstitution').checked;
    var shipToBilling = document.getElementById(prefix + 'shipToBilling').checked;

    var shippingName = document.getElementById(prefix + 'shippingName').value;
    var shippingAddress1 = document.getElementById(prefix + 'shippingAddress1').value;
    var shippingAddress2 = document.getElementById(prefix + 'shippingAddress2').value;
    var shippingCity = document.getElementById(prefix + 'shippingCity').value;
    var shippingState = document.getElementById(prefix + 'shippingState').value;    
    var shippingRegion = document.getElementById(prefix + 'shippingRegion').value;
    var shippingZip = document.getElementById(prefix + 'shippingZip').value;
    var shippingCountry = document.getElementById(prefix + 'shippingCountry').value;

    var hiddenInstitutionID = document.getElementById('hiddenInstitutionID').value;

    if (hiddenInstitutionID === '-1' && shipToInstitution)
    {
        alert('Please enter a shipping address or check "Ship to Billing". You previously removed your institution.');
        document.getElementById(prefix + 'shipToInstitution').disabled = false;
        document.getElementById(prefix + 'shipToInstitution').checked = false;
        document.getElementById(prefix + 'shipToInstitution').disabled = true;

        document.getElementById(prefix + 'shippingName').disabled = false;
        document.getElementById(prefix + 'shippingAddress1').disabled = false;
        document.getElementById(prefix + 'shippingAddress2').disabled = false;
        document.getElementById(prefix + 'shippingCity').disabled = false;
        document.getElementById(prefix + 'shippingState').disabled = false;
        document.getElementById(prefix + 'shippingZip').disabled = false;
        document.getElementById(prefix + 'shippingCountry').disabled = false;

        return;
    }
    
    if (shipToInstitution || shipToBilling)
    {
        document.forms[0].submit();
        return false;
    }


    var results = new Array();

    shippingName.isWithin(1, 64) ? results['shippingName'] = '' : results['shippingName'] = 'Must not be blank';
    shippingAddress1.isWithin(1, 64) ? results['shippingAddress1'] = '' : results['shippingAddress1'] = 'Must not be blank';
    shippingCity.isWithin(1, 64) ? results['shippingCity'] = '' : results['shippingCity'] = 'Must not be blank';
    
	if ( shippingCountry == 'USA' ) {
		shippingState.isWithin(1, 64) ? results['shippingState'] = '' : results['shippingState'] = 'Must not be blank';
    } else {
		shippingRegion.isWithin(1, 64) ? results['shippingState'] = '' : results['shippingState'] = 'Must not be blank';
    }

    !(shippingCountry === 'USA' && !isZipCode(shippingZip)) ? results['shippingZip'] = '' : results['shippingZip'] = 'Must be a valid zip code';

    submit_purple(results);
}

function submit_purple(ary)
{
    var submit = true;
    //var tab = '&nbsp;&nbsp;&nbsp;&nbsp;';

    for (var i in ary) {
        // get the text between the anchors
//        var text = document.getElementById(i + 'Div').innerHTML;
//        var start = text.search(/<a>/i);
//        var end = text.search(/<\/a>/i);
//        text = text.substring(start + 3, end);
        
        var internalprefix = '';
        if (i === 'email') 
        {    
            internalprefix = prefix;
        }
        
        // if there's an error, set the error and set text red 
        if (ary[i].length > 0)
        {
//            document.getElementById(i + 'Div').innerHTML = '<font color="#FF0000"><a>' + text +'</a></font>';
            document.getElementById(internalprefix + i + 'Error').innerHTML = ary[i];
            document.getElementById(internalprefix + i + 'Error').className = 'error';
            document.getElementById(internalprefix + i + 'Error').style.display = 'block';
            submit = false;
        }
        // otherwise, clear the error and set text black
        else
        {
//            document.getElementById(i + 'Div').innerHTML = '<font color="#660099"><a>' + text +'</a></font>';
            document.getElementById(internalprefix + i + 'Error').innerHTML = '';
            document.getElementById(internalprefix + i + 'Error').className = '';
            document.getElementById(internalprefix + i + 'Error').style.display = 'none';
        }
    }
         
    if (submit)
    {
        document.forms[0].submit();
    }
}

function submit(ary)
{
    var submit = true;
    //var tab = '&nbsp;&nbsp;&nbsp;&nbsp;';

    for (var i in ary) {
        // get the text between the anchors
        var text = document.getElementById(i + 'Div').innerHTML;
        var start = text.search(/<a>/i);
        var end = text.search(/<\/a>/i);
        text = text.substring(start + 3, end);
        
        var internalprefix = '';
        if (i === 'email') internalprefix = prefix;


        // if there's an error, set the error and set text red 
        if (ary[i].length > 0)
        {
            document.getElementById(i + 'Div').innerHTML = '<font color="#FF0000"><a>' + text +'</a></font>';
            document.getElementById(internalprefix + i + 'Error').innerHTML = '<font color="#FF0000">' + ary[i] +'</font>';
            submit = false;
        }
        // otherwise, clear the error and set text black
        else
        {
            document.getElementById(i + 'Div').innerHTML = '<font color="#000000"><a>' + text +'</a></font>';
            document.getElementById(internalprefix + i + 'Error').innerHTML = '';
        }
    }
         
    if (submit)
    {
        document.forms[0].submit();
    }
}

function toggleInput(box, page)
{
    var isChecked = box.checked;
    var results = new Array();

    //var prefix = 'ctl00_ctl00_Main_SubMain_';

    if (page === 3)
    {
        results['billingName'] = results['billingAddress1'] = results['billingAddress2'] = results['billingCity'] = results['billingZip'] = '';
        document.getElementById(prefix + 'billingState').disabled = !document.getElementById(prefix + 'billingState').disabled;
        document.getElementById(prefix + 'billingCountry').disabled = !document.getElementById(prefix + 'billingCountry').disabled;        
    }
    else if (page === 4)
    {
        if (isChecked)
        {
            if (box.id === prefix + "shipToInstitution")
            {
                document.getElementById(prefix + 'shipToBilling').checked = false;
            }
            else
            {
                document.getElementById(prefix + 'shipToInstitution').checked = false;
            }
        }
        
        results['shippingName'] = results['shippingAddress1'] = results['shippingAddress2'] = results['shippingCity'] = results['shippingZip'] = '';
        document.getElementById(prefix + 'shippingState').disabled = isChecked;
        document.getElementById(prefix + 'shippingCountry').disabled = isChecked;
  
    }

    // Clear errors and disallow input
    for (var i in results) {
        if (isChecked)//document.forms[0].shipToInstitution.checked || document.forms[0].shipToBilling.checked)
        {
            // get the text between the anchors
            var text = document.getElementById(i + 'Div').innerHTML;
            var start = text.search(/<a>/i);
            var end = text.search(/<\/a>/i);
            text = text.substring(start + 3, end);

            document.getElementById(i + 'Div').innerHTML = '<font color="#660099"><a>' + text +'</a></font>';
            document.getElementById(i + 'Error').innerHTML = '';
            document.getElementById(prefix + i).value = '';
        }
        document.getElementById(prefix + i).disabled = isChecked;
    }
}

function isEmail(src) {
    var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
    var regex = new RegExp(emailReg);
    return regex.test(src);
}

function isPhone(src) {
    var regex = new RegExp(/^.{10,32}$/);
    return regex.test(src);
}

function isZipCode(src) {
    var regex = new RegExp(/(^\d{5}-\d{4}$)|(^\d{5}$)/);
    return regex.test(src);
}

function isCreditCard(src) {
    var regex = new RegExp(/^\d{15,16}$/);
    return regex.test(src);
}
