/* Set of common functionality that can be used throughout profile building proccess. */

/*  hack to override this so we can use the body onload event
    Now if we want to use an onload event we just redefine the function called LoadPage()
    Note: this relies on the Javascript function getDisplayFriendID already exists.
*/
function initJSControl()
{
    try
    {    
        loadOverlay();
    }
    catch (ex){}

    try
    {
        SetContactLinkUrls();
        LoadPage();
    }
    catch (ex){}
    
} 



/*****************************************************
Example HTML that uses this:
<div id="contactLinks">
    <a id="addToFriends" name=”&lid=AddToFriends”>add to friends</a>
    <a id="addToFavorites" name=”&lid=AddToFavorites”>add to favorites</a>
    <a id="forwardToFriends" name=”&lid=ForwardToFriends”>forward to friends</a>
    <a id="addToGroups" name=”&lid=AddToGroup”>add to groups</a>
</div>
******************************************************/

function SetContactLinkUrls()
{
    var profileId = getDisplayFriendID();
    var addToFriendsUrl = 'http://collect.myspace.com/index.cfm?fuseaction=invite.addfriend_advertiser&friendID=' + profileId;
    var addToFavoritesUrl = 'http://collect.myspace.com/index.cfm?fuseaction=user.addToFavorite&friendID=' + profileId + '&public=1';
    var forwardToFriendsUrl = 'http://messaging.myspace.com/index.cfm?fuseaction=mail.forward&friendID=' + profileId + '&f=forwardprofile';
    var addToGroupsUrl = 'http://groups.myspace.com/index.cfm?fuseaction=groups.addtogroup&friendID=' + profileId;
	var sendMessageUrl = 'http://messaging.myspace.com/index.cfm?fuseaction=mail.message&friendID=' + profileId;
	var rankUserUrl = 'http://collect.myspace.com/index.cfm?fuseaction=RateImage.UserRating&UserID=' + profileId;
	var viewPicturesUrl = 'http://viewmorepics.myspace.com/index.cfm?fuseaction=user.viewAlbums&friendID=' + profileId;    
    var addToFriends2Url = 'http://collect.myspace.com/index.cfm?fuseaction=invite.addfriend_advertiser&friendID=' + profileId;
	
	
	
    //if these elements exist in the page replace them.
    if (document.getElementById('addToFriends') != null)
        document.getElementById('addToFriends').href = addToFriendsUrl;
    if (document.getElementById('addToFriends2') != null)
        document.getElementById('addToFriends2').href = addToFriendsUrl;
    if (document.getElementById('addToFavorites') != null)
        document.getElementById('addToFavorites').href = addToFavoritesUrl;
    if (document.getElementById('forwardToFriends') != null)
        document.getElementById('forwardToFriends').href = forwardToFriendsUrl;
    if (document.getElementById('addToGroups') != null)
        document.getElementById('addToGroups').href = addToGroupsUrl;
	if (document.getElementById('sendAMessage') != null)
        document.getElementById('sendAMessage').href = sendMessageUrl;
    if (document.getElementById('rankUser') != null)
        document.getElementById('rankUser').href = rankUserUrl;
    if (document.getElementById('viewPictures') != null)
        document.getElementById('viewPictures').href = viewPicturesUrl;
   if (document.getElementById('linkContestAddToFriends') != null)
           document.getElementById('linkContestAddToFriends').href = addToFriends2Url;

   
}




/***********************************
* returns boolean indicating 
* whether an input's value is empty
* or not.
***********************************/
function IsNotEmpty(inputElement, errorMessage)
{
    if (inputElement.value.length < 1)
    {
        alert(errorMessage);
        return false;
    }
    return true;
}

// performs email check via regex
function ValidateEmail(inputElement)
{
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	if (filter.test(inputElement.value))
	    return true;
	else 
	{
	    alert('Please enter a valid email address');
	    return false;
	}
}

// performs phone number check via regex
// phone # must be in format: (###) ###-####
function ValidatePhone(inputElement)
{
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	if (filter.test(inputElement.value))
	    return true;
	else 
	{
	    alert('Please enter a valid email address');
	    return false;
	}
}

// performs date check via regex
// date must be in format: mm/dd/yyyy
function IsValidDate(inputElement)
{
	var filter  = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/;
	
	if (filter.test(inputElement.value))
	    return true;
	else 
	{
	    alert('Please enter a valid date');
	    return false;
	}
}


// Validates an entire form, this is used for the band mp3 submission 
// http://adsupport.myspace.com/index.cfm?fuseaction=advertiser.customcontest&advertiseruserid=201183447
// we can move this out and refactor it accordingly, but for now its a good example for reference.
function ValidateSumbission()
{
    // band name
    if (IsNotEmpty(document.getElementById('ctl01_bandName'), 'Please enter band name.') == false )
        return false;
        
    // song name
    if (IsEmpty(document.getElementById('ctl01_songName'), 'Please enter song name.') == false )
        return false;

    // address
    if (document.getElementById('ctl01_streetAddress').value.length < 3)
    {
        alert('Please enter address.'); 
        return false;
    }

    // email 
    if (ValidateEmail(document.getElementById('ctl01_email')) == false)
        return false;
           
    // city
    if (IsEmpty(document.getElementById('ctl01_city'), 'Please enter city.') == false )
        return false;

    // phone    
    if ( ValidatePhone(document.getElementById('ctl01_pnumber')) == false )
        return false;
    
    // state
    if (document.getElementById('ctl01_state').value = '0')
    {
        alert('Please enter state.'); 
        return false;
    }
    
    // band url
    if (IsEmpty(document.getElementById('ctl01_bandURL'), 'Please enter band URL.') == false )
        return false;
    
    // birthday
    if ( IsValidDate(document.getElementById('ctl01_bdate')) == false )
        return false;
    
    // zip code
    if (IsNotEmpty(document.getElementById('ctl01_zip'), 'Please enter zip code.') == false)
        return false;
   
    // band members
    if (IsNotEmpty(document.getElementById('ctl01_bandMembers'), 'Please enter band Members.') == false)
        return false;
    
    // upload
    if (IsNotEmpty(document.getElementById('ctl01_attachedfile'), 'Please select file to upload.') == false)
        return false;
    
    // terms of agreement    
    if (document.getElementById('checkRules').checked == 0)
    {
        alert('Please check the official rules.'); 
        return false;
    }

    // success
    return true;
}

/************************************************************
* Opens a page if the user is logged in.  Otherwise, redirects
* them to the myspace login page. 
* Assumes that the isLoggedIn variable is defined on a page.
************************************************************/
function GetUrlWithLoginCheck(url)
{
	if (isLoggedIn > 0)
	    window.open(url);
	else
    	document.location = 'http://login.myspace.com/index.cfm?fuseaction=login';
}
