﻿

// Delete a domain

deleteDomain = function ( id )
{
    if ( confirm ( 'Are you sure you want to delete this domain?' ) )
    {
        document.location.href="home.aspx?DEL=" + id;
        return false;
    } else { return false; }
}

// URL refresher

refreshUrl = function ( domain, pageUrlID )
{
    $.get ( $.format ( 'generic.axd?TYPE=REFRESH&DOMAIN={0}&ID={1}' ) ( domain, pageUrlID ) );
    $ ( '#refresh_' + pageUrlID ).html ( 'Refresh Requested' );
    $ ( '#refresh_' + pageUrlID ).css ( 'display', 'none' );
    $ ( '#refresh_' + pageUrlID ).customFadeIn ( 'slow' );
}



// Update of phrases

phraseUpdate = function ( domain, phrases, phraseId )
{

    if ( ! confirm ( 'If you have deleted or modified any of the current phrases, you will loose ALL history data for that phrase. Are you sure you want to update phrases?' ) ) return false;

    $ ( '#' + phraseId ).attr ( 'disabled' , true);
    
    if ( $ ( '#phraseLoader' ).length == 0 )
    {
        $ ( '<img/>' ).attr ( 'src', 'images/loader.gif' ).attr ( 'id', 'phraseLoader' ).insertAfter ( '#' + phraseId );
    } else {
        $ ( '#phraseLoader' ).css ( 'display','inline' );
    }
    
    $.post ( $.format ( 'generic.axd?TYPE=PHRASE&ID={0}' ) ( domain ) , { PHRASES: phrases },
        function ( data ) { $ ( '#' + phraseId ).removeAttr ( 'disabled' ); $ ( '#phraseLoader' ).css ( 'display','none' ); } );      
}


// Competitor Update

competitorUpdate = function ( domain, competitors, competitorId )
{
    $ ( '#' + competitorId ).attr ( 'disabled' , true);
    
    if ( $ ( '#competitorLoader' ).length == 0 )
    {
        $ ( '<img/>' ).attr ( 'src', 'images/loader.gif' ).attr ( 'id', 'competitorLoader' ).insertAfter ( '#' + competitorId);
    } else {
        $ ( '#competitorLoader' ).css ( 'display','inline' );
    }
    
    $.post ( $.format ( 'generic.axd?TYPE=COMPETITOR&ID={0}' ) ( domain ) , { COMPETITORS: competitors },
        function ( data ) { $ ( '#' + competitorId ).removeAttr ( 'disabled' ); $ ( '#competitorLoader' ).css ( 'display','none' ); } );      
}


// Add competitor

addCompetitor = function ( domain, domainId )
{
    $ ( '#competitorList').val( $ ( '#competitorList').val() + '\n' + domain );
    
    competitorUpdate ( domainId , $ ( '#competitorList' ).val ( ), 'competitorUpdateButton' )
    
}

// Alert functions

setAlert = function ( itemID, uniqueName, status, clientArea, resolveArea )
{

    if ( status == undefined  ) return;


    $.get ( $.format ( 'generic.axd?TYPE=ALERT&ID={0}&NAME={1}&STATUS={2}' ) ( itemID, uniqueName, status ) );
    if ( status != 'unresolved' )
    {
        $ ( '#' + clientArea ).slideUp ( 'slow' );
    } else {
         $ ( '#' + resolveArea ).slideUp ( 'slow' );
    }
}


setAlertAdmin = function ( alertID, alertName, alertObjectID ) {
    $.get ( $.format ( 'generic.axd?TYPE=ALERT&ID={0}&NAME={1}&STATUS=unresolved' ) ( alertID, alertName ), function () {

        $('#alertStatus_' + alertObjectID).html('Default').css('font-weight', 'normal');
        $('#alertStatusSet_' + alertObjectID).html('');
    
    } );
}


// Google Maps Marker
createMarker = function (point, index, html) {
        var baseIcon = new GIcon(G_DEFAULT_ICON);
        baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
        baseIcon.iconSize = new GSize(20, 34);
        baseIcon.shadowSize = new GSize(37, 34);
        baseIcon.iconAnchor = new GPoint(9, 34);
        baseIcon.infoWindowAnchor = new GPoint(9, 2);

      // Create a lettered icon for this point using our icon class
      var letter = String.fromCharCode("A".charCodeAt(0) + index);
      var letteredIcon = new GIcon(baseIcon);
      letteredIcon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";

      // Set up our GMarkerOptions object
      markerOptions = { icon:letteredIcon };
      var marker = new GMarker(point, markerOptions);

      GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml(html);
      });
      return marker;
}






/********* TECH PAGE SECTION SELECTION WITH HISTORY *********/


/************* PDF CREATOR *****************/

createPDF = function(url) {
    document.location.href = 'pdf.asmx?URL=' + escape(url) + '&OVR=__0WNERSHIP&NAME=export';
}


/************* MATRIX **************/

matrixGoto = function(id, url) {

    if ($("#sel_" + id).length == 0) {
        document.location.href = url;
    } else {
        document.location.href = url.replace(/\[SEL\]/gi, $("#sel_" + id).val());
    }

}


/************** CHECK WEBSITE ******************/

doCheckSite = function() {
    var url = $('#url').val();
    if (url == '') {
        alert('No URL Entered');
        $('#url').focus();
    } else if (!isValidDomain(url)) {
        alert('Invalid URL');
        $('#url').focus();
    } else {
        var addr = 'http://optimizer.builtwith.com/checksite.aspx?';
        document.location.href = addr + escape(url);
    }
}

isValidDomain = function(url) {
    if (url.indexOf('.') == -1) return null;
    var parts = url.split('.');

    if (parts.length == 4) {
        var isIP = true;
        for (x = 0; x < parts.length; x++) {
            if (!isInt(parts[x])) isIP = false;
        }

        if (isIP) return null;
    }

    return url;
}

function isInt(x) {
    var y = parseInt(x);
    if (isNaN(y)) return false;
    return x == y && x.toString() == y.toString();
} 

