function clearTxtBox(obj, defaultValue)
{
	var txtboxToClear = document.getElementById(obj);
    if (txtboxToClear.value == defaultValue) {
   		txtboxToClear.value="";
		txtboxToClear.style.color="black";
	}
}

function clearTxtBoxWithClass(obj, defaultValue, inClassName)
{
	var txtboxToClear = document.getElementById(obj);
    if (txtboxToClear.value == defaultValue) {
   		txtboxToClear.value="";
		txtboxToClear.className=inClassName;
	}
}

function defaultTxtBox(obj, defaultValue)
{
	var txtboxToClear = document.getElementById(obj);
    if (txtboxToClear.value == "") {
   		txtboxToClear.value=defaultValue;
   		txtboxToClear.style.color="#CCCCCC"
	}
}

function defaultTxtBoxWithClass(obj, defaultValue)
{
	var txtboxToClear = document.getElementById(obj);
    if (txtboxToClear.value == "") {
		txtboxToClear.className="defaultInput";
   		txtboxToClear.value=defaultValue;
	}
}

function switchVisibility(obj) {
	var el = document.getElementById(obj);
	if ( el.style.display != 'none' ) {
		el.style.display = 'none';
	} else {
		el.style.display = '';
	}
}

function show(obj) {
	var el = document.getElementById(obj);
	el.style.display = '';
}

function hide(obj) {
	var el = document.getElementById(obj);
	el.style.display = 'none';
}

function giveFocus(obj) {
	var getsFocus = document.getElementById(obj);
	getsFocus.focus();
}

function confirmDelete(type) {
	return confirm('Are you sure you want to delete this '+type+'?  There is no undo and it will be permanently removed.');
}

function changePic(img, file)
{
	imgToSet = document.getElementById(img);
	newImg = new Image();
	newImg.src = file;
	imgToSet.src = file;
	imgToSet.height = newImg.height;
	imgToSet.width = newImg.width;
	imgToSet.alt = "Loading Picture...";
}

function resizePic(id, maxheight, maxwidth) {
	imgToSet = document.getElementById(id);
	
	if (imgToSet.height > maxheight) {
		imgToSet.width = imgToSet.width*(maxheight/imgToSet.height);
		imgToSet.height = maxheight;
	}

	//If the width is still greater than it should be
	if (imgToSet.width > maxwidth) {
		imgToSet.height = imgToSet.height*(maxwidth/imgToSet.width);
		imgToSet.width = maxwidth;
	}
}

function fillStars(selected) {
	var star1 = document.getElementById('star1');
	var star2 = document.getElementById('star2');
	var star3 = document.getElementById('star3');
	var star4 = document.getElementById('star4');
	var star5 = document.getElementById('star5');
	
	var full = './images/star_full.png';
	var empty = './images/star_empty.png';
	
	//If the first is selected then fill accordingly 
	if (selected == 1) {
		star1.src = full;
		star2.src = empty;
		star3.src = empty;
		star4.src = empty;
		star5.src = empty;
	} else if (selected == 2) {
		star1.src = full;
		star2.src = full;
		star3.src = empty;
		star4.src = empty;
		star5.src = empty;
	} else if (selected == 3) {
		star1.src = full;
		star2.src = full;
		star3.src = full;
		star4.src = empty;
		star5.src = empty;
	} else if (selected == 4) {
		star1.src = full;
		star2.src = full;
		star3.src = full;
		star4.src = full;
		star5.src = empty;
	} else if (selected == 5) {
		star1.src = full;
		star2.src = full;
		star3.src = full;
		star4.src = full;
		star5.src = full;
	}
}

function lockInStars(rating) {
	var storeRating = document.getElementById('hdnRating');
	storeRating.value = rating;
}

function fillLockedIn() {
	var storeRating = document.getElementById('hdnRating');
	selected = storeRating.value;
	
	var star1 = document.getElementById('star1');
	var star2 = document.getElementById('star2');
	var star3 = document.getElementById('star3');
	var star4 = document.getElementById('star4');
	var star5 = document.getElementById('star5');
	var full = './images/star_full.png';
	var empty = './images/star_empty.png';
	
	//If the first is selected then fill accordingly 
	if (selected == 0) {
		star1.src = empty;
		star2.src = empty;
		star3.src = empty;
		star4.src = empty;
		star5.src = empty;
	} else if (selected == 1) {
		star1.src = full;
		star2.src = empty;
		star3.src = empty;
		star4.src = empty;
		star5.src = empty;
	} else if (selected == 2) {
		star1.src = full;
		star2.src = full;
		star3.src = empty;
		star4.src = empty;
		star5.src = empty;
	} else if (selected == 3) {
		star1.src = full;
		star2.src = full;
		star3.src = full;
		star4.src = empty;
		star5.src = empty;
	} else if (selected == 4) {
		star1.src = full;
		star2.src = full;
		star3.src = full;
		star4.src = full;
		star5.src = empty;
	} else if (selected == 5) {
		star1.src = full;
		star2.src = full;
		star3.src = full;
		star4.src = full;
		star5.src = full;
	}
}

function showCorrectSubCategory() {
	var categoryId;
	var ix;
	ix = document.frmNewListing.cat.selectedIndex;
	categoryId = document.frmNewListing.cat.options[ix].value;
	
	hide(document.frmNewListing.hdnOldSub.value);
	show('slctSubCategory'+categoryId);
	document.frmNewListing.hdnOldSub.value = 'slctSubCategory'+categoryId;
	
}

function loadSubcategories (noDefOpt, forResults) {
    
    var currentCategory = $('#results_categories').val();
    $('#results_subcategories').html('Loading...');
    
    $.get("loadSubCat.php",
        { parentID: currentCategory, noDefaultOpt: noDefOpt, forResults: forResults },
        function(data){
            $('#results_subcategories').html(data);
        }
    );
    
}

function loadTypes (noDefOpt, forResults) {
    
    var currentCategory = $('#results_categories').val();
    $('#results_types').html('Loading...');
    
    $.get("loadTypes.php",
        { parentID: currentCategory, noDefaultOpt: noDefOpt, forResults: forResults },
        function(data){
            $('#results_types').html(data);
        }
    );   
}

function sortSearchResults () {
	var sel = document.getElementById('resultsSortSelector');
	location.href = location.href+'&order='+sel.value;
}

function addLanguage(id, counter) {
	var table = document.getElementById(id);
	var counter = document.getElementById(counter)
	//var numi = document.getElementById('langCount');
  	//var num = (document.getElementById('stepCount').value -1) + 2;
  	//numi.value = num;

	//Create the new section
	var newRow = document.createElement('div');
	newRow.innerHTML = 	"<select name=\"languageToAdd"+counter.value+"\">" +
								"<option value=\"\" selected\"\"> Select a language</option>" +
								"<option value=\"English\"> English</option>" +
								"<option value=\"Arabic\"> Arabic</option>" +
								"<option value=\"Bulgarian\"> Bulgarian</option>" +
								"<option value=\"Catalan\"> Catalan</option>" +
								"<option value=\"Chinese\"> Chinese</option>" +
								"<option value=\"Croatian\"> Croatian</option>" +
								"<option value=\"Czech\"> Czech</option>" +
								"<option value=\"Danish\"> Danish</option>" +
								"<option value=\"Dutch\"> Dutch</option>" +
								"<option value=\"Estonian\"> Estonian</option>" +
								"<option value=\"Finnish\"> Finnish</option>" +
								"<option value=\"French\"> French</option>" +
								"<option value=\"German\"> German</option>" +
								"<option value=\"Greek\"> Greek</option>" +
								"<option value=\"Hebrew\"> Hebrew</option>" +
								"<option value=\"Hungarian\"> Hungarian</option>" +
								"<option value=\"Icelandic\"> Icelandic</option>" +
								"<option value=\"Indonesian\"> Indonesian</option>" +
								"<option value=\"Italian\"> Italian</option>" +
								"<option value=\"Japanese\"> Japanese</option>" +
								"<option value=\"Korean\"> Korean</option>" +
								"<option value=\"Latvian\"> Latvian</option>" +
								"<option value=\"Lithuanian\"> Lithuanian</option>" +
								"<option value=\"Norwegian\"> Norwegian</option>" +
								"<option value=\"Polish\"> Polish</option>" +
								"<option value=\"Portuguese\"> Portuguese</option>" +
								"<option value=\"Romanian\"> Romanian</option>" +
								"<option value=\"Russian\"> Russian</option>" +
								"<option value=\"Serbian\"> Serbian</option>" +
								"<option value=\"Slovak\"> Slovak</option>" +
								"<option value=\"Slovenian\"> Slovenian</option>" +
								"<option value=\"Spanish\"> Spanish</option>" +
								"<option value=\"Swedish\"> Swedish</option>" +
								"<option value=\"Turkish\"> Turkish</option>" +
							"</select>" +
						"</td>";
	table.appendChild(newRow);
	counter.value = counter.value+1;
}

function updateFormAction(formName, action) {
	var myForm = document.getElementById(formName);
	myForm.action = './?action='+action;
}

function formSubmit(formName) {
	var myForm = document.getElementById(formName);
	myForm.submit();
}

function checkAll(myform) {
	//alert(myform.elements.length);
	
	for (i=0; i<myform.elements.length; i++) {
		if (myform.elements[i].type=='checkbox') {
			myform.elements[i].checked = 1;
		}
	}
}

function UpdateListingDisplay(selection) {
	if (selection == "view") {
		//Hide the print button if visible
		hide('btnPrint');
	} else if (selection == "print") {
		//Show the print button if visible
		show('btnPrint');
	}
}

function updateCharCount(inp, spanName, max) {
	var countSpan = document.getElementById(spanName);
	countSpan.innerHTML = max-inp.length;  
}

function validateNewListing() {
	var errors = false;
	
	//Validate that the title is not blank
	var title_error = document.getElementById('title_error');
	var title_input = document.getElementById('Title');
	if (trim(title_input.value) == '') {
		title_error.style.display = '';
		errors = true;
	} else {
		title_error.style.display = 'none';
	}
	
	//Validate the Email address if supplied
	var email_error = document.getElementById('email_error');
	var email_input = document.getElementById('Email');
	if (email_input != null) {
		if (isEmail(email_input.value)) {
			email_error.style.display = 'none';
		} else {
			email_error.style.display = '';
			errors = true;
		}
	}
	
	//Validate the postal code
	var zip_error = document.getElementById('zip_error');
	var zip_input = document.getElementById('PostalCode');
	var hdnErrors = document.getElementById('hdnErrors');
	//Do an ajax call and see if the db returns the value 1
	var foo = $.get("checkZip.php?&zip="+zip_input.value,
        	{ }, 
	        function(data){
	            if (data == '1') {
	            	zip_error.style.display = 'none';
	            	//Submit if we validate
					if (errors == false) {
						var myForm = document.getElementById('frmNewListing');
						myForm.submit();
					}
	            } else {
	            	zip_error.style.display = '';
	            }
	        }
    );
}

function validateSendEmail() {
	var email = document.getElementById('msg_email');
	var myForm = document.getElementById('frm_send_email');
	var error = document.getElementById('err_email');
	
	if (isEmail(email.value)) {
		error.style.display = 'none';
		myForm.submit();	
	} else {
		error.style.display = '';	
	}
}

function isEmail(str_email) {
	var filter = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)*\.([a-zA-Z]{2,6})$/;
	return str_email.match(filter);
}

function clipboard(myObj) {
	var foo = document.getElementById(myObj);
	
	Copied = foo.createTextRange();
	Copied.execCommand("Copy");
}

function selectall(myObj) {
	var foo = document.getElementById(myObj);
	foo.focus();
	foo.select();
}

function generateDigitalFlyer() {
		
	var bgColor = document.getElementById('bgColor');
	var borderColor = document.getElementById('borderColor');
	var branding = document.getElementById('branding');
	var myPanel = document.getElementById('mark_dig');
	var listing = document.getElementById('listingId');
	
	if (bgColor.value.length != 6) {
		bgColor.value = 'FFFFFF';
	}
	if (borderColor.value.length != 6) {
		borderColor.value = '9999CC';
	}
	if (branding.value == "on") {
		var brand = "&branding=on";
	} else {
		var brand = "";
	}
	
	var foo = $.get("/?action=display_ad_external&id="+listing.value+brand+"&bgColor="+bgColor.value+"&borderColor="+borderColor.value, { },
			function(data){
	            myPanel.value = data;
	        }
	);
}

function trim(inString) {
	return inString.replace(/^\s+|\s+$/g, '') ;
}

function showPic(element)
{
	element.src = element.src;	
}
