// JavaScript Document

function removeItem() {
  if(confirm('Are you sure you wish to remove item '+removeItem.arguments[1]+' from your Enrollment cart?'))
	if(removeItem.arguments[2] && removeItem.arguments[2] == 'event') {
		$cart = 'eventCart';
	} else {
		$cart = 'cart';
	}
	document.location = $cart+'.php?action=remove&course='+removeItem.arguments[0];
}

function removeFreeItem() {
  if(confirm('Are you sure you wish to remove item '+removeItem.arguments[1]+' from your FREE Event Signup cart?'))
	document.location = 'eventCart.php?action=remove&course='+removeItem.arguments[0];
}

function formCheck() { //v1.3 a modification of MM_vaildateForm() v4.0
  if(arguments[0].length > 15) args=arguments[0].split(','); else args=formCheck.arguments;
  var i,p,q,nm,test,num,min,max,errors='';
  for (i=0; i<(args.length-2); i+=4) { test=args[i+2]; val=MM_findObj(args[i]); 
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@'); s=val.indexOf('.');
        if (p<1 || p==(val.length-3) || s<1 || s==(val.length-2)) errors+=' - '+args[i+1]+' must contain an e-mail address.\n';
      } else if (test.indexOf('isValue')!=-1) { 
        if (val!=args[i+3]) errors+=' - '+args[i+1]+' is not valid.\n';
	  } else if (test.indexOf('isChecked')!=-1) { 
        if (document.getElementById(args[i]).checked == false) errors+=' - '+args[i+1]+' is required.\n';
	  } else if (test.indexOf('isDate')!=-1) { 
        if (!isDate(val)) errors+=' - '+args[i+1]+'\n';
	  } else if (test.indexOf('isPhone')!=-1) {
		if(val.charAt(0) != 0) errors +=' - '+args[i+1]+' doesn\'t start with a zero, please include your area code.\n';
  		if(val.length != 11) errors +=' - '+args[i+1]+' doesn\'t appear to be the correct length.';
	  } else if (test.indexOf('isCard')!=-1) { 
        if (makeNumber(val)!=makeNumber(args[i+3])) errors+=' - '+args[i+1]+' is not valid.\n';		
      } else if (test!='R') { num = makeNumber(val);
        if (isNaN(num)) errors+=' - '+args[i+1]+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=parseFloat(test.substring(8,p)); max=parseFloat(test.substring(p+1));
          if (num<min || max<num) errors+=' - '+args[i+1]+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += ' - '+args[i+1]+' is required.\n'; }
  } if (errors) { alert('There\'s something wrong with your form:\n'+errors); return false; }
  	else { 
	if(arguments[1])
	  replaceButton(arguments[1]);
	return true; }
  }
	
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;
}

function selectProduct() {
  if(document.getElementById('productCode').value) {
    new Ajax.Updater('productComponents', 'assets/includes/productComponents.php', {parameters:'method=ajax&productCode='+document.getElementById('productCode').value, evalScripts:true, asynchronous:true});
	$('courseLoading').src = 'assets/graphics/loadingSmall.gif';
  }
}

function replaceButton() {
  if($(arguments[0]))
    $(arguments[0]).innerHTML = '<img src="assets/graphics/loadingSmall.gif" width=20 height=20 alt="Sending data" />';
}

function selectProductArea($countryCode) {
	if(document.getElementById('productArea').value) {
		switch(document.getElementById('productArea').value) {
  			case 'UKT':
  				$dest = 'uktList';
  				$div = 'productDisplay'; 		
  				break;
  			case 'BMAT':
  			case 'UKCAT':
  			case 'LNAT':
  			case 'UKUAC':
  			case 'usmle1':
  			case 'usmle2':
  			case 'usmle3':
  				$('productLoading').src = 'assets/graphics/loadingSmall.gif';
  				$dest = 'courseList';
  				$div = 'secondDisplay';
  				break;
  			case 'USMLE':
  				$dest = 'usmleList';
  				$div = 'productDisplay';
  				break;
  			default:
  				if($countryCode == 10) {
  					$dest = 'typeList';
  					$div = 'productDisplay';
  				} else {
  					$dest = 'courseList';
  					$div = 'productDisplay';
  				}
  			break;
  			
		}
		new Ajax.Updater($div, 'assets/includes/'+$dest+'.php', {parameters:'method=ajax&productArea='+document.getElementById('productArea').value, evalScripts:true, asynchronous:true});
	} else {
		if($('productDisplay')) {
			$('productDisplay').innerHtml = '';
		}
		if($('secondDisplay')) {
			$('secondDisplay').innerHtml = '';
		}
	}
}

function selectProductType() {
	if($('productType').value) {
		new Ajax.Updater('secondDisplay', 'assets/includes/courseList.php', {parameters:'method=ajax&productArea='+$('productArea').value+'&productType='+$('productType').value, evalScripts:true, asynchronous:true});
	}
}


function hideButton() {
  if(document.getElementById(arguments[0]))
    document.getElementById(arguments[0]).style.display = 'none';
}

function openWindow() {
  $args=openWindow.arguments;
  thisWindow = window.open($args[1],$args[0],'width='+$args[2]+',height='+$args[3]+',scrollbars='+$args[4]);
  $leftPosition = (screen.width-$args[2])/2; $topPosition=(screen.height-$args[3]-40)/2;
  thisWindow.moveTo($leftPosition,$topPosition);
  thisWindow.focus();
  }

function showSpecify() {
  $specifyFields = new Array("Publication","College/Careers Fair","Employer","Other website","School","Other");
  if($specifyFields.include(document.getElementById('howHeard').options[document.getElementById('howHeard').selectedIndex].value) == true)
    document.getElementById('pleaseSpecify').style.display = 'block';
  else {
    document.getElementById('school').value = '';
	document.getElementById('pleaseSpecify').style.display = 'none'; }
}

/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=2007;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		return false
	}
return true
}

function ValidateForm(){
	var dt=document.frmSample.txtDate
	if (isDate(dt.value)==false){
		dt.focus()
		return false
	}
    return true
 }
 
 
function in_array (needle, haystack, argStrict) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: vlado houba
    // *     example 1: in_array('van', ['Kevin', 'van', 'Zonneveld']);
    // *     returns 1: true
    // *     example 2: in_array('vlado', {0: 'Kevin', vlado: 'van', 1: 'Zonneveld'});
    // *     returns 2: false
    // *     example 3: in_array(1, ['1', '2', '3']);
    // *     returns 3: true
    // *     example 3: in_array(1, ['1', '2', '3'], false);
    // *     returns 3: true
    // *     example 4: in_array(1, ['1', '2', '3'], true);
    // *     returns 4: false
 
    var key = '', strict = !!argStrict;
 
    if (strict) {
        for (key in haystack) {
            if (haystack[key] === needle) {
                return true;
            }
        }
    } else {
        for (key in haystack) {
            if (haystack[key] == needle) {
                return true;
            }
        }
    }
 
    return false;
}
