﻿/* **************************************************

	Javascript functions for the web implementation
	of KongresMaster
	
	Copyright 2005-2009 Rabbe Consult A/S
	All Rights Reserved

************************************************** */


/*
	Date Functions in Javascript
*/

function CancelRedirect( canform )
{
	document.forms[canform].submit();
}

function updateDate(form, dir)
{
	var theday = form.elements[dir + '_day'].value;
	var themonth = form.elements[dir + '_month'].value;
	var theyear = form.elements[dir + '_year'].value;
	
	var thevalue = themonth + '/' + theday + '/' + theyear;
	
	var thedate = new Date(theyear, themonth - 1, theday);
	
	var realday = thedate.getDate();
	var realmonth = thedate.getMonth() + 1;
	var realyear = thedate.getFullYear();
	
	thevalue = realmonth + '/' + realday + '/' + realyear;
	
	form.elements[dir].value = thevalue;
	
	setList(form, dir + '_day', realday);
	setList(form, dir + '_month', realmonth);
	setList(form, dir + '_year', realyear);
}

function setList(form, listname, thevalue)
{
	var theoptions = form.elements[listname].options;
	
	for( i = 0; i < theoptions.length; i++ )
	{
		if( theoptions[i].value == thevalue )
		{
			theoptions[i].selected = true;
			break;
		}
	}
}

function trim(value)
{
	var temp = value;
	var obj = /^(\s*)([\W\w]*)(\b\s*$)/;
	if (obj.test(temp))
	{
		temp = temp.replace(obj, '$2');
	}
	obj = / +/g;
	temp = temp.replace(obj, " ");
	if (temp == " ")
	{
		temp = "";
	}
	return temp;
}

function verifyPersonForm(the_form, msg1, msg2, msg3 )
{
	var error = false;
		
	if( trim(the_form.Fornavn.value) == '' )
	{
	//	alert("Firstname wasn't properly written");
		
		document.getElementById('firstnamestar').style.color = "red";
		error = true;
	}
	else
	{
		document.getElementById('firstnamestar').style.color = "black";
	}
	
	if( trim(the_form.Efternavn.value) == '' )
	{
		document.getElementById('lastnamestar').style.color = "red";
		error = true;
	}
	else
	{
		document.getElementById('lastnamestar').style.color = "black";
	}
	
	if( trim(the_form.SkyggeAdresse1.value) == '' )
	{
		document.getElementById('address1star').style.color = "red";
		error = true;
	}
	else
	{
		document.getElementById('address1star').style.color = "black";
	}
	
	if( trim(the_form.SkyggePostnr.value) == '' )
	{
		document.getElementById('zipstar').style.color = "red";
		error = true;
	}
	else
	{
		document.getElementById('zipstar').style.color = "black";
	}
	
	if( trim(the_form.SkyggeBy.value) == '' )
	{
		document.getElementById('citystar').style.color = "red";
		error = true;
	}
	else
	{
		document.getElementById('citystar').style.color = "black";
	}
	
	if( trim(the_form.SkyggeTelefon.value) == '' )
	{
		document.getElementById('phonestar').style.color = "red";
		error = true;
	}
	else
	{
		document.getElementById('phonestar').style.color = "black";
	}
	
	if( trim(the_form.Mobiltelefon.value) == '' )
	{
		document.getElementById('mobilestar').style.color = "red";
		error = true;
	}
	else
	{
		document.getElementById('mobilestar').style.color = "black";
	}
	
	// Find the payment method and check
	//alert ( the_form.PaymentChoise[0].type + '\n' + the_form.PaymentChoise[1].type );
	
	var pay_type = 'input';
	try
	{
		var pay_form_test = the_form.PaymentChoise[1].type;
	}
	catch (err)
	{
		pay_type = 'hidden';
	}
	
	var pay_val = '';
	if ( pay_type == 'input' )
	{
		for (var i=0; i < the_form.PaymentChoise.length; i++)
		{
			if (the_form.PaymentChoise[i].checked)
			{
				pay_val = the_form.PaymentChoise[i].value;
			}
		}
	}
	else
	{
		pay_val = the_form.PaymentChoise.value;
	}
	
	if ( trim(pay_val) == '' )
	{
		if ( error == true )
		{
			alert ( msg2 );
		}
		else
		{
			alert ( msg3 );
		}
		return false;
	}
	
	if ( trim(pay_val) == 'ean' )
	{
		if ( trim(the_form.EANno.value) == '' )
		{
			document.getElementById('eanstar').style.color = "red";
			error = true;
		}
		else
		{
			document.getElementById('eanstar').style.color = "black";
		}
		if ( trim(the_form.CVRnoEAN.value) == '' )
		{
			document.getElementById('eancvrstar').style.color = "red";
			error = true;
		}
		else
		{
			document.getElementById('eancvrstar').style.color = "black";
		}
	}

	if ( trim(pay_val) == 'fak' )
	{
		if ( trim(the_form.CVRnoInv.value) == '' )
		{
			document.getElementById('invcvrstar').style.color = "red";
			error = true;
		}
		else
		{
			document.getElementById('invcvrstar').style.color = "black";
		}
	}

	if ( trim(pay_val) == 'jc' )
	{
		var select_index = the_form.Jobcenter.selectedIndex;
		var selected_value = the_form.Jobcenter.options[select_index].value;
		if ( trim(selected_value) == '' )
		{
			document.getElementById('jcstar').style.color = "red";
			error = true;
		}
		else
		{
			document.getElementById('jcstar').style.color = "black";
		}
	}
	
//	alert('got past the checks, error = ' + error);
	
	if( error == true )
	{
		alert( msg1 );
		return false;
	}
		
	return true;
}

function verifyPersonForm2L(the_form, msg)
{
//	alert("First check");

	var error = false;
//	var the_form = document.forms[0];
	
//	alert("Checking form, Firstname.value = '" + the_form.Firstname.value + "'");
	
	if( trim(the_form.Fornavn.value) == '' )
	{
		document.getElementById('Firstnamestar').style.color = "red";
		error = true;
	}
	else
	{
		document.getElementById('Firstnamestar').style.color = "black";
	}
	
	if( trim(the_form.Efternavn.value) == '' )
	{
		document.getElementById('Lastnamestar').style.color = "red";
		error = true;
	}
	else
	{
		document.getElementById('Lastnamestar').style.color = "black";
	}
	
	if( trim(the_form.Skyggefirma.value) == '' )
	{
		document.getElementById('Companystar').style.color = "red";
		error = true;
	}
	else
	{
		document.getElementById('Companystar').style.color = "black";
	}
	
	if( trim(the_form.SkyggeAdresse1.value) == '' )
	{
		document.getElementById('Addess1star').style.color = "red";
		error = true;
	}
	else
	{
		document.getElementById('Addess1star').style.color = "black";
	}
	
	if( trim(the_form.DirekteTelefon.value) == '' )
	{
		document.getElementById('Phonestar').style.color = "red";
		error = true;
	}
	else
	{
		document.getElementById('Phonestar').style.color = "black";
	}

	
//	alert('got past the checks, error = ' + error);
	
	if( error == true )
	{
		alert( msg );
		return false;
	}
	
	return true;
}


function verifyPersonForm3L(the_form, msg1 )
{
//	alert("First check");

	var error = false;
//	var the_form = document.forms[0];
	
//	alert("Checking form, Firstname.value = '" + the_form.Firstname.value + "'");
	
	if( trim(the_form.Firstname.value) == '' )
	{
	//	alert("Firstname wasn't properly written");
		
		document.getElementById('Firstnamestar').style.color = "red";
		error = true;
	}
	else
	{
		document.getElementById('Firstnamestar').style.color = "black";
	}
	
	if( trim(the_form.Lastname.value) == '' )
	{
		document.getElementById('Lastnamestar').style.color = "red";
		error = true;
	}
	else
	{
		document.getElementById('Lastnamestar').style.color = "black";
	}
	
	var obj = /(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}/;
	if( trim(the_form.Email.value) == '' || !(obj.test(the_form.Email.value)) )
	{
	//	alert("email wasn't correct");
		document.getElementById('eMailstar').style.color = "red";
		error = true;
	}
	else
	{
		document.getElementById('eMailstar').style.color = "black";
	}
	
	if( trim(the_form.Company.value) == '' )
	{
		document.getElementById('Companystar').style.color = "red";
		error = true;
	}
	else
	{
		document.getElementById('Companystar').style.color = "black";
	}
		
	if( trim(the_form.Address1.value) == '' )
	{
		document.getElementById('Address1star').style.color = "red";
		error = true;
	}
	else
	{
		document.getElementById('Address1star').style.color = "black";
	}
		
	if( trim(the_form.Postcode.value) == '' )
	{
		document.getElementById('Postcodestar').style.color = "red";
		error = true;
	}
	else
	{
		document.getElementById('Postcodestar').style.color = "black";
	}
		
	if( trim(the_form.City.value) == '' )
	{
		document.getElementById('Citystar').style.color = "red";
		error = true;
	}
	else
	{
		document.getElementById('Citystar').style.color = "black";
	}
		
	if( trim(the_form.Phone.value) == '' )
	{
		document.getElementById('Phonestar').style.color = "red";
		error = true;
	}
	else
	{
		document.getElementById('Phonestar').style.color = "black";
	}
		
	
//	alert('got past the checks, error = ' + error);
	
	if( error == true )
	{
		alert( msg1 );
	//	window.event.returnValue = false;
		return false;
	}
		
	return true;
}

function verifyEmail(the_form, msg)
{
	var error = false;
	
	var obj = /(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}/;
	if( trim(the_form.email.value) == '' || !(obj.test(the_form.email.value)) )
	{
	//	alert("email wasn't correct");
		document.getElementById('eMailstar').style.color = "red";
		error = true;
	}
	else
	{
		document.getElementById('eMailstar').style.color = "black";
	}
	
	if( error == true )
	{
		alert( msg );
		the_form.email.focus();
		the_form.email.select();
		return false;
	}
	
	return true;
}

function verifyReqFields ( the_form, msg )
{
	var error = false;
	var validate = false;
	var json_text = document.getElementById('json_text').innerHTML;
	var json_result = eval('(' + json_text + ')');
	var count = json_result.count;
	var error_color = 'red';
	
	for ( i = 1; i <= count; i++ )
	{
		if ( json_result.requested[i].validation == 'x' )
		{
			if ( json_result.requested[i].field_function[1] == 'checkbox' )
			{
				if ( document.getElementById( 'field_' + json_result.requested[i].fieldno + '_1' ).checked )
				{
					validate = true;
					break;
				}
			}
		}
	}
	
	if ( validate )
	{
		for ( i = 1; i <= count; i++ )
		{
			if ( json_result.requested[i].require )
			{
				for ( j = 1; j <= json_result.requested[i].no_fields; j++ )
				{
					if ( json_result.requested[i].field_function[1] == 'text' || json_result.requested[i].field_function[1] == 'textarea')
					{
						if( trim ( document.getElementById('field_' + json_result.requested[i].fieldno + '_' + j ).value ) == '' )
						{
							document.getElementById( 'field_' + json_result.requested[i].fieldno + '_' + j ).style.backgroundColor = error_color;
							error = true;
						}
						else
						{
							document.getElementById( 'field_' + json_result.requested[i].fieldno + '_' + j ).style.backgroundColor = "transparent";
						}
					}
					else if ( json_result.requested[i].field_function[1] == 'popop' )
					{
						var selected = document.getElementById('field_' + json_result.requested[i].fieldno + '_' + j ).selectedIndex;
						var selected_content = document.getElementById('field_' + json_result.requested[i].fieldno + '_' + j ).options[selected].value;
						
						
						if ( trim ( selected_content ) == '' )
						{
							document.getElementById( 'field_' + json_result.requested[i].fieldno + '_' + j ).style.backgroundColor = error_color;
							error = true;
						}
						else
						{
							document.getElementById( 'field_' + json_result.requested[i].fieldno + '_' + j ).style.backgroundColor = "transparent";
						}
					}
					else if ( json_result.requested[i].field_function[1] == 'checkbox' )
					{
						if( !document.getElementById('field_' + json_result.requested[i].fieldno + '_' + j ).checked )
						{
							document.getElementById( 'field_' + json_result.requested[i].fieldno + '_' + j ).style.backgroundColor = error_color;
							error = true;
						}
						else
						{
							document.getElementById( 'field_' + json_result.requested[i].fieldno + '_' + j ).style.backgroundColor = "transparent";
						}
					}
					else if ( json_result.requested[i].field_function[1] == 'upload' )
					{
						// Wait a little with this one...
					}
				}
			}
		}
	}
	
	if ( error )
	{
		alert ( msg );
		return false;
	}
	
	return true;
}

function reallyCancelL(cn, msg)
{
	if( confirm(msg + cn + "?") )
	{
		document.forms['cancelform'].submit();
	}
}

function verifyChange(sel)
{
//	alert("sel.selectedIndex = " + sel.selectedIndex + ", sel.length = " + sel.length);
	if( sel.selectedIndex == -1 )
	{
		sel.selectedIndex = sel.length - 1;
	}
}

function toogleDiv( divId, imgId )
{
	if ( !(document.getElementById( divId ).className == 'hide') )
	{
		document.getElementById( divId ).className = 'hide'; 
		document[imgId].src = 'images/closed_dtu.gif';
	}
	else
	{
		document.getElementById( divId ).className = 'show'; 
		document[imgId].src = 'images/open_dtu.gif';
	}
	return false;
}

function ShowPayDetail ( type )
{
	document.getElementById ( "pay_dk" ).style.display = 'none';
	document.getElementById ( "pay_cc" ).style.display = 'none';
	document.getElementById ( "pay_71" ).style.display = 'none';
	document.getElementById ( "pay_ean" ).style.display = 'none';
	document.getElementById ( "pay_invoice" ).style.display = 'none';
	
	switch ( type )
	{
		case "dk":
			document.getElementById ( "pay_dk" ).style.display = 'block';
			break;
		case "kk":
			document.getElementById ( "pay_cc" ).style.display = 'block';
			break;
		case "71":
			document.getElementById ( "pay_71" ).style.display = 'block';
			break;
		case "ean":
			document.getElementById ( "pay_ean" ).style.display = 'block';
			break;
		case "fak":
			document.getElementById ( "pay_invoice" ).style.display = 'block';
			break;
	}
}

function popup(myform) 
{ 
	if (! window.focus)return true; 
	var d = new Date(); 
	
	windowname = d.getTime(); 
	window.open('xyz', windowname, 'top=100,left=100,height=500,width=400,location=no,resizable=yes,scrollbars=no,status=no'); 
	myform.target=windowname; 
	return true; 
} 

function payform_submit(paytype)
{
	if (! window.focus)return true; 
	var d = new Date(); 

	if ( paytype == 'dk' || paytype == 'kk' )
	{
		// Do nothing...
		/*windowname = d.getTime(); 
		window.open('xyz', windowname, 'top=100,left=100,height=500,width=400,location=no,resizable=yes,scrollbars=no,status=no'); 
		document.goform.target=windowname; 
		document.goform.submit();
		return true; */
	}
	else
	{
		document.goform.submit();
		return true; 
	}
}

function send_email ( courseno, coursetitle )
{
	var email = prompt ( 'Angiv den email, du vil sende kursusbeskrivelsen for "' + coursetitle + '" til', '' );
	
	if ( email != '' )
	{
		if (window.XMLHttpRequest)
		{	// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttp=new XMLHttpRequest();
		}
		else
		{	// code for IE6, IE5
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		xmlhttp.open("GET","a_send_desc.php?email=" + email + "&courseno=" + courseno, true );
		xmlhttp.send();

		alert ( 'Email lagt i kø til afsendelse. Den vil blive sendt inden for få minutter' );
	}
}

// Tools

// Modified from http://www.javascripter.net/faq/writingt.htm

function writeNewWindow( title, content ) 
{
	consoleRef=window.open('','profile', 'width=350,height=400,menubar=0,toolbar=0,status=0,scrollbars=1,resizable=1');
	consoleRef.document.writeln('<html>');
	consoleRef.document.writeln('<head>');
	consoleRef.document.writeln('<title>');
	consoleRef.document.writeln( title );
	consoleRef.document.writeln('</title>');
	consoleRef.document.writeln('<link href="global.css" rel="stylesheet" type="text/css" media="all">');
	consoleRef.document.writeln('<link href="run.css" rel="stylesheet" type="text/css" media="all">');
	consoleRef.document.writeln('</head>');
	
	consoleRef.document.writeln('<body onLoad="self.focus()">');
	consoleRef.document.writeln('<h3>');
	consoleRef.document.writeln( title );
	consoleRef.document.writeln('</h3>');
	consoleRef.document.write( content );
	consoleRef.document.writeln('</body>');
	
	consoleRef.document.writeln('</html>');
	
	top.consoleRef.document.close();
}

/* A little debug tool  */
/* http://geekswithblogs.net/svanvliet/archive/2006/03/23/simple-javascript-object-dump-function.aspx */
       var MAX_DUMP_DEPTH = 10;
      
       function dumpObj(obj, name, indent, depth) {
              if (depth > MAX_DUMP_DEPTH) {
                     return indent + name + ": <Maximum Depth Reached>\n";
              }
              if (typeof obj == "object") {
                     var child = null;
                     var output = indent + name + "\n";
                     indent += "\t";
                     for (var item in obj)
                     {
                           try {
                                  child = obj[item];
                           } catch (e) {
                                  child = "<Unable to Evaluate>";
                           }
                           if (typeof child == "object") {
                                  output += dumpObj(child, item, indent, depth + 1);
                           } else {
                                  output += indent + item + ": " + child + "\n";
                           }
                     }
                     return output;
              } else {
                     return obj;
              }
       }

