function confirmDeleteDocument(form)
{
  if (confirm("Are you sure you want to delete this document from the system?"))
  {
    form.action.value = 'ACTION-DeleteDocument';
    return true;
  }

  return false;
}

var xmlHttp;

function GetXmlHttpObject()
{
    var xmlHttp=null;
    try
    {
        // Firefox, Opera 8.0+, Safari
        xmlHttp=new XMLHttpRequest();
    }
    catch (e)
    {
        //Internet Explorer
        try
        {
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}

function showDocumentSearchByNameResults()
{
    xmlHttp=GetXmlHttpObject()
    if (xmlHttp==null)
    {
        alert ("Browser does not support HTTP Request")
        return
    }

    var name = document.getElementById('name').value;
    var url="/polypipe/controller?action=PP-DocumentSearchResults&name="
    url=url+name
    url=url+"&sid="+Math.random()
    xmlHttp.onreadystatechange=stateChangeShowDocumentSearchResults
    xmlHttp.open("GET",url,true)
    xmlHttp.send(null)

}

function showDocumentSearchResults()
{
    xmlHttp=GetXmlHttpObject()
    if (xmlHttp==null)
    {
        alert ("Browser does not support HTTP Request")
        return
    }
    var company = (document.getElementsByName('companyId'))[0].value;
    var url="/polypipe/controller?action=PP-DocumentSearchResults&companyId="
    url=url+company
    var category = (document.getElementsByName('categoryId'))[0].value;
    url=url+"&categoryId="+category
    url=url+"&sid="+Math.random()
    xmlHttp.onreadystatechange=stateChangeShowDocumentSearchResults
    xmlHttp.open("GET",url,true)
    xmlHttp.send(null)
}

function stateChangeShowDocumentSearchResults()
{
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    {
        document.getElementById("documentResults").innerHTML=xmlHttp.responseText
    }
}

function updateCategories()
{
    xmlHttp=GetXmlHttpObject()
    if (xmlHttp==null)
    {
        alert ("Browser does not support HTTP Request")
        return
    }
    var company = (document.getElementsByName('companyId'))[0].value;
    var url="/polypipe/controller?action=PP-CategorySearchResults&companyId="
    url=url+company
    url=url+"&sid="+Math.random()
    xmlHttp.onreadystatechange=stateChangeShowCategorySearchResults
    xmlHttp.open("GET",url,true)
    xmlHttp.send(null)
}

function stateChangeShowCategorySearchResults()
{
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    {
        document.getElementById("categoryResults").innerHTML=xmlHttp.responseText
    }
}

function downloadDocument(id)
{
    var docTodownload = id;
    alert(docTodownload);
}

function addDocumentToBasket(id){
    var params = {
        action: 'PP-LiteratureBasketSummary',
        state: 'add',
        docId: id,
        sid: Math.random()
    }

    var url = "/polypipe/controller";

    var onComplete = function(){
        $('#basket').show();
    }

    document.getElementById('viewBasketLink').style.display = "block";

    $('#basket').load(url, params, onComplete)

}

function removeDocumentFromBasket(id){

    $('#basket').load('/polypipe/controller', {
        action: 'PP-LiteratureBasketSummary',
        state: "remove",
        docId: id,
        sid: Math.random()
    });

}


function removeDocumentFromMainBasket(id){

    var url = '/polypipe/controller';

    var params = {
        action: 'PP-LiteratureBasketSummary',
        state: "remove",
        docId: id,
        sid: Math.random()
    };

   var onComplete = function() {
       $("#document-"+id).remove();
   }

   $.get(url, params, onComplete);
}

function stateChangeShowLiteratureBasket()
{
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    {
        document.getElementById("basket").innerHTML=xmlHttp.responseText
    }
}

function validateLiteratureRequestForm()
{
    if(null == document.getElementById("title").value  || "" == document.getElementById("title").value)
    {
        alert("Please enter your title");
        document.getElementById("title").focus();
        return false;
    }

    if(null == document.getElementById("name").value  || "" == document.getElementById("name").value)
    {
        alert("Please enter your first name");
        document.getElementById("name").focus();
        return false;
    }

    if(null == document.getElementById("surname").value  || "" == document.getElementById("surname").value)
    {
        alert("Please enter your surname");
        document.getElementById("surname").focus();
        return false;
    }

    if(null == document.getElementById("company").value  || "" == document.getElementById("company").value)
    {
        alert("Please enter your company");
        document.getElementById("company").focus();
        return false;
    }

    if(-1 == document.getElementById("company_type").options[document.getElementById("company_type").selectedIndex])
    {
        alert("Please enter your company type");
        document.getElementById("company_type").focus();
        return false;
    }

    if(null == document.getElementById("jobtitle").value  || "" == document.getElementById("jobtitle").value)
    {
        alert("Please enter your job title");
        document.getElementById("jobtitle").focus();
        return false;
    }


    if(null == document.getElementById("address1").value  || "" == document.getElementById("address1").value)
    {
        alert("Please enter the first line of your address");
        document.getElementById("address1").focus();
        return false;
    }

        if(null == document.getElementById("address2").value  || "" == document.getElementById("address2").value)
    {
        alert("Please enter the second line of your address");
        document.getElementById("address2").focus();
        return false;
    }

    if(null == document.getElementById("towncity").value  || "" == document.getElementById("towncity").value)
    {
        alert("Please enter your town / city");
        document.getElementById("towncity").focus();
        return false;
    }

    if(null == document.getElementById("postcode").value  || "" == document.getElementById("postcode").value)
    {
        alert("Please enter your postcode");
        document.getElementById("postcode").focus();
        return false;

    }

    if(null == document.getElementById("email").value  || "" == document.getElementById("email").value)
    {
        alert("Please enter your email");
        document.getElementById("email").focus();
        return false;

    }
    return true;
}

function validateContactForm()
{
    if(null == document.getElementById("title").value  || "" == document.getElementById("title").value)
    {
        alert("Please enter your title");
        document.getElementById("title").focus();
        return false;
    }
    if(null == document.getElementById("name").value  || "" == document.getElementById("name").value)
    {
        alert("Please enter your first name");
        document.getElementById("name").focus();
        return false;
    }

    if(null == document.getElementById("surname").value  || "" == document.getElementById("surname").value)
    {
        alert("Please enter your surname");
        document.getElementById("surname").focus();
        return false;
    }
    if(null == document.getElementById("company").value  || "" == document.getElementById("company").value)
    {
        alert("Please enter your company name");
        document.getElementById("company").focus();
        return false;
    }

    if(null == document.getElementById("jobtitle").value  || "" == document.getElementById("jobtitle").value)
    {
        alert("Please enter your job title");
        document.getElementById("jobtitle").focus();
        return false;
    }

    if(null == document.getElementById("address1").value  || "" == document.getElementById("address1").value)
    {
        alert("Please enter the first line of your address");
        document.getElementById("address1").focus();
        return false;
    }

    if(null == document.getElementById("address2").value  || "" == document.getElementById("address2").value)
    {
        alert("Please enter the second line of your address");
        document.getElementById("address2").focus();
        return false;
    }

    if(null == document.getElementById("towncity").value  || "" == document.getElementById("towncity").value)
    {
        alert("Please enter your town / city");
        document.getElementById("towncity").focus();
        return false;
    }

    if(null == document.getElementById("county").value  || "" == document.getElementById("county").value)
    {
        alert("Please enter your county");
        document.getElementById("county").focus();
        return false;
    }

    if(null == document.getElementById("country").value  || "" == document.getElementById("country").value)
    {
        alert("Please enter your country");
        document.getElementById("country").focus();
        return false;
    }


    if(null == document.getElementById("postcode").value  || "" == document.getElementById("postcode").value)
    {
        alert("Please enter your postcode");
        document.getElementById("postcode").focus();
        return false;

    }
    if(null == document.getElementById("telephone").value  || "" == document.getElementById("telephone").value)
    {
        alert("Please enter your telephone");
        document.getElementById("telephone").focus();
        return false;
    }
    if(null == document.getElementById("email").value  || "" == document.getElementById("email").value)
    {
        alert("Please enter your email");
        document.getElementById("email").focus();
        return false;
    }

    return true;
}



function showKeywordSearchResultsOld()
{
    var keyword = document.getElementById('keyword').value;

    document.getElementById("resultBreadcrumb").innerHTML = "Display Search Results For: " + keyword;

    if(keyword.length < 1)
        alert('Please enter a search term');

    xmlHttp=GetXmlHttpObject()
    if (xmlHttp==null)
    {
        alert ("Browser does not support HTTP Request")
        return
    }

    var url="/polypipe/controller?action=PP-DocumentSearchResults&name="
    url=url+keyword
    url=url+"&sid="+Math.random()
    xmlHttp.onreadystatechange=stateChangeShowKeywordSearchResults
    xmlHttp.open("GET",url,true)
    xmlHttp.send(null);
}


function stateChangeShowKeywordSearchResults()
{
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    {
        document.getElementById("recently-added").innerHTML=xmlHttp.responseText
        document.getElementById("literatureBarWrapper").style.display = 'block';

    }
}

function showKeywordSearchResults(){
    var keyword = document.getElementById('keyword').value;
    var url = '/polypipe/controller';
    var breadcrumb = "Display Search Results For: " + keyword;

    document.getElementById("resultBreadcrumb").innerHTML = breadcrumb;

    var params = {
        action: 'PP-DocumentSearchResults',
        name: keyword,
        sid: Math.random()
    }
    var onComplete = function(){
        $('#recently-added').hide();
        $('#literatureBarWrapper').show();
    }
    $('#literatureBarWrapper .contents').load(url, params, onComplete)

}

function showCompanySearchResults(){
    var company = $('#company').val();
    var category = $('#category').val()

    if(company == -1 && category == -1){
        alert('Please select a company or a category');
    }

    var url = '/polypipe/controller';

    var breadcrumb = "Display Search Results For: ";

    if(company != -1)
    {
    	breadcrumb += document.getElementById("company").options[document.getElementById("company").selectedIndex].text;
    }

    if(category != -1)
    {
    	if(company != -1)
    	{
    	    breadcrumb += " > ";
    	}
        breadcrumb += document.getElementById("category").options[document.getElementById("category").selectedIndex].text;
    }

    document.getElementById("resultBreadcrumb").innerHTML = breadcrumb;


    var params = {
        action: 'PP-DocumentSearchResults',
        companyId: company,
        categoryId: category,
        sid: Math.random()
    }

    var onComplete = function(){
        $('#recently-added').hide();
        $('#literatureBarWrapper').show();
    }

    $('#literatureBarWrapper .contents').load(url, params, onComplete)


}



