﻿function showBalloon(className, field){
	showBalloon2(className, field, '250','validation-advice');
}

function showBalloon2(className, field, marginLeft, classDiv){
	var advice = makeAdvice(className, field, classDiv);
	showAdvice(className, field, marginLeft);
}

function showAdvice(className, field, marginLeft){
	var advice = getAdvice(className, field);
	
	if (advice && !field[getPropName(className)] && (advice.getStyle('display') == "none" || advice.getStyle('visiblity') == "hidden" || advice.getStyle('opacity')==0)){
		field[getPropName(className)] = true;
		advice.setStyles({'display':'block','visibility':'hidden'});
		var h = advice.getSize().scrollSize.y;
		var pt = advice.getStyle('padding-top').toInt()
		var pb = advice.getStyle('padding-bottom').toInt();
		var mt = advice.getStyle('margin-top').toInt()
		var mb = advice.getStyle('margin-bottom').toInt()
		h = h-pt-pb;

		advice.setStyles({'opacity':0,'height':'0px','padding-top':'0px','padding-bottom':'0px','margin-top':'0px','margin-bottom':'0px','margin-right':'100%','margin-left':marginLeft + 'px'}).effects().start({'height':h,'opacity':1,'padding-top':pt,'padding-bottom':pb,'margin-top':mt,'margin-bottom':mb});
	}
}

function getAdvice(className, field) {
    return $('advice-' + className + '-' + getFieldId(field));
}

function getFieldId(field) {
    return field.id ? field.id : field.id = "input_"+field.name;
}

function makeAdvice(className, field, classDiv){
    useTitles: true;
    var errorMsg = field.id;
    var advice = getAdvice(className, field);

    if(!advice){
        advice = new Element('div').addClass(classDiv).setProperty('id','advice-'+className+'-'+getFieldId(field)).setStyle('display','none');
        adviceBody = new Element('div');
        adviceBody.injectInside( advice ).appendText( errorMsg );

        switch (field.type.toLowerCase()) {
            case 'radio':
                var p = $(field.parentNode);
                if(p) {
                    p.adopt(advice);
                    break;
                }
                default: advice.injectBefore($(field));
       };
    } else{
        advice.getFirst().setHTML(errorMsg);
    }
    
    return advice;
}

function hideAdvice(className, field){
    var advice = getAdvice(className, field);
    if(advice && field[getPropName(className)]) {
        field[getPropName(className)] = false;
            var h = advice.getSize().scrollSize.y;
            var pt = advice.getStyle('padding-top').toInt()
            var pb = advice.getStyle('padding-bottom').toInt();
            var mt = advice.getStyle('margin-top').toInt()
            var mb = advice.getStyle('margin-bottom').toInt()
            h = h-pt-pb;
            advice.effects().start({'height':0,'opacity':0,'padding-top':0,'padding-bottom':0,'margin-top':0,'margin-bottom':0}).chain(function(){
                advice.setStyles({'display':'none','height':h+'px','padding-top':pt+'px','padding-bottom':pb+'px','margin-top':mt+'px','margin-bottom':mb+'px'});
        });
    }
}

function validate() {
    var frm = window.frames["contenido"].document;
    var ret =true;
    
    frm.getElementById("position").value = upFirstChar(frm.getElementById("position").value);
    frm.getElementById("company").value = upFirstChar(frm.getElementById("company").value);
    
    if (frm.getElementById("name").value == '') {
        frm.getElementById('errorName').style.visibility = 'visible';
        ret = false;
    } 
    else {
        var validName=checkName(frm.getElementById("name").value);
        if(validName==false){
            frm.getElementById('errorName').style.visibility = 'visible';
            ret = false;
        }
         else {
            frm.getElementById("name").value=upFirstChar(frm.getElementById("name").value);
            frm.getElementById('errorName').style.visibility = 'hidden';
        }
    }

    frm.getElementById("email").value=trim(frm.getElementById("email").value);
    frm.getElementById("email").value=mtrim(frm.getElementById("email").value);
    
    if (frm.getElementById("email").value == '') {
        frm.getElementById('errorEmail').style.visibility = 'visible';
        ret= false;
    }
     else {
        var validEmail = checkEmail(frm.getElementById("email").value);
        if (validEmail == false) {
            frm.getElementById('errorEmail').style.visibility = 'visible';
            ret = false;
        } else {
            frm.getElementById('errorEmail').style.visibility = 'hidden';
        }
    }
    
    frm.getElementById("confirmation").value=trim(frm.getElementById("confirmation").value);
    frm.getElementById("confirmation").value=mtrim(frm.getElementById("confirmation").value);
    
    if (frm.getElementById("confirmation").value == '') {
        frm.getElementById('errorConfirmation').style.visibility = 'visible';
        ret = false;
    }
    else {
        var validEmail = checkEmail(frm.getElementById("confirmation").value);
        if(validEmail==false){
            frm.getElementById('errorConfirmation').style.visibility = 'visible';
            ret = false;
        }
        else {
            frm.getElementById('errorConfirmation').style.visibility = 'hidden';
        }
    }

    if ((frm.getElementById("email").value != '') && (frm.getElementById("confirmation").value != '') && (frm.getElementById("email").value != frm.getElementById("confirmation").value)){
        frm.getElementById('notEqual').style.visibility = 'visible';
        ret = false;
    }
    else {
        frm.getElementById('notEqual').style.visibility = 'hidden';
    }

    if (frm.getElementById("url").value == null || frm.getElementById("url").value == '') {
        frm.getElementById('errorUrl').style.visibility = 'visible';
        ret= false;
    }
    else {
        var validUrl = checkUrl(frm.getElementById("url").value);
        if(validUrl == false){
            frm.getElementById('errorUrl').style.visibility = 'visible';
            ret = false;
        }
        else {
            frm.getElementById("url").value=addHttp(frm.getElementById("url").value);
            frm.getElementById('errorUrl').style.visibility = 'hidden';
        }
    }

    if (frm.getElementById("surname").value == null || frm.getElementById("surname").value == '') {
        frm.getElementById('errorSurname').style.visibility = 'visible';
        ret = false;
    }
    else {
        frm.getElementById("surname").value=upFirstChar(frm.getElementById("surname").value);
        frm.getElementById('errorSurname').style.visibility = 'hidden';
    }
    
    if (frm.getElementById("phone").value == null || frm.getElementById("phone").value == '') {
        frm.getElementById('errorPhone').style.visibility = 'visible';
        ret = false;
    }
    else {
        frm.getElementById("phone").value=trim(frm.getElementById("phone").value);
        frm.getElementById("phone").value=mtrim(frm.getElementById("phone").value);
        var validPhone = checkPhone(frm.getElementById("phone").value);
        if(validPhone == false){
            frm.getElementById('errorPhone').style.visibility = 'visible';
            ret =false;
        }
        else {
            frm.getElementById('errorPhone').style.visibility = 'hidden';
        }
    }
    
    
    return ret;
}

function addHttp(direction){
    if(direction.indexOf('http://')!=0){
        direction='http://'+direction;
    }
    return direction;
}

function submit()
{
    if (validate())
    {
        var form = document.getElementById("aspnetForm");
        var frm = window.frames["contenido"].document;
   
        // Set hidden inputs
        document.forms["aspnetForm"].elements["nameH"].value = frm.getElementById("name").value
        document.forms["aspnetForm"].elements["lastnameH"].value = frm.getElementById("surname").value
        document.forms["aspnetForm"].elements["positionH"].value = frm.getElementById("position").value
        document.forms["aspnetForm"].elements["companyH"].value = frm.getElementById("company").value
        document.forms["aspnetForm"].elements["emailH"].value = frm.getElementById("email").value
        document.forms["aspnetForm"].elements["websiteH"].value = frm.getElementById("url").value
        document.forms["aspnetForm"].elements["phoneH"].value = frm.getElementById("phone").value
        document.forms["aspnetForm"].elements["commentsH"].value = frm.getElementById("comments").value
        
        form.submit();
    }
}

function checkPhone(text){

    if (text == null || text == '') {
         return false;
    }
    var i = 0;
    var j = text.length;
    var c;
    while (i < j) {
        c = text.charAt(i++);
        if (c < '0' || c > '9') {
            return false;
        }
    }
    
    return true;
}

function checkUrl(url) {
    alert("build url "+buildURL(url) );

    return isURL(buildURL(url));
}

function buildURL(text){
    var HTTP = "http://";
    text = text.toLowerCase();
    text = trim(text);
    var httpPos = text.indexOf(HTTP);
    
    if (httpPos < 0) {
        text = HTTP + text;
    }
    
    var barPos = text.length - 1;
    var character = text.charAt(barPos);
    while (character == '/') {
        character = text.charAt(--barPos);
    }
    
    return text.substring(0, barPos + 1);
}

function isURL(text) {
    var HTTP = "http://";

    if ('' == text) {
        return false;
    }

    var httpPos = text.indexOf(HTTP);
    if (httpPos < 0) {
        return false;
    }

    text = text.substring(HTTP.length, text.length);

    var character = text.charAt(0);
    if (!(character >= 'a' && character <= 'z' || character >= 'A' && character <= 'Z')) {
        return false;
    }

    character = text.charAt(text.length - 1);
    if (!(character >= 'a' && character <= 'z' || character >= 'A'&& character <= 'Z' || character == '/')) {
        return false;
    }

    var barPos = text.length - 1;
    while (character == '/') {
        character = text.charAt(--barPos);
    }
    text = text.substring(0, barPos + 1);
    
    var pointPos = text.length - 1;
    var ext = '';
    character = text.charAt(pointPos);
    while (pointPos > 0 && character != '.') {
        ext = character + ext;
        character = text.charAt(--pointPos);
    }
    
    if ('com' != ext && 'net' != ext && 'org' != ext && 'gov' != ext && 'tv' != ext && 'biz' != ext && 'info' != ext && 'us' != ext && 'pr' != ext && 'ar' != ext && 'edu' != ext) {
        return false;
    }
    text = text.substring(0, pointPos);

    for (var i = 0; i < text.length; i++) {
        character = text.charAt(i);

        if (!(character >= '0' && character <= '9' || character >= 'a' && character <= 'z' || character >= 'A' && character <= 'Z' || character == '_' || character == '.')) {
            return false;
        } else if (character == '.' && (character = text.charAt(++i)) >= '0' && character <= '9') {
            return false;
        }
    }

    return true;
}

function checkEmail(eMail){
    var ret = true;
    var first = eMail.indexOf('@',0);
    
    if(first <= 0){
        ret = false;
    }
    
    var last = eMail.indexOf('@',first+1);
    if(last != -1){
        ret = false;
    }
    
    var sufijo = eMail.substring(first+1);
    var arr = sufijo.split(".");
    var ind = arr.length;
    
    if(ind < 2){
        ret=false;
    }

    var end = arr[ind-1];
    if((end.length != 3) && (end != 'info')){
        if( end.length == 2 ){
            var aEnd = arr[ind-2];
            if ( aEnd != 'info' && aEnd != 'com' && aEnd!= 'net' && aEnd != 'org' && aEnd != 'gov' && aEnd != 'biz' && aEnd != 'edu'){
                ret = false;
            }
        }
        else{
            ret = false;
        }
    } else if(end != 'com' && end != 'net' && end != 'org' && end != 'gov' && end != 'biz' && end != 'edu'&& end != 'info' ){
        ret = false;
    }
    
    return ret;
}


function checkName(name){
    if(name.length < 4)
        return false;
        
    return true;
}

function upFirstChar(text){
    text = trim(text);
    if (text != null && text != '' ){
        text = text.substr(0, 1).toUpperCase() + text.substr(1);
        for(var i = 1 ; i < text.length ; i++){
            if(text.charAt(i-1) == ' '){
                text = text.substr(0,i) + text.substr(i,1).toUpperCase() + text.substr(i+1);
            }
            else{
                text = text.substr(0,i) + text.substr(i,1).toLowerCase() + text.substr(i+1);
            }
        }
    }
    
    return text;
}

function trim(s)
{
    return rtrim(ltrim(s));
}

function ltrim(s)
{
    return s.replace(/^\s+/, "");
}

function rtrim(s)
{
    return s.replace(/\s+$/, "");
}

function mtrim(s)
{
    var i = 0;
    var j = s.length;
    var c;
    
    while (i < j) {
        c = s.charAt(i++);
        if( c == " " ){
            s=s.substring(0,i-1)+s.substring(i,j);
            j--;
            i--;
        }
    }
    
    return s;
}

