formitems = 16;
formitems++;


function could_be_an_email(x)
// A very forgiving email address validation function. Will let certain invalid emails through
// more thorough checking is done server side
{
var atpos=x.indexOf("@");
var dotpos=x.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
  {
  return false;
  }
else
  {
  return true;
  }
}

function tgl_toggle(item_id)
{
	itemobj = document.getElementById(item_id);
	if (itemobj.style.display == 'none') itemobj.style.display = 'block'; else itemobj.style.display = 'none';

}

function onoff(item,flag)
{

    itemobj = document.getElementById(item);
    if(itemobj){	
	if (flag == 1) itemobj.style.display = 'block'; else if (flag == 0) itemobj.style.display = 'none';
	}
}

function onoffrow(rownum,flag)
{
	itema = 'row' + rownum + 'a';
	itemb = 'row' + rownum + 'b';
	
	tr = 'tr' + rownum;
	
	itemobja = document.getElementById(itema);
	itemobjb = document.getElementById(itemb);
	trobj = document.getElementById(tr);
	
	if (flag == 1)
	{
		itemobja.style.display = 'block'; 
		itemobjb.style.display = 'block'; 
		trobj.style.height = '25px';	
	}
	else if (flag == 0) 
	{
		itemobja.style.display = 'none';
		itemobjb.style.display = 'none';
		trobj.style.height = '0px';
	}
}

function onoffx2(item1,item2,flag)
{
	 if(flag==0) { onoff(item1,1); onoff(item2,0); }
	 if(flag==1) { onoff(item1,0); onoff(item2,1); }
}


function toggleform(onoff, start)
{

	var x = start;

	while(x < formitems)
	{
		var editnames = 'edit' + x
		var savednames = 'saved' + x
		
		if(onoff == 0)
		{
			if(document.getElementById(savednames).innerHTML == '')
			{
				document.getElementById(editnames).style.display = 'block';
				document.getElementById(savednames).style.display = 'none';

			}
			else
			{ // Hides the form
				document.getElementById(editnames).style.display = 'none';
				document.getElementById(savednames).style.display = 'block';
			}
			
		}
		else if(onoff == 1)
		{
			document.getElementById(editnames).style.display = 'block';
			document.getElementById(savednames).style.display = 'none';
		}
		x++;	
	}

}



function populateacross(direction,formitems)
{ // 0 is left to right

	var x = 1;
	formitems++;

	while(x < formitems)
	{
		var inputnames = 'input' + x
		var savednames = 'saved' + x
		
		if(direction == 0) document.getElementById(inputnames).value = document.getElementById(savednames).innerHTML;
		else if (direction == 1) document.getElementById(savednames).innerHTML = document.getElementById(inputnames).value;

		x++;	
	}


}


function isdefined(variable)
{
    return (typeof(window[variable]) == 'undefined')?  false: true;
}

function disableDiv(elmid) {
	
	elm = document.getElementById(elmid);

	_top = 0;
	_left = 0;
	pelm = elm;
    
	while (pelm.tagName !="BODY") {
	
		_top += pelm.offsetTop
		_left += pelm.offsetLeft

	    pelm = pelm.offsetParent
		    }

//	_top = elm.offsetTop
//	_left = elm.offsetLeft
	_width = elm.offsetWidth
	_height = elm.offsetHeight

	overlay = document.createElement('div')
	overlay.style.width = _width + 'px'
	overlay.style.height = _height + 'px'
	overlay.style.position = 'absolute'
	overlay.style.background = '#ffffff'
	overlay.style.top = _top + 'px'
	overlay.style.left = _left + 'px'
	overlay.id = 'overlay_' + elmid;

	overlay.style.filter = 'alpha(opacity=50)'
	overlay.style.opacity = '0.5'
	overlay.style.mozOpacity = '0.5'
	
	newtop = parseInt(_top) + (parseInt(_height) / 2) - 24;
	newleft = parseInt(_left) + (parseInt(_width) / 2) - 23;
	
	loader = document.createElement('img')
	loader.style.position = 'absolute'
	loader.style.top = newtop + 'px'
	loader.style.left = newleft + 'px'
	loader.src = js_docroot+'/images_ord/loader2.gif'
	loader.id = 'loader_' + elmid;

	document.getElementsByTagName("body")[0].appendChild(overlay);
	document.getElementsByTagName("body")[0].appendChild(loader);
}

function warningText(elmid) {
	
	elm = document.getElementById(elmid);

	_top = 0;
	_left = 0;
	pelm = elm;
    
	while (pelm.tagName !="BODY") {
	
		_top += pelm.offsetTop
		_left += pelm.offsetLeft

	    pelm = pelm.offsetParent
		    }

	_width = elm.offsetWidth
	_height = elm.offsetHeight
//	_top = elm.offsetTop
//	_left = elm.offsetLeft
	newtop = parseInt(_top) + (parseInt(_height) / 2);
	newleft = parseInt(_left) + (parseInt(_width) / 2) - 250;

	warningtext = document.createElement('div')
	warningtext.style.position = 'absolute'
	warningtext.style.width = '500px'
	warningtext.style.background = '#ffffff'
	warningtext.style.top = newtop + 'px'
	warningtext.style.background = '#FFCAA8'
	warningtext.style.left = newleft + 'px'
	warningtext.id = 'warningtext' + elmid;
	warningtext.innerHTML = 'Your payment is being processed. Please do not press the \'Back\', \'Forward\', \'Stop\' or \'Referesh\' buttons on your browser';
	
	
	document.getElementsByTagName("body")[0].appendChild(warningtext);
}
function toggleformitem(item,onoff)
{

	x = item.substr(5);

	var editnames = 'edit' + x
	var savednames = 'saved' + x
		
	if(onoff == 0)
	{
		document.getElementById(editnames).style.display = 'none';
		document.getElementById(savednames).style.display = 'block';
	}
	else if(onoff == 1)
	{
		document.getElementById(editnames).style.display = 'block';
		document.getElementById(savednames).style.display = 'none';
	}

}


function get_cart(custid)
{
        var default_handler = "show_cart_common"; //Use the cart handler in common as a default
        if (!window.js_cart_handler) js_cart_handler = default_handler; 
        else if ( js_cart_handler.replace(/[\s\c]/,'') == "" ) js_cart_handler = default_handler; 
        
        if(window.location.pathname.match(/pos\.php/))
        { // Set ADM flag if request made from POS
        makeRequest(js_docroot+"/ajax/get_cart.php?custid="+ custid + "&adm=1&handler=" + js_cart_handler);
        }
        else if(window.location.pathname.match(/index\.php/) || window.location.pathname.match(/\//) )
        { //Use get cart handler for the compact cart if in CMS index.php file, ideally the handlers should point straight to get_cart_compact in this situation, but
          //this will do for now. Another nicer approach is to combine the two functions, but will need to fish through the ajax scripts and HTML to change all the calls
        get_cart_compact(custid);
        }
        else
        {
        makeRequest(js_docroot+"/ajax/get_cart.php?custid="+ custid + "&handler=" + js_cart_handler);
        }
}

function get_cart_compact(custid)
{
    var default_handler = "show_cart_common"; //Use the cart handler in common as a default
    if (!window.js_cart_handler) js_cart_handler = default_handler; 
    else if ( js_cart_handler.replace(/\s*/,'') == "" ) js_cart_handler = default_handler; 
    makeRequest(js_docroot+"/ajax/get_cart.php?custid="+ custid + "&compact=1&handler="+js_cart_handler);
}

function del_from_cart(custid,id)
{                           
	makeRequest(js_docroot+"/ajax/cart_do.php?action=delete&custid="+ custid + "&id=" + id);
	
}

function enable_change_q(n)
{
	item1 = 'cart_e_' + n;
	item2 = 'cart_s_' + n;

	onoff(item1,1);
	onoff(item2,0);

}

function change_cart_q(custid,id,n)
{
	var	item = 'cart_i_' + n
	q = document.getElementById(item).value;
	makeRequest(js_docroot+"/ajax/cart_do.php?action=changeq&custid="+ custid + "&id=" + id + "&n=" + n + "&q=" + q);
}

function cart_check_out()
{
        window.location = ssl_docroot+'/payment.php';

}

function show_cart_common(val,id)
{
        //The default show cart function. There are other similar, but ''different''' functions to this in other files.
	if(id == 'my_cart') 
	{	
		document.getElementById('my_cart').innerHTML = unescape(val);
                makeRequest("/ajax/query_cart.php?itemid="+global_itemid+"&custid="+custid);	
        
	}

}

function add_to_cart(itemid,custid,additional)
{
	var	item = 'q' + itemid
	quantity = document.getElementById(item).value;
	
	if(quantity > 0) makeRequest("/ajax/cart_do.php?action=add&itemid="+itemid+"&custid="+custid+"&quantity="+quantity);

	// Extra requests here for conjoined but noncompulsory items such as the eTG cd
	if(additional != '')
	{
	  if(document.getElementById(additional.toLowerCase()).checked == true)
		{
		  additional_itemid = document.getElementById(additional.toLowerCase()).value;
		  additional_quantity = (document.getElementById('q'+additional.toLowerCase()) || {value:1}).value;
		  if(additional_itemid > 0){
		    //set timeout to ensure 1st request completes before second...
		    setTimeout(function(){
			makeRequest("/ajax/cart_do.php?action=add&itemid="+additional_itemid+"&custid="+custid+"&quantity="+1);
		      }, 80);
		  }
		}
	}
}

function add_coupon(itemid,custid)
{
	var	item = 'q' + itemid
	code = document.getElementById(item).value;
	if(code.length > 0) makeRequest("/ajax/cart_do.php?action=addcoupon&itemid="+itemid+"&custid="+custid+"&code="+code);

}


function add_other_discount(custid)
{
	var	item = 'disc_code'
	code = document.getElementById(item).value;
	if(code.length > 0) makeRequest("/ajax/cart_do.php?action=add_other_discount&custid="+custid+"&code="+code);

}

function post_cart_add(val,id)
{
        if(id=='error' && val.length > 0)
	{
		alert(val);
	}
        else if(id=='errors')
	{
		document.getElementById('errors').innerHTML = unescape(val);
	}
	if(id=='end') 
	{
		eval(val);
		//get_cart(custid); //commented out because this should be called by eval(val)
	}
}

function clear_items(items)
{
	for(n=0;n<items;n++)
	{
		id_item = 'd' + n;
		itemid = document.getElementById(id_item).value; 

		q_item = 'q' + itemid;		
		q_elem = document.getElementById(q_item);
		if(q_elem.type == "checkbox")
		  q_elem.checked=false;
		else
		  q_elem.value = ''; 
	}

	document.getElementById('ETGCD').checked = false;
	ISILO_dom = document.getElementById('ISILO');
	if (ISILO_dom) ISILO_dom.checked = false;

}

function get_prices(custid)
{
	parms = 'custid=' + custid;
	makePOSTRequest("/ajax/pricelist.php",parms);

}
function show_prices(val,id)
{
	if(id.substr(0,1) == 'p')
	{
                dom_element = document.getElementById(id);
                if (dom_element)
                    dom_element.innerHTML = '$' + val;
	}
}


function post_cart_del(val,id)
{
	if(id=='end')
	{
		get_cart(custid);
	}
}

function post_cart_changeq(val,id)
{
	if(id.substr(0,5)=='cart_')
	{
		
		item1 = id.substr(0,5) + 's_' + id.substr(5);
		oldval = document.getElementById(item1).innerHTML;
		document.getElementById(item1).innerHTML = val;

		if(oldval != val) get_cart(custid);
		
	}
	
}

function post_query_cart(val,id)
{
    if(id=='end')
    {
        if(val>0) document.getElementById('addbox').style.display='none';
        else document.getElementById('addbox').style.display='block';
        
    }

}


function getCheckedValue(radioObj) {
	if(!radioObj)
	return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
	if(radioObj.checked)
	return radioObj.value;
	else
	return "";
	for(var i = 0; i < radioLength; i++) {
	if(radioObj[i].checked) {
	return radioObj[i].value;
	}
	}
	return "";
}

function rem_lead_ws(val)
{
	while(val.substr(0,1) == ' ')
	{
		val = val.substr(1);
	}
	return val;
}

function post_get_form(val,id)
{
	if(id == 'end')
	{
		document.getElementById('saved0').innerHTML = unescape(val);
	}
}

function post_change_custid(val,id)
{
	if(id == 'message')
	{
		alert(val);
	}
	if(id == 'end')
	{       //Redirect browser to page custid was changed from. Note this is far from ideal but currently the pages this function is valled on require different
                //GET variable names, so the GET variables are all encoded in the request.
                //view.php requires: ref, ext
                //pos.php requires prev
		document.location = js_docroot+location.pathname+('?ref='+val+'&ext=1&prev='+val);
	}
}

function insert_change_custid_link()
{
	if(document.getElementById('saved0'))
	{
		if(document.getElementById('saved0').innerHTML.length > 0  && document.getElementById('saved0').innerHTML.substr(0,1) != '<')
		{ // Stop the link from being changed if a) its empty and b) its already been changed to a link
			document.getElementById('saved0').innerHTML = '<a href=# onclick=\'get_changecustid_form("' + document.getElementById('saved0').innerHTML + '"); return false;\'>' + document.getElementById('saved0').innerHTML + '</a>';
		}
	}
}

function get_changecustid_form(custid)
{
	parms = 'request=get_form&custid='+custid;
	makePOSTRequest(js_docroot+'/admin/ajax/change_custid.php',parms);

}

function initiate_custid_change(old_custid)
{
	new_custid = document.getElementById('new_custid').value;
	parms = 'request=change_custid&custid='+old_custid+'&new_custid='+new_custid;
	makePOSTRequest(js_docroot+'/admin/ajax/change_custid.php',parms);
}

function living_form()
{
	if(document.getElementById('saved1').innerHTML == 'Individual' || document.getElementById('input1').value == 'Individual')
	{
			onoffrow(2,0);
	}
	if(document.getElementById('saved1').innerHTML == 'Business' || document.getElementById('input1').value == 'Business')
	{
			onoffrow(2,1);
	}
	
	if(document.getElementById('input12').value == 'Specialist')
	{
			document.getElementById('row13a').innerHTML = 'Specialty:';

			onoffrow(13,1);
			onoffrow(14,0);
			onoffrow(15,0);

	}
	else if(document.getElementById('input12').value == 'Other')
	{
			document.getElementById('row13a').innerHTML = 'Specify:';

			onoffrow(13,1);
			onoffrow(14,0);
			onoffrow(15,0);

	}
	else if(document.getElementById('input12').value == 'Undergraduate')
	{
			document.getElementById('row13a').innerHTML = 'Faculty:';

			onoffrow(13,1);
			onoffrow(14,1)
			onoffrow(15,1)
	}
	else
	{
			onoffrow(13,0);
			onoffrow(14,0)
			onoffrow(15,0)
	}
}

function show_dialog_contents(val,id)
{
	if(id=='content')
	{
		document.getElementById('dialog-content').innerHTML = unescape(val);
	new Draggable('dialog', { revert: false, scroll: window, handle:'dialog-header'});
	}
	else if(id=='jscode')
	{
		eval(unescape(val));
	}	

}

function clearform(clearsaved)
{
	var x = 1;

	while(x < formitems)
	{
		
		var editnames = 'input' + x
		var savednames = 'saved' + x
		var rowa = 'row' + x + 'a';
		var rowb = 'row' + x + 'b';
		
		if(x==1) document.getElementById(editnames).value = 'Individual';
		else if(x==3) document.getElementById(editnames).value = 'Dr';
		else if(x==11) document.getElementById(editnames).value = 'Australia';
		else if(x==12) document.getElementById(editnames).value = 'igp';
		else if(x == 13 || x == 14 || x == 15)
		{
			onoff(rowa,0);
			onoff(rowb,0);
		}
		else
		{
			document.getElementById(editnames).value = '';
		}

		if(clearsaved == 1)
		{
			document.getElementById(savednames).innerHTML = '';
		}
		
		x++;	
	}
	

}

