// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults


function SubForm(html,index,element){
    this.rawHTML=html;
    this.lineIndex=index;
    this.parentElement=element;
    this.parsedHTML = function() {
        return this.rawHTML.replace(/INDEX/g, ++this.lineIndex);
    };
    this.add = function() {
        $(this.parentElement).append(this.parsedHTML());
    };
}


function validate_form()
{	
    var today_now = new Date();
    var year =today_now.getFullYear();
    var birth_year = $('user_birthday_1i').value;
    var month = today_now.getMonth() + 1;
    var result;

    if(birth_year%4 == 0)
    {
        if(birth_year%100 != 0)
        {
            var leap_year = 1;
        }
        else
        {
            if (birth_year % 400 == 0) {
                leap_year = 1;
            }
            else {
                leap_year = 0;
            }
        }
    }
    else{
        leap_year = 0;
    }

    var difference = year - birth_year;
    if ($('user_birthday_2i').value == "2" && $('user_birthday_3i').value >= 29 && leap_year == 0) {
        $('bdate').style.display = "block";
        $('bdate').innerHTML = "Invalid Date";
        return false;
    }
    else {
        if ($('user_birthday_2i').value == "2" || $('user_birthday_2i').value == "4" || $('user_birthday_2i').value == "6" || $('user_birthday_2i').value == "9" || $('user_birthday_2i').value == "11") {
            if ($('user_birthday_3i').value > 30) {
                $('bdate').style.display = "block";
                $('bdate').innerHTML = "Invalid Date";
                return false;
            }
            else {
                $('bdate').style.display = "block";
                $('bdate').innerHTML = " ";
            }
        }
        else {
            $('bdate').style.display = "block";
            $('bdate').innerHTML = " ";
        }
    }
	
	
    if(difference < 13 )
    {
        $('notice').style.display = "block";
        $('notice').innerHTML = "<a href='http://www.familytravelforum.com'>Family Travel Forum Inc.</a>(FTF) does not knowingly collect or store any personal information, even in an aggregate form, about site users ages 12 and under. When younger children access this and other FTF sites it should be under the guidance of parents.<br><br>When children younger than 13 years make contributions to these sites they must have the express written permission of their parents. Please use the FTF permission form for minor children <a href='/info/consent'>found on this page</a> and submit it to our staff for approval in order to activate your minor child's account.<br><br>If you have any questions regarding use of FTF sites by minors, please contact <a href='mailto:bovalan@travelbigo.com'>bovalan@travelbigo.com.</a>"
        $('submit').disabled = true;
        return false;
    }
	
    else if(difference <= 13 && $('user_birthday_2i').value > month)
    {
			
        $('notice').style.display = "block";
        $('notice').innerHTML = "<a href='http://www.familytravelforum.com'>Family Travel Forum Inc.</a>(FTF) does not knowingly collect or store any personal information, even in an aggregate form, about site users ages 12 and under. When younger children access this and other FTF sites it should be under the guidance of parents.<br><br>When children younger than 13 years make contributions to these sites they must have the express written permission of their parents. Please use the FTF permission form for minor children <a href='/info/consent'>found on this page</a> and submit it to our staff for approval in order to activate your minor child's account.<br><br>If you have any questions regarding use of FTF sites by minors, please contact <a href='mailto:bovalan@travelbigo.com'>bovalan@travelbigo.com.</a>"
        $('submit').disabled = true;
        return false;
    }
	
    else if(difference <= 13 && $('user_birthday_2i').value == month)
    {
        if( $('user_birthday_3i').value >= today_now.getDate())
        {
            $('notice').style.display = "block";
            $('notice').innerHTML = "<a href='http://www.familytravelforum.com'>Family Travel Forum Inc.</a>(FTF) does not knowingly collect or store any personal information, even in an aggregate form, about site users ages 12 and under. When younger children access this and other FTF sites it should be under the guidance of parents.<br><br>When children younger than 13 years make contributions to these sites they must have the express written permission of their parents. Please use the FTF permission form for minor children <a href='/info/consent'>found on this page</a> and submit it to our staff for approval in order to activate your minor child's account.<br><br>If you have any questions regarding use of FTF sites by minors, please contact <a href='mailto:bovalan@travelbigo.com'>bovalan@travelbigo.com.</a>"
            $('submit').disabled = true;
            return false;
        }
        else
        {
            $('notice').style.display = "block";
            $('notice').innerHTML = " ";
            return true;
        }
    }
	
    result = validate_zipcode();
    if( result == false)
    {
        return false;
    }
    else{
        return true;
    }
}


function validate_form_essay()
{	
    var today_now = new Date();
    var year =today_now.getFullYear();
    var birth_year = $('user_birthday_1i').value;
    var month = today_now.getMonth() + 1;
    var result;

    if(birth_year%4 == 0)
    {
        if(birth_year%100 != 0)
        {
            var leap_year = 1;
        }
        else
        {
            if (birth_year % 400 == 0) {
                leap_year = 1;
            }
            else {
                leap_year = 0;
            }
        }
    }
    else{
        leap_year = 0;
    }

    var difference = year - birth_year;
    if ($('user_birthday_2i').value == "2" && $('user_birthday_3i').value >= 29 && leap_year == 0) {
        $('bdate').style.display = "block";
        $('bdate').innerHTML = "Invalid Date";
        return false;
    }
    else {
        if ($('user_birthday_2i').value == "2" || $('user_birthday_2i').value == "4" || $('user_birthday_2i').value == "6" || $('user_birthday_2i').value == "9" || $('user_birthday_2i').value == "11") {
            if ($('user_birthday_3i').value > 30) {
                $('bdate').style.display = "block";
                $('bdate').innerHTML = "Invalid Date";
                return false;
            }
            else {
                $('bdate').style.display = "block";
                $('bdate').innerHTML = " ";
            }
        }
        else {
            $('bdate').style.display = "block";
            $('bdate').innerHTML = " ";
        }
    }
	
	
    if(difference < 13 )
    {
        $('notice').style.display = "block";
        $('notice').innerHTML = "<a href='http://www.familytravelforum.com'>Family Travel Forum Inc.</a>(FTF) does not knowingly collect or store any personal information, even in an aggregate form, about site users ages 12 and under. When younger children access this and other FTF sites it should be under the guidance of parents.<br><br>When children younger than 13 years make contributions to these sites they must have the express written permission of their parents. Please use the FTF permission form for minor children <a href='/info/consent'>found on this page</a> and submit it to our staff for approval in order to activate your minor child's account.<br><br>If you have any questions regarding use of FTF sites by minors, please contact <a href='mailto:bovalan@travelbigo.com'>bovalan@travelbigo.com.</a>"
        $('submit').disabled = true;
        $('submit1').disabled = true;
        return false;
    }
	
    else if(difference <= 13 && $('user_birthday_2i').value > month)
    {
			
        $('notice').style.display = "block";
        $('notice').innerHTML = "<a href='http://www.familytravelforum.com'>Family Travel Forum Inc.</a>(FTF) does not knowingly collect or store any personal information, even in an aggregate form, about site users ages 12 and under. When younger children access this and other FTF sites it should be under the guidance of parents.<br><br>When children younger than 13 years make contributions to these sites they must have the express written permission of their parents. Please use the FTF permission form for minor children <a href='/info/consent'>found on this page</a> and submit it to our staff for approval in order to activate your minor child's account.<br><br>If you have any questions regarding use of FTF sites by minors, please contact <a href='mailto:bovalan@travelbigo.com'>bovalan@travelbigo.com.</a>"
        $('submit').disabled = true;
        $('submit1').disabled = true;
        return false;
    }
	
    else if(difference <= 13 && $('user_birthday_2i').value == month)
    {
        if( $('user_birthday_3i').value >= today_now.getDate())
        {
            $('notice').style.display = "block";
            $('notice').innerHTML = "<a href='http://www.familytravelforum.com'>Family Travel Forum Inc.</a>(FTF) does not knowingly collect or store any personal information, even in an aggregate form, about site users ages 12 and under. When younger children access this and other FTF sites it should be under the guidance of parents.<br><br>When children younger than 13 years make contributions to these sites they must have the express written permission of their parents. Please use the FTF permission form for minor children <a href='/info/consent'>found on this page</a> and submit it to our staff for approval in order to activate your minor child's account.<br><br>If you have any questions regarding use of FTF sites by minors, please contact <a href='mailto:bovalan@travelbigo.com'>bovalan@travelbigo.com.</a>"
            $('submit').disabled = true;
            $('submit1').disabled = true;
            return false;
        }
        else
        {
            $('notice').style.display = "block";
            $('notice').innerHTML = " ";
            return true;
        }
    }
	
    result = validate_zipcode();
    if( result == false)
    {
        return false;
    }
    else{
        return true;
    }
}







function validate_age(past_parental_permission)
{	
    var today_now = new Date();
    var year =today_now.getFullYear();
    var birth_year = $('user_birthday_1i').value;
    var month = today_now.getMonth() + 1;
    var result;
    var difference = year - birth_year;
	
    if(difference < 13 )
    {
        $('notice').style.display = "block";
        $('notice').innerHTML = "<a href='http://www.familytravelforum.com'>Family Travel Forum Inc.</a>(FTF) does not knowingly collect or store any personal information, even in an aggregate form, about site users ages 12 and under. When younger children access this and other FTF sites it should be under the guidance of parents.<br><br>When children younger than 13 years make contributions to these sites they must have the express written permission of their parents. Please use the FTF permission form for minor children <a href='/info/consent'>found on this page</a> and submit it to our staff for approval in order to activate your minor child's account.<br><br>If you have any questions regarding use of FTF sites by minors, please contact <a href='mailto:bovalan@travelbigo.com'>bovalan@travelbigo.com.</a>"
        $('submit').disabled = true;
        return false;
    }
	
    else if(difference <= 13 && $('user_birthday_2i').value > month)
    {
			
        $('notice').style.display = "block";
        $('notice').innerHTML = "<a href='http://www.familytravelforum.com'>Family Travel Forum Inc.</a>(FTF) does not knowingly collect or store any personal information, even in an aggregate form, about site users ages 12 and under. When younger children access this and other FTF sites it should be under the guidance of parents.<br><br>When children younger than 13 years make contributions to these sites they must have the express written permission of their parents. Please use the FTF permission form for minor children <a href='/info/consent'>found on this page</a> and submit it to our staff for approval in order to activate your minor child's account.<br><br>If you have any questions regarding use of FTF sites by minors, please contact <a href='mailto:bovalan@travelbigo.com'>bovalan@travelbigo.com.</a>"
        $('submit').disabled = true;
        return false;
    }
	
    else if(difference <= 13 && $('user_birthday_2i').value == month)
    {
        if( $('user_birthday_3i').value >= today_now.getDate())
        {
            $('notice').style.display = "block";
            $('notice').innerHTML = "<a href='http://www.familytravelforum.com'>Family Travel Forum Inc.</a>(FTF) does not knowingly collect or store any personal information, even in an aggregate form, about site users ages 12 and under. When younger children access this and other FTF sites it should be under the guidance of parents.<br><br>When children younger than 13 years make contributions to these sites they must have the express written permission of their parents. Please use the FTF permission form for minor children <a href='/info/consent'>found on this page</a> and submit it to our staff for approval in order to activate your minor child's account.<br><br>If you have any questions regarding use of FTF sites by minors, please contact <a href='mailto:bovalan@travelbigo.com'>bovalan@travelbigo.com.</a>"
            $('submit').disabled = true;
            return false;
        }
        else
        {
            $('notice').style.display = "block";
            $('notice').innerHTML = " ";
            return true;
        }
    }
    else if(difference > 18 )
    {
        $('user_parental_permission').checked = true;
        $('user_parental_permission').disabled=true;
        $('parent_hidden').value = 1;
    }
    else if(difference >= 18 && $('user_birthday_2i').value < month  )
    {
		
        $('user_parental_permission').checked = true;
        $('user_parental_permission').disabled=true;
        $('parent_hidden').value = 1;
		
    }
    else if(difference >= 18 && $('user_birthday_2i').value == month)
    {
        if($('user_birthday_3i').value <= today_now.getDate())
        {
            $('user_parental_permission').checked = true;
            $('user_parental_permission').disabled=true;
            $('parent_hidden').value = 1;
        }
        else
        {
            if (past_parental_permission == 1) {
                $('user_parental_permission').disabled = false;
                $('user_parental_permission').checked = true;
                $('parent_hidden').value = 0;
            }
            else {
                $('user_parental_permission').disabled = false;
                $('user_parental_permission').checked = false;
                $('parent_hidden').value = 0;
            }
        }
    }
    else{
        if (past_parental_permission == 1) {
            $('user_parental_permission').disabled = false;
            $('user_parental_permission').checked = true;
            $('parent_hidden').value = 0;
        }
        else {
            $('user_parental_permission').disabled = false;
            $('user_parental_permission').checked = false;
            $('parent_hidden').value = 0;
        }
    }
	
    result = validate_form();
    if( result == false)
    {
        return false;
    }
    else{
        return true;
    }
}

function validate_country()
{	
    var country = $('user_country').value;
    var zipcode = $('user_postal_code').value.length
    if( country == "US" || country == "CA")
    {
        $('user_postal_code').disabled=false;
    }
    else{
        $('user_postal_code').disabled=true;
    }
}

function validate_USA_zipcode()
{
    var ziplen = $('user_postal_code').value.length
    if (ziplen >= 4 && ziplen <= 5) {
        $('zip_code').style.display = "block";
        $('zip_code').innerHTML = " ";
        $('submit').disabled = false;
    }
    else {
        $('zip_code').style.display = "block";
        $('zip_code').innerHTML = "Minimum 4 and maximum 5 characters are required";
        $('submit').disabled = true;
        return false;
    }

    return true;
}

function validate_Canada_zipcode()
{
    var ziplen = $('user_postal_code').value.length
    if (ziplen >= 6 && ziplen <= 7) {
        $('zip_code').style.display = "block";
        $('zip_code').innerHTML = " ";
        $('submit').disabled = false;
    }
    else {
        $('zip_code').style.display = "block";
        $('zip_code').innerHTML = "Minimum 6 and maximum 7 characters are required";
        $('submit').disabled = true;
        return false;
    }
    return true;
}

function validate_zipcode()
{
    var country = $('user_country').value;
    //var zipcode = $('user_postal_code').value.length
    if( country == "US" || country == "CA")
    {
        $('user_postal_code').disabled=false;
        if( country == "US") {
            return validate_USA_zipcode();
        } else {
            return validate_Canada_zipcode();
        }
    }
    else{
        $('user_postal_code').value="";
        $('user_postal_code').disabled=true;
        $('zip_code').innerHTML = "";
        $('submit').disabled = false;
    }
    return true;
}
/*
function validate_zip_old_delete_me()
{
    var country = $('user_country').value;
    var zipcode = $('user_postal_code').value.length
    if( country == "US" || country == "CA")
    {
        $('user_postal_code').disabled=false;
        if (zipcode >= 4 && zipcode <= 5) {
            $('zip_code').style.display = "block";
            $('zip_code').innerHTML = " ";
            $('submit1').disabled = false;
            $('submit').disabled = false;
        }
        else {
            $('zip_code').style.display = "block";
            $('zip_code').innerHTML = "Minimum 4 and maximum 5 characters are required";
            $('submit1').disabled = true;
            $('submit').disabled = true;
			
        }
    }
    else{
        $('user_postal_code').value="";
        $('user_postal_code').disabled=true;
        $('zip_code').innerHTML = "";
        $('submit1').disabled = false;
        $('submit').disabled = false;
    }
	
}
*/
function validate(){
    var birth_year = $('user_birthday_1i').value;
    var trip_year = $('content_date_of_trip_1i').value;
    var today_now = new Date();
    var year =today_now.getFullYear();
    var month = today_now.getMonth() + 1;
	
    if(birth_year%4 == 0)
    {
        if(birth_year%100 != 0)
        {
            var leap_year = 1;
        }
        else
        {
            if (birth_year % 400 == 0) {
                leap_year = 1;
            }
            else {
                leap_year = 0;
            }
        }
    }
    else{
        leap_year = 0;
    }
	
    if(trip_year < birth_year )
    {
        $('trip_date').style.display = "block";
        $('trip_date').innerHTML = "Invalid Date";
        return false;
    }
	
    if ($('user_birthday_2i').value == "2" && $('user_birthday_3i').value >= 29 && leap_year == "0") {
        $('bdate').style.display = "block";
        $('bdate').innerHTML = "Invalid Date";
        return false;
    }
    else {
        if ($('user_birthday_2i').value == "2" || $('user_birthday_2i').value == "4" || $('user_birthday_2i').value == "6" || $('user_birthday_2i').value == "9" || $('user_birthday_2i').value == "11")
        {
            if ($('user_birthday_3i').value > 30) {
                $('bdate').style.display = "block";
                $('bdate').innerHTML = "Invalid Date";
                return false;
            }
            else
            {
                $('bdate').style.display = "block";
                $('bdate').innerHTML = " ";
            }
        }
        else {
            $('bdate').style.display = "block";
            $('bdate').innerHTML = " ";
        }
    }
		
}


function add_left()
{
    var index = 0;
    var index_next = 0;
    var len = $('location_select').length;
    var count = 0;
    var found;
    index = $('location_select1').value;
	
    for (var i = 0; i < $('location_select1').length; i++) {
        if (($('location_select1').options[i] != null) && ($('location_select1').options[i].selected)) {
            //Check if this value already exist in the destList or not
            //if not then add it otherwise do not add it.
            var found = false;
            for (count = 0; count < len; count++) {
                if ($('location_select').options[count] != null) {
                    if ($('location_select1').options[i].text == $('location_select').options[count].text) {
						
                        found = true;
                        break;
                    }
                }
            }
            if (found != true) {
                $('location_select').options[len] = new Option($('location_select1').options[i].text, $('location_select1').options[i].value, $('location_select1').options[i].selected);
                len++;
				
            }
        }
    }
	
    return false;
}
	
function add_right()
{
    //var destList = window.document.forms[0].destList;
    var len = $('location_select').options.length;
    for(var i = (len-1); i >= 0; i--) {
        if (($('location_select').options[i] != null) && ($('location_select').options[i].selected == true)) {
            $('location_select').options[i] = null;
        }
    }
    return false;
}

function add_left_story()
{
    var index = 0;
    var index_next = 0;
    var len = $('story_select').length;
    var count = 0;
    var found;

    for (var i = 0; i < $('story_select1').length; i++) {
        if (($('story_select1').options[i] != null) && ($('story_select1').options[i].selected)) {
            //Check if this value already exist in the destList or not
            //if not then add it otherwise do not add it.
            var found = false;
            for (count = 0; count < len; count++) {
                if ($('story_select').options[count] != null) {
                    if ($('story_select1').options[i].text == $('story_select').options[count].text) {
                        found = true;
                        break;
                    }
                }
            }
            if (found != true) {
                $('story_select').options[len] = new Option($('story_select1').options[i].text, $('story_select1').options[i].value);
                len++;
            }
        }
    }
    return false;
}

function add_right_story()
{
    var len = $('story_select').options.length;
    for(var i = (len-1); i >= 0; i--) {
        if (($('story_select').options[i] != null) && ($('story_select').options[i].selected == true)) {
            $('story_select').options[i] = null;
        }
    }
    return false;
}

function submit_values_story()
{
    $('hidden_value').value = "";
    for(var j = 0; j< $('story_select').length; j++)
    {
        var arr = new Array($('story_select').length);
        arr[j] = $('story_select').options[j].value;
        $('hidden_value').value = $('hidden_value').value + arr[j].toString() + ';'
    }
    document.form.submit(arr);
}

function submit_values()
{
    $('hidden_value').value = "";
    for(var j = 0; j< $('location_select').length; j++)
    {
        var arr = new Array($('location_select').length);
        arr[j] = $('location_select').options[j].value;
        $('hidden_value').value = $('hidden_value').value + arr[j].toString() + ';'
    }
    document.form.submit(arr);
}

function validate_body(){
    var birth_year = $('user_birthday_1i').value;
    var trip_year = $('content_date_of_trip_1i').value;
    var today_now = new Date();
    var year =today_now.getFullYear();
    var month = today_now.getMonth() + 1;
    var textAreaid = ($("articleid").value);
    var fckcontent = escape(FCKeditorAPI.GetInstance("content_" + textAreaid + "_body_editor").GetXHTML()); //here 'message' is my fck id
    var result;
    if (fckcontent == "") {
        $('bodyerr').style.display = "block";
        $('bodyerr').innerHTML = "can't be blank";
        return false;
    }
    else {
        $('bodyerr').style.display = "block";
        $('bodyerr').innerHTML = " ";
    }
		
    result = validate();
    if (result == false) {
        return false;
    }
    else{
        return true;
    }
}

function validate_body_finish()
{
    var result_body;
    result_body = validate_body();
    if (result_body == false) {
        return false;
    }
    else{
        return confirm("Make sure you Save your essay before you hit Submit.");
    }
}

function validate_body_article(){
	
    var textAreaid = ($("articleid").value);
    var fckcontent = escape(FCKeditorAPI.GetInstance("content_" + textAreaid + "_body_editor").GetXHTML()); //here 'message' is my fck id
    //var result;
    if (fckcontent == "") {
        $('bodyerr').style.display = "block";
        $('bodyerr').innerHTML = "can't be blank";
        return false;
    }
    else {
        $('bodyerr').style.display = "block";
        $('bodyerr').innerHTML = " ";
        return true;
    }
}