var ExpandFormTimer;
var ContractFormTimer;
var POSTUrl = '../js/FormsControl.php';
var Browser = '';
if(navigator.userAgent.indexOf('Safari') >= 0){Browser = 'SF';}
if(navigator.userAgent.indexOf('Opera') >= 0){Browser = 'OP';}
if(navigator.userAgent.indexOf('Firefox') >= 0){Browser = 'FF';}
if(navigator.userAgent.indexOf('MSIE') >= 0){Browser = 'IE';}
if(navigator.userAgent.indexOf('Chrome') >= 0){Browser = 'CH';}


function ClearField(Field, DefValue){
	if(Field){
		if(Field.value == DefValue){Field.value = '';}
		Field.className = Field.className+' UserActive';
	}
}

function SendIfComplete(FormID, MandatoryStr, ErrStr, NotLegalErrStr){
	
	var IsComplete = true;
	var FieldToFocus;
	var LegalChk = document.getElementById('C'+FormID+'Legal_Chk');

	if(MandatoryStr != ''){
		var FieldsCouples = MandatoryStr.split('|');
		for(var i=0; i<FieldsCouples.length;i++){
			var FieldsSplit = FieldsCouples[i].split('~');
			var Field = document.getElementById(FieldsSplit[0]);
			var FieldDefValue = FieldsSplit[1];
			
			if(Field){
				if(Field.value == '' || Field.value == FieldDefValue){
					IsComplete = false;
					FieldToFocus = Field;
					break;
				}
			}
		}

	}
	
	if(!IsComplete){
		alert(ErrStr);
		FieldToFocus.focus();
		FieldToFocus.scrollIntoView();
	}
	else{
		if(LegalChk){
			if(!LegalChk.checked){alert(NotLegalErrStr);}
			else{StartFormSubmit(FormID);}
		}
		else{StartFormSubmit(FormID);}
	}
}

function ExpandForm(DivID){
	var FormDiv = document.getElementById(DivID);
	var FormDivHeight;
	var HeightCompensation = 20;

	if(FormDiv){
		if(FormDiv.style.display != 'block'){
			FormDiv.style.display = 'block';
			FormDiv.style.clear = 'both';				
			FormDivHeight = FormDiv.offsetHeight + HeightCompensation;
			FormDiv.style.overflow = 'hidden';
			FormDiv.style.height = '1px';
			
			ExpandFormTimer = setInterval(function(){ExpandFormMovement(FormDiv,FormDivHeight);},40);
		}
	}
}

function ExpandFormMovement(DivID, MaxHeight){
	var ActHeight = DivID.offsetHeight;
	if(ActHeight < (MaxHeight - 5)){DivID.style.height = (ActHeight + ((MaxHeight - ActHeight)/5)) + "px";}
	else{DivID.style.height = MaxHeight + "px"; clearInterval(ExpandFormTimer);}
}

function ContractForm(FormID){
	var FormDiv = document.getElementById('D' + FormID);
	ContractFormMovement

	if(FormDiv){
		FormDiv.style.display = 'block';
		FormDiv.style.clear = 'both';				
		FormDiv.style.overflow = 'hidden';
				
		ContractFormTimer = setInterval(function(){ContractFormMovement(FormDiv, FormID);},40);
	}
}

function ContractFormMovement(DivID, FormID){
	
	var ActHeight = DivID.offsetHeight;
	var MinHeight = 100;
	
	DivID.style.overflow = 'hidden';

	if(ActHeight > (MinHeight + 5)){DivID.style.height = (ActHeight - ((ActHeight - MinHeight)/5)) + "px";}
	else{
		DivID.style.height = MinHeight + 'px';
		clearInterval(ContractFormTimer);
		SendForm(FormID);
	}
}

function SendForm(FormID){

	var FormObj = document.getElementById('F'+FormID);
	var MessageObj = document.getElementById('M' + FormID);
	var ResImage = '';

	if(FormObj){
		var HttpReq =  getXMLHttpRequest();
		if(HttpReq){
			var FormFields = FormObj.elements;
			var POSTString = '';

			for(var i=0;i<FormFields.length;i++){
				if(FormFields[i].name != ''){
					if(POSTString != ''){POSTString += '&';}
					POSTString += FormFields[i].name + '=' + FormFields[i].value.replace(/\n/g, '<br>');
				}
			}
			
			HttpReq.open("POST", POSTUrl ,false);
			HttpReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			HttpReq.setRequestHeader("Content-length", POSTString.length);
			HttpReq.setRequestHeader("Connection", "close");
			HttpReq.send(POSTString);
			
			if (HttpReq.readyState == 4 && (HttpReq.status == 200 || HttpReq.status == 304)) {
				eval(HttpReq.responseText);
				if(ComResult[0] == 'OK'){ResImage = '../images/maqueta/MailOK.gif';}
				else{ResImage = '../images/maqueta/MailKO.gif';}

				MessageObj.innerHTML = '<p><img src="' + ResImage + '" alt="' + ComResult[1] + '" title="' + ComResult[1] + '" class="MessageIcon">' + ComResult[1] + '</p>';
			}
			else{
			}
		}
	}
}

function StartFormSubmit(FormID){
	var FormContainerObj = document.getElementById('D' + FormID);
	if(FormContainerObj){
		var SendingDiv = document.createElement('DIV');
		FormContainerObj.style.position = 'relative';
		SendingDiv.style.position = 'absolute';
		SendingDiv.className = 'Sending';
		SendingDiv.id = 'M' + FormID;
		FormContainerObj.appendChild(SendingDiv);
		SendingDiv.style.top = '0px';
		SendingDiv.style.left = '0px';
		SendingDiv.style.width = '100%';
		SendingDiv.style.height = '100%';
		SendingDiv.style.zIndex = '200';
		SendingDiv.style.display = 'none';
		SendingDiv.innerHTML = '<p>Enviando datos. Espere unos instantes, por favor.</p>';

		ShowSendingDiv(SendingDiv, FormID, 0);
	}
}

function ShowSendingDiv(SendingObj, FormID, ActOpacity){
	var FFOpac;
	var Velocidad = 8;

	if(SendingObj){
		SendingObj.style.display = 'block';
		ActOpacity = ActOpacity + Velocidad;
		
		if(ActOpacity < (100 + Velocidad)){
			FFOpac = ActOpacity / 100;
			SendingObj.style.opacity = FFOpac;
			SendingObj.style.filter = 'alpha(opacity=' + ActOpacity + ')';
			setTimeout(function(){ShowSendingDiv(SendingObj, FormID, ActOpacity);},10);
		}
		else{ContractForm(FormID);}
	}
}



function getXMLHttpRequest(){
	if (window.XMLHttpRequest) {return new window.XMLHttpRequest;}
	else {
		try {return new ActiveXObject("MSXML2.XMLHTTP.3.0");}
		catch(ex) {return null;}
	}
}
