// Common.js
// ---------
// 
// For EP UDC (Urban Design Compendium)
// Requires jQuery 1.1.2 or higher
// Initial code, Martin Paton, Developer, Amaze
// 13/08/2007

// Currently only handles popup image functionality with no function signature.

// Var to store interval ID
var intervalId = 0;

var inputButtonRolloverClass = "buttonRollover";

$(document).ready(function(){
  
    $("a.helper").focus(function() {
        $(this).addClass("focus");    
    });

    $("a.helper").blur(function() {
        $(this).removeClass("focus");    
    });
    
    try {
        document.execCommand("BackgroundImageCache", false, true);
    } catch(err) {}
    
    $("#Submit").hover(function(){
        $(this).addClass(inputButtonRolloverClass);
    },function(){
    
        $(this).removeClass(inputButtonRolloverClass);
    });
    
    $("#btnSubmit").hover(function(){
        $(this).addClass(inputButtonRolloverClass);
    },function(){
    
        $(this).removeClass(inputButtonRolloverClass);
    });
    
    $("input.searchbutton").hover(function(){
   
        $(this).addClass(inputButtonRolloverClass);
    },function(){
    
        $(this).removeClass(inputButtonRolloverClass);
    });
    
    $("#printcasestudy").html("<a href='#' onclick='javascript:window.print();return false;'>Print Case Study</a>");

    $("a", "#linkstable").click(function() {
    
        if ($(this).attr("href") == "#skipnav") return true;
        return popURL($(this).attr("href"), 'LINK');
    });
    
    $("a", "li.externallink").click(function() {
        return popURL($(this).attr("href"), 'LINK');
    });   
    
    
    // Inject link and magnifying glass
    var enlarge = "<span class=\"enlarge\"><a class=\"thickbox\" href=\"%1s\">Click to enlarge <img src=\"/img/mglass.gif\" border=\"0\"/></a></span>";    
    $("a.thickbox", "div.imageright").each(function(i) {    
        var href = $(this).attr("href");
        $(this).after($.sprintf(enlarge, href));
    });
    
    $("a.thickbox", "div.imageleft").each(function(i) {    
        var href = $(this).attr("href");
        $(this).after($.sprintf(enlarge, href));
    });
    
    
    // Initiate Google check
    intervalId = setInterval("googleCheck()", 500);           
 });
 
 // Code to check that Google scripts are available
function googleCheck()
{
    if (urchinTracker)
    {       
        clearInterval(intervalId);
        
        // ---------------------------------------------------------
        // Google Analytics - track downloads & search
        $("a").click(function() {
                //urchinTracker ('/downloads/map');
                var url = $(this).attr("href");
                categoriseType(url);
        });       
        
        $("#searchsite").click(function() {    
                callTracker('/internal search/' + $("#searchterm").attr("value"));
        });   
        
        $("#gobody").click(function() {
                callTracker('/internal search/' + $("#searchtermbody").attr("value"));
        }); 
        // ---------------------------------------------------------        
    }
}
 
 // Actually instigate tracking call
function callTracker(pageview) 
{
       //alert("lll");
       urchinTracker(pageview);
}
   
  function categoriseType(s) {

    var c = '';
    if (s.indexOf('http://') > -1) {
        c = '/url clicked/';
    } else if (s.indexOf('/public/documents/') > -1) {
        c = '/document viewed/';
    }
    
    if (c != '') {
        //$("#searchterm").val(c + adjustPath(s));
        callTracker(c + adjustPath(s));
    }
}

 function adjustPath(s) {
    
    if (s.indexOf('/public/documents/') > -1) {
        s = s.replace('/public/documents/','');
    }
    return s;
}

 function popURL (href, type) {      
    var url = href;      
    var w = window.open(url, type ,'');     
    w.focus();
           
    return false;
}

 function resize() {
 
     if (navigator.appVersion.match(/MSIE [0-6]\./)) {
        var currentSize = document.body.clientWidth;
                
        if (currentSize <= 830) 
        {            
            $("#sitecontainer").css("width", "700px");
        } else if ((currentSize > 830) && (currentSize <= 950)) {
            $("#sitecontainer").css("width", "85%");
        } else if (currentSize > 950) {
            $("#sitecontainer").css("width", "72%");
        }               
    }
 }
 
//addEvent(window, "load", resize);
//addEvent(window, "resize", resize);  

function addEvent(obj, evType, fn){
 if (obj.addEventListener){
    obj.addEventListener(evType, fn, true);
    return true;
 } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
 } else {
    return false;
 }
}

// ------------------------------------------------------------------------------
//
// Validation Functionality.
//
// ------------------------------------------------------------------------------

 function checkTitle(source, args)
 {
    // Check that something is selected other than the "Please select" bit
    args.IsValid = ($("#TitlePrefix").children("[@selected]").val() != "");
 }
 
  function checkSector(source, args)
 {
    // Check that something is selected other than the "Please select" bit
    // If other, check other box
    var val = $("#RequestorSector").children("[@selected]").val();
   // alert("Sector:" + val);    
    if (val == "") 
    {
        args.IsValid = false;
    }
    else if (val == "xx") 
    {
        args.IsValid = ($("#OtherSector").val() != "");
    }   
    else 
    {
        args.IsValid = true;
    }   
 }
 
function checkUDCOption(source, args)
{
    args.IsValid = (($("#UDC1").attr("checked")) || ($("#UDC2").attr("checked")));    
}

function checkProtectionOption(source, args)
{
    args.IsValid = (($("#NoRetain").attr("checked")) || ($("#RetainDetails").attr("checked")) || ($("#RetainUpdate").attr("checked")));    
}

function checkPhone(source, args)
{
    var tel = $("#ContactTelephone").val();
   // var fax = $("#ContactFax").val();
  
      args.IsValid = (checkNumber(tel));  
      
    /*if ((tel == "") && (fax == "")) {
        args.IsValid = false;
    } else if (checkNumber(tel)) {
        args.IsValid = true;
    } else {
        if (tel == "") {
            args.IsValid = true;
        } else {
            args.IsValid = false;
        }
    }  */      
}

function checkFax(source, args)
{
    var tel = $("#ContactTelephone").val();
    var fax = $("#ContactFax").val();
    var pattern = /\d{1}[\d|\s]*/;    
    
    if ((tel == "") && (fax == "")) {
        args.IsValid = false;
    } else if (checkNumber(fax)) {
        args.IsValid = true;
    } else {
        if (fax == "") {
            args.IsValid = true;
        } else {
            args.IsValid = false;
        }
    }        
}

function checkNumber(s) {
      var pattern = /\d{1}[\d|\s]*/;    
      return (s.search(pattern) > -1);
}      


function checkCountry(source, args)
{
    // Check that something is selected other than the "Please select" bit
    // If other, check other box
    var val = $("#ddlCountries").children("[@selected]").val(); 
    //alert("country:" + val);   
    if (val == "") 
    {
        args.IsValid = false;
    }   
    else 
    {
        args.IsValid = true;
    }

}