function getPopupFeatures(wwidth, wheight){
	if ((wwidth == null) || (wwidth == "")){ wwidth = 300; }
	if ((wheight == null) || (wheight == "")){ wheight = 170; }
	
	if (screen){
		var leftpos = (screen.width / 2) - (wwidth/2);
		var toppos = (screen.height / 2) - (wheight/2);		
	}

	return'width=' + wwidth + ', height=' + wheight + ', left=' + leftpos + ', top=' + toppos + ',resizable=yes, status=yes, scrollbars=yes';  
}

function checkAddListing(){
	var msg = "";
	if(document.addlisting.name.value == ""){
		msg = " - Please enter a name for the listing\n";
	}
	
	if(msg != ""){
		alert(msg);
		return false;
	}
}

function checkEditListing(){
	var msg = "";
	if(document.editlisting.name.value == ""){
		msg = " - Please enter a name for the project\n";
	}
	if(document.editlisting.active.value == ""){
		msg += " - Please indicate whether the project is active\n";
	}
	
	if(msg != ""){
		alert(msg);
		return false;
	}
}

function checkActive(){
	if((document.editlisting.active[0].checked != true) && (document.editlisting.num_photos.value > 0)){
		alert ('Note that this project and its photos will not be shown on the website until you change "Is the Listing Active?" to "Yes" then press "Save Details".');
	}
}

function checkPhotoListing(seq_num){
	var j_sel = 0;
	var i_sel = 0;
	
	for(i=1; i <= seq_num; i++){
		for(j=1; j <= seq_num; j++){
			i_sel = eval("document.photolisting.sequence_" + i + ".options[document.photolisting.sequence_" + i + ".selectedIndex].value");
			j_sel = eval("document.photolisting.sequence_" + j + ".options[document.photolisting.sequence_" + j + ".selectedIndex].value");			
			if((j_sel == i_sel) && (i != j)){
				alert("The sequence for photos " + i + " and " + j + " is the same - please correct this then press the Update/Delete Photos button again.");
				return false;
			}
		}
	}
	
	return confirm("Are you sure you want to delete checked photos, update categories and update sequences? If you are deleting photo/s, note that if any other listings use these photo/s they will no longer appear!");
}

function checkContactForm(){
	var msg = "";
	var fields = new Array("first_name", "email_address", "hear_about", "comments");
	var rep = RegExp('_','gi');
	for(i=0; i < fields.length; i++){
		if(eval("document.contactform." + fields[i] + ".value") == ""){
			if(fields[i] == "hear_about"){
				msg += " - Please select where you heard about us\n";	
			}else{
				msg += " - Please enter your " + fields[i].replace(rep, ' ') + "\n";
			}
		}
	}
	
	msg += checkEmail(document.contactform.email_address.value, "");
	msg += checkPhone(document.contactform.phone_number.value, "Phone number");
	msg += checkPhone(document.contactform.mobile.value, "Mobile number");

	if (msg != ""){
		msg = "Please fill in the following required fields:\n" + msg;
		alert(msg);
		return false;
	}
	return true;	
}

function checkEmail(email, fieldname){
	if(email){
		var regex = RegExp(/^([a-zA-Z0-9_\-\.]{2,})@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/);
		if ((email.search(regex) == -1) && (email != "")){
			if ((fieldname == "undefined") || (fieldname == "")){
				return " - Email address incorrect format\n";
			}else{
				return " - " + fieldname + " incorrect format\n";			
			}
		}
	}
	return "";
}

function checkPhone(phone, fieldname){
	if(phone){
		phone = phone.replace(/\(/gi, "");
		phone = phone.replace(/\)/gi, "");
		phone = phone.replace(/ /gi, "");
		regex = RegExp(/(^0[1-9]{1}[0-9]{8}$)|(^13\d{4}$)|(^04\d{2,3}\d{6}$)/);
		if ((phone.search(regex) == -1) && (phone != "")){
			return " - " + fieldname + " incorrect format - please include the area code \n";;
		}
	}
	return "";
}

function checkNumField(evt){
	var chCode = (evt.which)?evt.which:evt.keyCode;
	if (typeof document.getElementById!="undefined" && typeof document.all=="undefined"){
		if ((34<evt.charCode && evt.charCode<41) || evt.charCode==46) return false;
		if ((34<chCode && chCode<41) || chCode==46) return true;
	}
	if((chCode<48 || 57<chCode) && chCode>31) return false;
	return true;
}

