var upload_progress = false;
  
function trimLeft(s) {
	var whitespaces = " \t\n\r";
	for(n = 0; n < s.length; n++) { 
		if (whitespaces.indexOf(s.charAt(n)) == -1) 
			return (n > 0) ? s.substring(n, s.length) : s; 
	}
	return("");
}
function trimRight(s){
	var whitespaces = " \t\n\r";
	for(n = s.length - 1; n  > -1; n--) { 
		if (whitespaces.indexOf(s.charAt(n)) == -1) 
			return (n < (s.length - 1)) ? s.substring(0, n+1) : s; 
	}
	return("");
}
function trim(s) {
	return ((s == null) ? "" : trimRight(trimLeft(s))); 
}
function isBlank(edit_id, ErrorMsg) {
    var ff=document.getElementById(edit_id);
	var strTrimmed = trim(ff.value);
	if (strTrimmed.length > 0) return false;
	alert(ErrorMsg);
	ff.focus();
	return true;
}

function isEmptyFile(edit_id, ErrorMsg) {
	var edit_id2 = edit_id+"_file";
    var ff=document.getElementById(edit_id2);
	var strTrimmed = trim(ff.value);
	if (strTrimmed.length > 0){
		upload_progress=true;
		 return false;
	}
	alert(ErrorMsg);
	ff.focus();
	return true;
}

function isFile(edit_id, ErrorMsg) {
	var edit_id2 = edit_id+"_file";
    var ff=document.getElementById(edit_id2);
	var strTrimmed = trim(ff.value);
	if (strTrimmed.length > 0){
		upload_progress=true;
	}
	return false;
}

function isFileUpload(edit_id, ErrorMsg) {
    var edit_id2 = edit_id+"_file";
    var ff=document.getElementById(edit_id2);
    var strTrimmed = trim(ff.value);
    if (strTrimmed.length > 0){
        upload_progress=true;
    }
    return false;
}

function isNotBlank(edit_id, ErrorMsg) {
    var ff=document.getElementById(edit_id);
        var strTrimmed = trim(ff.value);
        if (strTrimmed.length > 0) return true;
        alert(ErrorMsg);
        ff.focus();
        return false;
}
function isSamePassword (edit_id, ErrorMsg, onTheFly) {
	var ff1 = document.getElementById('new1_password_1_');
	var ff2 = document.getElementById('new2_password_1_');
	if ((ff1 == null) || (ff1 == undefined) || (ff2 == null) || (ff2 == undefined))
		return true;
	if ((trim(ff1.value)) == (trim(ff2.value))) {
		return true;
	}
	alert(ErrorMsg);
	ff1.focus();
	return false;
}
function isPassword(edit_id, ErrorMsg) {
var 	id1=concat("_new1_",edit_id);
alert(id1.value);
    var ff1=document.getElementById("_new1_".edit_id);
    var ff2=document.getElementById("_new2_".edit_id);
        var strTrimmed = trim(ff.value);
        if (strTrimmed.length > 0) return true;
        alert(ErrorMsg);
        ff.focus();
        return false;
}
function isBlankRadio(edit_id, ErrorMsg) {
    var ff=document.getElementById(edit_id);

    return(false);
    // This needsd to be debugged
	if (returnSelection(ff) == null) {
		alert(ErrorMsg);
		ff.focus();
		return true;
	} else {
		return false;
	}
}

function isBlankStarRating (id, ErrorMsg) {
	var name = id.substr(0, id.length - 3);
	var index = id.substr(id.length - 2, 1);
	var name = name + '[' + index + ']';
	var control = $("input[name='" + name + "']:checked");
	var value = control.val();
	if (value == undefined || value == null || value == '') {
		alert(ErrorMsg);
		return true;
	}
	return false;
}

function isBadURL(edit_id, ErrorMsg) {
    var ff=document.getElementById(edit_id);

	var strTrimmed = trim(ff.value);
	if (strTrimmed.length == 0 || 
		strTrimmed.substring(0,7) == 'http://' || 
		strTrimmed.substring(0,6) == 'ftp://' || 
		strTrimmed.substring(0,7) == 'mailto:' || 
		strTrimmed.substring(0,8) == 'https://') return false;
	alert(ErrorMsg);
	ff.focus();
	return true;
}
function isNumber(edit_id, ErrorMsg) {
    var ff=document.getElementById(edit_id);

	var strVal = trim(ff.value);
	if (strVal.length == 0) return true;
	var 	x = 0;
	for (i=0;i < strVal.length; i++) { 
			if ((strVal.charAt(i) >= '0' && strVal.charAt(i) <= '9') || 
                   	strVal.charAt(i) == "," ||
					strVal.charAt(i) == "." || 
					strVal.charAt(i) == '£'){
				   	x++;
			}else{
				alert(strVal.charAt(i));
			}
	}
	if (strVal.length > x) {
		alert(ErrorMsg);
		ff.focus();
		return false;
	} else {
		return true;
	}
}
function isEmail(edit_id, ErrorMsg) {
    var ff=document.getElementById(edit_id);

	var strMsg = ""; 
	var chAt  = '@'; 
	var chDot = '.'; 
	var strEmailAddr = trim(ff.value);
	   if (strEmailAddr.length == 0) return true;
	   if (strEmailAddr.indexOf(" ") == -1)
	   {
	       var iFirstAtPos = strEmailAddr.indexOf(chAt);
	       var iLastAtPos = strEmailAddr.lastIndexOf(chAt);
	       if (iFirstAtPos > 0 && iFirstAtPos < (strEmailAddr.length - 1) &&iFirstAtPos == iLastAtPos) {
		   // look for '.' there must be at least one char between '@' and '.'
		   var iDotPos = strEmailAddr.indexOf(chDot, iFirstAtPos + 1);
		   if (iDotPos > (iFirstAtPos + 1) && iDotPos < (strEmailAddr.length -1)) return true;
	       }
	   }
	   alert(ErrorMsg);
	   ff.focus();
	   return false;
}

function isFirstOption(edit_id, ErrorMsg) {

    var ff=document.getElementById(edit_id);

	if (ff.options[0].selected) {
		alert(ErrorMsg);
	    ff.focus();
		return true;
	} else {
		return false;
	}
}

function returnSelection(radioButton) {

	var selection=null;
    alert("Radio Button");
	for(var i=0; i<radioButton.length; i++) {
		    alert(i);
		if(radioButton[i].checked) {
		    alert("selected");
			selection=radioButton[i].value;
			return selection;
		}
	}
	return selection; 
}

function isChecked(edit_id, ErrorMsg) {
    var ff=document.getElementById(edit_id);

	if ( ff.checked == 1 ) 
	{
		return true;
	} else { 	
		alert(ErrorMsg);
		ff.focus();
		return false;
	}
}
function addLoadEvent(func) {
    var oldonload = window.onload;
    if(typeof window.onload != "function")
        window.onload = func;
    else
        window.onload = function() {
        oldonload();
        func();
    }
}
  function DisableSubmit(btnSubmit){
	if(validate_form()){
    	var submitButton = document.getElementById(btnSubmit);
    	submitButton.disabled = true;
		scroll(0,0);
		//document.getElementById('dyna_msg').innerhtml="Please wait, this may take a short time";
		document.getElementById('cjt_to_be_hidden').style.display='none';
		document.getElementById('inprogress').style.display='block';
		document.getElementById('inprogress').style.backgroundImage='url(/ukcarads/upload_progress.gif)';
		//alert("Submitting Advert - Please wait this may take some time ...");
		return(true);
	}
	return(false);
  }

function changePage(newLoc)
 {
   nextPage = newLoc.options[newLoc.selectedIndex].value
		
   if (nextPage != "")
   {
      document.location.href = nextPage
   }
}

//<!--[if lt IE 7]>
//<script language="JavaScript">
//window.attachEvent("onload", correctPNG);
//</script>
//<![endif]-->

function correctPNG() // correctly handle PNG transparency in Win IE 5.5 & 6.
{
   var arVersion = navigator.appVersion.split("MSIE")
   {
	  for(var i=0; i<document.images.length; i++)
	  {
		 var img = document.images[i]
		 var imgName = img.src.toUpperCase()
		 if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
		 {
			var imgID = (img.id) ? "id='" + img.id + "' " : ""
			var imgClass = (img.className) ? "class='" + img.className + "' " : ""
			var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
			var imgStyle = "display:inline-block;" + img.style.cssText 
			if (img.align == "left") imgStyle = "float:left;" + imgStyle
			if (img.align == "right") imgStyle = "float:right;" + imgStyle
			if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
			var strNewHTML = "<span " + imgID + imgClass + imgTitle
			+ " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
			+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
			+ "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
			img.outerHTML = strNewHTML
			i = i-1
		 }
	  }
   }    
}

function input_star_rating (field_name, field_id, max_rating, image_size, labels) {

	function star_url (value, image_size) {
		var colour = value >= 1 ? 'gold' : 'white';
		return '/images/stars/' + colour + '/star-' + colour + image_size + '.png';
	}

	function star (id, field_name, star_value, value, image_size) {
		var url = star_url(value, image_size);
		var s = document.createElement('span');
		var id = id;
		var field_name = field_name;
		s.id = id + 'star' + star_value;
		s.value = star_value;
		s.className = 'rating-star';
		s.style.display = 'block';
		s.style.cssFloat = 'left';
		s.style.styleFloat = 'left';
		s.style.height = image_size + 'px';
		s.style.width = image_size + 'px';
		s.style.backgroundImage = 'url("' + url + '")';
		s.style.cursor = 'pointer';
		s.onclick = function() { star_click(s, field_name); };
		s.onmouseover = function() { star_hover(s, field_name); };
		s.onmouseout = function() { updateRating(); };
		return s;
	}

	function rating_label (id, value, labels, image_size) {
		var l = document.createElement('div');
		l.id = id;
		l.className = 'rating-label';
		l.style.display = 'table-cell';
		l.style.height = image_size + 'px';
		l.style.width = image_size + 'px';
		l.style.verticalAlign = 'middle';
		l.style.paddingLeft = '0.5em';
		l.style.cssFloat = 'left';
		l.style.styleFloat = 'left';
		if (labels[value - 1] !== undefined && labels[value - 1] !== null) {
			l.innerHTML = labels[value - 1];
		}
		return l;
	}

	function star_click (s, field_name) {
		value = s.value;
		$("input[name='" + field_name + "'][value='" + s.value + "']").attr('checked', true);
		updateRating();
	}

	function star_hover (s, field_name) {
		var temp_value = s.value;
		for (var x = max_rating; x > 0; x--) {
			var star_value = max_rating - x + 1;
			document.getElementById(field_id + "star" + star_value).style.backgroundImage = 'url("' + star_url(temp_value, image_size) + '")';
			temp_value--;
		}
		document.getElementById(field_id + 'label').innerHTML = (labels[s.value - 1] !== undefined && labels[s.value - 1] !== null) ? labels[s.value - 1] : '';
	}

	function updateRating () {
		var temp_value = value;
		for (var x = max_rating; x > 0; x--) {
			var star_value = max_rating - x + 1;
			document.getElementById(field_id + "star" + star_value).style.backgroundImage = 'url("' + star_url(temp_value, image_size) + '")';
			temp_value--;
		}
		document.getElementById(field_id + 'label').innerHTML = (labels[value - 1] !== undefined && labels[value - 1] !== null) ? labels[value - 1] : '';
	}

	var container = $('div#' + field_id);
	container.hide();
	var value = $("input[name='" + field_name + "']:checked").val();

	var temp_value = value;
	for (var x = max_rating; x > 0; x--) {
		var star_value = max_rating - x + 1;
		container.before(star(field_id, field_name, star_value, temp_value, image_size));
		temp_value--;
	}

	container.before(rating_label(field_id + 'label', value, labels, image_size));

}

