// absolute path of the website
var rootPath = "";

/****************************************************************
*	Name: isDefined
*	Purpose: determines if a variable is defined or not
*	Attributes:
*		-->variable: STRING with the name of the variable to check
*		<--return: BOOLEAN value
****************************************************************/
function isDefined(variable)
{
	return (typeof(variable) == "undefined") ? false : true;
}

/****************************************************************
*	Name: getQuerystringValue
*	Purpose: gets a value from the querystring
*	Attributes:
*		-->arg: STRING with the name of the variable to get
*		<--return: a STRING of the value or NULL
****************************************************************/
function getQuerystringValue(arg)
{
	var queryString = location.search;
	if ( queryString.indexOf(arg) >= 0 )
	{
		var position = queryString.indexOf(arg) + arg.length + 1;
		if ( queryString.indexOf("&", position) >= 0 )
			return queryString.substring(position, queryString.indexOf("&", position));
		else
			return queryString.substring(position, queryString.length);
	}
	else
	{
		return null;
	}
}

function openWindow(url, width, height, properties)
{
	var props;
	
	if (properties == '')
		props = "resizable=no,scrollbars=no,menubar=no,status=no,toolbar=no,location=no";
	else
		props = properties;
		
	var win = window.open(url, 'hello', 'width=' + width + ',height=' + height + ',' + properties);
}

function previewUniversity(id)
{
	var props;
	props = "resizable=yes,scrollbars=yes,menubar=no,status=no,toolbar=no,location=no";
	openWindow(rootPath + '/universityDetails.aspx?univID=' + id, 760, 500, props);
}

function showDiv(divName)
{
	document.getElementById(divName).style.display = "";
}

function hideDiv(divName)
{
	document.getElementById(divName).style.display = "none";
}

function showOtherDiv(divName, value)
{
	if (value == "Other")
		document.getElementById(divName).style.display = "";
	else
		document.getElementById(divName).style.display = "none";
}

function showForm()
{
	if (document.getElementById('submitted').value == "1")
	{
		if (document.form1.radAirportPickup[0].checked)
			document.getElementById('flightInformation').style.display = "";
		
		type = 0;
		if (document.form1.radReferralType[0].checked)
			type = 1;
		else if (document.form1.radReferralType[1].checked)
			type = 2;
		else if (document.form1.radReferralType[2].checked)
			type = 3;
		configureReferralForm(type);
	}
}

function configureReferralForm(type)
{
	document.getElementById('tblStudentPerInfo').style.display = "";
	document.getElementById('tblStudentSchoolInfo').style.display = "";
	document.getElementById('tblStudentContactInfo').style.display = "";
	document.getElementById('tblAdditional').style.display = "";
	
	if (type == 1)	//going to study in usa
	{
		document.getElementById('trDestCountry').style.display = "none";
		document.getElementById('trFamily').style.display = "";
		document.getElementById('trFlightInfo').style.display = "";
		
		document.getElementById('studentSchoolHeader').innerHTML = "<h3>Student's Destination in the USA</h3>";
		document.getElementById('contactHeader').innerHTML = "<h3>Student's Contact Information in the USA</h3>";
		//document.getElementById('tdSchoolState').innerHTML = "*State";
		//document.getElementById('tdState').innerHTML = "*State";
	}
	else if (type == 2)		//going to study in another country other than usa
	{
		document.getElementById('trDestCountry').style.display = "";
		document.getElementById('trFamily').style.display = "";
		document.getElementById('trFlightInfo').style.display = "none";
		
		document.getElementById('studentSchoolHeader').innerHTML = "<h3>Student's Destination</h3>";
		document.getElementById('contactHeader').innerHTML = "<h3>Student's Contact Information in new country</h3>";
		//document.getElementById('tdSchoolState').innerHTML = "*State/Province";
		//document.getElementById('tdState').innerHTML = "*State/Province";
	}
	else if (type == "3")	//returning to home country
	{
		document.getElementById('trDestCountry').style.display = "none";
		document.getElementById('trFamily').style.display = "none";
		document.getElementById('trFlightInfo').style.display = "none";
		
		document.getElementById('studentSchoolHeader').innerHTML = "<h3>Student's USA Data</h3>";
		document.getElementById('contactHeader').innerHTML = "<h3>Student's Contact Information in his/her country</h3>";
		//document.getElementById('tdSchoolState').innerHTML = "*State";
		//document.getElementById('tdState').innerHTML = "*State/Province";
	}
}

function showImage()
{
	var imageSrc;
	var univId;
	var src;
	
	src = document.frmPhoto.lstPhotos.options[document.frmPhoto.lstPhotos.selectedIndex].text;
	if (src.indexOf("[PRIMARY]") >= 0)
	{
		src = src.substring(0, src.indexOf(" - [PRIMARY]"));
	}
	
	univId = getQuerystringValue("univId");
	document.frmPhoto.imgUnivPhoto.src = rootPath + "/images/colleges/" + univId + "/" + src;
}

function makePhotoPrimary()
{
	var photos = document.frmPhoto.lstPhotos;
	var itemCount = 0;
	
	if (photos.selectedIndex != -1)
	{
		for (var i=0; i < photos.options.length; i++)
		{
			if (photos.options[i].selected) itemCount++;
		}
		
		if (itemCount > 1)
		{
			alert("Please select only one photo");
			return false;
		}
		else if (photos.options[photos.selectedIndex].text.indexOf("[PRIMARY]") >= 0)
		{
			alert("This photo is already the primary photo");
			return false;
		}
		else
		{
			return true;
		}
	}
	else
	{
		alert("You must select a photo to make it primary");
		return false;
	}
}

function removePhoto()
{
	var photos = document.frmPhoto.lstPhotos;
	var selectedPrimary = false;
	var itemCount = 0;
	var confirmed;
	
	if (photos.selectedIndex != -1)
	{
		for (var i=0; i < photos.options.length; i++)
		{
			if (photos.options[i].selected)
			{
				itemCount++;
				
				if (photos.options[i].text.indexOf("[PRIMARY]") >= 0)
				{
					selectedPrimary = true;
				}
			}
		}
		
		if (itemCount > 1)
			confirmed = confirm("Are you sure you want to delete these photos?");
		else
			confirmed = confirm("Are you sure you want to delete this photo?");
		
		if (confirmed)
		{
			if (selectedPrimary)
				return confirm("You are about to delete the primary photo. Are you sure you want to do this?");
		}
		else
		{
			return false;
		}
	}
	else
	{
		alert("You must select a photo to remove it");
		return false;
	}
}

function showPhoto(src)
{
	var imageSrc;
	var univId;
	var path;
	
	univId = getQuerystringValue("univID");
	
	if (univId == null)
	{
		//do nothing
	}
	else if (src == null)
	{
		if ( document.getElementById('photo1') )
		{
			imageSrc = document.getElementById('photo1').href;

			if (imageSrc != "")
			{
				imageSrc = imageSrc.substring(22, imageSrc.length - 2);
				path = rootPath + "/images/colleges/" + univId + "/" + imageSrc;
				
				document.getElementById('imgUnivPhoto').src = path;
				document.getElementById('lnkPhoto').href = "javascript:openWindow('" + path + "', 660, 500, '');";
			}
			else
			{
				showNoPhoto();
			}
		}
		else
		{
			showNoPhoto();
		}
	}
	else	//called from the links on the page to the other images
	{
		path = rootPath + "/images/colleges/" + univId + "/" + src;
		document.getElementById('imgUnivPhoto').src = path;
		document.getElementById('lnkPhoto').href = "javascript:openWindow('" + path + "', 660, 500, '');";
	}
}

function showNoPhoto()
{
	document.frmDetails.imgUnivPhoto.src = rootPath + "/images/colleges/no_image.gif";
	document.getElementById('lnkPhoto').href = rootPath + "/images/colleges/no_image.gif";
}

function removePartner()
{
	var partnerList = document.form1.dropPartnerList;
	
	if (partnerList.selectedIndex != -1 && partnerList.options[partnerList.selectedIndex].value != 0)
	{
		return confirm("Are you sure you want to remove this partner?");
	}
	else
	{
		alert("You must first select a partner");
		return false;
	}
}

function removePartnership()
{
	if (document.form1.dropUnivPartnershipList.selectedIndex != -1)
	{
		return confirm("Are you sure you want to remove your partnership with this University?");
	}
	else
	{
		alert("You must first select a University");
		return false;
	}
}

function addPartnership()
{
	if (document.form1.dropUniversityList.selectedIndex != -1)
	{
		return true;
	}
	else
	{
		alert("You must first select a University");
		return false;
	}
}

function sendEmailToPartner()
{
	var partnerList = document.form1.dropPartnerList;
	
	if (partnerList.selectedIndex != -1 && partnerList.options[partnerList.selectedIndex].value != 0)
	{
		var temp_array = partnerList.options[partnerList.selectedIndex].value.split(":");
		var emailAddress = temp_array[1];
		window.location.replace('mailto:' + emailAddress);
	}
	else
	{
		alert("You must first select a partner");
	}
	
	return false;
}

function showAdvocacy()
{
	if (document.form1.dropUniversityList.selectedIndex != -1)
	{
		var id = document.form1.dropUniversityList.options[document.form1.dropUniversityList.selectedIndex].value;
		var win = window.open(rootPath + "/members/showAdvocacy.aspx?id=" + id,"Advocacy","width=400,height=500,resizable=yes,scrollbars=yes,menubar=no,status=no");
	}
	else
	{
		alert("You must first select a university");
	}
}

function editAdvocacy()
{
	if (document.form1.dropPersonnelList.selectedIndex == -1)
	{
		alert("You must first select person");
		return false;
	}
	else
	{
		return true;
	}
}

function showCalendar()
{
	var properties = "width=230,height=160,resizable=no,scrollbars=no,menubar=no,status=no,toolbar=no,location=no";
	var Win = window.open(rootPath + "/calendar.aspx","getDate",properties);
}

function displayDate(date)
{
	document.form1.txtArrivalDate.value = date;
}

function removeAmericanUniversity()
{
	var partnershipList = document.form1.dropPartnerships;
	var idList = document.form1.txtRelationships;
	var deleteList = document.form1.txtRemoveRelations;
	var id = idList.value.split(",");
	
	if (partnershipList.selectedIndex != -1)
	{
		for(var i = 0; i < id.length; i++)
		{
			if (id[i] == partnershipList.options[partnershipList.selectedIndex].value)
			{
				id.splice(i, 1);
				break;
			}
		}
		idList.value = id.join(",");
		partnershipList.remove(partnershipList.selectedIndex);
	}
	else
	{
		alert("You must first select a university");
	}
}

function addUniversityToForm(text, value)
{
	var alreadyInList = false;
	
	// loop through universities on parent form
	for(var j = 0; j < document.form1.dropPartnerships.length; j++)
	{
		if (document.form1.dropPartnerships.options[j].value == value)
		{
			alreadyInList = true;
			break;
		}
	}
	
	if (alreadyInList == false)
	{
		var o = new Option(text, value, false, false)
		document.form1.dropPartnerships.options[document.form1.dropPartnerships.length] = o;
		
		if (document.form1.txtRelationships.value != "") document.form1.txtRelationships.value += ",";
		document.form1.txtRelationships.value += value;
	}
}
