<?php



//include("xml2array.php");

session_start();



$oXML = @simplexml_load_string(@file_get_contents("contact.xml"));

if (!is_object($oXML))

{

	die ("Error reading XML file");

}



$content			= @file_get_contents("contact.xml");

$form_title			= $oXML->{'header'}->{'title'};

$form_description	= $oXML->{'header'}->{'description'};

$form_cols			= (int)$oXML->{'body'}->{'layout'}->{'cols'};

$form_numbers		= ($oXML->{'body'}->{'layout'}->{'numbers'} == "yes");

$form_fields		= array();

foreach ($oXML->{'body'}->{'fields'} as $field)

{

	$prompt		= $field->{'prompt'};

	$name		= $field->{'input_name'};

	$id			= $field->{'input_id'};

	$type		= $field->{'input_type'};

	$attrib		= isset($field->{'attrib'})			? $field->{'attrib'}		: "";

	$errmsg		= isset($field->{'errmsg'})			? $field->{'errmsg'}		: "";

	$frmmsg		= isset($field->{'frmmsg'})			? $field->{'frmmsg'}		: "";

	$val_type	= isset($field->{'val_type'})		? $field->{'val_type'}		: "";

	$val_attrib	= isset($field->{'val_attrib'})		? $field->{'val_attrib'}	: "";

	$ovalues	= isset($field->{'input_values'})	? $field->{'input_values'}	: "";

	$values		= array();

	if (is_object($ovalues))

	{

		foreach ($ovalues->{'value'} as $ovalue)

		{

			$id		= $ovalue->{'id'};

			$text	= $ovalue->{'text'};

			$values[] = array("id" => $id, "text" => $text);

		}

	}

	

	$form_fields[] = array("prompt" => $prompt, "name" => $name, "type" => $type, "id" => $id, "attrib" => $attrib, "errmsg" => $errmsg, "frmmsg" => $frmmsg, "values" => $values, "val_type" => $val_type, "val_attrib" => $val_attrib);

}



function build_field($field, &$txt_ctr, &$tar_ctr, &$chk_ctr, &$rad_ctr, &$sel_ctr, &$js)

{

	$prompt	= isset($field["prompt"])		? $field["prompt"]		: "";

	$name	= isset($field["name"])			? $field["name"]		: "";

	$type	= isset($field["type"])			? $field["type"]		: "";

	$value	= isset($field["value"])		? $field["value"]		: "";

	$attrib	= isset($field["attrib"])		? $field["attrib"]		: "";

	$id		= isset($field["id"])			? $field["id"]			: $name;

	$errmsg	= isset($field["errmsg"])		? $field["errmsg"]		: "";

	$frmmsg	= isset($field["frmmsg"])		? $field["frmmsg"]		: "";

	$vtype	= isset($field["val_type"])		? $field["val_type"]	: "";

	$vattr	= isset($field["val_attrib"])	? $field["val_attrib"]	: "";

	$values	= isset($field["values"])		? $field["values"]		: array();



	$js			= "";

	$ihtml		= "";

	//$txt_ctr	= 1;

	//$tar_ctr	= 1;

	//$chk_ctr	= 1;

	if ($type == "text")

	{

		$ihtml = "

						<li>

							<span id=\"sprytextfield$txt_ctr\">

							<label for=\"$name\">$prompt</label>

							<br/>

							<input type=\"text\" name=\"$name\" id=\"$id\" />

							<br/>

							<span class=\"textfieldRequiredMsg\">$errmsg</span><span class=\"textfieldInvalidFormatMsg\">$frmmsg</span></span> 

							</li>";

		if ($vtype) $js		=  "var sprytextfield$txt_ctr = new Spry.Widget.ValidationTextField(\"sprytextfield$txt_ctr\", \"$vtype\", {{$vattr}validateOn:[\"blur\"]});";

		$txt_ctr ++;

	}

	else if ($type == "textarea")

	{

		$ihtml = "

						<li>

							<span id=\"sprytextarea$tar_ctr\">

							<label for=\"$name\">$prompt</label>

							<br/>

							<textarea name=\"$name\" id=\"$id\" $attrib></textarea><br/>

							<span class=\"textareaRequiredMsg\">$errmsg</span></span> 

							</li>";

		$js		=  "var sprytextarea$tar_ctr = new Spry.Widget.ValidationTextarea(\"sprytextarea$tar_ctr\", {validateOn:[\"blur\"]});";

		$tar_ctr ++;

	}

	else if ($type == "checkbox")

	{

		$ihtml = "

						<li>

							<span id=\"sprycheckbox$chk_ctr\">

							<label for=\"$name\">$prompt</label>

							<br/><br/>

							<table width=\"99%\" border=\"0\" cellspacing=\"1\" cellpadding=\"1\">";



		$row_cut = 1;

		foreach ($values as $value1)

		{

			$key	= $value1["id"];

			$value	= $value1["text"];

			if ($row_cut == 1)

			{

				$ihtml .= "

				<tr>";

			}



			$ihtml .= "

								<th width=\"33%\" align=\"left\" valign=\"middle\" scope=\"col\"> 

								<input type=\"checkbox\" name=\"$name\" value=\"$key\" id=\"$id\"/>

								<label for=\"$key\">$value</label>

								</th>";



			$row_cut ++;

			if ($row_cut == 4)

			{

				$ihtml .= "

				</tr>";

				$row_cut = 1;

			}

		}



		$ihtml .="

             </table>

						<span class=\"checkboxMinSelectionsMsg\">$errmsg</span>

						</span>

						</li>

			 ";



		$js		=  "var sprycheckbox$chk_ctr = new Spry.Widget.ValidationCheckbox(\"sprycheckbox$chk_ctr\", {isRequired:false, validateOn:[\"blur\"], minSelections:1});";

		$chk_ctr ++;

	}
	
	else if ($type == "radio")

	{

		$ihtml = "

						<li>

							<span id=\"sprycheckbox$rad_ctr\">

							<label for=\"$name\">$prompt</label>

							<br/><br/>

							<table width=\"99%\" border=\"0\" cellspacing=\"1\" cellpadding=\"1\">";



		$row_cut = 1;

		foreach ($values as $value1)

		{

			$key	= $value1["id"];

			$value	= $value1["text"];

			if ($row_cut == 1)

			{

				$ihtml .= "

				<tr>";

			}



			$ihtml .= "

								<th width=\"33%\" align=\"left\" valign=\"middle\" scope=\"col\"> 

								<input type=\"radio\" name=\"$name\" value=\"$key\" id=\"$id\"/>

								<label for=\"$key\">$value</label>

								</th>";



			$row_cut ++;

			if ($row_cut == 4)

			{

				$ihtml .= "

				</tr>";

				$row_cut = 1;

			}

		}



		$ihtml .="

             </table>

						<span class=\"checkboxMinSelectionsMsg\">$errmsg</span>

						</span>

						</li>

			 ";



		$js		=  "var sprycheckbox$rad_ctr = new Spry.Widget.ValidationCheckbox(\"sprycheckbox$rad_ctr\", {isRequired:false, validateOn:[\"blur\"], minSelections:1});";

		$rad_ctr ++;

	}

	else if ($type == "select")

	{

		$options = "";
		foreach ($values as $value1)

		{

			$key	= $value1["id"];

			$value	= $value1["text"];

			$options .= "<option value=\"$key\">$value</option>\n";
		}

		$ihtml = "

						<li>

							<span id=\"sprytextfield$sel_ctr\">

							<label for=\"$name\">$prompt</label>

							<br/>

							<select size=\"1\" name=\"$name\" id=\"$id\" />
							{$options}
							</select>

							<br/>

							<span class=\"textfieldRequiredMsg\">$errmsg</span><span class=\"textfieldInvalidFormatMsg\">$frmmsg</span></span> 

							</li>";

		if ($vtype) $js		=  "var sprytextfield$sel_ctr = new Spry.Widget.ValidationTextField(\"sprytextfield$sel_ctr\", \"$vtype\", {{$vattr}validateOn:[\"blur\"]});";

		$sel_ctr ++;

	}

	else if ($type == "hidden")

	{

		$itml = "

			<input name=\"$name\" type=\"hidden\" id=\"$id\" value=\"$value\" />";

	}

	else if ($type == "text-captcha")

	{

		$ops	= array('+', '-', '*', '/');

		$op		= $ops[rand(0, 3)];

		$num1	= 0;

		$num2	= 0;

		$res	= 0;

		if ($op == '+')

		{

			$num1	= rand(0, 100);

			$num2	= rand(0, 100);

			$res 	= $num1 + $num2;

			$prompt .= "&nbsp;".$num1.' + '.$num2;

			$_SESSION["{$name}"] = $res;

		}

		else if ($op == '*')

		{

			$num1	= rand(0, 100);

			$num2	= rand(0, 100);

			$res 	= $num1 * $num2;

			$prompt .= "&nbsp;".$num1.' * '.$num2;

			$_SESSION["{$name}"] = $res;

		}

		else if ($op == '-')

		{

			$num1	= rand(0, 100);

			$num2	= rand(0, $num1);

			$res 	= $num1 - $num2;

			$prompt .= "&nbsp;".$num1.' - '.$num2;

			$_SESSION["{$name}"] = $res;

		}

		else if ($op == '/')

		{

			$num1	= rand(0, 100);

			$num2	= rand(0, 100);

			$res 	= $num1 * $num2;

			$num2	= $res;

			$res	= $num2 / $num1;

			$prompt .= "&nbsp;".$num2.' / '.$num1;

			$_SESSION["{$name}"] = $res;

		}

		$ihtml	= "

						<li>

							<span id=\"sprytextfield$txt_ctr\">

							<label for=\"$name\">$prompt</label>

							<br/>

							<input type=\"text\" name=\"$name\" id=\"$id\" />

							<br/>

							<span class=\"textfieldRequiredMsg\">$errmsg</span><span class=\"textfieldInvalidFormatMsg\">$frmmsg</span></span> 

							</li>";

		if ($vtype) $js		=  "var sprytextfield$txt_ctr = new Spry.Widget.ValidationTextField(\"sprytextfield$txt_ctr\", \"$vtype\", {{$vattr}validateOn:[\"blur\"]});";

		$txt_ctr ++;

	}



	return $ihtml;

}



function draw_field($field, &$txt_ctr, &$tar_ctr, &$chk_ctr, &$rad_ctr, &$sel_ctr, &$js)

{

	print build_field($field, &$txt_ctr, &$tar_ctr, &$chk_ctr, &$rad_ctr, &$sel_ctr, &$js);

}



function draw_form_fields($form_fields, &$jss)

{

	$jss		= "";

	$txt_ctr	= 1;

	$tar_ctr	= 1;

	$chk_ctr	= 1;

	$rad_ctr	= 1;

	$sel_ctr	= 1;

	for ($x = 0;$x < count($form_fields);$x ++)

	{

		$js = "";

		draw_field($form_fields[$x], $txt_ctr, $tar_ctr, $chk_ctr, $rad_ctr, $sel_ctr, $js);

		$jss .= $js."\n";

	}

}



function draw_form($action, $name, $form_fields, $submit, $method = "post", $target = "_self", $id = "")

{

	if (!is_array($form_fields)) return false;



	$submit_name	= "";

	$submit_id		= "";

	$submit_value	= "";

	if (is_array($submit))

	{

		$submit_name	= isset($submit["name"])	? $submit["name"]	: "submit";

		$submit_id		= isset($submit["id"])		? $submit["id"]		: "submit";

		$submit_value	= isset($submit["value"])	? $submit["value"]	: "Submit";

	}



	print "

		 <form action=\"$action\" method=\"$method\" name=\"$name\" target=\"$target\" id=\"$id\" >

				 

				 <span id=\"form\">

				  <ol>";



	$js = "";

	draw_form_fields($form_fields, $js);



	print "

				  </ol>

					

				  <div align=\"left\" style=\"margin:10px 0 0 25px;\">

          <input name=\"$submit_name\" type=\"submit\" id=\"$submit_id\" value=\"$submit_value\" class=\"btn\"/>

          </div>

				 </span>

				 </form>

				 <br/>

				 <script type=\"text/javascript\">

				 $js

				 </script>

				 ";

}



$send_now	= isset($_REQUEST["sendContact"]) ? ($_REQUEST["sendContact"] == "SUBMIT" ? true : false) : false;

$email_sent	= false;



if ($send_now)

{

	$mail_content = "";

	if ($oXML->{'header'}->{'sendmail'}->{'mime_type'} == "text/html")

	{

		$mail_content = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">

<html xmlns=\"http://www.w3.org/1999/xhtml\">

<head>

<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />

<title>$form_title</title>

</head>



<body leftmargin=\"0\" topmargin=\"0\" bottommargin=\"0\" rightmargin=\"0\" bgcolor=\"#FFFFFF\">

<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">

";

	}



	for ($x = 0;$x < count($form_fields);$x ++)

	{

		$field	= $form_fields[$x];

		$prompt	= isset($field["prompt"])	? $field["prompt"]	: "";

		$name	= isset($field["name"])	? $field["name"]		: "";

		$type	= isset($field["type"])	? $field["type"]		: "";

		$value	= "";

		if ($type != "checkbox")

		{

			if ($type != "text-captcha")

			{

				$value = isset($_REQUEST["{$name}"]) ? $_REQUEST["{$name}"] : "";

			}

			else

			{

				$value1 = isset($_SESSION["{$name}"]) ? $_SESSION["{$name}"] : "";

				$value2 = isset($_REQUEST["{$name}"]) ? $_REQUEST["{$name}"] : "";

				if ($value1 != $value2 || !$value1 || !$value2)

				{

					header("Location: ".$_SERVER["PHP_SELF"]."?err=captcha");

					exit();

				}

			}

		}

		else

		{

			if (strstr($name, "[]"))

			{

				$name = str_replace("[]", "", $name);

			}

			$ivalues	= isset($_REQUEST["{$name}"])	? $_REQUEST["{$name}"]	: array();

			$fvalues	= isset($field["values"])		? $field["values"]		: array();

			foreach ($fvalues as $value1)

			{

				$key	= $value1["id"];

				$text	= $value1["text"];

				if (in_array($key, $ivalues))

				{

					$value .= "{$key} ({$text}), ";

				}

			}

			$value = substr($value, 0, -2);

		}



		if ($oXML->{'header'}->{'sendmail'}->{'mime_type'} == "text/html")

		{

			$mail_content .= "

	<tr>

		<td>{$prompt}:</td>

		<td>{$value}</td>

	</tr>\n";

		}

		else

		{

			$mail_content .= "{$prompt}: {$value}\n";

		}

	}



	if ($oXML->{'header'}->{'sendmail'}->{'mime_type'} == "text/html")

	{

		$mail_content .= "		</body>

</html>";

	}



	$subject 	= $oXML->{'header'}->{'sendmail'}->{'subject'};

	$list_to	= $oXML->{'header'}->{'sendmail'}->{'list_to'};

	$from_name	= explode(':', $oXML->{'header'}->{'sendmail'}->{'from_name'});

	$from_email	= explode(':', $oXML->{'header'}->{'sendmail'}->{'from_email'});

	$fromname	= "";

	$fromemail	= "";

	if ($from_name[0] == "input")

	{

		$fromname = isset($_REQUEST["{$from_name[1]}"]) ? $_REQUEST["{$from_name[1]}"] : "";

	}

	else

	{

		$fromname = $from_name[1];

	}

	if ($from_email[0] == "input")

	{

		$fromemail = isset($_REQUEST["{$from_email[1]}"]) ? $_REQUEST["{$from_email[1]}"] : "";

	}

	else

	{

		$fromemail = $from_email[1];

	}

	$headers = "";

	$headers = "From: $fromname <$fromemail>\r\n";

	$headers .= "Content-type: ".$oXML->{'header'}->{'sendmail'}->{'mime_type'}."; charset=iso-8859-1\r\n";

	foreach ($oXML->{'header'}->{'sendmail'}->{'list_to'}->{'address'} as $to)
	{

		$address = $to;
		foreach ($address->{'transform'} as $trans)
		{
			$from	= $trans->{'from'};
			$var	= "";
			if (preg_match("/^\w+$/", $from))
				eval('$var = $'.$from.';');
			else if (preg_match("/^form\.(\w+)$/", $from, $m))
			{
				if (isset($_REQUEST[$m[1]])) eval('$var = '.$_REQUEST[$m[1]].';');
			}
			else if (preg_match("/^session\.(\w+)$/", $from, $m))
			{
				if (isset($_SESSION[$m[1]])) eval('$var = '.$_SESSION[$m[1]].';');
			}
			$tos = array();
			foreach ($trans->{'to'} as $to2)
			{
				$i = $to2->{'in'};
				$o = $to2->{'out'};
				$tos["{$i}"] = "{$o}";
			}
			if (isset($tos[$var])) $address = $tos[$var];
		}

		if ($address) @mail($address, $subject, $mail_content, $headers); 

		$email_sent = true;

	}

}



?>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Formulario</title>
<!--<script src="http://dm2studio.com/Form-Mek/SpryAssets/SpryValidationTextField.js" type="text/javascript"></script>-->

<script src="SpryAssets/SpryValidationTextField.js" type="text/javascript"></script>

<script src="SpryAssets/SpryValidationTextarea.js" type="text/javascript"></script>

<script src="SpryAssets/SpryValidationCheckbox.js" type="text/javascript"></script>

<link href="SpryAssets/SpryValidationTextField.css" rel="stylesheet" type="text/css" />

<link href="SpryAssets/SpryValidationTextarea.css" rel="stylesheet" type="text/css" />

<link href="grafika.css" rel="stylesheet" type="text/css" media="screen" />
</head>



<body>



		 <?php

		 if (!$email_sent)

		 {

		 ?>

 <h1 class="azul"><?=$form_title;?></h1>

		 <!--<p class="celeste"><?=$form_description;?></p>-->

		 <div class="form-container">

		 <?php

		 if (isset($_REQUEST["err"]))

		 {

			$err_captcha = ($_REQUEST["err"] == "captcha");

			if ($err_captcha)

			{

				print "<font color=\"#ff0000\">Valor de captcha invalido</font>\n";

			}

		 }

		 draw_form($_SERVER["PHP_SELF"], "contact", $form_fields, array("name" => "sendContact", "id" => "sendContact", "value" => "SUBMIT"));

		 ?>

		 </div>
         
		<?php

		 }

		 else

		 {

			?>

      <p>Your email was successfully sent, we will contact you soon.<br/> <strong>Thank you.</strong></p>
			<?php

		 }

		 ?>



</body>

</html>