var xmlHttp;

function createXMLHttpRequest() {
	if(window.ActiveXObject) {
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else if (window.XMLHttpRequest) {
		xmlHttp = new XMLHttpRequest();
	}
}

function GoToUrl(){
	var URL;
	URL = document.nav_pulldown_form.url.options[document.nav_pulldown_form.url.selectedIndex].value;
	if (URL == 'AboutRegistration') {
		AboutRegistration();
	} else {
		//alert ('hi');
		//alert (URL);
		window.location.href = URL;
	}
}

function AboutRegistration() {
	alert("You must be logged in to use this function.  If you are already registerd please login.  If you do not have an account yet, please click on the user registration link.");
}

function ClearDefaultForm() {
	document.form.keyword_or.value = '';
	document.form.keyword_and.value = '';
	document.form.keyword_not.value = '';
	document.form.keyword_exact.value = '';
	document.form.keyword_number.value = '';
	document.form.keyword_photographer.value = '';
	}

function ClearAndOrNotExact() {
	document.form.keyword_or.value = '';
	document.form.keyword_and.value = '';
	document.form.keyword_not.value = '';
	document.form.keyword_exact.value = '';
}
function ClearSelect(this_form) {
	//var this_form = document.form.orientation;
	
	for(var x=1;x<this_form.length;x++) {
		this_form[x].checked = false;
	}
}

function JyunbiChu() {
	alert('Currently under construction');
}

function entsub(myform) {
  if (window.event && window.event.keyCode == 13)
    myform.submit();
  else
    return true;
}

var newwindow = '';

function CompPopUp(theURL) {

  if (!newwindow.closed && newwindow.location) {
   newwindow.location.href = theURL;
  }
  else {
    newwindow=window.open(theURL,'Image_Details','menubar=yes,scrollbars=yes,resizable=yes,width=600,height=800,left=40,top=10');
    if (!newwindow.opener) newwindow.opener = self;
  }
  if (window.focus) {newwindow.focus()}
}

function toggleHiddenLayer(whichLayer)
{
	if (document.getElementById)
	{
		// this is the way the standards work
		var style2 = document.getElementById(whichLayer).style;
		style2.display = style2.display? "":"block";
	}
	else if (document.all)
	{
		// this is the way old msie versions work
		var style2 = document.all[whichLayer].style;
		style2.display = style2.display? "":"block";
	}
	else if (document.layers)
	{
		// this is the way nn4 works
		var style2 = document.layers[whichLayer].style;
		style2.display = style2.display? "":"block";
	}
}

function DeleteMessage(id, rm, formName) {
	if(window.formName) {
		var thisForm = document.getElementByTagName(formName);
		thisForm.rm.value=rm;
		thisForm.delete_message.value=id;
		thisForm.submit();
	}
	
	else {
		document.form.delete_message.value=id;
		document.form.rm.value=rm;
		document.form.submit();
	}

}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function ShowHelp(id) {
	if($('help_text').style.display == 'none'){
		$('help_text').show();
		$('hide_' + id).style.display='inline';
		$('show_' + id).style.display='none';
	}
	else{
		$('help_text').hide();
		$('hide_' + id).style.display='none';
		$('show_' + id).style.display='inline';
	}
}

function removeElement(divNum, contents_pid) {
	var d = $('additional_images');
	var olddiv = $(divNum);

	// Ajax call to remove the item from the invoice.
	if(contents_pid) {
		createXMLHttpRequest();
		xmlHttp.open("GET", "/cgi-bin/search.cgi?rm=invoice_delete_image&amp;contents_pid="+contents_pid, true);
		xmlHttp.send(null);
	}
	olddiv.remove();
}

function modifyInvoice() {
	document.invoice_actions.rm.value = 'invoice_create_edit';
	document.invoice_actions.submit();
}

function sendInvoice() {
	document.invoice_actions.rm.value = 'invoice_save_send';
	document.invoice_actions.send.value = '1';
	document.invoice_actions.submit();
}

function cancelInvoice() {
	document.invoice_actions.rm.value = 'invoice_cancel';
	document.invoice_actions.submit();
}

function searchForClient (searchType, idIdentifier, fieldName, val) {

	var scode;
	var functionCall = 'setClientId';
	var extraSearchName = '';
	if(searchType) {
		searchType = "&amp;"+searchType+"=1";
		functionCall = 'setScodeId';
	} else {
		searchType = '';
	}
	if(idIdentifier) {
		scode = idIdentifier;
		scode = "&amp;scode="+scode;
		idIdentifier = "_"+idIdentifier
	} else {
		idIdentifier = '';
	}
	if(fieldName) {
		fieldName = fieldName+idIdentifier;
		extraSearchName = "&amp;search_name="+val;
	} else {
		fieldName = '';
	}
//	var charCode = (evt.which) ? evt.which : event.keyCode
//	if(charCode == 37 || charCode == 38 || charCode == 39 || charCode == 40) {
//		return false;
//	}

	if(searchType) {
		new Ajax.Autocompleter("autocomplete"+idIdentifier, "autocomplete_choices"+idIdentifier, "/cgi-bin/search.cgi", {paramName: '\''+fieldName+'\'', minChars: 3, indicator: 'indicator1'+idIdentifier, parameters: 'rm=invoice_user_lookup'+searchType+scode+extraSearchName, afterUpdateElement:functionCall});
	}
	else {
		new Ajax.Autocompleter("autocomplete", "autocomplete_choices", "/cgi-bin/search.cgi", {paramName: 'search_name', minChars: 3, indicator: 'indicator1', parameters: 'rm=invoice_user_lookup', afterUpdateElement:setClientId});
	}
}

function completeInvoice() {
	document.invoice_actions.rm.value = 'invoice_complete';
	document.invoice_actions.submit();
}

function setClientId(input_field, list_item) {
	var user_id = list_item.id.replace("auto_search_","");
	document.form.existing_user_id.value = user_id;
}

function setScodeId(input_field, list_item) {
	var user_id = list_item.id.replace("auto_search_","");
	var scode = user_id.replace("SC:([^_]+)_", "$1");
	user_id = user_id.replace("SC:"+scode+"_(.*)", "$1");
	$('photographers_primary_id_'+scode).value = user_id;
}

function fetchClientInfo (user_id) {

	// Ajax call to fetch the user info.
	if(user_id) {
		createXMLHttpRequest();
		xmlHttp.onreadystatechange = handleClientInfoStateChange;
		xmlHttp.open("GET", "/cgi-bin/search.cgi?rm=invoice_user_lookup&amp;pid="+user_id, true);
		xmlHttp.send(null);
	}

}

function handleClientInfoStateChange() {
	if(xmlHttp.readyState == 4) {
		if(xmlHttp.status == 200) {
			$("client_details").innerHTML = xmlHttp.responseText;
		}
	}
}
