
/*<![CDATA[*/


function checkPostCode (toCheck) {

  // Permitted letters depend upon their position in the postcode.
  var alpha1 = "[abcdefghijklmnoprstuwyz]";                       // Character 1
  var alpha2 = "[abcdefghklmnopqrstuvwxy]";                       // Character 2
  var alpha3 = "[abcdefghjkpmnrstuvwxy]";                         // Character 3
  var alpha4 = "[abehmnprvwxy]";                                  // Character 4 
  var alpha5 = "[abdefghjlnpqrstuwxyz]";                          // Character 5
  
  // Array holds the regular expressions for the valid postcodes
  var pcexp = new Array ();
  
  // Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1,2})$","i"));

  // Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1,2})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
  
  // Expression for postcodes: ANA NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}[0-9]{1}" + alpha3 + "{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));

  // Expression for postcodes: AANA  NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "{1}" + "?[0-9]{1}" + alpha4 +"{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
  
  // Exception for the special postcode GIR 0AA
  pcexp.push (/^(GIR)(\s*)(0AA)$/i);
  
  // Standard BFPO numbers
  pcexp.push (/^(bfpo)(\s*)([0-9]{1,4})$/i);
  
  // c/o BFPO numbers
  pcexp.push (/^(bfpo)(\s*)(c\/o\s*[0-9]{1,3})$/i);
  
  // Overseas Territories
  pcexp.push (/^([A-Z]{4})(\s*)(1ZZ)$/i);

  // Load up the string to check
  var postCode = toCheck;

  // Assume we're not going to find a valid postcode
  var valid = false;
  
  // Check the string against the types of post codes
  for ( var i=0; i<pcexp.length; i++) {
    if (pcexp[i].test(postCode)) {
    
      // The post code is valid - split the post code into component parts
      pcexp[i].exec(postCode);
      
      // Copy it back into the original string, converting it to uppercase and
      // inserting a space between the inward and outward codes
      postCode = RegExp.$1.toUpperCase() + " " + RegExp.$3.toUpperCase();
      
      // If it is a BFPO c/o type postcode, tidy up the "c/o" part
      postCode = postCode.replace (/C\/O\s*/,"c/o ");
      
      // Load new postcode back into the form element
      valid = true;
      
      // Remember that we have found that the code is valid and break from loop
      break;
    }
  }
  
  // Return with either the reformatted valid postcode or the original invalid 
  // postcode
  if (valid) {return postCode;} else return false;
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}


function nsdPCCheck1(sender, args)
{   
	var myDoc;
	var myPostCode;
	var myMiles;
	var myBase;
	
	myDoc = document;
	myPostCode = myDoc.getElementById('ctl03_MonopSearch2_1_desktopwordsearch_pkhd1_pkhd_postcode');
   
    if(checkPostCode(trim(myPostCode.value)) == false)
    {
         args.IsValid = false;
         return;
    }
    args.IsValid = true;
}

function nsdCatJump(svcid, category, type)
{   
	var myDoc;
	var myPostCode;
	var myMiles;
	var myBase;
	
	if(type == 'portal')
	{
		var a = top.document.getElementById('sdframe');
		myDoc = a.contentWindow.document;
		//a.contentWindow ? a.contentWindow.document: a.contentDocument;

		//myMiles = myDoc.form1.ctl02$MonopSearch2_1$desktopwordsearch_pkhd1$ddlMiles;
		//myPostCode = myDoc.getElementsByName('ctl02$MonopSearch2_1$desktopwordsearch_pkhd1$pkhd_postcode');
		
		myPostCode = myDoc.getElementById('ctl02_MonopSearch2_1_desktopwordsearch_pkhd1_pkhd_postcode');
		myMiles = myDoc.getElementById('ctl02_MonopSearch2_1_desktopwordsearch_pkhd1_ddlMiles');
		myRegVal = myDoc.getElementById('ctl02_MonopSearch2_1_desktopwordsearch_pkhd1_CustomValidator1');
		
		myBase = 'nsdBCCportal.aspx';
		
	} else {
		myDoc = document;
		
		myPostCode = myDoc.getElementById('ctl03_MonopSearch2_1_desktopwordsearch_pkhd1_pkhd_postcode');
		myMiles = myDoc.getElementById('ctl03_MonopSearch2_1_desktopwordsearch_pkhd1_ddlMiles');
		myRegVal = myDoc.getElementById('ctl03_MonopSearch2_1_desktopwordsearch_pkhd1_CustomValidator1');
		myBase = 'nsdBCC.aspx';
	}
	
    var dropdownIndex = myMiles.selectedIndex;
    var dropdownValue = myMiles[dropdownIndex].value;
   
    if(checkPostCode(trim(myPostCode.value)) == false)
    {
        myRegVal.style.display = 'inline';	
        return;
    }
		
	SideBar_RedirectUrl = "?&p=parent&theme=0&fontsize=0&sid=&aud=3&sc=" + category + "&postcode=" + trim(myPostCode.value) + "&miles=" + dropdownValue + "&age=0&orderby=&lsid=&currentpage=1&pagesize=10&sestatus=newsearch&nsduc=results&searchmethod=30&svcid=&areaarid=&osc=&svcid=" + svcid;
	setTimeout( "window.location.href = SideBar_RedirectUrl", 0 );
	
	//alert("nsdBCC.aspx?&p=parent&theme=0&fontsize=0&sid=&aud=3&sc=" + category + "&postcode=" + myPostCode.value + "&miles=" + dropdownValue + "&age=0&orderby=&lsid=&currentpage=1&pagesize=10&sestatus=newsearch&nsduc=results&searchmethod=30&svcid=&areaarid=&osc=&svcid=" + svcid);
	//window.location.href = "nsdBCC.aspx?&p=parent&theme=0&fontsize=0&sid=&aud=3&sc=" + category + "&postcode=" + f.value + "&miles=" + dropdownValue + "&age=0&orderby=&lsid=&currentpage=1&pagesize=10&sestatus=newsearch&nsduc=results&searchmethod=30&svcid=&areaarid=&osc=&svcid=" + svcid;
}

function ecdJump(svcid, type)
{   
	var myDoc;
	var myPostCode;
	var myMiles;
	var myBase;
	
	if(type == 'portal')
	{
		var a = top.document.getElementById('sdframe');
		myDoc = a.contentWindow.document;
		//a.contentWindow ? a.contentWindow.document: a.contentDocument;

		//myMiles = myDoc.form1.ctl02$MonopSearch2_1$desktopwordsearch_pkhd1$ddlMiles;
		//myPostCode = myDoc.getElementsByName('ctl02$MonopSearch2_1$desktopwordsearch_pkhd1$pkhd_postcode');
		
		myPostCode = myDoc.getElementById('ctl02_MonopSearch2_1_desktopwordsearch_pkhd1_pkhd_postcode');
		myMiles = myDoc.getElementById('ctl02_MonopSearch2_1_desktopwordsearch_pkhd1_ddlMiles');
		myRegVal = myDoc.getElementById('ctl02_MonopSearch2_1_desktopwordsearch_pkhd1_CustomValidator1');
		
		myBase = 'nsdBCCportal.aspx';
		
	} else {
		myDoc = document;
		
		myPostCode = myDoc.getElementById('ctl03_MonopSearch2_1_desktopECDMain1_pkhd_postcode');
		myMiles = myDoc.getElementById('ctl03_MonopSearch2_1_desktopECDMain1_ddlMiles');
		myRegVal = myDoc.getElementById('ctl03_MonopSearch2_1_desktopECDMain1_CustomValidator1');
		myBase = 'nsdBCC.aspx';
	}
	
    var dropdownIndex = myMiles.selectedIndex;
    var dropdownValue = myMiles[dropdownIndex].value;
   
    if(checkPostCode(myPostCode.value) == false)
    {
        myRegVal.style.display = 'inline';	
        return;
    }
		
	SideBar_RedirectUrl = "?&p=parent&theme=0&fontsize=0&sid=&aud=3&sc=&postcode=" + myPostCode.value + "&miles=" + dropdownValue + "&age=0&orderby=&lsid=&currentpage=1&pagesize=10&sestatus=ecdsearch&nsduc=results&searchmethod=30&svcid=&areaarid=&osc=&svcid=" + svcid;
	setTimeout( "window.location.href = SideBar_RedirectUrl", 0 );
	
	//alert("nsdBCC.aspx?&p=parent&theme=0&fontsize=0&sid=&aud=3&sc=" + category + "&postcode=" + myPostCode.value + "&miles=" + dropdownValue + "&age=0&orderby=&lsid=&currentpage=1&pagesize=10&sestatus=newsearch&nsduc=results&searchmethod=30&svcid=&areaarid=&osc=&svcid=" + svcid);
	//window.location.href = "nsdBCC.aspx?&p=parent&theme=0&fontsize=0&sid=&aud=3&sc=" + category + "&postcode=" + f.value + "&miles=" + dropdownValue + "&age=0&orderby=&lsid=&currentpage=1&pagesize=10&sestatus=newsearch&nsduc=results&searchmethod=30&svcid=&areaarid=&osc=&svcid=" + svcid;
}

function showSelectedOption(selectedOption)
            {
            var rwAgeArea = document.getElementById("search_rwAgeArea")                                  
            var rwAtoZ = document.getElementById("search_rwAtoZ")                                  
            var rwSearchTextBox = document.getElementById("search_rwSearchTextBox")                                  
            var rwSearchButton = document.getElementById("search_rwSearchButton")
            
            if (selectedOption == "Provider")
               {
                rwAgeArea.style.display = "none"
                rwSearchTextBox.style.display = "none"
                rwSearchButton.style.display = "none"
                rwAtoZ.style.display = "block"
                }
            else
                {
                rwAgeArea.style.display = "block"                
                rwSearchTextBox.style.display = "block"                
                rwSearchButton.style.display = "block"                
                rwAtoZ.style.display = "none"                
                }
                
            }
            
            
            function MM_openBrWindow(theURL,winName,features) { //v2.0
              window.open(theURL,winName,features);
            }

           
         function showMoreInfo(NameOfDiv){
				if(document.getElementById(NameOfDiv).style.display == "none")
				{
				document.getElementById(NameOfDiv).style.display = "block";	
				document.getElementById(NameOfDiv + "Title").innerText = "Less Info..."
				}
					else
					{
				document.getElementById(NameOfDiv).style.display = "none";	
				document.getElementById(NameOfDiv + "Title").innerText = "More Info..."
				}
				
				//Signposting specific iframe fix
				if (document.domain == 'www.signposting.org.uk') 
				    {
	                var the_height=top.document.getElementById('sdframe').contentWindow.document.body.scrollHeight
	                var the_width=top.document.getElementById('sdframe').contentWindow.document.body.scrollWidth
	                top.document.getElementById('sdframe').height=the_height+10
	                top.document.getElementById('sdframe').width=the_width+10
                    }	
                    
                //Demo Iframe  fix
				if (document.domain == 'demo.signposting.org.uk') 
				    {
	                var the_height=top.document.getElementById('sdframe').contentWindow.document.body.scrollHeight
	                var the_width=top.document.getElementById('sdframe').contentWindow.document.body.scrollWidth
	                top.document.getElementById('sdframe').height=the_height+10
	                top.document.getElementById('sdframe').width=the_width+10
                    }			
			}
		

         function showAdvancedSearch(NameOfDiv, HrefName){
				if(document.getElementById(NameOfDiv).style.display == "none")
				{
				document.getElementById(NameOfDiv).style.display = "block";	
				document.getElementById(HrefName).innerHTML = "Hide search tools..."
				}
					else
					{
				document.getElementById(NameOfDiv).style.display = "none";	
				document.getElementById(HrefName).innerHTML = "Refine your search..."
				}
							
			}

		

		
		
		
		 function showPage(ShowNameOfDiv, MaxDivCount)
		 {
				for (i = 0; i <= MaxDivCount; i++)
                {
                  document.getElementById("divResultsPage" + i).style.display = "none";	
                  document.getElementById("pageno" + i).style.color = "blue";
                }
                document.getElementById("divResultsPage" + ShowNameOfDiv).style.display = "block";
                document.getElementById("pageno" + ShowNameOfDiv).style.color = "red";
    	 }

function showServiceType(ShowDiv, HideDiv)
    {
                document.getElementById(ShowDiv).style.display = "block";	
				document.getElementById(HideDiv).style.display = "none";	
    }
    
    
    
    function getSearchCriteria(txtID)
    {
                var returnStr = ""
                var clickCount = document.getElementById("txtClicks").value
                if (clickCount == 0)
                {
                returnStr = encodeURIComponent(document.getElementById(txtID).value);
                document.getElementById("txtClicks").value = 1
   				}
   				return returnStr
   				
    }
    
    
    
    function clearText(theInput)
{
	var v = trim(theInput.value);
	if(v == "Postcode")
		theInput.value = "";
}
function setDefaultText(theInput)
{
	var v = trim(theInput.value);
	if(v == "")
		theInput.value = "Postcode";
}
function trim(s)
{
	while(s.indexOf(" ") == 0)
		s = s.substr(1, s.length - 1);
	while(s.length > 0 && s.lastIndexOf(" ") == s.length - 1)
		s = s.substr(0, s.length - 1);
	return s;
}
    
    
    
/*]]>*/