//FORMULAIRE CONTACTO//

function checkLen(txtID,max){
var txt = document.getElementById(txtID);
if (txt.value.length > max) 
 txt.value = txt.value.substring(0, max); 
}

function doJump(menu) { 
window.location = menu.options[menu.selectedIndex].value;
}

function checkdate(inputID,invalidDateFormat,invalidrange,mandatorymsg,mandatory){    
    var returnval="";
    var input = document.getElementById(inputID);    
    
    if (mandatory && input.value.length == 0) {
        return mandatorymsg;
    }
    
    
    if (input.value.length > 0) {    
        var validformat=/^\d{2}[\/\.]\d{2}[\/\.]\d{4}$/; //Basic check for format validity    
        
        if (!validformat.test(input.value))
            returnval = invalidDateFormat;
        else{ //Detailed check for valid date ranges
            var spv = input.value.split("/");
            if (spv.length < 2) {
                spv = input.value.split(".");
         }
        
        var dayfield=spv[0];
        var monthfield=spv[1];
        var yearfield=spv[2];
        var dayobj = new Date(yearfield, monthfield-1, dayfield);
        
        if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield))
            returnval=invalidrange;
        else
            returnval="";
        }
    } 
    
    return returnval
}


function validateMandatoryTextField(id,txtob,mandatory)
{
	var result = "";
	
	if (mandatory) {	    
	    var txt = document.getElementById(id).value;
	    var number = "";
		if(txt.length == 0)
			result = "- "+txtob+"\n"; 
	}
    
    
    	

	return result;
}


function validateMail(mailStr)
{
    var emailPat = /^(.+)@(.+)$/;
    var specialChars = "\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
    var validChars = "\[^\\s" + specialChars + "\]";
    var quotedUser = "(\"[^\"]*\")";
    var ipDomainPat = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
    var atom = validChars + '+';
    var word = "(" + atom + "|" + quotedUser + ")";
    var userPat = new RegExp("^" + word + "(\\." + word + ")*$");
    var domainPat = new RegExp("^" + atom + "(\\." + atom +")*$");
    var matchArray = mailStr.match(emailPat);
    
    if (matchArray == null)
	    return false;
	    
    var user = matchArray[1];
    var domain = matchArray[2];

    if (user.match(userPat)==null)
        return false;

    var IPArray = domain.match(ipDomainPat);
    
    if (IPArray != null)
    {
	    for (var i = 1; i <= 4; i++)
	    {
	        if (IPArray[i] > 255)
		        return false;
	    }
    
        return true;
    }

    var domainArray = domain.match(domainPat);

    if (domainArray == null)
        return false;

    var atomPat = new RegExp(atom,"g");
    var domArr = domain.match(atomPat);
    var len = domArr.length;
    
    if (domArr[domArr.length-1].length < 2 || domArr[domArr.length-1].length > 6)
        return false;

    if (len < 2)
        return false

    return true;
}

function validateEmail(id,invdateerror,mandatorymsg,mandatory)
{
	var result = "";
	var txtMail = document.getElementById(id).value;
	
	if(txtMail.length == 0 && mandatory){
	    result = "- " + mandatorymsg + "\n";
	}	
	else {
	    if(!validateMail(txtMail))  	    //(txtMail.indexOf(".") < 0) && (txtMail.indexOf("@") < 0))
		    result = "- "+invdateerror+"\n";
    }
								
	return result;
}

function validateCombos(id,txtob)
{
	var result = "";
	var cmb1 = document.getElementById(id);
	
	if(cmb1.selectedIndex == 0)
		result = "- "+txtob+"\n"; 
		
	return result;
}



function validateNumericField(id,txtob,mandatorymsg,mandatory)
{
	var result = true;
	var i = 0;
	var str = document.getElementById(id).value;	
	
	if(str.length == 0 && mandatory)
		return mandatorymsg;
	else if (str.length > 0)
	{
		str = str.replace(/ /g, "");		
		
		while(result && i < str.length)
		{
			result = (str.charAt(i) >= '0' && str.charAt(i) <= '9')
			i++;
		}		
	}
	else return "";
	
	//else return mandatorymsg;	
	
	if(result)
		return "";
	else
		return "- "+txtob+"\n";
}



function BrowserCheck() {
var b = navigator.appName;
if (b == "Netscape") this.b = "NS";
else if (b == "Microsoft Internet Explorer") this.b = "IE";
else this.b = b;
this.v = parseInt(navigator.appVersion);
this.NS = (this.b == "NS" && this.v>=4);
this.NS4 = (this.b == "NS" && this.v == 4);
this.NS5 = (this.b == "NS" && this.v == 5);
this.IE = (this.b == "IE" && this.v>=4);
this.IE4 = (navigator.userAgent.indexOf('MSIE 4')>0);
this.IE5 = (navigator.userAgent.indexOf('MSIE 5')>0);
if (this.IE5 || this.NS5) this.VER5 = true;
if (this.IE4 || this.NS4) this.VER4 = true;
this.OLD = (! this.VER5 && ! this.VER4) ? true : false;
this.min = (this.NS||this.IE);
}


function hideAlert(){
alertBox.visibility = "hidden";}


function makeAlert(aTitle,aMessage){
is = new BrowserCheck();
alertBox = (is.VER5) ? document.getElementById("alertLayer").style
: (is.NS) ? document.layers["alertLayer"]
: document.all["alertLayer"].style;

    document.all.alertLayer.innerHTML = "<table border=0 width=100% height=100%>" +
    "<tr height=5><td colspan=4 class=alertTitle>" + " " + aTitle + "</td></tr>" +
    "<tr height=5><td width=5></td></tr>" +
    "<tr><td width=5></td><td width=20 align=left><img src='alert.gif'></td><td align=center class=alertMessage>" + aMessage + "<BR></td><td width=5></td></tr>" + 
    "<tr height=5><td width=5></td></tr>" +
    "<tr><td width=5></td><td colspan=2 align=center><input type=button value='OK' onClick='hideAlert()' class=okButton><BR></td><td width=5></td></tr>" +
    "<tr height=5><td width=5></td></tr></table>";
    thisText = aMessage.length;
    if (aTitle.length > aMessage.length){ thisText = aTitle.length; }

    aWidth = (thisText * 5) + 80;
    aHeight = 100;
    if (aWidth < 150){ aWidth = 200; }
    if (aWidth > 350){ aWidth = 350; }
    if (thisText > 60){ aHeight = 110; }
    if (thisText > 120){ aHeight = 130; }
    if (thisText > 180){ aHeight = 150; }
    if (thisText > 240){ aHeight = 170; }
    if (thisText > 300){ aHeight = 190; }
    if (thisText > 360){ aHeight = 210; }
    if (thisText > 420){ aHeight = 230; }
    if (thisText > 490){ aHeight = 250; }
    if (thisText > 550){ aHeight = 270; }
    if (thisText > 610){ aHeight = 290; }

    alertBox.width = aWidth;
    alertBox.height = aHeight;
    alertBox.left = (document.body.clientWidth - aWidth)/2;
    alertBox.top = (document.body.clientHeight - aHeight)/2;

alertBox.visibility = "visible";
}

function getV(v){
 return document.getElementById(v);
}

var req=null;
function sendReq(url,method,data) {   
   if (typeof XMLHttpRequest != "undefined") {
       req = new XMLHttpRequest();
   } else if (window.ActiveXObject) {
       req = new ActiveXObject("Microsoft.XMLHTTP");
   }
   req.open(method, url, true);
   req.onreadystatechange = onComplete2;
   req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
   req.send(data);       
}
function onComplete2(){	
    if (req.readyState == 4 && req.status == 200 ) 
    {
    	alert('ready');
    	if ( req.responseText == "Not found" ) 
        {
		alert('not found');

	}
	else {
		var rps = req.responseText;
		alert(rps);
	}
    }
    else alert('not ready');
}

function validate(){
	var data = qSData();
	document.getElementById('data').value = data;
		
 	/*var decodedData=Base64.Decode(data);
        decodedData = decodedData.substring(0,decodedData.length-1);
        alert(decodedData);
 	var alias = getV('Alias').value;
 	var user = getV('controlid').value; 	
	var cl_ref = qSA(decodedData,'cl_ref');
	var ct_ref = qSA(decodedData,'ct_ref');
	var fd_id = qSA(decodedData,'fd_id');
	var cp_ref = qSA(decodedData,'cp_ref');		
	sendAjaxRequestToCheckFilledForm(alias,user,cl_ref,ct_ref,fd_id,cp_ref);*/
 	
}
function sendAjaxRequestToCheckFilledForm(alias,user,cl_ref,ct_ref,fd_id,cp_ref) {
    var actionForm = document.getElementsByTagName('form')[0].action;
    var url_pref = actionForm.substring(0,actionForm.lastIndexOf('/'));    
    var url = url_pref + '/CheckMarketFormFilled.aspx';
    var data = "alias=" + alias + "&user=" + user + "&fd_id="+fd_id+ "&cl_ref="+cl_ref+ "&ct_ref="+ct_ref+ "&cp_ref="+cp_ref;
    alert(data);
    
    var globalUri = url + '?' + data;
    
    //alert(globalUri);    
    //sendReq(globalUri);    
    sendReq(url,'GET',data);    
    /*
    AjaxRequest.SetEncoding('UTF-8');
    AjaxRequest.SetCharset('UTF-8');        
    AjaxRequest.OnComplete = onComplete;
    AjaxRequest.OnError = onError;
    AjaxRequest.SendGet(url+'?'+data);
    */
}

function onComplete(){
	alert('complete');
	var filled_flag = parseInt(AjaxRequest.GetRequest().responseText);	
	
	if (filled_flag = 1){
	  alert('You already filled this survey');
	}
	else alert('You have not filled this survey yet');
}
function onError(){
	alert('error processing request');
}
function onAsyncError(){
  alert(e.get_message() + "\n" + e.get_stackTrace());
}

function qSA(querystring,parameter)
{
    gy = querystring.split("&");
    
    for (i=0;i<gy.length;i++)
    {
        ft = gy[i].split("=");
        if (ft[0] == parameter)
            return ft[1];
    }
}


function qS(parameter)
{
    hu = window.location.search.substring(1);
    return qSA(hu,parameter);
}

function qSData()
{
    hu = window.location.search.substring(1);
    gy = hu.split("&");
    txt = gy[0];        

    var key = 'data=';
    substr1 = txt.substring(txt.indexOf(key)+key.length,txt.length);
    var ind1= substr1.indexOf('&');
    var result = ind1>0 ? substr1.substirng(0,ind1-1) : substr1;
    
    return result;
}



