/*
* JavaScript functions for /bestill/
*
*/

function hideshow( parent_orderline_id )
	{
		if( parent_orderline_id.src.match( 'plus_open.png' ) != null )
			{
				parent_orderline_id.src = 'includes/graphics/plus_closed.png';
			}
			else
				{
					parent_orderline_id.src = 'includes/graphics/plus_open.png';
				}
			
		var tags = document.getElementsByTagName("tr");
		
		var i;
		for( i = 0; i<tags.length; i++)
			{
				if( tags[i].id.match( parent_orderline_id.id + '_' ) != null )
					{
						if( document.getElementById( tags[i].id ).style.display == "none" )
							{
								document.getElementById( tags[i].id ).style.display = "";
							}
							else
								{
									document.getElementById( tags[i].id ).style.display = "none";
								}
					}
					
			}
	}
	
function about( domain )
	{
		popup_window( 'about' , domain );
		return false;
	}
	
function terms( tld )
	{
		popup_window( 'terms' , tld );
		return false;	
	}

function conn_failure()
	{
		popup_window( 'conn_failure' , '' );
		return false;	
	}

function invalid_domain( domain )
	{
		popup_window( 'invalid_domain' , domain );
		return false;
	}
	
function forgot_password()
	{
		popup_window( 'forgot_password' , '' );
		return false;	
	}

function popup_window( type , param1 )
	{
		var url;
		var query_string;
		var window_name;
		
		switch ( type )
			{
				case 'about':
					url = "includes/pops/about.php";
					query_string = "?domain=" + param1;
					window_name = "about";
					break;
				case 'terms':
					url = "includes/pops/terms.php";
					query_string = "?tld=" + param1;
					window_name = "terms";
					break;
				case 'conn_failure':
					url = "includes/pops/conn_failure.php";
					query_string = "";
					window_name = "conn_failure";
					break;
				case 'invalid_domain':
					url = "includes/pops/invalid.php";
					query_string = "?domain=" + param1;
					window_name = "invalid";
					break;
				case 'forgot_password':
					url = "/minside/forgot_password_order_process.php";
					query_string = "";
					window_name = "forgot_password";
					break;
			}
			
		url = url + query_string;
		
		window.open( url , window_name , "directories=no, height=400, left=100, location=yes, menubar=no, resizable=no, scrollbars=yes, status=no, titlebar=no, top=100, width=600" );
		return false;
	}
	
function form_submit( form_id )
	{
		document.getElementById( form_id ).submit();	
	}
	
function switch_form( form )
	{
		document.location = "index.php?step=4&do=2&f=" + form;
	}
	
function validate_form_a()
	{
		var errors = Array(0);
		var arr_length;
		var error_string;
		
		var input_string;
		
		var form = document.getElementById('form_a');
		if( form.b_day.value == '' )
			{
				errors.push('- Fødselsdag mangler.\n');
				mark("b_day");
			}

		if( form.b_month.value == '' )
			{
				errors.push('- Fødselsmåned.\n');
				mark("b_month");
			}
		
		if( form.b_year.value == '' )
			{
				errors.push('- Fødselsår mangler.\n');
				mark("b_year");
			}

		if( form.first_name.value == '' )
			{
				errors.push('- Fornavn mangler.\n');
				mark("first_name");				
			}
		
		if( form.last_name.value == '' )
			{
				errors.push('- Etternavn mangler.\n');
				mark("last_name");				
			}
			
		if( form.email.value == '' )
			{
				errors.push('- E-postadresse mangler.\n');
				mark("email");				
			}
			else
				{
					input_string = form.email.value;
					if ( input_string.match(/^([a-zA-Z0-9_\-\.]+)@([a-zA-ZæøåÆØÅ0-9_\-\.]+)\.([a-zA-ZæøåÆØÅ]{2,5})$/) == null )
						{
							errors.push('- Ugyldig e-postadresse.\n');
							mark("email");	
						}
				}
				
		if( form.email_confirm.value != form.email.value )
			{
				errors.push('- "Bekreft e-post" må samsvare med "E-post".\n');
				mark("email");
				mark("email_confirm");				
			}
			
		if( form.phone_1.value == '' )
			{
				errors.push('- Telefonnummer mangler.\n');
				mark("phone_1");				
			}

		if( form.address.value == '' )
			{
				errors.push('- Adresse mangler.\n');
				mark("address");				
			}			
			
		
		if( form.zip.value == '' )
			{
				errors.push('- Postnummer mangler.\n');
				mark("zip");				
			}
			else if ( form.country.value == 'NO' )
				{
					input_string = form.zip.value;
					if ( input_string.match(/^[0-9]{4}$/) == null )
						{
							errors.push('- Ugyldig postnummer. Skal være nøyaktig fire siffer.\n');
							mark("zip");	
						}
				}
	
		if( form.accept_terms.checked == false )
			{
				errors.push('- Avtalevilkår ikke godtatt.\n');
				mark("accept_terms");				
			}

		/*document.write( errors );*/
		
		arr_length = errors.length;
		
		if( arr_length > 0 )
		{
			for( var i = 0; i < arr_length; i++)
				{
					if( i == 0 )
					{
						error_string = errors[0];
					}
					else
						{
							error_string = error_string + errors[i] + '\n';
						}
				}
			
			alert( 'Vennligst fyll ut alle påkrevde felt.\n\nFølgende feil ble funnet: \n' + error_string );
			return false;
		}
		else
			{
				return true;
			}
	}
	
function validate_form_b()
	{
		var errors = Array(0);
		var arr_length;
		var error_string;
		
		var input_string;
		
		var form = document.getElementById('form_b');
		
		if( form.org_num.value == '' )
			{
				errors.push('- Organisasjonsnummer mangler.\n');
				mark("org_num");
			}
		
		if( form.company_name.value == '' )
			{
				errors.push('- Firmanavn mangler.\n');
				mark("company_name");
			}
			
		if( form.email.value == '' )
			{
				errors.push('- E-postadresse mangler.\n');
				mark("email");				
			}
			else
				{
					input_string = form.email.value;
					if ( input_string.match(/^([a-zA-Z0-9_\-\.]+)@([a-zA-ZæøåÆØÅ0-9_\-\.]+)\.([a-zA-ZæøåÆØÅ]{2,5})$/) == null )
						{
							errors.push('- Ugyldig e-postadresse.\n');
							mark("email");	
						}
				}
				
		if( form.email_confirm.value != form.email.value )
			{
				errors.push('- "Bekreft e-post" må samsvare med "E-post".\n');
				mark("email");
				mark("email_confirm");				
			}
			
		if( form.phone_1.value == '' )
			{
				errors.push('- Telefonnummer mangler.\n');
				mark("phone_1");				
			}
		
		if( form.address.value == '' )
			{
				errors.push('- Fakturaadresse mangler.\n');
				mark("address");				
			}			
			
		
		if( form.zip.value == '' )
			{
				errors.push('- Postnummer mangler.\n');
				mark("zip");				
			}
			else if ( form.country.value == 'NO' )
				{
					input_string = form.zip.value;
					if ( input_string.match(/^[0-9]{4}$/) == null )
						{
							errors.push('- Ugyldig postnummer. Skal være nøyaktig fire siffer.\n');
							mark("zip");	
						}
				}
				
		if( form.postal_address.value == '' )
			{
				errors.push( '- Poststed mangler. Vennligst fyll inn et postnummer og poststed vil automatisk bli hentet.' );
				mark( "postal_address" );	
			}
		
		if( form.contact_first_name.value == '' )
			{	
				errors.push( '- Fornanvn på kontaktperson må fylles ut.' );
				mark( "contact_first_name" );			
			}		
			
		if( form.contact_last_name.value == '' )
			{	
				errors.push( '- Etternavn på kontaktperson må fylles ut.' );
				mark( "contact_last_name" );			
			}
			
		if( form.contact_phone.value == '' )
			{	
				errors.push( '- Telefonnummer til kontaktperson må fylles ut.' );
				mark( "contact_phone" );			
			}
			
		if( form.accept_terms.checked == false )
			{
				errors.push('- Avtalevilkår ikke godtatt.\n');
				mark("accept_terms");				
			}
		
		/*document.write( errors );*/
		
		arr_length = errors.length;
		
		if( arr_length > 0 )
		{
			for( var i = 0; i < arr_length; i++)
				{
					if( i == 0 )
					{
						error_string = errors[0];
					}
					else
						{
							error_string = error_string + errors[i] + '\n';
						}
				}
			
			alert( 'Vennligst fyll ut alle påkrevde felt.\n\nFølgende feil ble funnet: \n' + error_string );
			return false;
		}
		else
			{
				return true;
			}
	}
	
function mark( element_id )
	{
		if( !document.getElementById(element_id) )
			{
				alert( 'No element with the id ' + element_id + ' was found.' );	
			}
		document.getElementById(element_id).style.backgroundColor = 'red';
	}
	
function trim( elementId )
	{
		var element = document.getElementById( elementId );
		
		var prevValue = element.value;
		var newValue = prevValue.replace( /^\s+|\s+$/g , '' );

		element.value = newValue;	
	}