// JavaScript Document
var xmlhttp;

function changeLineup(str) { /* AJAX for Numerical or Alphabetical lineups */
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null) 	  {
	  alert ("Browser does not support HTTP Request");
	  return;
	}
	var url=str;
	url=url+"?sid="+Math.random(); /**/
	xmlhttp.onreadystatechange=lineupChanged;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

function lineupChanged() { /* Lineup order has changed, so show it now. */
	if (xmlhttp.readyState==4){
		document.getElementById("myLineup").innerHTML=xmlhttp.responseText;
	}
}

function GetXmlHttpObject() {
	if (window.XMLHttpRequest)   {   // code for IE7+, Firefox, Chrome, Opera, Safari
	  return new XMLHttpRequest();
	}
	if (window.ActiveXObject)   {   // code for IE6, IE5
	  return new ActiveXObject("Microsoft.XMLHTTP");
	}
	return null;
}

function showAdmin(str) { /* AJAX for Numerical or Alphabetical lineups */
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null) 	  {
	  alert ("Browser does not support HTTP Request");
	  return;
	}
	var url=str;
	url=url+"?sid="+Math.random(); /**/
	xmlhttp.onreadystatechange=adminChanged;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

function adminChanged() { /* Lineup order has changed, so show it now. */
	if (xmlhttp.readyState==4){
		document.getElementById("admin").innerHTML=xmlhttp.responseText;
	}
}

function validate_quotes(f) { // Business quote
	if (f.name.value =='') {
		alert('Please provide a contact name.');
		f.name.focus();
	} else if (f.can_be_reached.value =='') {
		alert('Please provide a number to be reached at.');
		f.can_be_reached.focus();
	} else {
		return true;
	}
	return false;
}

function validate_wholesale_quote(f) { // Wholesale services quote
	if (f.size_special_circuit.value =='') {
		alert('Please provide the size of the special circuit.');
		f.size_special_circuit.focus();
	} else if (f.address_of_point_A.value =='') {
		alert('Please provide the complete address of Point A.');
		f.address_of_point_A.focus();
	} else if (f.address_of_point_B.value =='') {
		alert('Please provide the complete address of Point B.');
		f.address_of_point_B.focus();
	} else if (f.company.value =='') {
		alert('Please provide the company name.');
		f.company.focus();
	} else if(f.email.value == "" ||
		f.email.value.indexOf('@')==-1 ||
		f.email.value.indexOf('.')==-1 ||
		f.email.value.length < 10) {
		alert('Please type a valid e-mail address.');
		f.email.focus();
	} else {
		return true;
	}
	return false;
}

function validate_residential(f) { // Residential application
	if (f.responsible_party.value =='') {
		alert('Please provide a responsible party.');
		f.responsible_party.focus();
	} else if (f.birth_date.value =='') {
		alert('Please provide a birth date.');
		f.birth_date.focus();
	} else if (f.password.value =='') {
		alert('Please provide a password.');
		f.password.focus();
	} else if (f.physical_address.value =='') {
		alert('Please provide a physical address.');
		f.physical_address.focus();
	} else if (f.city.value =='') {
		alert('Please provide a cify.');
		f.city.focus();
	} else if (f.reached_phone.value =='') {
		alert('Please provide a phone number.');
		f.reached_phone.focus();
	} else {
		return true;
	}
	return false;
}

function validate_business(f) { // Business application
	if (f.business_name.value =='') {
		alert('Please provide a business name.');
		f.business_name.focus();
	} else if (f.primary_contact.value =='') {
		alert('Please provide a primary contact.');
		f.primary_contact.focus();
	} else if (f.password.value =='') {
		alert('Please provide a password.');
		f.password.focus();
	} else if (f.physical_address.value =='') {
		alert('Please provide a physical address.');
		f.physical_address.focus();
	} else if (f.city.value =='') {
		alert('Please provide a cify.');
		f.city.focus();
	} else if (f.reached_phone.value =='') {
		alert('Please provide a phone number.');
		f.reached_phone.focus();
	} else {
		return true;
	}
	return false;
}

function validate_contact(f) { // email and name values must be OK
	if(f.email.value == "" ||
		f.email.value.indexOf('@')==-1 ||
		f.email.value.indexOf('.')==-1 ||
		f.email.value.length < 10) {
		alert('Please type a valid e-mail address.');
		f.email.focus();
	} else if (f.name.value == "") {
		alert('Please type a valid name.');
		f.name.focus();
	} else if (f.comments.value == "") {
		alert('Please type your comments.');
		f.comments.focus();
	} else {
		return true;
	}
	return false;
}

function validate_email(f) { // email value must be OK
	if(f.email.value == "" ||
	    f.email.value == "my email" || 
		f.email.value.indexOf('@')==-1 ||
		f.email.value.indexOf('.')==-1 ||
		f.email.value.length < 10) {
		alert('Please type a valid e-mail address.');
		f.email.focus();
		return false;
	} 
	return true;
}

function validate_admin(f) { // Admin login must be OK
	if(f.username.value == "") {
		alert('Please type a valid username.');
		f.username.focus();
		return false;
	} else if (f.password.value == "") {
		alert('Please type a valid password.');
		f.password.focus();
		return false;
	}
	return true;
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function community() { //  Read the community cookie and then display it
	var x = readCookie('vtc_community');
	if (!x) {
		document.getElementById('links').innerHTML="AZ - Portal, Cochise"; // Default community
	} else {
		document.getElementById('links').innerHTML=x; // Previously set their community
	}
}

function update_community(name) { // Save the new community as a cookie, then display it
	createCookie('vtc_community',name,'365');
	
	//community();
}

function searched(f) { // Is the search value non-blank, if not then alert me
	if (f.search.value == "") {
		alert("Type a search value.");
		return false;
	} else {
		return true;
	}

}

function image_preloader() {
     var i = 0;
     var images = ["nav-bundles-on.gif","nav-bundles.gif","nav-digitaltv-on.gif","nav-digitaltv.gif","nav-internet-on.gif","nav-internet.gif","nav-phone-on.gif","nav-phone.gif","nav-business-on.gif","nav-business.gif","nav-contact-on.gif","nav-contact.gif"];
	imagearray = new Array();
     // start preloading
     for(i=0; i<images.length; i++) {
		 imagearray[i] = new Image();
         imagearray[i].src = "images/" + images[i];
     }
} 

// Used on home_messages.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/
	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
	}
	form.ad_images.value = images;
	form.ad_urls.value = urls;
	
	// Inactive ads
	var images = ""; // foo.jpg
	var urls = ""; // http://www.foo.com/
	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
	}
	form.ad_images_inactive.value = images;
	form.ad_urls_inactive.value = urls;
	return true;
} 

function presentChange(form,index) {  // Display present ad image
	document.adImage.src = "ads/" + form.present.options[index].text;
	//document.imageForm.reports.value = "http://www2.vtc.net/new/page.php?page=ads_report";  +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 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 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 newCommunity() { // Open a new window and prompt for a new community
	mywindow = window.open('change_community.php','Change Community','resizable=no,width=300,height=170');
	mywindow.location.href = 'change_community.php';
	if (myindow.opener == null) mywindow.opener = self;
}
