// Used on edit_ads.php, Edit ads as Admin

function saveAds(form) { // Return a list of JPG images used for ads. Gets passed to ads_edit.php, then saved in MySQL db.
	// Active ads
	var images = ""; // foo.jpg
	var urls = ""; // http://www.foo.com/
	var ids = ""; // 347 = Daniel Payne Photography
	for (var i =0; i< form.present.length; i++) {
		images += form.present.options[i].text+"|"; // my ads.jpg|your ads.jpg
		urls += form.links.options[i].text+"|"; // www.cnn.com|www.aol.com
		ids += form.company.options[i].value+"|"; // 347|212
	}
	form.ad_images.value = images;
	form.ad_urls.value = urls;
	form.ad_ids.value = ids;
	
	// Inactive ads
	var images = ""; // foo.jpg
	var urls = ""; // http://www.foo.com/
	var ids = ""; // 347 = Daniel Payne Photography
	for (var i =0; i< form.uploaded.length; i++) {
		images += form.uploaded.options[i].text+"|"; // my ads.jpg|your ads.jpg
		urls += form.links_inactive.options[i].text+"|"; // www.cnn.com|www.aol.com
		ids += form.company_inactive.options[i].value+"|"; // 347|212
	}
	form.ad_images_inactive.value = images;
	form.ad_urls_inactive.value = urls;
	form.ad_ids_inactive.value = ids;
	return true;
} 

function savePhotos(form) { // Return a list of JPG images used for photos. Gets passed to photos_edit.php, then saved in MySQL db.
	// Active ads
	var images = ""; // foo.jpg
	for (var i =0; i< form.present.length; i++) {
		images += form.present.options[i].text+"|"; // my ads.jpg|your ads.jpg
	}
	form.ad_images.value = images;
	
	// Inactive ads
	var images = ""; // foo.jpg
	for (var i =0; i< form.uploaded.length; i++) {
		images += form.uploaded.options[i].text+"|"; // my ads.jpg|your ads.jpg
	}
	form.ad_images_inactive.value = images;
	return true;
} 

function presentChange(form,index) {  // Display present ad image
	document.adImage.src = "ads/" + form.present.options[index].text;
	document.imageForm.reports.value = "http://www.sherwoodchamber.org/?page=ads_report&id="+form.company.options[index].value;
	form.links.selectedIndex = index; // select the web url when an image name is clicked
	form.company.selectedIndex = index; // select company name
	form.present.selectedIndex = index;
	form.uploaded.selectedIndex = -1; // unselect Uploaded
	form.links_inactive.selectedIndex = -1;
	form.company_inactive.selectedIndex = -1;
} 

function presentPhoto(form,index) {  // Display present photo image
	document.adImage.src = "pano_gallery/" + form.present.options[index].text;
	form.present.selectedIndex = index;
	form.uploaded.selectedIndex = -1; // unselect Uploaded
} 

function uploadedChange(form,index) {
	document.adImage.src = "ads/" + document.imageForm.uploaded.options[index].text;
	form.uploaded.selectedIndex = index; // select
	form.links_inactive.selectedIndex = index;
	form.company_inactive.selectedIndex = index;
	form.present.selectedIndex = -1; // unselect
	form.links.selectedIndex = -1;
	form.company.selectedIndex = -1;
}

function uploadedPhoto(form,index) {
	document.adImage.src = "pano_gallery/" + form.uploaded.options[index].text; // was document.imageForm.uploaded.options[index].text;
	form.uploaded.selectedIndex = index; // select
	form.present.selectedIndex = -1; // unselect
}

function imageAction(form,action) { // Uploaded or present action
	if (action == 'inactivate') { // Inactivate present ad
		var selIndex = form.present.selectedIndex; // remember ad incdex
		var addValue = form.present.options[selIndex].value; // 1
		var addText = form.present.options[selIndex].text; // abc.jpg
		form.uploaded.options[form.uploaded.length] = new Option(addText, addValue); // abc.jpg, 1
		var addText = form.links.options[selIndex].text; // http://www.abc.com/
		form.links_inactive.options[form.links_inactive.length] = new Option(addText, addValue); // http://www.abc.com/, 1
		var addValue = form.company.options[selIndex].value; // 341
		var addText = form.company.options[selIndex].text; // ABC Banking Corp
		form.company_inactive.options[form.company_inactive.length] = new Option(addText, addValue); // ABC Banking Corp, 341
		
		form.present.remove(form.present.selectedIndex); // remove from Present list
		form.links.remove(document.imageForm.links.selectedIndex);
		form.company.remove(document.imageForm.company.selectedIndex);
		document.adImage.src = "images/blank.jpg"; // replace image with a blank
		

	} else if (action == 'up') { // Up
		var selIndex = form.present.selectedIndex;
		if (selIndex>0) {
			var upValue = form.present.options[selIndex-1].text; // Move the ad image text up
			form.present.options[selIndex-1].text = form.present.options[selIndex].text;
			form.present.options[selIndex].text = upValue;
			form.present.selectedIndex = selIndex-1;
			
			var upWeb = form.links.options[selIndex-1].text; // Move the ad web link text up
			form.links.options[selIndex-1].text = form.links.options[selIndex].text;
			form.links.options[selIndex].text = upWeb;
			form.links.selectedIndex = selIndex-1;
			
			var upCompany = form.company.options[selIndex-1].text; // Move the ad web link text up
			form.company.options[selIndex-1].text = form.company.options[selIndex].text;
			form.company.options[selIndex].text = upCompany;

			var upIndex = form.company.options[selIndex-1].value;
			form.company.options[selIndex-1].value = form.company.options[selIndex].value;
			form.company.options[selIndex].value = upIndex;
			form.company.selectedIndex = selIndex-1;
		}
		form.present_action[1].checked = false; // uncheck Move Up
	} else if (action == 'down') { // Down
		var selIndex = form.present.selectedIndex;
		if (selIndex<form.present.length-1) {
			var downValue = form.present.options[selIndex+1].text;
			form.present.options[selIndex+1].text = form.present.options[selIndex].text;
			form.present.options[selIndex].text = downValue;
			form.present.selectedIndex = selIndex+1;
			
			var downWeb = form.links.options[selIndex+1].text;
			form.links.options[selIndex+1].text = form.links.options[selIndex].text;
			form.links.options[selIndex].text = downWeb;
			form.links.selectedIndex = selIndex+1;
			
			var downCompany = form.company.options[selIndex+1].text;
			form.company.options[selIndex+1].text = form.company.options[selIndex].text;
			form.company.options[selIndex].text = downCompany;

			var downIndex = form.company.options[selIndex+1].value;
			form.company.options[selIndex+1].value = form.company.options[selIndex].value;
			form.company.options[selIndex].value = downIndex;
			form.company.selectedIndex = selIndex+1;
		}
	} else if (action == 'delete') { // Remove Uploaded ad
		form.uploaded.remove(document.imageForm.uploaded.selectedIndex); // Image file name
		form.company_inactive.remove(document.imageForm.company_inactive.selectedIndex); // Company name
		form.links_inactive.remove(document.imageForm.links_inactive.selectedIndex); // Web link
		document.adImage.src = "images/blank.jpg"; // replace image with a blank
	} else if (action == 'activate') { // Add uploaded image to present
		var selIndex = form.uploaded.selectedIndex; // Inactive ad
		var addValue = form.uploaded.options[selIndex].value; // 1
		var addText = form.uploaded.options[selIndex].text; // abc.jpg
		form.present.options[form.present.length] = new Option(addText, addValue); // abc.jpg, 1
		var addText = form.links_inactive.options[selIndex].text; // http://www.abc.com/
		form.links.options[form.links.length] = new Option(addText, addValue); // http://www.abc.com, 1
		var addValue = form.company_inactive.options[selIndex].value; // 341
		var addText = form.company_inactive.options[selIndex].text; // ABC Banking Corp
		form.company.options[form.company.length] = new Option(addText, addValue); // ABC Banking Corp, 341
		
		form.uploaded.remove(selIndex); // remove from inactive lists
		form.links_inactive.remove(selIndex);
		form.company_inactive.remove(selIndex);
	}

}

function photoAction(form,action) { // Uploaded or present action
	if (action == 'inactivate') { // Inactivate present ad
		var selIndex = form.present.selectedIndex; // remember ad index
		var addValue = form.present.options[selIndex].value; // 1
		var addText = form.present.options[selIndex].text; // abc.jpg
		form.uploaded.options[form.uploaded.length] = new Option(addText, addValue); // abc.jpg, 1
		
		form.present.remove(form.present.selectedIndex); // remove from Present list
		document.adImage.src = "images/blank.jpg"; // replace image with a blank
		

	} else if (action == 'up') { // Up
		var selIndex = form.present.selectedIndex;
		if (selIndex>0) {
			var upValue = form.present.options[selIndex-1].text; // Move the ad image text up
			form.present.options[selIndex-1].text = form.present.options[selIndex].text;
			form.present.options[selIndex].text = upValue;
			form.present.selectedIndex = selIndex-1;
			
		}
		form.present_action[1].checked = false; // uncheck Move Up
	} else if (action == 'down') { // Down
		var selIndex = form.present.selectedIndex;
		if (selIndex<form.present.length-1) {
			var downValue = form.present.options[selIndex+1].text;
			form.present.options[selIndex+1].text = form.present.options[selIndex].text;
			form.present.options[selIndex].text = downValue;
			form.present.selectedIndex = selIndex+1;
			
		}
	} else if (action == 'delete') { // Remove Uploaded ad
		form.uploaded.remove(document.imageForm.uploaded.selectedIndex); // Image file name
		document.adImage.src = "images/blank.jpg"; // replace image with a blank
	} else if (action == 'activate') { // Add uploaded image to present
		var selIndex = form.uploaded.selectedIndex; // Inactive ad
		var addValue = form.uploaded.options[selIndex].value; // 1
		var addText = form.uploaded.options[selIndex].text; // abc.jpg
		form.present.options[form.present.length] = new Option(addText, addValue); // abc.jpg, 1
		
		form.uploaded.remove(selIndex); // remove from inactive lists
	}

}

// admin login

function validate_paypal (form) { // Set the PayPal item_number based on what is selected.
	if (form.amount.options[0].selected) {
		form.item_number.value = form.amount.options[0].text; // Member text
	} else {
		form.item_number.value = form.amount.options[1].text; // Non-member text
	}
	return true;
}

function validate_admin (form){
  if (form.name.value =="") {
    alert('Please type a valid name.');
	form.name.focus();
	return false; 
  } else if (form.password.value == "" ||
	form.password.value.length < 6) {
    alert('Pleae type a valid password.');
	form.password.focus();
	return false;
  } else {
    return true;
  }
}

// Navigate to a secure login page, or a regular page
function pages(where) {
	var here = window.location.pathname;
	var index = here.indexOf("concept1");
	if (index>-1) {
		var prefix = "concept1/";
	} else {
		var prefix = "";
	}
	if (where.indexOf("login") == 0) {
		window.location = "https://www.sherwoodchamber.org/"+prefix+"?page="+where; /* should be https in productions */
	} else {
		window.location = "http://www.sherwoodchamber.org/"+prefix+"?page="+where;
	}
}

// Contact page, validate non-blank entries
function validate_contact(form) { 
  if (form.from.value == "") {
  	alert('Please type a valid  name');
	form.from.focus();
  } else if(form.email.value == "" ||
    form.email.value.indexOf('@')==-1 ||
    form.email.value.indexOf('.')==-1 ||
    form.email.value.length < 10) {
    alert('Please type a valid e-mail address.');
    form.email.focus();
  } else if (form.subject.value == "") {
  	alert('Please type a valid subject');
	form.subject.focus();
  } else if (form.message.value == "") {
  	alert('Please type a valid message');
	form.message.focus();
  } else {
    return true;
  }
  return false;
}

// Contact page, validate non-blank entries
function validate_relocation(form) { 
  if (form.from.value == "") {
  	alert('Please type a valid  name');
	form.from.focus();
  } else if(form.email.value == "" ||
    form.email.value.indexOf('@')==-1 ||
    form.email.value.indexOf('.')==-1 ||
    form.email.value.length < 10) {
    alert('Please type a valid e-mail address.');
    form.email.focus();
  } else if (form.address.value == "") {
  	alert('Please type an address');
	form.address.focus();
  } else {
    return true;
  }
  return false;
}

// All pages, the search value must be non-blank
function searched(form) { 
	if (form.txtsearch.value == "") {
		alert("Type a search value.");
		return false;
	} else {
		return true;
	}

}

// Join page, validate required entries
function validate_join(form) { 
  if (form.company.value =="") {
	  alert('Please type a valid company name.');
	  form.company.focus();
  } else if (form.address.value == "") {
	  alert('Please type a valid address.');
	  form.address.focus();
  } else if (form.city.value =="" ) {
	  alert('Please type a valid city.');
	  form.city.focus();
  } else if (form.zip.value == "" || form.zip.value.length < 5) {
	  alert('Please type a valid zipcode.');
	  form.zip.focus();
  } else if (form.phone.value == "" || form.phone.value.length<10) {
	  alert('Please type a valid phone number.');
	  form.phone.focus();
  } else if (form.email.value == "" ||
    form.email.value.indexOf('@')==-1 ||
    form.email.value.indexOf('.')==-1 ||
    form.email.value.length < 10) {
    alert('Please type a valid e-mail address.');
    form.email.focus();
  } else if (form.memdesc.value == "" || form.memdesc.value.length < 10) {
	  alert('Please type a valid description.');
	  form.memdesc.focus();
  } else if (form.category.value =="1") {
	  alert('Please choose a business category');
	  form.category.focus();
  } else if (form.owner.value == "" || form.owner.value.length < 5) {
	  alert('Please type a valid owner name.');
	  form.owner.focus();
  } else if (form.memcontact.value =="" || form.memcontact.value.length < 5) {
	  alert('Please type a valid contact name.');
	  form.memcontact.focus();
  } else return true;
  return false;
}

// Forgot page, validate non-blank entries
function validate_forgot(form) { 
  if(form.email.value == "" ||
    form.email.value.indexOf('@')==-1 ||
    form.email.value.indexOf('.')==-1 ||
    form.email.value.length < 10) {
    alert('Please type a valid e-mail address.');
    form.email.focus();
  } else {
    return true;
  }
  return false;
}

// Forgot page, validate non-blank entries
function validate_login(form) { 
	if(form.email.value == "" ||
		form.email.value.indexOf('@')==-1 ||
		form.email.value.indexOf('.')==-1 ||
		form.email.value.length < 10) {
		alert('Please type a valid e-mail address.');
		form.email.focus();
	} else if (form.password.value == "") {
		alert("Type a password value.");
		form.password.focus();
	} else {
		return true;
	}
	return false;
}

// Event page
function validate_event(form) { 
	if(form.event.value == "" ) {
		alert('Type a valid event title.');
		form.event.focus();
	} else if (form.holiday[0].checked == true) {
		return true;
	} else if (form.hostedby.value == "") {
		alert("Type a valid hosted by value.");
		form.hostedby.focus();
	} else if (form.host.value == "") {
		alert("Type a valid host value.");
		form.host.focus();
	} else if (form.hostedby.value == "") {
		alert("Type a valid hosted by value.");
		form.hostedby.focus();
	} else if (form.local.value == "") {
		alert("Type a valid location.");
		form.local.focus();
	} else if (form.address.value == "") {
		alert("Type a valid address.");
		form.address.focus();
	} else if (form.city.value == "") {
		alert("Type a valid city.");
		form.city.focus();
	} else if (form.phone.value == "") {
		alert("Type a valid phone.");
		form.phone.focus();
	} else if (form.descript.value == "") {
		alert("Type a valid description.");
		form.descript.focus();
	} else {
		return true;
	}
	return false;
}

// Event page
function validate_event_my(form) { 
	if(form.event.value == "" ) {
		alert('Type a valid event title.');
		form.event.focus();
	} else if (form.hostedby.value == "") {
		alert("Type a valid hosted by value.");
		form.hostedby.focus();
	} else if (form.host.value == "") {
		alert("Type a valid host value.");
		form.host.focus();
	} else if (form.hostedby.value == "") {
		alert("Type a valid hosted by value.");
		form.hostedby.focus();
	} else if (form.local.value == "") {
		alert("Type a valid location.");
		form.local.focus();
	} else if (form.address.value == "") {
		alert("Type a valid address.");
		form.address.focus();
	} else if (form.city.value == "") {
		alert("Type a valid city.");
		form.city.focus();
	} else if (form.phone.value == "") {
		alert("Type a valid phone.");
		form.phone.focus();
	} else if (form.descript.value == "") {
		alert("Type a valid description.");
		form.descript.focus();
	} else {
		return true;
	}
	return false;
}
