var popWin;
var winStatus;
var ImAPopup = 0;

function Breakout()
{
  if (top.location != location) {
    top.location.href = document.location.href ;
  }
}



function CMSValidator()
{
    //Find the proper form : its action must contain "EmailForm.php"
    var theForm = false;
    for (var i = 0; i < document.forms.length; i++) {
        aForm = document.forms[i];
        if (aForm.action.indexOf('EmailForm.php') != -1) {
            theForm = aForm;
            break;
        }
    }
    
    //Not found : cancel submission !
    if (!theForm) {
        alert('Javascript Error : this form is not submittable. Please inform its'
            + ' author - you can find about him by clicking \'Get information '
            + 'about this page\' at the bottom of this page');
        return false;
    }
    
    var eleName;
    var radioOn;
    if(confirm('Continue with the submission of this form?')){
       
        for(i=0; i<theForm.elements.length; i++){
            eleName = theForm.elements[i].name;
            //alert(eleName + ' ' + theForm.elements[i].value);
            if(eleName.indexOf('MAND-') == 0){
                if((theForm.elements[i].type == "text" || theForm.elements[i].type == "textarea") 
                    && theForm.elements[i].value == ""){
                        alert("Please fill in all of the mandatory fields.");
                        theForm.elements[i].focus();
                        return false;
                }
                
                if ((theForm.elements[i].type == 'select-one' || theForm.elements[i].type == 'select-multiple') 
                    && theForm.elements[i].value == "-"){
                        alert("Please fill in all of the mandatory fields.");
                        theForm.elements[i].focus();
                        return false;                        
                }
                if (theForm.elements[i].type == 'radio'){
                    radioOn = false;
                    for(ii=0; ii<theForm.elements[eleName].length; ii++){
                        if (theForm.elements[eleName][ii].checked) {
                            radioOn = true;
                        }
                    }
                    if(!radioOn){
                        eleName = eleName.substr(5);
                        alert("Please check at least one radio button for question " + eleName + ".");
                        theForm.elements[i].focus();
                        return false;
                    }
                }
            }
        }
        
        return true;
    }else{
        return false;   
    }
   
}


function pop_me_up(pURL, features){
	new_window = window.open(pURL, "popup_window", features);
	new_window.focus();
}



//cookie management

function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}


function GetCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
      return getCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break; 
  }
  return null;
}

function SetCookie (name,value,expires,path,domain) {
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "");
}

function DeleteCookie (name,path,domain) {
  if (GetCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

function cleanUp(){
	if(winStatus){
		if(!popWin.closed){
		 popWin.close();	
		}	
	winStatus = false;
	}
}


function HelpWindow(headHelp,txtHelp){
		cleanUp();
		var txtHTML;

		txtHTML="<HTML><HEAD><TITLE>HELP</TITLE>" + 
				"<STYLE>" +
				".heading {" +
				" background: transparent url(/images/aoc/headingBG.gif) repeat top left;" +
				" color: #787878;" +
				" font-family: Arial, Helvetica, sans-serif;" +
				" font-size: 18px;" +
				" font-weight: 600;" +
				" padding-top: 16px; padding-right: 6px; padding-bottom: 2px; padding-left: 6px;" +
				" }" +
				".bodytext{" +
				"	color: #050505;" +
				" 	font-family: Arial, Helvetica, sans-serif;" +
				" 	font-size: 12px;" +
				" }" +
				"</STYLE>" + 
				"</HEAD><BODY><DIV CLASS='heading'>" + 
				headHelp + 
				"</DIV>" +
				"<DIV CLASS='bodytext'><p>" + 
				txtHelp +
				"</p></DIV>" +
				"<TABLE width='100%' border='0'><TR><TD align='center'>" +
				"<A class='bodytext' href='javascript:window.close();'>Close window</A>" +
				"</TD></TR></TABLE>" +
				"</BODY></HTML>";	

		popWin = window.open(null,"displayWindow","toolbar=no,resizable=no,menubar=no,scrollbars=yes,status=no,width=480,height=300"); 
		
		if(popWin){			
			popWin.document.write(txtHTML);
			winStatus=true;
		}

}

//calendar pop-up
function show_calendar4(str_target, str_datetime, str_boldDays) {
	var arr_months = ["January", "February", "March", "April", "May", "June",
		"July", "August", "September", "October", "November", "December"];
	var week_days = ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"];
	var n_weekstart = 1; // day week starts from (normally 0 or 1)
    
    //Extract values passed
    var boldDays = Object();
    if (!str_boldDays) {
        var str_boldDays = '';
    }

    var boldMonths = str_boldDays.split(',');
    for (var i = 0; i < boldMonths.length; i++) {
        month = boldMonths[i].substr(0, 6);
        var days = boldMonths[i].substr(6);
        while (days) {
            boldDays[month + days.substr(0, 2)] = true;
            days = days.substr(2);
        }
    }

	var dt_datetime = (str_datetime == null || str_datetime =="" ?  new Date() : str2dt4(str_datetime));
	var dt_prev_month = new Date(dt_datetime);
	dt_prev_month.setMonth(dt_datetime.getMonth()-1);
	var dt_next_month = new Date(dt_datetime);
	dt_next_month.setMonth(dt_datetime.getMonth()+1);
	var dt_firstday = new Date(dt_datetime);
	dt_firstday.setDate(1);
	dt_firstday.setDate(1-(7+dt_firstday.getDay()-n_weekstart)%7);
	var dt_lastday = new Date(dt_next_month);
	dt_lastday.setDate(0);
	
	// html generation (feel free to tune it for your particular application)
	// print calendar header
	var str_buffer = new String (
		"<html>\n"+
		"<head>\n"+
		"	<title>Calendar</title>\n"+
		"</head>\n"+
		"<body bgcolor=\"White\">\n"+
		"<table cellspacing=\"0\" border=\"0\">\n"+
		"<tr><td bgcolor=\"#808080\">\n"+
		"<table cellspacing=\"1\" cellpadding=\"3\" border=\"0\">\n"+
		"<tr>\n	<td bgcolor=\"#808080\"><a href=\"javascript:window.opener.show_calendar4('"+
		str_target+"', '"+ dt2dtstr4(dt_prev_month)+"', '" + str_boldDays + "');\">"+
		"<img src=\"/images/prev.gif\" width=\"16\" height=\"16\" border=\"0\""+
		" alt=\"previous month\"></a></td>\n"+
		"	<td bgcolor=\"#808080\" colspan=\"5\" align=\"center\">"+
		"<font color=\"white\" face=\"tahoma, verdana\" size=\"2\">"
		+arr_months[dt_datetime.getMonth()]+" "+dt_datetime.getFullYear()+"</font></td>\n"+
		"	<td bgcolor=\"#808080\" align=\"right\"><a href=\"javascript:window.opener.show_calendar4('"
		+str_target+"', '"+dt2dtstr4(dt_next_month)+"', '" + str_boldDays + "');\">"+
		"<img src=\"/images/next.gif\" width=\"16\" height=\"16\" border=\"0\""+
		" alt=\"next month\"></a></td>\n</tr>\n"
	);

	var dt_current_day = new Date(dt_firstday);
	// print weekdays titles
	str_buffer += "<tr>\n";
	for (var n=0; n<7; n++)
		str_buffer += "	<td bgcolor=\"#C0C0C0\">"+
		"<font color=\"white\" face=\"tahoma, verdana\" size=\"2\">"+
		week_days[(n_weekstart+n)%7]+"</font></td>\n";
	// print calendar table
	str_buffer += "</tr>\n";
	while (dt_current_day.getMonth() == dt_datetime.getMonth() ||
		dt_current_day.getMonth() == dt_firstday.getMonth()) {
		// print row heder
		str_buffer += "<tr>\n";
		for (var n_current_wday=0; n_current_wday<7; n_current_wday++) {
				if (dt_current_day.getDate() == dt_datetime.getDate() &&
					dt_current_day.getMonth() == dt_datetime.getMonth())
					// print current date
					str_buffer += "	<td bgcolor=\"#97D2F8\" align=\"right\">";
				else if (dt_current_day.getDay() == 0 || dt_current_day.getDay() == 6)
					// weekend days
					str_buffer += "	<td bgcolor=\"#DBEAF5\" align=\"right\">";
				else
					// print working days of current month
					str_buffer += "	<td bgcolor=\"white\" align=\"right\">";

                boldOpen = (boldDays[date2YMD(dt_current_day)]) ? '<span style="background-color: lightgreen;">' : '';
                boldClose = (boldDays[date2YMD(dt_current_day)]) ? '</span>' : '';
				if (dt_current_day.getMonth() == dt_datetime.getMonth())
					// print days of current month
					str_buffer += "<a href=\"javascript:window.opener."+str_target+
					".value='"+dt2dtstr4(dt_current_day)+"'; window.close();\">"+
					"<font color=\"black\" face=\"tahoma, verdana\" size=\"2\">";
				else 
					// print days of other months
					str_buffer += "<a href=\"javascript:window.opener."+str_target+
					".value='"+dt2dtstr4(dt_current_day)+"'; window.close();\">"+
					"<font color=\"gray\" face=\"tahoma, verdana\" size=\"2\">";
				str_buffer += boldOpen + dt_current_day.getDate()+boldClose+"</font></a></td>\n";
				dt_current_day.setDate(dt_current_day.getDate()+1);
		}
		// print row footer
		str_buffer += "</tr>\n";
	}
	// print calendar footer
	str_buffer +=
		"</table>\n" +
		"</tr>\n</td>\n</table>\n" +
		"</body>\n" +
		"</html>\n";

	var vWinCal = window.open("", "Calendar", 
		"width=200,height=200,status=no,resizable=yes,top=200,left=200");
	vWinCal.focus();
	vWinCal.opener = self;
	var calc_doc = vWinCal.document;
	calc_doc.write (str_buffer);
	calc_doc.close();
}
// datetime parsing and formatting routimes. modify them if you wish other datetime format
function str2dt4 (str_datetime) {
	var re_date = /^(\d+)\/(\d+)\/(\d+)$/;
	if (!re_date.exec(str_datetime))
		return alert("Invalid Datetime format: "+ str_datetime);
	return (new Date (RegExp.$3, RegExp.$1-1, RegExp.$2));
}
function dt2dtstr4 (dt_datetime) {
    var month = dt_datetime.getMonth()+1
    var strMonth = (month < 10) ? '0' + month.toString() : month.toString();
    var day = dt_datetime.getDate();
    var strDay = (day < 10) ? '0' + day.toString() : day.toString();
	return (new String(strMonth+"/"+strDay+"/"+dt_datetime.getFullYear()));
}
function date2YMD (dt_datetime) {
    month = dt_datetime.getMonth()+1;
    strMonth = (month < 10) ? '0' + month.toString() : month.toString();
    day = dt_datetime.getDate();
    strDay = (day < 10) ? '0' + day.toString() : day.toString();
    year = dt_datetime.getFullYear();
	return year.toString() + strMonth + strDay;
}
//end of calendar pop-up

function findPosition(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 scrollToDiv(divID)
{
    var resultsCoordinates = findPosition(document.getElementById(divID));
    window.scrollTo(0, resultsCoordinates[1]);
}

//Custom Dialog Function
//Displays a javascript-generated DIV which acts like a dialog box.
//Text has to be set, and buttons with labels and javascript effect
//The only parameter to pass is an object litteral who will contain all the
//parameters. Typical example (one button added) :
//{title: 'Warning !',
// text: 'This is a warning',
// button1: {name:'Save',value:'saveForms()'}
//}
//By default a 'Cancel' button will be added to hide the div, but it can
//be removed by adding "noCancel:1" to the object literal parameter
//The cancel button can also have a different label, set it with cancelLabel
function customDialog(oArg){
    var dialogHTML;
    var buttons = '';
    var newdiv = document.createElement('div');
    var title = 'title' in oArg? oArg.title : "Attention";
    var instructions = 'text' in oArg? oArg.text : "Please click a button";
    var butn;
    var showCancel = 'noCancel' in oArg ? false : true;
    var currentTime = new Date();
    
    for(i=1;i<7;i++){
        butn = eval('oArg.button' + i);
        if(typeof butn != 'undefined'){
            if(butn.name != 'undefined'){
                  buttons += '<td style="padding:5px 5px 5px 5px;">';
                  buttons += '<input type="button" name="button' + i + '" value="' + butn.name + '" onClick="' + butn.value + '; document.getElementById(\'thedialog\').style.visibility=\'hidden\'" class="rollover">';
                  buttons += '</td>';
             }
        }
    }
    //remove existing dialog object
    if(document.getElementById('thedialog')){
       document.getElementById('thedialog').parentNode.removeChild(document.getElementById('thedialog'));
    }

    //create new dialog layer
    newdivName = 'thedialog';
    newdiv.setAttribute('id', newdivName);
    newdiv.style.width='99%';
    newdiv.style.height='99%';
    newdiv.style.position='absolute';
    newdiv.style.top = 0;
    newdiv.style.background = '';

    //build the dialog box
    dialogHTML = '<div id="jspo" style="position:absolute; top:40%; width:100%;">';
    dialogHTML += '<form name="' + currentTime.getTime() + '">';
    dialogHTML += '<table width="100%" cellpadding="0" cellspacing="0">';
    dialogHTML += ' <tr>';
    dialogHTML += '  <td align="center">';
    dialogHTML += '<table width="10%" cellpadding="0" cellspacing="0">';
    dialogHTML += ' <tr>';
    dialogHTML += '  <td align="center">';
    dialogHTML += '     <div id="aa" style="position: relative; left: 2px; top: 3px; margin-right: 2px; margin-bottom: 2px;">';
    dialogHTML += '     <div style="position: relative; background: #B8B6B8;>';
    dialogHTML += '     <div style="position: relative;>';
    dialogHTML += '     <div style="position: relative; left: -2px; top: -3px; background: #F1F0F1; border: 1px solid #989698;>';
    dialogHTML += '     <div style="position: relative;>';
    dialogHTML += '         <table width="100%" cellpadding="0" cellspacing="0" border="0">';
    dialogHTML += '             <tr>';
    dialogHTML += '                 <td style="font:bold 12pt verdana,sans-serif; background-color:#C00; color:#FFF; padding:12px 5px 12px 5px;" nowrap>';
    dialogHTML += title;
    dialogHTML += '                 </td>';
    dialogHTML += '             </tr>';
    dialogHTML += '             <tr>';
    dialogHTML += '                 <td style="font:10pt verdana,sans-serif; color:#333; padding:12px 5px 5px 5px;">';
    dialogHTML += instructions;
    dialogHTML += '                 </td>';
    dialogHTML += '             </tr>';
    dialogHTML += '         </table>';
    dialogHTML += '         <table align="center" cellpadding="10" cellspacing="0" border="0">';
    dialogHTML += '             <tr>';
    dialogHTML += buttons;
    if (showCancel) {
        var cancelLabel = 'cancelLabel' in oArg ? oArg.cancelLabel : 'Cancel';
        dialogHTML += '  <td style="padding:5px 5px 5px 5px;">';
        dialogHTML += '   <input type="button" name="cancelbutton" value="' + cancelLabel + '" onClick="document.getElementById(\'thedialog\').style.visibility=\'hidden\'" class="rollover">';
        dialogHTML += '  </td>';
    }
    dialogHTML += '             </tr>';
    dialogHTML += '         </table>';
    dialogHTML += '     </div>';
    dialogHTML += '     </div>';
    dialogHTML += '     </div>';
    dialogHTML += '     </div>';
    dialogHTML += '     </div>';
    dialogHTML += '  </td>'
    dialogHTML += ' </tr>';
    dialogHTML += '</table>';
    dialogHTML += '  </td>'
    dialogHTML += ' </tr>';
    dialogHTML += '</table>';
    dialogHTML += '</form></div>';

    newdiv.innerHTML = dialogHTML;
    document.body.appendChild(newdiv);
    scrollToDiv(newdivName);
}


