function checkEnter(e, page, source_id)
{
    var characterCode // literal character code will be stored in this variable

    if(e && e.which){ //if which property of event object is supported (NN4)
        e = e;
        characterCode = e.which; //character code is contained in NN4's which property
    }    
    else
    {
        //e = event;
        characterCode = e.keyCode; //character code is contained in IE's keyCode property
    }
    if(characterCode == 13)
    {
        //if generated character code is equal to ascii 13 (if enter key)
        downloadSubmit(page, source_id);
        return false;
    }
    else
    {
        return true
    }
}

function echeck(str) {
    var at="@"
    var dot="."
    var lat=str.indexOf(at)
    var lstr=str.length
    var ldot=str.indexOf(dot)
    if (str.indexOf(at)==-1){
        //alert("Invalid E-mail ID")
        return false
    }

    if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
        //alert("Invalid E-mail ID")
        return false
    }

    if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
        //alert("Invalid E-mail ID")
        return false
    }

    if (str.indexOf(at,(lat+1))!=-1){
        //alert("Invalid E-mail ID")
        return false
    }

    if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
        //alert("Invalid E-mail ID")
        return false
    }

    if (str.indexOf(dot,(lat+2))==-1){
        //alert("Invalid E-mail ID")
        return false
    }

    if (str.indexOf(" ")!=-1){
        //alert("Invalid E-mail ID")
        return false
    }

    return true                   
}

function resetDownloadForm()
{
    var d = document.getElementById('download-div');
    d.style.display = 'block';
    var e = document.getElementById('download-result');
    e.style.display = 'none';
    
    document.getElementById('userInput').value = 'Email or Phone #';
} 

var downloadResponseSuccess = function(o){
    var d = document.getElementById('download-div');
    d.style.display = 'none';
    var e = document.getElementById('download-result');
    e.style.display = 'block';
    e.innerHTML = o.responseText;
};

var downloadResponseFailure = function(o){
    document.getElementById('download-phone').innerHTML = "Failed to load requested file: " + o.statusText;
}

var downloadCallback =
{    
    success:downloadResponseSuccess,
    failure:downloadResponseFailure,
    timeout:30000
};

function clearErrors(page)
{
    document.getElementById('download-error-invalid-input-'+page).style.display = 'none'; 
    document.getElementById('download-error-blank-input-'+page).style.display = 'none'; 
}  

function downloadSubmit(page, source_id)
{   
    clearErrors(page);
    if(page=='coming_soon')
    {
        var index = document.downloadForm.phoneList.selectedIndex;
        var device_id = document.downloadForm.phoneList.options[index].value;
    }
    var user_input = document.getElementById('userInput').value;
    
    // check if either the phone number or email is valid
    if(user_input != '' && user_input != 'Email or Phone #' && !validatePhoneNumber(user_input) && !echeck(user_input))
    {
        // show error
        document.getElementById('download-error-invalid-input-'+page).style.display = 'block';
    }
    // now we know one or the other is valid... not both... so which is it?
    else if(user_input != '' && user_input != 'Email or Phone #' && validatePhoneNumber(user_input))
    {
        // it's a phone number
        document.location = 'http://stitcher.com/newLandingSubmit.php?phone=' + user_input + '&page=' + page + '&source_id=' + source_id + '&device_id=' + device_id;
    }
    else if(user_input != '' && user_input != 'Email or Phone #' && echeck(user_input))
    {
        document.location = 'http://stitcher.com/newLandingSubmit.php?email=' + user_input + '&page=' + page + '&source_id=' + source_id + '&device_id=' + device_id;  
    }
    else
    {
        document.getElementById('download-error-blank-input-'+page).style.display = 'block'; 
        //alert('Please enter your email or phone number.');
    }
}

function goItunes(page)
{
    //document.location = "http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=288087905&mt=8";
    document.location = "itunesRedirect.php?page="+page;
}

function validatePhoneNumber(phone_number)
{
    // remove spaces, leading zeroes, and weird characters
    phone_number = phone_number.replace(/\s+/g,'');
    
    // \s+ Description
    // + 1 or more of previous expression.
    // \s Matches any white-space character.
    // Equivalent to the Unicode character categories [\f\n\r\t\v\x85\p{Z}].
    // If ECMAScript-compliant behavior is specified with the ECMAScript option,
    // \s is equivalent to [ \f\n\r\t\v].
    
    // this is magic code from the interwebs
    phoneRe = /^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,5})|(\(?\d{2,6}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$/;
    if (phoneRe.test(phone_number))
    {
        //alert(myform.phone.value + ' is invalid'); 
        //alert('is invalid');
        return true;
    }
    else
    {
        //alert('is valid');
        return false;
    }
}