// JavaScript Document




function populateMap(divMap, postcode, commentMap ) {
 //this requires an ajax api call, to get the lon/lat for a postcode as teh maps api does not provide reliable postcode data.
 //this in turn is passedtothe google maps api as a point
  localSearch.setSearchCompleteCallback(null,
    function() {
      
      if (localSearch.results[0]) {    
        var resultLat = localSearch.results[0].lat;
        var resultLng = localSearch.results[0].lng;
        var point = new GLatLng(resultLat,resultLng);
        displayMap(divMap, point, commentMap);
		//alert(point)
      }else{
        //alert("Postcode not found!");
		var point = "";
		displayMap(divMap, point, commentMap);
      }
    });  
    
  localSearch.execute(postcode + ", UK");
}




function displayMap(divMap, point, commentMap)
	{
		var map = null
		//var geocoder = null
		
		// divMap - id of div to populate with google map
		// addressMap - address to center on, more detail the better, min 1st line & post code
		// commentMap - string to place in marker on map, HTML accepted
		
		if (GBrowserIsCompatible())
		{
			var map = new GMap2(document.getElementById(divMap));
			
			map.setUIToDefault();;
			//geocoder = new GClientGeocoder();
				
						if(!point) {
							// next block executes if the address cannot be found
							var divMapsHTML = null
							divMapsHTML = document.getElementById(divMap).innerHTML;
							document.getElementById(divMap).innerHTML = divMapsHTML + "<div class='mapErrorText'>Error locating address. Please contact owner for the location.</div>";
							window.focus();
						} else {
							// address found - add point to map
							
							map.setCenter(point, 13);
							var marker = new GMarker(point);
							map.addOverlay(marker);
							map.setZoom(11);
							//marker.openInfoWindowHtml(commentMap,{maxWidth:150});
						
							window.focus();
						}
				
			
		}
	}

function hideMap (divMap){
		var map = null
		var geocoder = null
		
		// divMap - id of div to populate with google map

		
		if (GBrowserIsCompatible())
		{
			var map = new GMap2(document.getElementById(divMap));
			map.unload();
		}
	}
	
	
	
//for captcha image changing
function resetCaptcha() {
	//var objImage = document.images[valImageId];
	//if (objImage == undefined) {
	//	return;
	//}
	var now = new Date();
	$('.captchaImage').attr({src : 'theme/fsgWeb/assets/captcha.asp?x='+ now.toUTCString()});
	//objImage.src = objImage.src.split('?')[0] + '?x=' + now.toUTCString();
}

function uploadFile(oForm,sIframeName,sStatusDiv){
	//record action for later
	var currentAction=oForm.action;
	
	//alter form target for file upload
	oForm.target = sIframeName;
	oForm.action = '../theme/tgif/assets/uploadCapture.asp';
	oForm.encoding = 'multipart/form-data';
	oForm.submit();
	
	//show the upload bar
	//document.getElementById(sIframeName).style.visibility = 'visible';
	document.getElementById(sStatusDiv).innerHTML= '<img src="../theme/tgif/assets/loadingbar.gif"/>';
	
	//now put back all the form actiosn etc to be as they were
	oForm.target = '_self';
	oForm.action = currentAction;
	oForm.encoding = 'application/x-www-form-urlencoded';
	
}	
function populateNoOfRecords(){

//alert('test');
}

//from sda.js used on edit page
function popupManual(href, name, features) {
	theWindow = window.open(href, name, features);
	theWindow.focus();
}
function popup_size (obj,iW,iH) {
	var winW = iW;
	var winH = iH;
	var winName = '_blank';
	var winL = (screen.width - winW) / 2;
	var winT = (screen.height - winH) / 2;
	window.open(obj.href, winName, 'location=no, toolbar=no, status=no, directories=no, scrollbars=no, menubar=0, resizable=yes, left=' + winL + ', top=' + winT +', width=' + winW + ', height=' + winH);
}
function popupAuto(href, name, width, height) {
	var w = width
	var h = height
	var name = name
	var l = (screen.width - w) / 2;
    var t = (screen.height - h) / 2;
	theWindow = window.open(href, name, 'location=no, toolbar=no, status=yes, directories=no, scrollbars=yes, menubar=0, resizable=yes, left=' + l + ', top=' + t +', width=' + w + ', height=' + h);
	theWindow.focus();
}
function hide(targetId){   
	if (document.getElementById){         
	target = document.getElementById(targetId);                          
		target.className = "noDisplay";    
	} 
}
function show(targetId){   
	if (document.getElementById){         
	target = document.getElementById(targetId);                          
		target.className = "";    
	} 
}

//from form.js
//enabled submit on if tickbox is ticked


function flipSubmit(oObject) {

	if(navigator.userAgent.indexOf("MSIE")!=-1){
		//IE
		if (oObject.checked == true) {
			//enable submit button			
			document.all.submit.disabled = false; 
		} else { 
			//disabled submit button			
			document.all.submit.disabled = true; //IE
		}		
	} else { 
		//Firefox, and others
		if (oObject.checked == true) {
			//enable submit button			
			document.getElementById('submit').disabled = false;
		} else {
			//disabled submit button
			document.getElementById('submit').disabled = true; //Firefox
		}		
	}
	
}

function validateForm(oForm) {
	sValidationString = oForm.elements['validation'].value;
	//alert(sValidationString);
	
	//question submission
	bConfirm = confirm('Are you sure you wish to submit this form?')
	if (bConfirm) {
		//loop through all form fields
		sFieldName = '';
		sReturn = '';
		iTotal = 0;
		bTest = false;
		for (var i=0;i < oForm.length;i++) {
			//check if field is require for validation
			
			
			//for (var iSub=0;iSub < oForm.getElementsByTagName('label').length;iSub++) {			
				
				//if (oForm.elements[i].type == 'label' && oForm.elements[i].name == oForm.elements[iSub].getAttribute('for')) {
				//if (oForm.elements[i].type == 'label') {
					//sReturn += '-' + oForm.elements[iSub].getAttribute('for')
				//}
			//}
				
			sFieldName = oForm.elements[i].name
			sFieldTitle = oForm.elements[i].title
			sFieldValue = oForm.elements[i].value
			if (sFieldTitle == '') {
				sFieldTitle = sFieldName
			}	
			bTest = false;
	//		alert(sFieldName)
			if (sValidationString.indexOf(sFieldName + '[isEmail]') >= 1 && sFieldName != '') {
				if (sFieldValue == '') {
					sReturn += '- ' + sFieldTitle + ' is required\n';
					oForm.elements[i].style.backgroundColor = '#FFCCCC';
				} else if (sFieldValue.indexOf('@') <= 0) {
					sReturn += '- ' + sFieldTitle + ' must be an e-mail address\n';
					oForm.elements[i].style.backgroundColor = '#FFCCCC';
				} else {
	//				sReturn += '-' + sFieldName + '(' + sFieldValue + ') is OK\n';
					oForm.elements[i].style.backgroundColor = '';
				}
			} else if (sValidationString.indexOf(sFieldName + '[isNum]') >= 1 && sFieldName != '') {
				if (sFieldValue == '') {
					sReturn += '- ' + sFieldTitle + ' is required\n';
					oForm.elements[i].style.backgroundColor = '#FFCCCC';
				} else if (isNaN(sFieldValue)) {
					sReturn += '- ' + sFieldTitle + ' is not a number\n';
					oForm.elements[i].style.backgroundColor = '#FFCCCC';
				} else {
	//				sReturn += '-' + sFieldName + '(' + sFieldValue + ') is OK\n';
					oForm.elements[i].style.backgroundColor = '';
				}
			} else if (sValidationString.indexOf(sFieldName + '[isWholeNum]') >= 1 && sFieldName != '') {
				if (sFieldValue == '') {
					sReturn += '- ' + sFieldTitle + ' is required\n';
					oForm.elements[i].style.backgroundColor = '#FFCCCC';
				} else if (isNaN(sFieldValue)) {
					sReturn += '- ' + sFieldTitle + ' is not a number\n';
					oForm.elements[i].style.backgroundColor = '#FFCCCC';
				} else if (sFieldValue.indexOf('.') > -1) {
					sReturn += '- ' + sFieldTitle + ' must be a whole number\n';
					oForm.elements[i].style.backgroundColor = '#FFCCCC';
				} else if (sFieldValue < 0) {
					sReturn += '- ' + sFieldTitle + ' must be a positive whole number. Please contact Budget-Pack on 01453 793020 for advice on submitting negative amounts.\n';
					oForm.elements[i].style.backgroundColor = '#FFCCCC';
				} else {
	//				sReturn += '-' + sFieldName + '(' + sFieldValue + ') is OK\n';
					oForm.elements[i].style.backgroundColor = '';
				}
			} else if (sValidationString.indexOf(sFieldName + '[isNotZero]') >= 1 && sFieldName != '') {
				if (sFieldValue == '') {
					sReturn += '- ' + sFieldTitle + ' is required\n';
					oForm.elements[i].style.backgroundColor = '#FFCCCC';
				} else if (sFieldValue == 0) {
					sReturn += '- ' + sFieldTitle + ' must be selected\n';
					oForm.elements[i].style.backgroundColor = '#FFCCCC';
				} else {
	//				sReturn += '-' + sFieldName + '(' + sFieldValue + ') is OK\n';
					oForm.elements[i].style.backgroundColor = '';
				}				
			} else if (sValidationString.indexOf(sFieldName + '[isSelected]') >= 1 && sFieldName != '') {
				//loop through entire form again to find checked radio buttons
				for (var iSub=0;iSub < oForm.length;iSub++) {				
					if (oForm.elements[iSub].name == sFieldName && oForm.elements[iSub].checked) {
						//if field is checked then set bol
						bTest = true
					}
				}
				//output message
				if (bTest && sFieldName != sPreviousFieldName) {
	//				sReturn += '-' + sFieldName + '(' + sFieldValue + ') is OK\n';
					oForm.elements[i].style.backgroundColor = '';
				} else if (!bTest && sFieldName != sPreviousFieldName) {
					sReturn += '- ' + sFieldTitle + ' is required\n';					
					oForm.elements[i].style.backgroundColor = '#FFCCCC';
				} else if (!bTest) {
					// colour all same names check boxes
					oForm.elements[i].style.backgroundColor = '#FFCCCC';
				} else {
					oForm.elements[i].style.backgroundColor = '';
				}			
	//		if (sValidationString.indexOf(sFieldName + '[R]') >= 1 && sFieldName != '') {
			} else if (sValidationString.indexOf(sFieldName) >= 1 && sFieldName != '') {
				if (sFieldValue == '') {
					sReturn += '- ' + sFieldTitle + ' is required\n';
					oForm.elements[i].style.backgroundColor = '#FFCCCC';
				} else {
	//				sReturn += '-' + sFieldName + '(' + sFieldValue + ') is OK\n';
					oForm.elements[i].style.backgroundColor = '';
				}
			} else {
	//			sReturn += '-' + sFieldName + '(' + sFieldValue + ') is OK\n';
				oForm.elements[i].style.backgroundColor = '';
			}		
			
			sPreviousFieldName = sFieldName
		}
		
		if (sReturn) {
			alert('Please fill out all the following required fields\nbefore submitting this form.\n' + sReturn);
			return(false)
		} else {
			
		
			return(true);
		}
	} else {
			return(false)
	}	
		
}

function validateFormNoConfirm(oForm) {
	
	sValidationString = oForm.elements['validation'].value;
	//alert(sValidationString);
	
	//question submission
	bConfirm = true;
	if (bConfirm) {
		//loop through all form fields
		sFieldName = '';
		sReturn = '';
		iTotal = 0;
		bTest = false;
		for (var i=0;i < oForm.length;i++) {
			//check if field is require for validation
			
			
			//for (var iSub=0;iSub < oForm.getElementsByTagName('label').length;iSub++) {			
				
				//if (oForm.elements[i].type == 'label' && oForm.elements[i].name == oForm.elements[iSub].getAttribute('for')) {
				//if (oForm.elements[i].type == 'label') {
					//sReturn += '-' + oForm.elements[iSub].getAttribute('for')
				//}
			//}
				
			sFieldName = oForm.elements[i].name
			sFieldTitle = oForm.elements[i].title
			sFieldValue = oForm.elements[i].value
			if (sFieldTitle == '') {
				sFieldTitle = sFieldName
			}	
			bTest = false;
	//		alert(sFieldName)
			if (sValidationString.indexOf(sFieldName + '[isEmail]') >= 1 && sFieldName != '') {
				if (sFieldValue == '') {
					sReturn += '- ' + sFieldTitle + ' is required\n';
					oForm.elements[i].style.backgroundColor = '#FFCCCC';
				} else if (sFieldValue.indexOf('@') <= 0) {
					sReturn += '- ' + sFieldTitle + ' must be an e-mail address\n';
					oForm.elements[i].style.backgroundColor = '#FFCCCC';
				} else {
	//				sReturn += '-' + sFieldName + '(' + sFieldValue + ') is OK\n';
					oForm.elements[i].style.backgroundColor = '';
				}
			} else if (sValidationString.indexOf(sFieldName + '[isNum]') >= 1 && sFieldName != '') {
				if (sFieldValue == '') {
					sReturn += '- ' + sFieldTitle + ' is required\n';
					oForm.elements[i].style.backgroundColor = '#FFCCCC';
				} else if (isNaN(sFieldValue)) {
					sReturn += '- ' + sFieldTitle + ' is not a number\n';
					oForm.elements[i].style.backgroundColor = '#FFCCCC';
				} else {
	//				sReturn += '-' + sFieldName + '(' + sFieldValue + ') is OK\n';
					oForm.elements[i].style.backgroundColor = '';
				}
			} else if (sValidationString.indexOf(sFieldName + '[isWholeNum]') >= 1 && sFieldName != '') {
				if (sFieldValue == '') {
					sReturn += '- ' + sFieldTitle + ' is required\n';
					oForm.elements[i].style.backgroundColor = '#FFCCCC';
				} else if (isNaN(sFieldValue)) {
					sReturn += '- ' + sFieldTitle + ' is not a number\n';
					oForm.elements[i].style.backgroundColor = '#FFCCCC';
				} else if (sFieldValue.indexOf('.') > -1) {
					sReturn += '- ' + sFieldTitle + ' must be a whole number\n';
					oForm.elements[i].style.backgroundColor = '#FFCCCC';
				} else {
	//				sReturn += '-' + sFieldName + '(' + sFieldValue + ') is OK\n';
					oForm.elements[i].style.backgroundColor = '';
				}
			} else if (sValidationString.indexOf(sFieldName + '[isNotZero]') >= 1 && sFieldName != '') {
				if (sFieldValue == '') {
					sReturn += '- ' + sFieldTitle + ' is required\n';
					oForm.elements[i].style.backgroundColor = '#FFCCCC';
				} else if (sFieldValue == 0) {
					sReturn += '- ' + sFieldTitle + ' must be selected\n';
					oForm.elements[i].style.backgroundColor = '#FFCCCC';
				} else {
	//				sReturn += '-' + sFieldName + '(' + sFieldValue + ') is OK\n';
					oForm.elements[i].style.backgroundColor = '';
				}				
			} else if (sValidationString.indexOf(sFieldName + '[isSelected]') >= 1 && sFieldName != '') {
				//loop through entire form again to find checked radio buttons
				for (var iSub=0;iSub < oForm.length;iSub++) {				
					if (oForm.elements[iSub].name == sFieldName && oForm.elements[iSub].checked) {
						//if field is checked then set bol
						bTest = true
					}
				}
				//output message
				if (bTest && sFieldName != sPreviousFieldName) {
	//				sReturn += '-' + sFieldName + '(' + sFieldValue + ') is OK\n';
					oForm.elements[i].style.backgroundColor = '';
				} else if (!bTest && sFieldName != sPreviousFieldName) {
					sReturn += '- ' + sFieldTitle + ' is required\n';					
					oForm.elements[i].style.backgroundColor = '#FFCCCC';
				} else if (!bTest) {
					// colour all same names check boxes
					oForm.elements[i].style.backgroundColor = '#FFCCCC';
				} else {
					oForm.elements[i].style.backgroundColor = '';
				}			
	//		if (sValidationString.indexOf(sFieldName + '[R]') >= 1 && sFieldName != '') {
			} else if (sValidationString.indexOf(sFieldName) >= 1 && sFieldName != '') {
				if (sFieldValue == '') {
					sReturn += '- ' + sFieldTitle + ' is required\n';
					oForm.elements[i].style.backgroundColor = '#FFCCCC';
				} else {
	//				sReturn += '-' + sFieldName + '(' + sFieldValue + ') is OK\n';
					oForm.elements[i].style.backgroundColor = '';
				}
			} else {
	//			sReturn += '-' + sFieldName + '(' + sFieldValue + ') is OK\n';
				oForm.elements[i].style.backgroundColor = '';
			}		
			
			sPreviousFieldName = sFieldName
		}
		
		if (sReturn) {
			alert('Please fill out all the following required fields\nbefore submitting this form.\n' + sReturn);
			return(false)
		} else {
			return(true);
		}
	} else {
			return(false)
	}	
		
}	



// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) 
{
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) 
	{
		if(radioObj[i].checked) 
		{
			return radioObj[i].value;
		}
	}
	return "";
}

function protectMail(address, domain, ext) {
	document.write('<a href="');
	document.write('ma'+'il'+'to:');
	document.write(address);
	document.write('&#64;');
	document.write(domain);
	document.write('.');
	document.write(ext);  
	document.write('">'); 
	document.write(address + '&#64;' + domain + '.' + ext + '</a>');
}