// JavaScript Document
function showUserInformation(insertRowId, userId){
	var reLoadDiv = insertRowId+'_insert';
	var params = "func=showUserInformation&userId="+userId+"&insertRow="+insertRowId
	$.post('asp/remotes/genericRemotes.asp', params, function(data){$('#'+reLoadDiv).html(data); $('#'+insertRowId).show()})	
}


function closeElement(elemId){
	$('#'+elemId).hide()
}


function isInteger(str)
{
 return (str.search(/^[0-9]*$/) != -1);
}

function isMoney(str)
{
 return (str.search(/^[0-9]*(\.[0-9]{1,2})?$/) != -1);
}

function isPeriod(str)
{
 if(str.charAt(str.length - 2) == 0) {
   return (str.search(/^2[0-9]{3}[0][0-9]$/) != -1);}
 else {
   return (str.search(/^2[0-9]{3}[1][012]$/) != -1);}
}

function isEmailAddress(str)
{
   return /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(str);
}  

function isBlank(str){
	for (var i = 0; i <str.length; i++){
		var c = str.charAt(i);
	if ((c != ' ') && (c != '\n') && (c !='')) return false;
	}
	return true
}

function isDateShort(str)
 {
 if(str.search(/^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{1,4}$/) != -1)
  {
    var elements = str.split("/");
    if(elements.length == 3) 
    {
     var eDate = new Date(parseInt(elements[2], 10), parseInt(elements[1], 10) - 1, parseInt(elements[0], 10));
      if(!isNaN(eDate) && ((eDate.getMonth() + 1) == (elements[1] - 0))) 
      {
        return true;
      }
    }    
  }
  return false;
}  

function isDateFull(str)
{
 return (str.search(/^\d{2}(\-|\/|\.)\d{2}\1\d{4}$/) != -1);
} 

function prepInput(elementID, origValue){
	var elmtValue = document.getElementById(elementID).value;
	
	if(elmtValue == origValue){
		document.getElementById(elementID).value = "";
	}else{
		document.getElementById(elementID).select();
	}
} 


function showFullDate(date,element)
//Either include validation.js or have the date validation functions present
{
 if(isDateShort(date))
 {
 var currYear = new Date()
 currYear = new String(currYear.getFullYear())
 var tempDate = date.split("/")
 var tempDay = tempDate[0]
 var tempMonth = tempDate[1]
 var tempYear = tempDate[2]
 if(tempDay.length == 1)
 {tempDay = "0"+tempDay}
 if(tempMonth.length == 1)
 {tempMonth = "0"+tempMonth}
 if(tempYear.length == 1)
 {tempYear = currYear.substring(0,3)+tempYear}
 if(tempYear.length == 2)
 {tempYear = currYear.substring(0,2)+tempYear}
 tempDate = tempDay+"/"+tempMonth+"/"+tempYear
 document.getElementById(element).value = tempDate	
 } 
}

function makeWindow(url,name,width,height)
{
	url=encodeURI(url);
	window.open(url,name,"menubar=0, width="+width+",height="+height+",location=0, toolbar=0, resizeable=0, scrollbars=1")
}

//Begin Capture screen size to cookies!
function getScreenSize() {
var xy = navigator.appVersion; 
xz = xy.substring(0,4); 
document.cookie = "ScreenWidth=" + screen.width
document.cookie = "ScreenHeight=" + screen.height
}

function checkInputLength(elem,length,restriction)
{
	if(length > restriction){
		alert("Sorry you cannot enter more than " + restriction + " characters in this field.\nYour text will now be truncated")
		getElement(elem).value = getElement(elem).value.substring(0,restriction)
	}
}

function fnAjaxError(responseText, lengthInt){
	//location.href = 'default.asp?errMsgCode=TIMEOUT';
	alert(String(responseText).substring(String(responseText).length-lengthInt,String(responseText).length))
}

function toggleCSS(id,origCSS,newCSS) {
	var curCSS = document.getElementById(id).href;
	var URLend = curCSS.lastIndexOf("/");
	var baseURL = left(curCSS,URLend+1);
	
	/*alert(curCSS.lastIndexOf("Base2.css"))*/
	if (curCSS.lastIndexOf(newCSS)>0) {
		document.getElementById(id).href = baseURL+origCSS
		document.getElementById('gridCSS').href = baseURL+"reset-fonts-grids.css"
	} else {
		document.getElementById(id).href = baseURL+newCSS
		document.getElementById('gridCSS').href = baseURL+"reset-fonts-grids-B.css"
	}
}

function left(str, n){
	if (n <= 0)
		return "";
	else if (n > String(str).length)
		return str;
	else
		return String(str).substring(0,n);
}

function right(str, n){
	if (n <= 0)
	   return "";
	else if (n > String(str).length)
	   return str;
	else {
	   var iLen = String(str).length;
	   return String(str).substring(iLen, iLen - n);
	}
}

function goback() {
    history.go(-1);
}

var POSTCODE_LOOKUP_PAGE = "/asp/remotes/postcode.asp"

function lookupPostcode(){
	$('#ajaxLoading').removeClass('invisible');
	$('#ajaxLoading').show();
	
	var args = { 
		postcode: $('#postcode').val(), 
		premises: $('#houseNumber').val()
	};

	$.get(POSTCODE_LOOKUP_PAGE, args,
		function(data){
			$('#addressOptions').html(data).hide();
			
			if(data == "No Matches"){
			
				$('#addressOptions').show();
				$('#selectPrompt').hide();
				$('#ajaxLoading').hide();
				return;
				
			}
			
			if($('#foundvals option').length > 1){
			
				$('#addressOptions').show();
				$('#selectPrompt').removeClass('invisible');
				$('#selectPrompt').show();
				
			}else{
			
				$('#selectPrompt').hide();
			
			}
			
			$('#ajaxLoading').hide();
			insertVals($('#foundvals').val());
			
		},"html");
	
}

function insertVals(vals, removePrompt){
	if (removePrompt){
	
		$('#addressOptions').hide();
		$('#selectPrompt').hide();
	
	}
	
	var valArray = vals.split("|",3);
	
	$('#houseNumber').val(valArray[0].replace(/\+/g," "));
	$('#addressLine1').val(valArray[1].replace(/\+/g," "));
	$('#addressLine2').val(valArray[2].replace(/\+/g," "));
	$('#houseNumber').effect("highlight", {}, 1000);
	$('#addressLine1').effect("highlight", {}, 1000);
	$('#addressLine2').effect("highlight", {}, 1000);
	
}

function getElement(id)
// Sorts out getElementByID etc across browsers
{
  if(document.getElementById) return document.getElementById(id);
  return document.all[id];
}

function ifOther(dropObj,divToToggle) {
	
	var selObj = document.getElementById(dropObj);
	var selIndex = selObj.selectedIndex;
	var selText = selObj.options[selIndex].text;

	if (selText!='Other') {
		hide(divToToggle)
	} else {
		show(divToToggle)
	}
}

function hide(id)
// Hides element id
{
  var element = getElement(id);
  if(element)
  {
    with(element)
    {
      style.visibility = "hidden";
      style.height = "0px";
      style.display = "none";
    }
  }
}
function show(id)
// Shows element id
{
  var element = getElement(id);
  if(element)
  {
    with(element)
    {
      style.visibility = "visible";
      style.height = "auto";
      //style.display = "inline";
	  style.display = "";
    }
  }
}

