/**
* Del
* Reverted back as was wrong change to core
**/

/**
* Postcode anywhere turboShop driver javascript
**/

var thirdpartyReload = ''; // one of yes or selectaddress - if yes then got signle address prefil


function validateLookup(formElementObj)
{
	if (formElementObj.form.buildingname.value == '')
	{
		alert('Please enter a building name or house number.');
		formElementObj.form.buildingname.focus();

		return false;
	}
	else if (formElementObj.form.postcode.value == '')
	{
		alert('Please enter a postcode.');
		formElementObj.form.postcode.focus();

		return false;
	}
	else if (!formElementObj.form.postcode.value.match(/^[A-Za-z]{1,2}[0-9]{1,2}[A-Za-z]? ?[0-9][A-Za-z]{2}$/))
	{
		alert('Please enter a valid UK postcode with a space.');
		formElementObj.form.postcode.focus();

		return false;
	}
	else
		return true;
}
/**
* check postcode asnywhere fields and submit module form as normal but bypassing normal validation
**/
function lookupAddress(formElementObj)
{

	if (!validateLookup(formElementObj))
	{
		return false;
	}

	formElementObj.form.comd.value = 'thirdparty';
	formElementObj.form.thirdparty.value = 'postcodeanywhere';

	formElementObj.form.submit();
}

function isValidObject(objToTest)
{

  if (null == objToTest)
  {
    return false;
	}
	if ("undefined" == typeof(objToTest) )
	{
	  return false;
	}
	return true;
}

function fillAddressBoxesOldFormat(addressSelectObj)
{
	if (addressSelectObj.selectedIndex>0)//first option is select option
	{
		var addrParts = addressSelectObj.options[addressSelectObj.selectedIndex].value.split('|');
		addressSelectObj.form.ts_commadd1.value = addrParts[0];
		addressSelectObj.form.ts_commadd2.value = addrParts[1];

		if (isValidObject(addressSelectObj.form.ts_commadd3) == true)
		{
		  addressSelectObj.form.ts_commadd3.value = addrParts[2];
		}

		if (isValidObject(addressSelectObj.form.ts_commadd4) == true)
		{
		  addressSelectObj.form.ts_commadd4.value = addrParts[3];
		}

		if (isValidObject(addressSelectObj.form.ts_commadd5) == true)
		{
		  addressSelectObj.form.ts_commadd5.value = addrParts[4];
		}

		if (isValidObject(addressSelectObj.form.ts_commpost) == true)
		{
		  addressSelectObj.form.ts_commpost.value = addrParts[5];
	  }
	  if (isValidObject(addressSelectObj.form.ts_commadd6) == true)
	  {
	    addressSelectObj.form.ts_commadd6.value = addrParts[5];
	  }

	  if (isValidObject(addressSelectObj.form.ts_commcoun) == true)
	  {
		  addressSelectObj.form.ts_commcoun.selectedIndex = 1;
		}
		if (isValidObject(addressSelectObj.form.ts_commadd7) == true)
	  {
		  addressSelectObj.form.ts_commadd7.selectedIndex = 0;
		}

	}
}


function fillAddressBoxesForOldRegi(addressSelectObj)
{
	if (addressSelectObj.selectedIndex>0)//first option is select option
	{
		var addrParts = addressSelectObj.options[addressSelectObj.selectedIndex].value.split('|');
		addressSelectObj.form.ts_regiadd1.value = addrParts[0];
		addressSelectObj.form.ts_regiadd2.value = addrParts[1];

		if (isValidObject(addressSelectObj.form.ts_regiadd3) == true)
		{
		  addressSelectObj.form.ts_regiadd3.value = addrParts[2];
		}

		if (isValidObject(addressSelectObj.form.ts_regiadd4) == true)
		{
		  addressSelectObj.form.ts_regiadd4.value = addrParts[3];
		}

		if (isValidObject(addressSelectObj.form.ts_regiadd5) == true)
		{
		  addressSelectObj.form.ts_regiadd5.value = addrParts[4];
		}

		if (isValidObject(addressSelectObj.form.ts_regipcod) == true)
		{
		  addressSelectObj.form.ts_regipcod.value = addrParts[5];
	  }

	  if (isValidObject(addressSelectObj.form.ts_regicoun) == true)
	  {
		  addressSelectObj.form.ts_regicoun.selectedIndex = 1;
		}

	}
}


function fillAddressBoxes(addressSelectObj)
{
	if (addressSelectObj.selectedIndex>0)//first option is select option
	{
		var code = tsGetElementById('td').value;
		var addrParts = addressSelectObj.options[addressSelectObj.selectedIndex].value.split('|');
		for (var i=1; i<7; i++)
		{
			if (i==6 && addrParts.length==7 && tsGetElementById('ts_'+code+'pos1') != null)
		 	{ // split postcode added 24/05/2006
		  		tsGetElementById('ts_'+code+'pos1').value  = addrParts[5];

		  		if (tsGetElementById('ts_'+code+'pos2') != null)
		  			tsGetElementById('ts_'+code+'pos2').value  = addrParts[6];
		 	}
			else if (tsGetElementById('ts_'+code+'add'+i) != null && i<=addrParts.length)
		 	{
		  		tsGetElementById('ts_'+code+'add'+i).value  = addrParts[i-1];
		 	}

		}

		if (tsGetElementById('ts_'+code+'add7')!=null)
		{
			try {
			tsGetElementById('ts_'+code+'add7').selectedIndex = 1;
			}
			catch(e) {
			tsGetElementById('ts_'+code+'add7').focus();
			}
		}

	}

}

function popUpPostcode(formID)
{
	tsGetElementById('postcodeanywherediv').style.display = 'block';
	tsGetElementById('findAddressArea').style.display = 'none';

}

function closepopUpPostcode()
{

	tsGetElementById('postcodeanywherediv').style.display = 'none';
	tsGetElementById('findAddressArea').style.display = 'block';
}

