<?php
	function create_guid($namespace = ''){    
		static $guid = '';
		$uid = uniqid("", true);
		$data = $namespace;
		$data .= $_SERVER['REQUEST_TIME'];
		$data .= $_SERVER['HTTP_USER_AGENT'];
		$data .= $_SERVER['LOCAL_ADDR'];
		$data .= $_SERVER['LOCAL_PORT'];
		$data .= $_SERVER['REMOTE_ADDR'];
		$data .= $_SERVER['REMOTE_PORT'];
		$hash = strtoupper(hash('ripemd128', $uid . $guid . md5($data)));
		$guid = '{' .  
				substr($hash,  0,  8) .
				'-' .
				substr($hash,  8,  4) .
				'-' .
				substr($hash, 12,  4) .
				'-' .
				substr($hash, 16,  4) .
				'-' .
				substr($hash, 20, 12) .
				'}';
		return $guid;
	}
	set_time_limit(0);
	$buf = create_guid();
	$GUID = substr($buf, 1, 32);
	
	
	$xml=new DomDocument('1.0','utf-8');
	$xml->appendChild($xml->createComment(iconv("Windows-1251", "UTF-8", "Èçãîòîâëåíî ñ ïîìîùüþ ñèñòåìû www.tehplan-online.ru")));
	$STD_TP = $xml->appendChild($xml->createElement('STD_TP'));
	$STD_TP->setAttribute('CodeType', '23');
	$STD_TP->setAttribute('Version', '02');
	$STD_TP->setAttribute('GUID', $GUID);
		$Building = $STD_TP->appendChild($xml->createElement('Building'));		
			$Package = $Building->appendChild($xml->createElement('Package'));
				$New_Building = $Package->appendChild($xml->createElement('New_Building'));
					$Register = $New_Building->appendChild($xml->createElement('Register'));
						$Number_Register = $Register->appendChild($xml->createElement('Number_Register'));
							$CadastralNumberParent = $Number_Register->appendChild($xml->createElement('CadastralNumberParent'));
							$CadastralNumberParent->appendChild($xml->createTextNode(iconv("Windows-1251", "UTF-8", $_POST['Cad_Number'])));
							if($_POST['add_new_parent_'] >= 1){
							$Parent_CadastralNumbers = $Number_Register->appendChild($xml->createElement('Parent_CadastralNumbers'));
							}
							for($i = 1; $i <= $_POST['add_new_parent_']; $i++){
							$CadastralNumber = $Parent_CadastralNumbers->appendChild($xml->createElement('CadastralNumber'));
							$CadastralNumber->appendChild($xml->createTextNode(iconv("Windows-1251", "UTF-8", $_POST['Parent_CadastralNumber'.$i])));
							}
					$Assignation_Code = $New_Building->appendChild($xml->createElement('Assignation_Code'));
					$Assignation_Code->appendChild($xml->createTextNode(iconv("Windows-1251", "UTF-8", $_POST['dAss_b'])));
					$Elements_Construct = $New_Building->appendChild($xml->createElement('Elements_Construct'));
						for($i = 1; $i <= $_POST['add_new_mat_']; $i++){
						$Material = $Elements_Construct->appendChild($xml->createElement('Material'));
						$Material->setAttribute('Wall', $_POST['select_mat_'.$i]);
						}
					$Exploitation_Char = $New_Building->appendChild($xml->createElement('Exploitation_Char'));
					if($_POST['Fin_Year'] != ""){
					$Exploitation_Char->setAttribute('Year_Built', $_POST['Fin_Year']);
					}
					if($_POST['Ent_Year'] != ""){
					$Exploitation_Char->setAttribute('Year_Used', $_POST['Ent_Year']);
					}
					if($_POST['Floors'] != "" || $_POST['Sub_Floors'] != ""){
					$Floors = $New_Building->appendChild($xml->createElement('Floors'));
					}
					if($_POST['Floors'] != ""){
					$Floors->setAttribute('Floors', $_POST['Floors']);
					}
					if($_POST['Sub_Floors'] != ""){
					$Floors->setAttribute('Underground_Floors', $_POST['Sub_Floors']);
					}
					$Area = $New_Building->appendChild($xml->createElement('Area'));
					$Area->appendChild($xml->createTextNode($_POST['Area']));
					$Location = $New_Building->appendChild($xml->createElement('Location'));
					
					$Entity_Spatial = $New_Building->appendChild($xml->createElement('Entity_Spatial'));
					$Sub_Buildings = $New_Building->appendChild($xml->createElement('Sub_Buildings'));
			
			$Contractor = $Building->appendChild($xml->createElement('Contractor'));
			
			
			$Coord_Systems = $Building->appendChild($xml->createElement('Coord_Systems'));
			
			
			$Client = $Building->appendChild($xml->createElement('Client'));
			
			
			$Input_Data = $Building->appendChild($xml->createElement('Input_Data'));
			
			
			$Survey = $Building->appendChild($xml->createElement('Survey'));
			
			
			$Conclusion = $Building->appendChild($xml->createElement('Conclusion'));
			
			
			$Scheme_Geodesic_Plotting = $Building->appendChild($xml->createElement('Scheme_Geodesic_Plotting'));
			
			
			$Scheme_Disposition_Building = $Building->appendChild($xml->createElement('Scheme_Disposition_Building'));
			
			
			$Diagram_Contour = $Building->appendChild($xml->createElement('Diagram_Contour'));
			
			
			$Appendix = $Building->appendChild($xml->createElement('Appendix'));
	
	
	/*$sorts = $xml->appendChild($xml->createElement('sorts'));
	$sort = $sorts->appendChild($xml->createElement('sort'));
	$name = $sort->appendChild($xml->createElement('name'));
	$name->appendChild($xml->createTextNode('ßáëîêî'));*/
	$xml->formatOutput = true;
	$xml->save('test.xml');
?>