	
/* Some Global Variables to track the number of items in our cart */


var gintPhotoItems
var gintVideoItems

function RepopulateCompany(SelectID, ulID){
	xmlhttp.open("GET", "/getSearchArray.asp", true);
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			RepopulateOptions(getObj(SelectID),ConvertToOptionArray(xmlhttp.responseText),ulID)
		}
	}
	xmlhttp.send(null)	
}

function SelectAllCompanies(objCheckBox,blnSimple){
	var arrInputs = document.MediaSearch.GroupCompany
	var blnChecked = true
	if(!objCheckBox.checked){
		blnChecked = false
	}
	if(typeof document.MediaSearch.GroupCompany.length === 'number'){
		for(var i=0; i<arrInputs.length; i++) {
				arrInputs[i].checked = blnChecked;
		}	
	}else if(document.MediaSearch.GroupCompany){
		arrInputs.checked = blnChecked;
	}
	if(blnSimple){
		UpdateSimpleProjects()
	}else{
		searchChangeCompany(objCheckBox)
	}
}
function UpdateSimpleProjects(){
	var arrCheckBox = document.MediaSearch.GroupCompany
	var strCompanyList = ""
	//alert(arrCheckBox.length);
	if(typeof arrCheckBox.length == "undefined" && document.MediaSearch.GroupCompany.checked){
		strCompanyList = document.MediaSearch.GroupCompany.value
	}else if(typeof arrCheckBox.length == "undefined" && getObj('ALL') && getObj('ALL').checked){
		getObj('ALL').checked = false
	}else{
		for(var i=0; i<arrCheckBox.length; i++) {
			if(arrCheckBox[i].checked){
				strCompanyList = strCompanyList + arrCheckBox[i].value + ',';
			}
		}
	}
	//alert(strCompanyList)
	if(strCompanyList!=""){
		var strURL = "/getMediaSearchOptions.asp?getType=simpleprojects&companies=" + strCompanyList
		//alert(strURL)
		xmlhttp2.open("GET", strURL, true);
		xmlhttp2.onreadystatechange=function() {
			//alert("xmlhttp.readyState=" + xmlhttp2.readyState + " ")//xmlhttp.status=" + xmlhttp.status)	
			if (xmlhttp2.readyState==4 && xmlhttp2.status==200) {
				//Check for trailing | and remove
				var strResponseText = xmlhttp2.responseText
				if(strResponseText.substring(strResponseText.length-1,strResponseText.length) == "|"){
					strResponseText = strResponseText.substring(0,strResponseText.length-1)
				}
				//alert(strResponseText)
				if(strResponseText!=''){
					//alert(xmlhttp.responseText)	
					RepopulateOptions(getObj('project'),ConvertToOptionArray(strResponseText),true)
				}else{
					getObj('project').options.length = 0;
				}
				//DisplayList(getObj('activities'),'ActivityList')
				//DisplayList(getObj('projects'),'ProjectList')
				//DontWait();
				document.MediaSearch.project[0].selected = true;
		
			}
		}
		xmlhttp2.send(null)		
	}else{
		if(getObj('CompanyAll')){getObj('CompanyAll').checked = false}	
		getObj('project').options.length = 0;
	}
}



/* Advanced Search */

function searchChangeCompany(objCompany){
	PleaseWait();
	if(typeof objCompany == "object"  && !objCompany.checked){
		getObj('CompanyAll').checked = false;	
	}
	var strCompanyList = getCompanyList();
	var objSourceSelect = getObj('activities')
	var sourceSelectedItems = ""
	sourceSelectedItems = getSelectedItems(objSourceSelect)
	if(sourceSelectedItems!=''){
		var strURL = "/getMediaSearchOptions.asp?getType=projects&value=" + sourceSelectedItems + "&match=activity&companies=" + strCompanyList
	}else{
		var strURL = "/getMediaSearchOptions.asp?getType=projects&companies=" + strCompanyList
	}
	//alert(strURL)
		xmlhttp2.open("GET", strURL, true);
		xmlhttp2.onreadystatechange=function() {
			//alert("xmlhttp.readyState=" + xmlhttp2.readyState + " ")//xmlhttp.status=" + xmlhttp.status)	
			if (xmlhttp2.readyState==4 && xmlhttp2.status==200) {
				//Check for trailing | and remove
				var strResponseText = xmlhttp2.responseText
				if(strResponseText.substring(strResponseText.length-1,strResponseText.length) == "|"){
					strResponseText = strResponseText.substring(0,strResponseText.length-1)
				}
				if(strResponseText!=''){
					//alert(xmlhttp.responseText)	
					RepopulateOptions(getObj('projects'),ConvertToOptionArray(strResponseText),'ActivityList')
				}else{
					getObj('projects').options.length = 0;
				}
				//DisplayList(getObj('activities'),'ActivityList')
				//DisplayList(getObj('projects'),'ProjectList')
				DontWait();
			}
		}
		xmlhttp2.send(null)		
}

function searchChangeClassification(sourceSelectID, targetSelectID, ulID){
	PleaseWait();
	var objSourceSelect = getObj(sourceSelectID)
	var strURL = "/getMediaSearchOptions.asp?getType=activity&match=classification&value=" + objSourceSelect[objSourceSelect.selectedIndex].value
	//alert(strURL);
	xmlhttp.open("GET", strURL, true);
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			//alert(xmlhttp.responseText)	
			if(xmlhttp.responseText!=''){
				//alert(xmlhttp.responseText)	
				RepopulateOptions(getObj(targetSelectID),ConvertToOptionArray(xmlhttp.responseText),ulID);
			}else{
				getObj(targetSelectID).options.length = 0;
			}			
			
			//DisplayList(objSourceSelect,ulID);
			searchChangeActivities(targetSelectID, "ActivityList", "projects");
		}
	}
	xmlhttp.send(null)	
}

function searchChangeActivities(){
	PleaseWait();
	var strCompanyList = getCompanyList();
	var objSourceSelect = getObj('activities')
	var sourceSelectedItems = ""
	sourceSelectedItems = getSelectedItems(objSourceSelect)
	if(sourceSelectedItems!=''){
		var strURL = "/getMediaSearchOptions.asp?getType=projects&value=" + sourceSelectedItems + "&match=activity"
		if(strCompanyList!=''){
			strURL = strURL + "&companies=" + strCompanyList
		}

		xmlhttp2.open("GET", strURL, true);
		xmlhttp2.onreadystatechange=function() {
			if (xmlhttp2.readyState==4 && xmlhttp2.status==200) {
				//alert(xmlhttp.responseText)	
				// For performance reasons this array come s with a trailing | so we gotta strip it off
				if(xmlhttp.responseText!=''){
					//alert(xmlhttp.responseText)		
					RepopulateOptions(getObj('projects'),ConvertToOptionArray(xmlhttp2.responseText.substring(0,xmlhttp2.responseText.length - 1)),'ActivityList')
				}else{
					getObj('projects').options.length = 0;
				}
				// Update our lists
				//DisplayList(getObj('activities'),'ActivityList')
				//DisplayList(getObj('projects'),'ProjectList')
				DontWait();
			}
		}
		xmlhttp2.send(null)		
	}else{
		getObj('projects').options.length = 0;
		//DisplayList(getObj('activities'),'ActivityList')
		//DisplayList(getObj('projects'),'ProjectList')		
		DontWait();	
	}
}

function getCompanyList(){
	//var arrInputs = getElementsByTagName("input")
	var arrInputs = document.MediaSearch.GroupCompany
	var strReturn = ""
	if(document.MediaSearch.GroupCompany){
		for(var i=0; i<arrInputs.length; i++) {
			if(arrInputs[i].checked){
				strReturn = strReturn + arrInputs[i].value + ","
			}
		}	
	}
	return strReturn
}






function UnescapeHTML(string){
	// 'Borrowed' from http://prototype.conio.net/'s Prototype framework.
    var div = document.createElement('div');
    div.innerHTML = string.replace(/<\/?[^>]+>/gi, '');
    return div.childNodes[0] ? div.childNodes[0].nodeValue : ''; 	
}

function getExsistingOptions(objSelect){
	var arrExsistingOptions = [];
	for (var loop=0; loop<objSelect.options.length; loop++) {
		arrExsistingOptions[loop] = {optText:objSelect.options[loop].text, optValue:objSelect.options[loop].value, optSelected:objSelect.options[loop].selected};
	}
	return arrExsistingOptions;
}


function getSelectedItems(objSelect){
	var strReturn = ""
	for(var i=0; i<objSelect.options.length; i++){
		if(objSelect.options[i].selected == true){
			strReturn = strReturn + objSelect.options[i].value + ","
		}
	}
	return strReturn
}

/*
function getRemoteData(strURL){
	xmlhttp.open("GET", strURL, false);
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			return xmlhttp.responseText
		}
	}
	xmlhttp.send(null)	
}
*/


//function DisplayList(objSelect,ulID){
//	if(ulID!=''){	
		// First Clear the list
		//ClearSelections(ulID)
		// Repopulate the list
//		var objUL = getObj(ulID)
//		for(var i=0; i<objSelect.options.length; i++){
//			if(objSelect.options[i].selected == true){
				//alert(objSelect.options[i].text) 
//				var newText = document.createTextNode(objSelect.options[i].text);
//				var newNode = document.createElement("li");
//				newNode.appendChild(newText);
//				objUL.appendChild(newNode);
//			}
//		}
//	}
//}

function ClearSelections(ulID){
	//alert(ulID)	
	//alert(ulID)
	//if(ulID!=''){	
	//	var objUL = getObj(ulID)
	//	var arrLI = objUL.getElementsByTagName("li")
	//	var intLILength = arrLI.length
		// Loop thru the list array. As the array changes as we remove nodes we must
		// loop thru the length variable and always remove the first[0] item 
	//	for(var i=0; i<intLILength; i++){	
	//		objUL.removeChild(arrLI[0]);
	//	}	
	//}
}

function ResetForm(){
	//ClearSelections('ActivityList');	
	//ClearSelections('LocationList');
	//ClearSelections('ProjectList');	
	getObj('activities').options.length = 0;
	getObj('projects').options.length = 0;
}



function getObj(id){
	return document.getElementById(id)
}



function getWidth(){
	var myWidth = 0	
	if( typeof( window.innerWidth ) == 'number' ) {
	//Non-IE
		myWidth = window.innerWidth;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	//IE 4 compatible
		myWidth = document.body.clientWidth;
	}	
	return myWidth
}

/*
function addEvent(obj, evType, fn, useCapture){
  if (obj.addEventListener){
    obj.addEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.attachEvent){
    var r = obj.attachEvent("onClick", fn);
    return r;
  } else {
    alert("Handler could not be attached");
  }
}
*/



function UpdateDelivery(){
	var frmObj = document.detailsConfirmationForm;
	frmObj.deladdress.value = frmObj.address.value;
	frmObj.delsuburb.value = frmObj.suburb.value;
	frmObj.delstate.value = frmObj.state.value;
	frmObj.delpostcode.value = frmObj.postcode.value;
	frmObj.delcountry.value = frmObj.country.value
}



function AddToShortList(intID, strAID, blnShortList, OrderType, strBaseURL){
	var strURL = "/UpdateMediaCart.asp?mediaaction=add&sku=" + intID + "&OrderType=" + OrderType + "&random=" + Math.floor(Math.random() * 100000);
	//alert(strURL);
	WaitForServerResponse();
	xmlhttp.open("GET", strURL, true);
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			//alert(xmlhttp.responseText)	
			if(xmlhttp.responseText!=''){
				//alert(xmlhttp.responseText)	
				CloseWait();
				var aObj = xDOM(strAID,0)
				if(OrderType=="Video"){
					var objShortList = xDOM("VideoNumber",0);
					gintVideoItems = xmlhttp.responseText;
				}else{
					var objShortList = xDOM("PhotoNumber",0);
					gintPhotoItems = xmlhttp.responseText;
				}
				aObj.innerHTML = "Remove from shortlist";	
				aObj.setAttribute("href","javascript:RemoveFromShortList('" + intID + "','" + strAID + "'," + blnShortList + ",'" + OrderType + "')")
				objShortList.innerHTML = xmlhttp.responseText;	
				aObj.className = "RemList";		
				UpdateMediaIcons(strBaseURL);
				}else{
				
			}			

		}
	}
	xmlhttp.send(null)		
}

function RemoveFromShortList(intID, strAID, blnShortList, OrderType, strBaseURL){
	var strURL = "/UpdateMediaCart.asp?mediaaction=remove&sku=" + intID + "&OrderType=" + OrderType + "&random=" + Math.floor(Math.random() * 100000);
	//alert(strURL);
	WaitForServerResponse();
	xmlhttp.open("GET", strURL, true);
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			//alert(xmlhttp.responseText)	
			if(xmlhttp.responseText!=''){
				//alert(xmlhttp.responseText)	
				CloseWait();
				if(OrderType=="Video"){
					var objShortList = xDOM("VideoNumber",0);
					gintVideoItems = xmlhttp.responseText;
				}else{
					var objShortList = xDOM("PhotoNumber",0);
					gintPhotoItems = xmlhttp.responseText;
				}
				objShortList.innerHTML = xmlhttp.responseText;	
				var aObj = xDOM(strAID,0)
				if(blnShortList){
					var imgObj = xDOM("img" + intID,0)	
					var titleObj = xDOM("title" + intID,0)	
					if(xmlhttp.responseText == 0 && xDOM("StartShortList",0)){
						xDOM("StartShortList",0).style.display = "none"	
					}
					imgObj.className = "GreyedOut";	
					aObj.style.display = "none";
					titleObj.className = "GreyedOutText";	
				}else{
					
					aObj.innerHTML = "Add to shortlist";	
					aObj.setAttribute("href","javascript:AddToShortList('" + intID + "','" + strAID + "'," + blnShortList + ",'" + OrderType + "')")
					
					aObj.className = "AddList";		
				}
				
				UpdateMediaIcons(strBaseURL);
			}else{
				
			}			

		}
	}
	xmlhttp.send(null)		
}

function UpdateMediaIcons(strURL){
	var strMediaIcons = '';
	
	if(gintPhotoItems > 0){
		strMediaIcons = strMediaIcons + '<a href="' + strURL + '/frmaction/shortlist/type/Photo" title="View photography shortlist">     <img src="/images/photo_icon.gif" alt="Photo logo" border="0" height="20" width="21"> </a>';
	}	
	if(gintVideoItems > 0){
		strMediaIcons = strMediaIcons + '<a href="' + strURL + '/frmaction/shortlist/type/Video" title="View video shortlist">     <img src="/images/video_icon.gif" alt="Video logo" border="0" height="20" width="21"> </a>';
	}	
    if(strMediaIcons != ''){
		strMediaIcons = 'View my shortlist ' + strMediaIcons + '&nbsp;';	
		
	}

	xDOM("mediaIcons").innerHTML = strMediaIcons	
}





function ValidateMediaSearch(objForm,strType){
	var strAlert = '';

	var isAlert = false;
	var count = 0;
	var groupsUnchecked = 0
	
	if(typeof objForm.GroupCompany.length === 'number'){
		for(var i=0; i < objForm.GroupCompany.length; i++){
			if (objForm.GroupCompany[i].checked != 1){
				groupsUnchecked++;
			}
			count++;
		}
	}else{
		count = 1
		if(!objForm.GroupCompany.checked){
			groupsUnchecked++;	
		}
	}
	
	if(strType=='simple'){
		//alert(groupsUnchecked + "=" +count)
		var strProjects = objForm.project.value
		var strKeywords = objForm.keywords.value
		
		if (groupsUnchecked == count){
			isAlert = true;
			strAlert += "- Please select a Group Company\n";
		}		
		if (strProjects == '' && strKeywords == '' && groupsUnchecked == count){
			isAlert = true;
			strAlert += "- Please specify a Project/Subject or a Keyword.\n";
		}		
		
	}else{
		if (groupsUnchecked == count){
			isAlert = true;
			strAlert += "- Please select at least one Group Company\n";
		}
		if (objForm.fromMonth.value == ''){
			//if (objForm.fromYear.value != ''){
			//	isAlert = true;
			//	strAlert += "- Please select a From Month\n";
			//}
		}
		if (objForm.fromYear.value == ''){
			if (objForm.fromMonth.value != ''){
				isAlert = true;
				strAlert += "- Please select a From Year\n";
			}
		}
		if (objForm.toMonth.value == ''){
			//if (objForm.toYear.value != ''){
			//	isAlert = true;
			//	strAlert += "- Please select a To Month\n";
			//}
		}
		if (objForm.toYear.value == ''){
			if (objForm.toMonth.value != ''){
				isAlert = true;
				strAlert += "- Please select a To Year\n";
			}
		}
	}
	if (isAlert == true){
		alert (strAlert);
		return false;
	}else {
		return true;
	}
}


function DownloadLargeImage(objButton,intID,strURL,OrderType){
	var OpenImage  = window.open(strURL,'photo','');

	
	var strURL = "/UpdateMediaCart.asp?mediaaction=remove&sku=" + intID + "&OrderType=" + OrderType;
	//alert(strURL);
	xmlhttp.open("GET", strURL, true);
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			if(parseInt(xmlhttp.responseText) == 0){
				xDOM("PlaceOrder",1).display = "none";
				xDOM("DownloadIntro",1).display = "none";
				xDOM("CheckoutList",1).display = "none";
				xDOM("EmptyCart",1).display = "block";
			}
			objButton.disabled = true;
			xDOM("download" + intID,1).color = "#ccc";
			xDOM("download" + intID,1).display = "none";
		}
	}
	xmlhttp.send(null)		
	
}


function WaitForServerResponse(){
	var WaitObj = xDOM("MediaWait",1)
	WaitObj.display = "block"
	WaitObj.left = (getWidth() / 2 - 140) + "px"; 
}
function CloseWait(){
	xDOM("MediaWait",1).display = "none"
}