//***********************************************
//File: utility.js
//Purpose: General functions used throughout site
//***********************************************

//------------------------------------------
//Function: clearDefault
//Purpose: clears default value of input box
//------------------------------------------
function clearDefault(el) {
if (el.defaultValue==el.value) el.value = ""
}

//------------------------------------------------------------------
//Function: restoreDefaultNum
//Purpose: restores default value of input box plus validates number
//------------------------------------------------------------------
function restoreDefaultNum(el) {
	if (el.value < 0 || el.value.length == 0 || isNaN(el.value))
	el.value = el.defaultValue;
}

//-------------------------------
//Function: toggle
//Purpose: Shows or Hides a panel
//-------------------------------
function toggle(id)
{
	var aString = 'ad_' + id;

	if(document.getElementById(aString).style.display=='none')
	{
		document.getElementById(aString+'_link').innerHTML = '-';
		document.getElementById(aString).style.display = '';
	}
	else
	{
		document.getElementById(aString+'_link').innerHTML = '+';
		document.getElementById(aString).style.display = 'none';
	}
}

//--------------------------------
//Function: confirmMsg
//Purpose: Confirmation dialog box
//--------------------------------
function confirmMsg(msg) {
	
	var MyResult;
	
	if (typeof(Page_ClientValidate) == 'function'){
		Page_ClientValidate();
		MyResult = Page_IsValid;
	}
	else {
		MyResult = true;
	}

	if(MyResult != false) {
	MyResult = confirm(msg);
	}
	
	return MyResult;
}

//-----------------------------------------
//Function: setHiddenfieldValue
//Purpose: sets the value of a hidden field
//-----------------------------------------
var setHiddenfieldValue= function(fieldName,val)
{document.getElementById(fieldName).value=val;}

//-----------------------------------------------------------
//Function: setMultiHiddenfieldValue
//Purpose: sets the value of a hidden field with multi-select
//-----------------------------------------------------------
var setMultiHiddenfieldValue= function(fieldName,fieldMulti)
{
	var obj = document.getElementById(fieldMulti);
	var outArray = new Array();

	for (var i=0; i<obj.options.length; i++) {
		if (obj.options[i].selected) {outArray.push(obj.options[i].value);}
	}
	document.getElementById(fieldName).value=outArray;
}

//----------------------------------------------------------------
//	textCounter() - Counts character-limit remaining for input box
//----------------------------------------------------------------
function textCounter(fieldName, countfield, maxlimit) {

	var obj = document.getElementById(fieldName)
	var objcount = document.getElementById(countfield)

	if (obj.value.length > maxlimit) // if too long...trim it!
	obj.value = obj.value.substring(0, maxlimit);
	// otherwise, update 'characters left' counter
	else 
	objcount.value = maxlimit - obj.value.length;
}

//--------------------------------------------------------------------------
//	popDisplayAddress() - Set the Display Address to the Address Line 1
//--------------------------------------------------------------------------
function popDisplayAddress(addrName, dispName){

	var objAddr = document.getElementById(addrName)
	var objDisp = document.getElementById(dispName)

	objDisp.value = objAddr.value;
}

//--------------------------------------------------------------------------
//	toggleVisibility() - toggles visibility of element
//--------------------------------------------------------------------------
function toggleVisibility(id, type) {
	el = document.getElementById(id);
	if(el.style){
		if(type == 'on'){el.style.display = 'inline';} else {el.style.display = 'none';}
	} else {
		if(type == 'on'){el.display = 'inline';} else {el.display = 'none';}
	}
}

//--------------------------------------------------------------------------
//	SelectAll() - ticks or unticks a group of checkboxes
//--------------------------------------------------------------------------
function SelectAll(CheckBoxControl){
	if (CheckBoxControl.checked == true){
		var i;
		for (i=0; i < document.forms[0].elements.length; i++){
			if (document.forms[0].elements[i].type == 'checkbox'){
				document.forms[0].elements[i].checked = true;
			}
		}
	}
	else{
		var i;
		for (i=0; i < document.forms[0].elements.length; i++){
			if (document.forms[0].elements[i].type == 'checkbox'){
				document.forms[0].elements[i].checked = false;
			}
		}
	}
}

//--------------------------------------------------------------------------
//	roomChange() - Refresh the bedroom details table when a change is made
//--------------------------------------------------------------------------
function roomChange(singName, doubName, twinName, dispName){

	var objSing = document.getElementById(singName)
	var objDoub = document.getElementById(doubName)
	var objTwin = document.getElementById(twinName)
	var objDisp = document.getElementById(dispName)

	//Update the total values after any change
	objDisp.value = parseInt(objSing.value) + parseInt(objDoub.value) + parseInt(objTwin.value);
}

//--------------------------------------------------------------------------
//	priceChange() - Refresh the price details table when a change is made
//--------------------------------------------------------------------------
function priceChange(singName, doubName, twinName, dispName){

	var objSing = document.getElementById(singName)
	var objDoub = document.getElementById(doubName)
	var objTwin = document.getElementById(twinName)
	var objDisp = document.getElementById(dispName)

	//Update the total values after any change
	objDisp.value = parseFloat(objSing.value) + parseFloat(objDoub.value) + parseFloat(objTwin.value);
}
/*==============================================*/
/*Function: getCounties()						*/
/*Purpose: Populates dropdown onchange via AJAX	*/
/*==============================================*/
function getCounties(ddlCountry,ddlCounty,ddlDistrict) {
	var selectedCountry = ddlCountry.options[ddlCountry.selectedIndex].value;

	var remoteConnection = new ASXML();

	remoteConnection.setCallback(
		function (loadOK){
		if(loadOK)	{

			var x = remoteConnection.xmlResponse();

			//populate county dropdown
			remoteConnection.populateDDL(ddlCounty, x.selectNodes("//root/county"), "Please select", 0,"");
			
			//clear district dropdown
			remoteConnection.clearDDL(ddlDistrict,"Please select", 0);
			}
		}
	);
	remoteConnection.getXML("/GetAJAX.aspx?t=county&id="+selectedCountry);
}
/*==============================================*/
/*Function: getDistricts()						*/
/*Purpose: Populates dropdown onchange via AJAX	*/
/*==============================================*/
function getDistricts(ddlCounty,ddlDistrict,colour,startTxt) {
	var selectedCounty = ddlCounty.options[ddlCounty.selectedIndex].value;
	
	var remoteConnection = new ASXML();

	remoteConnection.setCallback(
		function (loadOK){
		if(loadOK)	{

			var x = remoteConnection.xmlResponse();
            if (selectedCounty == -1)
                remoteConnection.populateDDL(ddlDistrict, x.selectNodes("//root/district"), "Not Applicable", 0,colour);
			//populate district dropdowns
			else
			    remoteConnection.populateDDL(ddlDistrict, x.selectNodes("//root/district"), startTxt, 0,colour);
			}
		}
	);
	remoteConnection.getXML("/GetAJAX.aspx?t=district&id="+selectedCounty);
	//remoteConnection.getXML("GetAJAX.aspx?t=college&id="+selectedCounty);
}
/*==============================================*/
/*Function: getDistrictGroups()					*/
/*Purpose: Populates dropdown onchange via AJAX	*/
/*==============================================*/
function getDistrictGroups(ddlCounty,ddlDistrictGroup) {
	var selectedCounty = ddlCounty.options[ddlCounty.selectedIndex].value;

	var remoteConnection = new ASXML();

	remoteConnection.setCallback(
		function (loadOK){
		if(loadOK)	{

			var x = remoteConnection.xmlResponse();

			//populate district dropdowns
			remoteConnection.populateDDL(ddlDistrictGroup, x.selectNodes("//root/districtgroup"), "", 0,"#DFE7F2");
			}
		}
	);
	remoteConnection.getXML("/GetAJAX.aspx?t=districtgroup&id="+selectedCounty);
}
/*==============================================*/
/*Function: getPType()							*/
/*Purpose: Populates dropdown onchange via AJAX	*/
/*==============================================*/
function getPType(rdButton,ddlPType) {
	var selectedMarket = rdButton.value;
	
	//compensate for letting category values (e.g. we prepend the value with "20")
	if (selectedMarket.length > 2){selectedMarket = selectedMarket.charAt(selectedMarket.length - 1);}

	var remoteConnection = new ASXML();

	remoteConnection.setCallback(
		function (loadOK){
		if(loadOK)	{

			var x = remoteConnection.xmlResponse();

			//populate PType dropdown
			remoteConnection.populateDDL(ddlPType, x.selectNodes("//root/ptype"), "", 0,"#DFE7F2");
			}
		}
	);
	remoteConnection.getXML("/GetAJAX.aspx?t=ptype&id="+selectedMarket);
}

/*==============================================*/
/*Function: getPType()							*/
/*Purpose: Populates dropdown onchange via AJAX	*/
/*==============================================*/
function getPTypeDDL(ddlPMarket,ddlPType) {
	var selectedMarket = ddlPMarket.options[ddlPMarket.selectedIndex].value;
	
	//compensate for rooms and overseas market
	if (selectedMarket == -1){selectedMarket = 2;}
    if (selectedMarket == -2){selectedMarket = 2;}
    
	var remoteConnection = new ASXML();

	remoteConnection.setCallback(
		function (loadOK){
		if(loadOK)	{

			var x = remoteConnection.xmlResponse();

			//populate PType dropdown
			remoteConnection.populateDDL(ddlPType, x.selectNodes("//root/ptype"), "Please Select", 0,"#DFE7F2");
			}
		}
	);
	remoteConnection.getXML("/GetAJAX.aspx?t=ptype&id="+selectedMarket);
}

/*==============================================*/
/*Function: getPrice()							*/
/*Purpose: Populates dropdown onchange via AJAX	*/
/*==============================================*/
function getPrice(rdButton,ddlPrice,type) {
	var selectedMarket = rdButton.value;

	//compensate for letting category values (e.g. we prepend the value with "20")
	if (selectedMarket.length > 2){selectedMarket = selectedMarket.charAt(selectedMarket.length - 1);}

	var remoteConnection = new ASXML();

	remoteConnection.setCallback(
		function (loadOK){
		if(loadOK)	{

			var x = remoteConnection.xmlResponse();

			//populate Price dropdown
			if (type == 'pricemin'){remoteConnection.populateDDL(ddlPrice, x.selectNodes("//root/price"), "Any", 0,"");}
			if (type == 'pricemax'){remoteConnection.populateDDL(ddlPrice, x.selectNodes("//root/price"), "Any", 999999999,"");}
			}
		}
	);
	remoteConnection.getXML("/GetAJAX.aspx?t="+type+"&id="+selectedMarket);
}

/*==============================================*/
/*Function: getColleges()						*/
/*Purpose: Populates dropdown onchange via AJAX	*/
/*==============================================*/
function getColleges(ddlCounty, ddlCollege, colour, startTxt) {

	var selectedCounty = ddlCounty.options[ddlCounty.selectedIndex].value;

	var remoteConnection = new ASXML();

	remoteConnection.setCallback(
		function (loadOK){
		if(loadOK)	{

			var x = remoteConnection.xmlResponse();

			//populate district dropdowns
			
			remoteConnection.populateDDL(ddlCollege, x.selectNodes("//root/college"), startTxt, 0,colour);
			}
		}
	);
	remoteConnection.getXML("/GetAJAX.aspx?t=college&id="+selectedCounty);
}

/*==============================================*/
/*Function: getCollegesDB()						*/
/*Purpose: Populates dropdown onchange via AJAX	*/
/*==============================================*/
function getCollegesDB(ddlCounty, ddlCollege, colour, startTxt) {

    var selectedCounty = ddlCounty.options[ddlCounty.selectedIndex].value;

    var remoteConnection = new ASXML();

    remoteConnection.setCallback(
		function (loadOK) {
		    if (loadOK) {

		        var x = remoteConnection.xmlResponse();

		        //populate district dropdowns

		        remoteConnection.populateDDL(ddlCollege, x.selectNodes("//root/college"), startTxt, 0, colour);
		    }
		}
	);
	remoteConnection.getXML("/GetAJAX.aspx?t=collegedb&id=" + selectedCounty);
}

/*==============================================*/
/*Function: getRooms()	    					*/
/*Purpose: Populates dropdown onchange via AJAX	*/
/*==============================================*/
function getRooms(ddlPType, ddlRooms, colour, startTxt) {

    var selectedPType = ddlPType.options[ddlPType.selectedIndex].value;
    var remoteConnection = new ASXML();

    remoteConnection.setCallback(
        function (loadOK){
        if(loadOK)	{
        
            var x = remoteConnection.xmlResponse();

            //populate rooms dropdowns
            
            remoteConnection.populateDDL(ddlRooms, x.selectNodes("//root/room"), startTxt, 0,colour);
            }
        }
    );
    remoteConnection.getXML("/GetAJAX.aspx?t=rooms&id="+selectedPType);       
}

/*==============================================*/
/*Function: getBeds()	    					*/
/*Purpose: Populates dropdown onchange via AJAX	*/
/*==============================================*/
function getBeds(ddlPMarket, ddlRooms, colour, startTxt) {

    var selectedPMarket = ddlPMarket.options[ddlPMarket.selectedIndex].value;
    var remoteConnection = new ASXML();

    remoteConnection.setCallback(
        function (loadOK){
        if(loadOK)	{
        
            var x = remoteConnection.xmlResponse();

            //populate rooms dropdowns
            if ( (selectedPMarket == -1) || (selectedPMarket == 7) || (selectedPMarket == 8)|| (selectedPMarket == 11))
            {
                remoteConnection.populateDDL(ddlRooms, x.selectNodes("//root/room"), "Not Applicable", 0,colour);
            }
            else
            {
                remoteConnection.populateDDL(ddlRooms, x.selectNodes("//root/room"), "Please Select", 0,colour);
            }
        }
        }
    );
    remoteConnection.getXML("/GetAJAX.aspx?t=rooms&id="+selectedPMarket);       
}

/*==============================================*/
/*Function: getAvailabilty()	    				*/
/*Purpose: Populates dropdown onchange via AJAX	*/
/*==============================================*/
function getStatus(ddlPMarket,ddlStatus) {
	var selectedMarket = ddlPMarket.options[ddlPMarket.selectedIndex].value;

	var remoteConnection = new ASXML();

	remoteConnection.setCallback(
		function (loadOK){
		if(loadOK)	{

			var x = remoteConnection.xmlResponse();

			//populate district dropdowns
			remoteConnection.populateDDL(ddlStatus, x.selectNodes("//root/status"), "", 0,"#DFE7F2");
			}
		}
	);
	remoteConnection.getXML("/GetAJAX.aspx?t=status&id="+selectedMarket);
}

/*==============================================*/
/*Function: checkMarket()	    				*/
/*Purpose: Populates dropdown onchange via AJAX	*/
/*==============================================*/
function checkMarket(ddlPMarket, ddlCounty, ddlDistrict, mode, colour, startTxt) {
    
    var selectedPMarket = document.getElementById('ctlSearchControl2___hidPMarket').value;
    var selectedCountry = document.getElementById('ctlSearchControl2___hidCounty').value;

    var remoteConnection = new ASXML();

    remoteConnection.setCallback(
        function (loadOK)
        {
            if(loadOK)	
            {
                
                var x = remoteConnection.xmlResponse();
                
                if (mode == "county") //dropdown county has changed
                {
                    //selectedPMarket = 
                    if (selectedPMarket == -2) //ddCounty populated with countRies
                    {
                        //populate ddDistrict with counties
                        remoteConnection.populateDDL(ddlDistrict, x.selectNodes("//root/county"), "Select County", 0, colour);
                    }
                    else
                    {
                        //a county has been selected
                        //populate ddDistrict with districts
                        remoteConnection.populateDDL(ddlDistrict, x.selectNodes("//root/district"), "Select District", 0, colour);
                    }
                                            
                }
                else //dropdown PMarket has changed
                {
                    //selectedPMarket = ddlPMarket.options[ddlPMarket.selectedIndex].value
                    if (selectedPMarket == -2) //overseas
                    {   
                        //populate ddCounty with countRies
                        remoteConnection.populateDDL(ddlCounty, x.selectNodes("//root/country"), "Select Country", 0,colour);
                    }
                    else
                    {
                        selectedPMarket = 1;
                        //populate ddCounty with counties
                        remoteConnection.populateDDL(ddlCounty, x.selectNodes("//root/county"), "Select County", 0,colour);
                        remoteConnection.clearDDL(ddlDistrict,"Select District", 0);
                    }
                }
            }
        }
    );
    
    if (selectedPMarket == -2) //overseas
    {
        if (mode == "county") //ddCounty has changed
        {
            //populate ddDistrict with counties of the selected countRy
            remoteConnection.getXML("/GetAJAX.aspx?t=county&id="+selectedCountry);
        }
        else
        {
            //ddPMarket has changed
            //populate ddCounty with countRies
            remoteConnection.getXML("/GetAJAX.aspx?t=country");
            //clear district dropdown
	        remoteConnection.clearDDL(ddlDistrict,"Select County", 0);
	    }
        
    }
    else //not overseas (ddCounty is for counties and ddDistrict is for districts)
    {
        if (mode == "county") //ddCounty has changed
            //populate ddDistrict with districts
            remoteConnection.getXML("/GetAJAX.aspx?t=district&id="+selectedCountry);
        else //ddPMarket has changed
            //populate ddCounty with counties of Ireland
            remoteConnection.getXML("/GetAJAX.aspx?t=county&id=1"); 
    }      

}


/*==============================================*/
/*Function: getCounties()						*/
/*Purpose: Populates dropdown onchange via AJAX	*/
/*==============================================*/
function getCountiesOnly(ddlCountry,ddlCounty) {
	var selectedCountry = ddlCountry.options[ddlCountry.selectedIndex].value;

	var remoteConnection = new ASXML();

	remoteConnection.setCallback(
		function (loadOK){
		if(loadOK)	{

			var x = remoteConnection.xmlResponse();

			//populate county dropdown
			remoteConnection.populateDDL(ddlCounty, x.selectNodes("//root/county"), "Please select", 0,"");
			}
		}
	);
	remoteConnection.getXML("/GetAJAX.aspx?t=county&id="+selectedCountry);
}

/*==============================================*/
/*Function: getLType()  						*/
/*Purpose: Populates dropdown onchange via AJAX	*/
/*==============================================*/
function getLType(ddlPType, ddlLType, colour, startTxt) {

    var selectedPType = ddlPType.options[ddlPType.selectedIndex].value;
    var remoteConnection = new ASXML();
   
    remoteConnection.setCallback(
        function (loadOK){
        if(loadOK)	{
        
            var x = remoteConnection.xmlResponse();

            //populate rooms dropdowns
            
            remoteConnection.populateDDL(ddlLType, x.selectNodes("//root/ltype"), startTxt, 0,colour);
            }
        }
    );
    remoteConnection.getXML("/GetAJAX.aspx?t=ltype&id="+selectedPType);       
}

/*==================================================*/
/*Function: getPropDetails()						*/
/*Purpose: Populates div with HTML onclick via AJAX	*/
/*==================================================*/
function getPropDetails(type,thisid) {

	document.getElementById('sub_'+thisid).innerHTML = "<img src='/images/loading.gif'>";

	var remoteConnection = new ASXML();

	remoteConnection.setCallback(
		function (loadOK){
		if(loadOK)	{
			var x = remoteConnection.textResponse();
			var y = document.getElementById('sub_'+thisid);
			y.innerHTML = x;
			}
		}
    );	
	remoteConnection.getXML("/GetAJAX.aspx?t="+type+"&id="+thisid);
}

/*==================================================*/
/*Function: getStudentAccomodationDetails()						*/
/*Purpose: Populates div with HTML onclick via AJAX	*/
/*==================================================*/
function getStudentAccomodationDetails(type,thisid) {

	document.getElementById('sub_'+thisid).innerHTML = "<img src='/images/loading.gif'>";

	var remoteConnection = new ASXML();

	remoteConnection.setCallback(
		function (loadOK){
		if(loadOK)	{

			var x = remoteConnection.textResponse();
			var y = document.getElementById('sub_'+thisid);
			y.innerHTML = x;
			}
		}
	);

	remoteConnection.getXML("/GetAJAX.aspx?t="+type+"&id="+thisid);

}

/*==============================*/
/*Function: processAd()			*/
/*Purpose: Saves or Blocks ad	*/
/*==============================*/
function processAd(type, thisid) {
	var remoteConnection = new ASXML();

	remoteConnection.setCallback(
		function (loadOK){
		if(loadOK)	{

			var x = remoteConnection.textResponse();
			var y = document.getElementById('sub_'+thisid);
			y.innerHTML = x;
			}
		}
	);
	remoteConnection.getXML("/GetAJAX.aspx?t="+type+"&id="+thisid);
}

/*================================================================*/
/*Function: blockChar()											  */
/*Purpose: Prevents < or > characters from being entered in input */
/*================================================================*/
function blockChar(objEvent){
	var iKeyCode, strKey;

	if (objEvent.keyCode){
		//is IE
		iKeyCode = objEvent.keyCode
	} else {
		iKeyCode = objEvent.which;
	}
	strKey = String.fromCharCode(iKeyCode);
	
	if ((strKey == '<') || (strKey == '>')) return false;
}

/*================================================================*/
/*Function: toggleLayer()										  */
/*Purpose: script for hiding or showing divs					  */
/*================================================================*/
function toggleLayer(whichLayer)
{
	if (document.getElementById)
	{
	// this is the way the standards work
		var style2 = document.getElementById(whichLayer).style;
		style2.display = style2.display? "":"block";
	}
	else if (document.all)
	{
	// this is the way old msie versions work
		var style2 = document.all[whichLayer].style;
		style2.display = style2.display? "":"block";
	}
	else if (document.layers)
	{
	// this is the way nn4 works
		var style2 = document.layers[whichLayer].style;
		style2.display = style2.display? "":"block";
	}
}