// ----------------------------------------------------------------------------------------------------
// Air Hollywood DAM - Global JavaScript File (global.js) ~ Sol Media Development
// Developed By Ashley J. Dawson (16/01/06)
// ----------------------------------------------------------------------------------------------------

//Macromedia DOM Search function
function MM_findObj(n, d) //v4.01
{
  var p,i,x;  
  if(!d) d=document; 
  if((p=n.indexOf("?"))>0&&parent.frames.length) {
	  d=parent.frames[n.substring(p+1)].document; 
	  n=n.substring(0,p);
  }
  if(!(x=d[n])&&d.all) x=d.all[n]; 
  for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

//Populate Age Range CSV for stock manager form
function PopulatAgeRange()
{
	var sCSV = "";
	for(i=0;i<8;i++)
	{
		if(MM_findObj('frmAgeRange').options[i].selected)
		{	sCSV += MM_findObj('frmAgeRange').options[i].value+",";
		}
	}
	sCSV = sCSV.substr(0, (sCSV.length - 1));
	MM_findObj('frmAgeRangeCSV').value = sCSV;
}

//Perform Login
function frmUserLogin()
{
	MM_findObj('frmUserLogin').submit();
}

//Confirm Delete (Redirects on confirm)
function ConfirmDelete(sName, sURL)
{
	if(confirm('Are you sure that you want to delete \''+sName+'\'?'))
		window.location.href = sURL;
}

//Confirm sending mail to mailing list
function ConfirmMailSend(sName, sURL)
{
	if(confirm('Are you sure that you want to send \''+sName+'\'?'))
		window.location.href = sURL;
}

//Select country given hidden form field
function SelectCountry()
{
	MM_findObj('frmCountry').value = MM_findObj('frmSelectedCountry').value;
}

//Email validator
function checkMail(email)
{
	var x = email;
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(x))
	{
		return true;
	}
	else
	{
		return false;
	}
}

//Validate user form
function chkUserForm()
{
	var frmUsername = MM_findObj('frmUsername').value;
	var frmPassword = MM_findObj('frmPassword').value;
	var frmEmail = MM_findObj('frmEmail').value;
	var frmFirstName = MM_findObj('frmFirstName').value;
	var frmLastName = MM_findObj('frmLastName').value;
	var frmConfPassword = MM_findObj('frmConfirmPassword').value;
	var errMsg = '';
	
	//if(frmUsername == '')
	//{
	//	errMsg = errMsg + ' - \'Username\'\n';
	//}
	
	if(frmPassword == '')
	{
		errMsg = errMsg + ' - \'Password\'\n';
	}

	if(frmConfirmPassword == '')
	{
		errMsg = errMsg + ' - \'Confirm Password\'\n';
	}
	
	if(frmConfirmPassword != frmPassword)
	{
		errMsg = errMsg + ' - \'Passwords don\'t match\'\n';
	}
	
	if(checkMail(frmEmail) == false)
	{
		errMsg = errMsg + ' - \'Email\' is invalid\n';
	}
	
	if(frmFirstName == '')
	{
		errMsg = errMsg + ' - \'First Name\'\n';
	}
	
	if(frmLastName == '')
	{
		errMsg = errMsg + ' - \'Last Name\'\n';
	}
	
	if(errMsg == '')
	{
		return true;
	}
	else
	{
		errMsg = 'Please check the following fields:\n\n' + errMsg;
		alert(errMsg);
		return false;
	}
}

//Populate the Inventory Manager hidden CSV field with select box status IDs
function PopulateInventoryManagerCSV()
{
	//var sIDCSV = "";
	//var oSels = document.getElementsByTagName('select');
	//for(x=0;x<oSels.length;x++)
	//{
	//	var sSelId = oSels[x].id;
	//	if(sSelId.indexOf('frmStatus') > -1)  // Get only the select boxes for the inventory manager
	//	{
	//		sIDCSV += sSelId.substr((sSelId.lastIndexOf('_')+1),sSelId.length) + ',';
	//	}
	//}
	//sIDCSV = sIDCSV.substr(0, (sIDCSV.length - 1));
	//MM_findObj('frmStatusCSV').value = sIDCSV;
	
	var sIDCSV = "";
	for(x=0;x<MM_findObj('frmRecordCount').value;x++)
	{
		sIDCSV += MM_findObj('frmStatusId_'+x).value + ',';
	}
	sIDCSV = sIDCSV.substr(0, (sIDCSV.length - 1));
	MM_findObj('frmStatusCSV').value = sIDCSV;
}

//Collapse all submenus in category tree
function CollapseAll(num)
{
	for(x=1;x<num+1;x++)
	{
		if(MM_findObj('menu'+x))
		{
			MM_findObj('menu'+x).style.display = 'none';
			MM_findObj('link'+x).innerHTML = ' + ';
		}
	}
}

//Expand submenus in category tree
function ExpandMenu(id)
{
	if(MM_findObj('menu'+id))
	{
		if(MM_findObj('menu'+id).style.display == 'block')
		{
			MM_findObj('menu'+id).style.display = 'none';
			MM_findObj('link'+id).innerHTML = ' + ';
		}
		else if(MM_findObj('menu'+id).style.display == 'none')
		{
			MM_findObj('menu'+id).style.display = 'block';
			MM_findObj('link'+id).innerHTML = ' - ';
		}
		else
		{
			MM_findObj('menu'+id).style.display = 'none';
			MM_findObj('link'+id).innerHTML = ' + ';
		}
	}
}

//Check/uncheck selected box and children
function CheckBoxes(id)
{
	/*var num = MM_findObj('frmCategoryCount').value + 1;
	var sCatStr = ''
	var aCatStr = MM_findObj('frmCat'+id).value;
	aCatStr = aCatStr.split('|');
	//Check/uncheck children
	for(x=1;x<num;x++)
	{
		if(MM_findObj('frmCat'+x))
		{
			sCatStr = MM_findObj('frmCat'+x).value;
			if(sCatStr.indexOf('|'+id+'|') > -1)
			{
				if(MM_findObj('frmCat'+id).checked)
				{
					MM_findObj('frmCat'+x).checked = true;
					//If parent expand category tree
					if(MM_findObj('menu'+x))
					{
						if(MM_findObj('menu'+x).style.display == 'none')
						{
							ExpandMenu(x);
						}
					}
				}
				else
				{
					MM_findObj('frmCat'+x).checked = false;
				}
			}
		}
	}
	//Check/uncheck parentage
	for(x=2;x<aCatStr.length-2;x++)
	{
		if(MM_findObj('frmCat'+id).checked)
		{
			MM_findObj('frmCat'+aCatStr[x]).checked = true;
		}
	}*/
}

//Show all selected category ids
function ShowSelectedIds()
{
	var num = MM_findObj('frmCategoryCount').value + 1;
	allChecked = '';
	for(x=1;x<num;x++)
	{
		if(MM_findObj('frmCat'+x))
		{
			if(MM_findObj('frmCat'+x).checked)
			{
				allChecked += MM_findObj('frmCat'+x).id + ',';
			}
		}
	}
	allChecked = allChecked.replace(/frmCat/g,'');
	allChecked = allChecked.substring(0,(allChecked.length-1))
	if(!allChecked)
	{
		alert("No checked items");
	}
	else
	{
		MM_findObj('frmSelectedCategories').value = allChecked;
		//alert(MM_findObj('frmSelectedCategories').value);
	}
}

//Check search form value
function ChkForm()
{
	if(MM_findObj('frmCategory').value == '')
	{
		return false;
	}
	return true;
}

//Populate search dropdowns
function PopulateSelect()
{
	var frmType = MM_findObj('frmType').value;
	MM_findObj('frmCategory').length = 0;
	if(frmType == 'stock_footage')
	{
		for(x=0;x<aStock.length;x++)
		{
			sStock = aStock[x].split('|');
			oNewOption = new Option(sStock[1],sStock[0]);
			MM_findObj('frmCategory').options.add(oNewOption);
		}
	}
	else
	{
		for(x=0;x<aProps.length;x++)
		{
			sProps = aProps[x].split('|');
			oNewOption = new Option(sProps[1],sProps[0]);
			MM_findObj('frmCategory').options.add(oNewOption);
		}
	}
}

//Add sub-category
function AddToCategory()
{
	var oFrmCat = MM_findObj('frmCat');
	var x;
	var selCat;
	var isSelected = false;
	// Get value of selected option
	for(x=0;x<oFrmCat.length;x++)
	{
		if(oFrmCat[x].checked)
		{
			selCat = oFrmCat[x].value;
			isSelected = true;
		}
	}
	if(isSelected)
	{
		selCat = selCat.substring(1,(selCat.length-1))
		var aContents = selCat.split('|');
		var sURL = document.URL;
		if(sURL.indexOf('action') > 0)
		{
			sURL = sURL.substring(0,sURL.length-12)
		}
		MM_findObj('selID').value = aContents[0];
		MM_findObj('frmCategoryTree').action = sURL + '?action=add_form';
		MM_findObj('frmCategoryTree').submit();
	}
	else
	{
		alert('Please select a category');
	}
}

//Edit Category
function EditCategory()
{
	var oFrmCat = MM_findObj('frmCat');
	var x;
	var selCat;
	var isSelected = false;
	// Get value of selected option
	for(x=0;x<oFrmCat.length;x++)
	{
		if(oFrmCat[x].checked)
		{
			selCat = oFrmCat[x].value;
			isSelected = true;
		}
	}
	if(isSelected)
	{
		var sURL = document.URL;
		if(sURL.indexOf('action') > 0)
		{
			sURL = sURL.substring(0,sURL.length-12)
		}
		selCat = selCat.substring(1,(selCat.length-1))
		var aContents = selCat.split('|');
		MM_findObj('selID').value = aContents[0];
		MM_findObj('frmCategoryTree').action = sURL + '?action=edit_form';
		MM_findObj('frmCategoryTree').submit();
	}
	else
	{
		alert('Please select a category');
	}
}

//Delete Category
function DeleteCategory()
{
	var oFrmCat = MM_findObj('frmCat');
	var x;
	var selCat;
	var isSelected = false;
	// Get value of selected option
	for(x=0;x<oFrmCat.length;x++)
	{
		if(oFrmCat[x].checked)
		{
			selCat = oFrmCat[x].value;
			isSelected = true;
		}
	}
	if(isSelected)
	{
		selCat = selCat.substring(1,(selCat.length-1))
		var aContents = selCat.split('|');
		if(aContents.length > 2)
		{
			if(confirm('Are you sure you want to delete this category? \nAll subcategories will also be deleted'))
				var sURL = document.URL;
				if(sURL.indexOf('action') > 0)
				{
					sURL = sURL.substring(0,sURL.length-12)
				}
				MM_findObj('selID').value = aContents[0];
				MM_findObj('frmCategoryTree').action = sURL + '?action=delete';
				MM_findObj('frmCategoryTree').submit();
		}
		else
		{
			// if the length of aContents less than 2 it is a root category
			alert('You can not delete a root category')
		}
	}
	else
	{
		alert('Please select a category');
	}
}

//Redirect back to catagory select page onclick of cancel button
function CategoryCancel()
{
	window.location.href = 'categorymanager.php';
}

//Confirm Price Code Delete (Submit form on confirm)
function ConfirmPriceCodeDelete(iID, sName) 
{
	if(confirm('Are you sure that you want to delete \''+sName+'\'?'))
	{
		MM_findObj('frmFunction_'+iID).value = 'delete';
		MM_findObj('frmSavePrice_'+iID).submit();
	}
	else
		return false;
}

//Open "send mail" window
function SendItem()
{
	var sURL = document.URL;
	sURL = sURL.replace(/&/g,'[and]');
	window.open('tellafriend.php?sURL='+sURL,'','scrollbars=no,resize=no,toolbar=no,left=10,top=10,status=no,location=no,menubar=no,width=400,height=400');
}

//Validate email address and populate email list
function AddToList()
{
	var sEmail = MM_findObj('frmEmail').value;
	var flag = false;
	if(checkMail(sEmail))
	{
		var oEmailList = MM_findObj('frmEmailList');
		if(oEmailList.length > 0)
		{
			for(x=0;x<oEmailList.length;x++)
			{
				if(sEmail == oEmailList[x].value)
				{
					flag = true;
				}
			}
		}
		if(!flag)
		{
			oNewOption = new Option(sEmail,sEmail);
			//MM_findObj('frmEmailList').options.add(oNewOption);
			MM_findObj('frmEmailList').options[oEmailList.length] = oNewOption;
			MM_findObj('frmEmail').value = '';
		}
	}
	else
	{
		alert('You have entered an invalid email address');
	}
}

//Remove email address from list
function RemoveFromList()
{
	var oEmailList = MM_findObj('frmEmailList');
	oEmailList.remove(oEmailList.selectedIndex);
}

//Validate send mail form
function chkSendMail()
{
	var sEmail = MM_findObj('frmFromEmail').value;
	var msg = '';
	var sCSV = '';
	if((checkMail(sEmail)) && (MM_findObj('frmEmailList').length > 0))
	{
		//populare email csv
		for(x=0;x<MM_findObj('frmEmailList').length;x++)
		{
			sCSV += MM_findObj('frmEmailList')[x].value + ',';
		}
		sCSV = sCSV.substring(0,(sCSV.length-1))
		MM_findObj('frmEmailListCSV').value = sCSV;
		return true;
	}
	else
	{
		if(!checkMail(sEmail))
		{
			msg += ' - invlaid \"From\" email address\n';
		}
		if(MM_findObj('frmEmailList').length == 0)
		{
			msg += ' - there are no recipient email addresses\n';
		}
		alert('Please check the following fields:\n\n' + msg);
	}
	return false;
}

//Delete lightbox
function DeleteLightbox(id)
{
	if(confirm('Are you sure you want to delete this ClipBin?'))
		window.location.href = 'lightboxes.php?action=delete&id='+id;
}

function AddToLightbox(id,ClipId)
{
	alert('Adding clip '+ClipId+' to lightbox '+id);
}

function CheckAll()
{
	var oInput = document.getElementsByTagName('input');
	var checkValue = false;
	if (document.getElementById("masterChecker").checked == true) {
		var checkValue = true;
	} 
	for(x=0;x<oInput.length;x++)
	{
		var sInputId = oInput[x].id;
		if(sInputId.indexOf('frmItem_') > -1)  // Get only the checkboxes
		{
			oInput[x].checked = checkValue;
		}
	}
}

function SendMultiItem()
{
	var sIDCSV = "";
	var oInput = document.getElementsByTagName('input');
	for(x=0;x<oInput.length;x++)
	{
		var sInputId = oInput[x].id;
		if(sInputId.indexOf('frmItem_') > -1)  // Get only the checkboxes
		{
			if(oInput[x].checked == true)
			{
				sIDCSV += sInputId.substr((sInputId.lastIndexOf('_')+1),sInputId.length) + ',';
			}
		}
	}
	sIDCSV = sIDCSV.substr(0, (sIDCSV.length - 1));
	if(sIDCSV.length > 0)
	{
		window.open('tellafriend.php?sCSV='+sIDCSV,'','scrollbars=no,resize=no,toolbar=no,left=10,top=10,status=no,location=no,menubar=no,width=400,height=400');
	}
	else
	{
		alert('Please select at least one item');
	}
}

function RequestQuote()
{
	var sIDCSV = "";
	var oInput = document.getElementsByTagName('input');
	for(x=0;x<oInput.length;x++)
	{
		var sInputId = oInput[x].id;
		if(sInputId.indexOf('frmItem_') > -1)  // Get only the checkboxes
		{
			if(oInput[x].checked == true)
			{
				sIDCSV += sInputId.substr((sInputId.lastIndexOf('_')+1),sInputId.length) + ',';
			}
		}
	}
	sIDCSV = sIDCSV.substr(0, (sIDCSV.length - 1));
	if(sIDCSV.length > 0)
	{
		window.open('requestquote.php?sCSV='+sIDCSV,'','scrollbars=no,resize=no,toolbar=no,left=10,top=10,status=no,location=no,menubar=no,width=450,height=400');
	}
	else
	{
		alert('Please select at least one item');
	}
}
function RequestSingleQuote(id)
{
	var sIDCSV = id;
		window.open('requestquote.php?sCSV='+sIDCSV,'','scrollbars=no,resize=no,toolbar=no,left=10,top=10,status=no,location=no,menubar=no,width=430,height=450');
}

function RedirectParent(url)
{
	window.opener.location.href = url;
	window.self.close();
}

function CloseWindow()
{
	self.close();
}

function termPopup(mypage,myname,w,h,pos,infocus)
{

	if(pos=='random'){
	LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;
	TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;}
	if(pos=='center'){
	LeftPosition=(screen.width)?(screen.width-w)/2:100;
	TopPosition=(screen.height)?(screen.height-h)/2:100;}
	else if((pos!='center' && pos!='random') || pos==null){LeftPosition=50;TopPosition=50;}
	settings='width='+ w + ',height='+ h + ',top=' + TopPosition + ',left=' + LeftPosition + ',location=0,menubar=0,toolbar=0,status=0,scrollbars=1,resizable=0,dependent=0';
	clipPopupWindow=window.open('',myname,settings);
	if(infocus=='front'){clipPopupWindow.focus();clipPopupWindow.location=mypage;}
}



// ----------------------------------------------------------------------------------------------------
