
function survey()
{
    document.japanSurvey.action = "index.php?content=surveyJapanContent.php&fromWhere=survey";
    document.japanSurvey.method = "post";

    submitForm = true;

    validated = validateRadioButton('interested');

    if (!validated)
    {
        alert("Elegir si usted está interesado en ir a Japón");
        submitForm = false;
    }
    
    if (getRadioButtonValue('interested') == 'yes')
    {
        if (submitForm)
        {
            validated = validateRadioButton('duration');

            if (!validated)
            {
                alert("Elegir por cuánto tiempo te gustaría ir");
                submitForm = false;
            }
        }

        if (submitForm)
        {
            validated = validateRadioButton('season');

            if (!validated)
            {
                alert("Elegir en qué época del año sería la mejor");
                submitForm = false;
            }
        }

        if (submitForm)
        {
            validated = validateTextField();

            if (!validated)
            {
                alert("Menciona al menos tres cosas qué te gustaría hacer");
                submitForm = false;
            }
        }
    }

    if (submitForm)
    {
        document.japanSurvey.submit();
    }
}

function newsInsert(newsInsertAction)
{
	if (newsInsertAction == 'imageUpload')
	{
		document.getElementById("imageUpload").innerHTML = "<br><span class='alerttitle'>UPLOADING IMAGE - PLEASE WAIT</span>";
		
		document.insertNews.action = "index.php?content=newsInsert.php";
		document.insertNews.method = "post";
		document.insertNews.enctype = "multipart/form-data";
		document.insertNews.imageUploaded.value = "yes";
	}
	
	if (newsInsertAction == 'newsInsert')
	{
		document.insertNews.action = "index.php?content=newsInsert.php&fromWhere=newsInsert";
		document.insertNews.method = "post";
	}
	
	document.insertNews.submit();
}

function imageUpload(imagePath)
{
	//AJAX implementation to upload news images.
	
	if (typeof XMLHttpRequest != "undefined")
	{
		//XMLHttpRequest object
		req = new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
       	//XMLHttpRequest object
       	req = new ActiveXObject("Microsoft.XMLHTTP");
   	}
	req.open("POST", url, true);
	//req.open("GET", url, true);
	req.onreadystatechange = callbackImageUpload;
			
	//req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	 		
	 req.send(null);
}

function callbackImageUpload()
{
	if (req.readyState == 4)
	{
        if (req.status == 200)
        {
			imageUploadResults();
			
		}
	}
}

function imageUploadResults()
{
	var resDocText = req.responseText;
	
	if (resDocText.trim() != "")
	{
    	document.getElementById("imageUpload").innerHTML = resDocText;
	}
}

function validateTextField()
{
    textFieldCount = 0;

    if(document.japanSurvey.likeToDo_1.value.trim() != '')
    {
        textFieldCount++;
    }

     if(document.japanSurvey.likeToDo_2.value.trim() != '')
    {
        textFieldCount++;
    }

     if(document.japanSurvey.likeToDo_3.value.trim() != '')
    {
        textFieldCount++;
    }

     if(document.japanSurvey.likeToDo_4.value.trim() != '')
    {
        textFieldCount++;
    }

     if(document.japanSurvey.likeToDo_5.value.trim() != '')
    {
        textFieldCount++;
    }

     if(document.japanSurvey.likeToDo_6.value.trim() != '')
    {
        textFieldCount++;
    }

     if(document.japanSurvey.likeToDo_7.value.trim() != '')
    {
        textFieldCount++;
    }

     if(document.japanSurvey.likeToDo_8.value.trim() != '')
    {
        textFieldCount++;
    }

     if(document.japanSurvey.likeToDo_9.value.trim() != '')
    {
        textFieldCount++;
    }

    if(textFieldCount < 3)
    {
        return false;
    }
    else
    {
        return true;
    }
}

function validateRadioButton(radioName)
{
    var rb = document.getElementsByName(radioName);

    for(var i=0;i<rb.length;i++)
    {
        if(rb[i].checked)
        {
            if (rb[i].value == 'other')
            {
                if(document.japanSurvey.other_season.value.trim() == '')
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            else
            {
                return true;
            }
           
        }
    }
    return false;
}

function getRadioButtonValue(radioName)
{
    var radioButtonValue = '';
    var rb = document.getElementsByName(radioName);

    for(var i=0;i<rb.length;i++)
    {
        if(rb[i].checked)
        {
            radioButtonValue = rb[i].value;
        }
    }
    return radioButtonValue;
}

String.prototype.trim = function() {
a = this.replace(/^\s+/, '');
return a.replace(/\s+$/, '');
};

