function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function MM_findObj(n, d) { //v4.0
  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 && document.getElementById) x=document.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

var displayRow = (navigator.appName=='Microsoft Internet Explorer' ? 'block' : 'table-row');

function clickDownload(report) {
	if(report!='') { document.getElementById('report').value=report; }
	document.getElementById('download').value='1';
	document.form1.submit();
	if(report!='') { document.getElementById('report').value=''; }
	document.getElementById('download').value='';
}
function highlightLine(line) {
	var line_id = 'line_'+line;
	document.getElementById(line_id).className = 'inputrequired';
}
function confirmdelete(record,field,script) {
	if(confirm("Delete record ?")) {
		var hasQMark = script.indexOf('?');
		var qsChar = (hasQMark==-1 ? '?' : '&');
		window.location=script+qsChar+"delete=y&"+field+"="+record
	} else {
		window.location=script
	}
	return false;
}
function imagedelete(record,script) {
	if(confirm("Delete image ?")) {
		window.location=script+"&deleteimage=y&"+record+"#amend"
	} else {
		window.location=script
	}
	return false;
}
function docdelete(record,script) {
	if(confirm("Delete document ?")) {
		window.location=script+"&deletedoc=y&"+record+"#amend"
	} else {
		window.location=script
	}
	return false;
}
function img_resize(pic_id,min_height,max_height) {
	if(document[pic_id].height==min_height) {
		document[pic_id].height=max_height
	} else {
		document[pic_id].height=min_height
	}
}
var calWin = null;
function opencalWin(field,date,e) {
    e = e || window.event;
	x = e.screenX;
	y = e.screenY;
	calWin = window.open("calendar.php?field="+field+"&d="+date, "sWin", "width=180,height=150,left="+x+",top="+y+",scrollbars=0");
	calWin.focus();
}
var cal3MWin = null;
function opencalWin3M(field,date,e) {
    e = e || window.event;
    if (e.screenX || e.screenY) {
        x = e.screenX;
        y = e.screenY;
    } else {
        x = e.clientX + 
            (document.documentElement.scrollLeft || 
            document.body.scrollLeft) - 
            document.documentElement.clientLeft;
        y = e.clientY + 
            (document.documentElement.scrollTop || 
            document.body.scrollTop) - 
            document.documentElement.clientTop;
    }
	cal3MWin = window.open("calendar3M.php?field="+field+"&d="+date, "sWin", "width=600,height=170,left="+x+",top="+y+",scrollbars=0");
	cal3MWin.focus();
}
function makeObliqueDate(d) {
	char_find = new Array('-',' ',':','.');
	for(x in char_find) {
		d = d.replace(char_find[x],"/");
		d = d.replace(char_find[x],"/");
	}
	d_split = d.split("/");
	d_split[0] = (d_split[0].length==1 ? '0'+d_split[0] : d_split[0]);
	d_split[1] = (d_split[1].length==1 ? '0'+d_split[1] : d_split[1]);
	d_split[2] = (d_split[2].length==2 ? '20'+d_split[2] : d_split[2]);
	d_oblique = d_split[0]+'/'+d_split[1]+'/'+d_split[2];
	return d_oblique;
}
var DAPopUpWin = null;
function openDAPopUpWin(script,height,width) {
	DAPopUpWin = window.open(script, "daWin", "height="+height+",width="+width+",scrollbars=auto");	
	DAPopUpWin.focus();
}
// Popup Link Menu Activator
function gotoPopUpLink(form, field) {
	var url = eval("form."+field+".options[form."+field+".selectedIndex].value");
	if (url != "") location.href = url;
	return false;
}
function selectPage(script,displayPerPage,sortString) {
	var page = document.getElementById('pageSelector').options[document.getElementById('pageSelector').selectedIndex].value;
	var startAt = (page-1)*displayPerPage;
	var url = script+'?start_at='+startAt+sortString;
	if (url != "") location.href = url;
	return false;
}
function setAllChecked(field,number) {
	for(i=0; i<number; i++) {
		var obj = field+i;
		document.getElementById(obj).checked = true;
	}
}
function setAllUnChecked(field,number) {
	for(i=0; i<number; i++) {
		var obj = field+i;
		document.getElementById(obj).checked = false;
	}
}
function closeWindow() {	
	if(top.window.opener) {
		top.window.opener.location.reload();
		top.window.opener = top;
		top.window.open('','_parent','');
		setTimeout('top.window.close()',500);
	} else {
		window.close();  
	}
}
function show_hide(section, display_type) {
	obj = section;
	display_type = (display_type!='' ? display_type : 'block');
	if(document.getElementById(obj).style.display==display_type) {
		document.getElementById(obj).style.display='none';
	} else {
		document.getElementById(obj).style.display=display_type;
	}
}
function showRow(section) {
	var val = document.getElementById(section).options[document.getElementById(section).selectedIndex].value;
	var display_obj = section+'_section';
	if(val=='Yes') {
		document.getElementById(display_obj).style.display = displayRow;
	} else {
		document.getElementById(display_obj).style.display = 'none';
	}
}
function showRows(section_id, start_no, stop_no, dd) {
	var dd = (dd!='' ? dd : 'ccs');
	var val = document.getElementById(dd).options[document.getElementById(dd).selectedIndex].value;
	for(var i=start_no; i<=stop_no; i++) {
		var section = section_id+i;
		if(i<=val) {
			document.getElementById(section).style.display = displayRow;
		} else {
			document.getElementById(section).style.display = 'none';
		}
	}
}
function clearFilters() {
	document.getElementById('f_title').selectedIndex = 0;
	document.getElementById('f_internal_group').selectedIndex = 0;
	document.getElementById('f_media_host').selectedIndex = 0;
	document.getElementById('f_client_signed').selectedIndex = 0;
	document.getElementById('f_status').selectedIndex = 0;
	document.getElementById('f_title').className = "";
	document.getElementById('f_internal_group').className = "";
	document.getElementById('f_media_host').className = "";
	document.getElementById('f_client_signed').className = "";
	document.getElementById('f_status').className = "";
}
function checkFiletype(theField,validTypes) {
	var fileSplit = theField.value.split(".");
	var fileType = fileSplit[1];
	var typeSplit = validTypes.split(":");
	var valid = 0;
	for(var i=0; i<typeSplit.length; i++) {
		if(typeSplit[i].toUpperCase()==fileType.toUpperCase()) {
			valid = 1;
		}
	}
	if(!valid) {
		alert('Incorrect file type detected: '+theField.value);
		theField.focus();
	}
}
function clickSubmit() {
	document.getElementById('Submit').value = '1';
	document.form1.submit();
	document.getElementById('Submit').value = '';
}
function arraySort(field, direction) {
	document.getElementById('asort').value=field;
	document.getElementById('asortdir').value=direction;
	document.form1.submit();
	document.getElementById('asort').value='';
	document.getElementById('asortdir').value='';
}
function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}
function formatDecimal(originalNumber, decPlaces) {
	if(isNaN(originalNumber)) {
		return '0.00'; 
	}
	originalNumber += 0.00001;
	originalNumber = originalNumber.toString();
	var decimalIndex = originalNumber.search(/\./);
	if (decimalIndex >= -1) {
		var formatted = originalNumber.substring(0, decimalIndex + 1 + decPlaces);
	} else {
		var formatted = originalNumber;
	}
	return(formatted);
}
function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + '\u00A3' + num + '.' + cents);
}

/* ==========================================================*/
var crminfoWin = null;
function openCRMinfoWin(relationship) {
	crminfoWin = window.open("crminfo.php?relationship="+relationship, "sciWin", "width=660, height=750, scrollbars=none");
	crminfoWin.focus();
}
var contactLookupWin = null;
function openContactLookupWin(parent_field,requirement) {
	contactLookupWin = window.open("crm_contactlookup.php?parent_field="+parent_field+"&requirement="+requirement, "clWin", "width=650, height=400, scrollbars=none");
	contactLookupWin.focus();
}
var contactAddWin = null;
function openContactAddWin() {
	contactAddWin = window.open("crm_contactadd.php", "conWin", "width=700, height=600, scrollbars=none");
	contactAddWin.focus();
}
var contactLeaverWin = null;
function openContactLeaverWin() {
	contactLeaverWin = window.open("a_contact_j_leaver.php", "conWin", "width=700, height=400, scrollbars=none");
	contactLeaverWin.focus();
}
var companyLookupWin = null;
function openCompanyLookupWin(parent_field,requirement) {
	companyLookupWin = window.open("crm_companylookup.php?parent_field="+parent_field+"&requirement="+requirement, "clWin", "width=650, height=400, scrollbars=none");
	companyLookupWin.focus();
}
var companyAddWin = null;
function openCompanyAddWin() {
	companyAddWin = window.open("crm_companyadd.php", "comWin", "width=700, height=550, scrollbars=none");
	companyAddWin.focus();
}
var companyCategoryWin = null;
function openCompanyCategoryWin() {
	companyCategoryWin = window.open("a_companies_j_category.php", "comWin", "width=700, height=550, scrollbars=none");
	companyCategoryWin.focus();
}
var diaryAddWin = null;
function openDiaryAddWin(user_id,contact_id) {
	diaryAddWin = window.open("crm_diary_add.php?user_id="+user_id+"&contact_id="+contact_id, "diaryWin", "width=676, height=590, scrollbars=none");
	diaryAddWin.focus();
}
var writtenAddWin = null;
function openWrittenAddWin(user_id,contact_id) {
	writtenAddWin = window.open("crm_written_add.php?user_id="+user_id+"&contact_id="+contact_id, "writtenWin", "width=676, height=350, scrollbars=none");
	writtenAddWin.focus();
}
var brandLookupWin = null;
function openBrandLookupWin(parent_field,requirement) {
	brandLookupWin = window.open("crm_brandlookup.php?parent_field="+parent_field+"&requirement="+requirement, "cbWin", "width=650, height=400, scrollbars=none");
	brandLookupWin.focus();
}
var InvWin = null;
function openInvoiceWindow(id,p) {
	p = (p==1 ? "&print=1" : "");
	InvWin = window.open("rpt_invoice_view.php?id="+id+p, "pdfWin");
	InvWin.focus();
}
var alarmWin = null;
function viewAlarm(asset_id) {
	alarmWin = window.open("jb_j_assetalarm.php?referer=jb_amend.php&asset_id="+asset_id, "alarmWin", "width=650, height=900, left=100, top=100, scrollbars=1");
	alarmWin.focus();
}
var currencyConvertWin = null;
function openCurrencyConvertWin(parent_field) {
	val = document.getElementById(parent_field).value;
	url = "http://www.google.com/finance/converter?a="+val+"&from=GBP&to=USD";
	currencyConvertWin = window.open(url, "ccWin", "width=400, height=400, scrollbars=none");
	currencyConvertWin.focus();
}
function playSound(soundobj) {
	var thissound=document.getElementById(soundobj);
	thissound.Play();
	return true;
}
function delay(millisecs) {
	var then,now; 
	then=new Date().getTime();
	now=then;
	while((now-then)<millisecs) {
		now=new Date().getTime();
	}
}
function dtmfDialler(phone_number) {
	for(i=0; i<phone_number.length; i++) {
		arg = 'sound_'+phone_number.substr(i,1);
		playSound(arg);
		delay(300);
	}
}
