// Miscellaneous core Javascript functions for Moodle

function popUpProperties(inobj) {
  op = window.open();
  op.document.open('text/plain');
  for (objprop in inobj) {
    op.document.write(objprop + ' => ' + inobj[objprop] + '\n');
  }
  op.document.close();
}

function fillmessagebox(text) {
  document.form.message.value = text;
}

function copyrichtext(textname) {
/// Legacy stub for old editor - to be removed soon
  return true;
}

function checkall() {
  void(d=document);
  void(el=d.getElementsByTagName('INPUT'));
  for(i=0;i<el.length;i++)
    void(el[i].checked=1)
}

function checknone() {
  void(d=document);
  void(el=d.getElementsByTagName('INPUT'));
  for(i=0;i<el.length;i++)
    void(el[i].checked=0)
}

function lockoptions(form, master, subitems) {
  // subitems is an array of names of sub items
  // requires that each item in subitems has a
  // companion hidden item in the form with the
  // same name but prefixed by "h"
  if (eval("document."+form+"."+master+".checked")) {
    for (i=0; i<subitems.length; i++) {
      unlockoption(form, subitems[i]);
    }
  } else {
    for (i=0; i<subitems.length; i++) {
      lockoption(form, subitems[i]);
    }
  }
  return(true);
}

function lockoption(form,item) {
  eval("document."+form+"."+item+".disabled=true");/* IE thing */
  eval("document."+form+".h"+item+".value=1");
}

function unlockoption(form,item) {
  eval("document."+form+"."+item+".disabled=false");/* IE thing */
  eval("document."+form+".h"+item+".value=0");
}

function submitFormById(id) {
    var theform = document.getElementById(id);
    if(!theform) {
        return false;
    }
    if(theform.tagName != 'FORM') {
        return false;
    }
    if(!theform.onsubmit || theform.onsubmit()) {
        return theform.submit();
    }
}

function select_all_in(elTagName, elClass, elId) {
    var inputs = document.getElementsByTagName('INPUT');
    inputs = filterByParent(inputs, function(el) {return findParentNode(el, elTagName, elClass, elId);});
    for(var i = 0; i < inputs.length; ++i) {
        if(inputs[i].type == 'checkbox') {
            inputs[i].checked = 'checked';
        }
    }
}

function deselect_all_in(elTagName, elClass, elId) {
    var inputs = document.getElementsByTagName('INPUT');
    inputs = filterByParent(inputs, function(el) {return findParentNode(el, elTagName, elClass, elId);});
    for(var i = 0; i < inputs.length; ++i) {
        if(inputs[i].type == 'checkbox') {
            inputs[i].checked = '';
        }
    }
}

function confirm_if(expr, message) {
    if(!expr) {
        return true;
    }
    return confirm(message);
}


/*
    findParentNode (start, elementName, elementClass, elementID)
    
    Travels up the DOM hierarchy to find a parent element with the
    specified tag name, class, and id. All conditions must be met,
    but any can be ommitted. Returns the BODY element if no match
    found.
*/
function findParentNode(el, elName, elClass, elId) {
    while(el.nodeName != 'BODY') {
        if(
            (!elName || el.nodeName == elName) &&
            (!elClass || el.className.indexOf(elClass) != -1) &&
            (!elId || el.id == elId))
        {
            break;
        }
        el = el.parentNode;
    }
    return el;
}

/*
    elementToggleHide (element, elementFinder)

    If elementFinder is not provided, toggles the "hidden" class for the specified element.
    If elementFinder is provided, then the "hidden" class will be toggled for the object
    returned by the function call elementFinder(element).

    If persistent == true, also sets a cookie for this.
*/
function elementToggleHide(el, persistent, elementFinder) {
    if(!elementFinder) {
        var obj = el;
    }
    else {
        var obj = elementFinder(el);
    }
    if(obj.className.indexOf('hidden') == -1) {
        obj.className += ' hidden';
        var shown = 0;
    }
    else {
        obj.className = obj.className.replace(new RegExp(' ?hidden'), '')
        var shown = 1;
    }

    if(persistent == true) {
        new cookie('hide:' + obj.id, 1, (shown ? -1 : 356), '/').set();
    }
}


function elementCookieHide(id) {
    var obj  = document.getElementById(id);
    var cook = new cookie('hide:' + id).read();
    if(cook != null) {
        elementToggleHide(obj, false);
    }
}

function filterByParent(elCollection, parentFinder) {
    var filteredCollection = [];
    for(var i = 0; i < elCollection.length; ++i) {
        var findParent = parentFinder(elCollection[i]);
        if(findParent.nodeName != 'BODY') {
            filteredCollection.push(elCollection[i]);
        }
    }
    return filteredCollection;
}

/*
    All this is here just so that IE gets to handle oversized blocks
    in a visually pleasing manner. It does a browser detect. So sue me.
*/

function fix_column_widths() {
    var agt = navigator.userAgent.toLowerCase();
    if ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1)) {
        fix_column_width('left-column');
        fix_column_width('right-column');
    }
}

function fix_column_width(colName) {
    if(column = document.getElementById(colName)) {
        if(!column.offsetWidth) {
            setTimeout("fix_column_width('" + colName + "')", 20);
            return;
        }

        var width = 0;
        var nodes = column.childNodes;

        for(i = 0; i < nodes.length; ++i) {
            if(nodes[i].className.indexOf("sideblock") != -1 ) {
                if(width < nodes[i].offsetWidth) {
                    width = nodes[i].offsetWidth;
                }
            }
        }

        for(i = 0; i < nodes.length; ++i) {
            if(nodes[i].className.indexOf("sideblock") != -1 ) {
                nodes[i].style.width = width + 'px';
            }
        }
    }
}

function addEvent(elm, evType, fn, useCapture) {
    // cross-browser event handling for IE5+, NS6 and Mozilla
    // By Scott Andrew
    if (elm.addEventListener) {
      elm.addEventListener(evType, fn, useCapture);
	  //alert("Event type: " + evType + ", Function: " + fn);
      return true;
    } else if (elm.attachEvent) {
      var r = elm.attachEvent('on' + evType, fn);
	  //alert("Event type: " + evType + ", Function: " + fn);
      return r;
    } else {
      elm['on' + evType] = fn;
    }	
}
  

	  
function findTarget(e) {
	
	var e1;	  
	if(window.event && window.event.srcElement)		
		e1 = window.event.srcElement;	
	if(e && e.target)		
		e1 = e.target;	

	if(!e1)		
		return;	
	else		
		return e1;	
}

var switchPaypal = {
		
		//wwwroot: 'http://moodle.untra.com',		
		wwwroot: 'accesspsp2.untrahost.com/moodle',

		SINGLEcmd: "_cart",
		SINGLEnotify_url: "http://accesspsp2.untrahost.com/moodle/enrol/paypal/ipn.php",
		SINGLEreturn: "http://accesspsp2.untrahost.com/moodle/enrol/paypal/return.php?id=",
		SINGLEcancel_return: "http://accesspsp2.untrahost.com/moodle",		

		RECURRINGcmd: "_xclick-subscriptions",
		RECURRINGnotify_url: "http://accesspsp2.untrahost.com/moodle/enrol/paypal/ipn_sub.php",
		RECURRINGreturn: "http://accesspsp2.untrahost.com/moodle/enrol/paypal/return.php?id=&subscript=YES",
		RECURRINGcancel_return: "http://accesspsp2.untrahost.com/moodle",
	
		
		init: function() {	
			
			if(!document.getElementsByTagName)		
				return;	
	
			var all_inputs = document.getElementsByTagName('input');	
			var show_first = '';
			
			for(var i = 0; i < all_inputs.length; i++) 
			{				
				var current_input = all_inputs[i];		

				if(current_input.type == 'radio' && (document.getElementById('payment_split')))		
				{		
					//alert("Event added to " + current_input.value);
					addEvent(current_input,'click',switchPaypal.changeHidden,false);		
				}
			}	
		},

		changeHidden: function(e) {
			
			var local_target = findTarget(e);	
			var payment_split = document.getElementById(local_target.name).value;
			var current_total = document.getElementById('amount').value;
			var discount = document.getElementById('senior_discount').value * 100;
			var discounted_total = document.getElementById('discounted_amount').value;
			var regular_total = document.getElementById('regular_amount').value;
			var num_courses = document.getElementById('num_courses').value;
			var msg = "";

			//recurring payment
			if(local_target.value == "1")
			{
						
				document.getElementById('amount').value = document.getElementById('regular_amount').value;
				document.getElementById('cart_amount').innerHTML = document.getElementById('regular_amount').value;
				document.getElementById('a3').value = (document.getElementById('amount').value/document.getElementById('num_payment_periods').value).toFixed(2);

				for(var i = 1; i <= num_courses; i++) {
					document.getElementById('amount_' + i).value = document.getElementById('regular_amount_' + i).value;
					document.getElementById('cart_amount_' + i).innerHTML = document.getElementById('regular_amount_' + i).value;
				}

				document.getElementById('cmd').value = switchPaypal.RECURRINGcmd;
				document.getElementById('cmd').value = switchPaypal.RECURRINGcmd;
				document.getElementById('notify_url').value = switchPaypal.RECURRINGnotify_url;
				document.getElementById('return').value = switchPaypal.RECURRINGreturn;
				document.getElementById('cancel_return').value = switchPaypal.RECURRINGcancel_return;				

				msg = "Switch to Incremental Payments (no discount): ";
			}
			//single payment
			else
			{

				document.getElementById('amount').value = document.getElementById('discounted_amount').value;
				document.getElementById('cart_amount').innerHTML = document.getElementById('discounted_amount').value + " (Save " + discount + "%!)";
				document.getElementById('a3').value = document.getElementById('amount').value;
				
				//reset course amounts
				for(var i = 1; i <= num_courses; i++) {
					if(num_courses >= 3) {
						document.getElementById('amount_' + i).value = document.getElementById('discounted_amount_' + i).value;
						document.getElementById('cart_amount_' + i).innerHTML = document.getElementById('discounted_amount_' + i).value;
					}
					else {
						document.getElementById('amount_' + i).value = document.getElementById('regular_amount_' + i).value;
						document.getElementById('cart_amount_' + i).innerHTML = document.getElementById('regular_amount_' + i).value;
					}
				}				

				document.getElementById('cmd').value = switchPaypal.SINGLEcmd;
				document.getElementById('notify_url').value = switchPaypal.SINGLEnotify_url;
				document.getElementById('return').value = switchPaypal.SINGLEreturn;
				document.getElementById('cancel_return').value = switchPaypal.SINGLEcancel_return;
				msg = "Switch to Pay In Full (discounted): ";
			}
			
			/*
			alert(document.getElementById('amount').value);
			alert(document.getElementById('a3').value);
			

			for(var i = 1; i <= num_courses; i++) {
					msg += document.getElementById('amount_' + i).value;
				}

				alert(msg);
			*/
			

		}
}

addEvent(window,'load',switchPaypal.init, false);