<?php
use Orchard\Feature\Feature;

$modalWindowID = Zend_Controller_Front::getInstance()->getRequest()->getParam('modalWindowID', '');
$this->placeholder('buttons')->set('<button class="btn btn-default" onclick="' . $modalWindowID . ' .close()" id="cancel_button">' . $this->translate('CANCEL') . '</button>
    <a class="btn btn-primary" onmousedown="submitMyFrm()"><span id="submit_button">' . $this->translate('Save News Story') . '</span></a>');
?>

<form id="news_form" name="news_form" method="post" action="/artistnews/savenews">
<?=$this->form->news_id->renderViewHelper();?>
<?=$this->form->artist_id->renderViewHelper();?>
    <div class="form-group">
        <label><?=$this->translate('News Headline')?> *</label>
        <?=$this->form->link_text->setAttrib('onblur', 'validateLinkText(); validateBlank()')->setAttrib('class', 'form-control')->renderViewHelper();?>
        <div id="linkTextError" class="error top-space"></div>
    </div>
    <div class="form-group">
        <label><?=$this->translate('URL')?></label>
        <?=$this->form->link->setAttrib('onblur', 'validateLink(); validateBlank()')->setAttrib('class', 'form-control')->renderViewHelper();?>
        <p class="smalltext helptext example-text top-space"><?=$this->translate('Example: http://www.theorchard.com')?></p>
    </div>
    <div class="form-group">
        <label><?=$this->translate('News Blurb')?></label>
        <?=$this->form->blurb->setAttrib('onblur', 'validateBlank()')->setAttrib('class', 'form-control')->renderViewHelper();?>
    </div>
    <div class="form-group">
        <label><?=$this->translate('News Source')?></label>
        <?=$this->form->source->setAttrib('onblur', 'validateSource(); validateBlank()')->setAttrib('class', 'form-control')->renderViewHelper();?>
        <div id="newsSourceError" class="error top-space"></div>
        <p class="smalltext helptext example-text top-space"><?=$this->translate('Example: Journalist Name - New York Times')?></p>
    </div>
    <div class="alert alert-info">
        <b><?=$this->translate('Info!')?></b><?=$this->translate(' News Headlines and URL will be published to Facebook.')?>
    </div>
    <div id="noDataError"></div>
<?=$this->form->querystring->renderViewHelper();?>
</form>
<script type="text/javascript">

function validateText(value) {
	if (!value) {return false;} return true;
}

function validateURL(value) {
	/*
	if(jQuery.trim(value) != ""){
		var regexp = /^(?:\w*:\/\/)?((?:[\w-_]*\.?)+:?\d*(?:\/?[\w-_.]+\/?)*)[\?]?[\&]?([\w-_]*)?=?([\/\w-_\?\)%\.\(\*\[\]\^<>\\]*)?[\&]?([\w-_]*)?=?([\/\w-_\?\)%\.\(\*\[\]\^<>\\]*)?[\&]?([\w-_]*)?=?([\/\w-_\?\)%\.\(\*\[\]\^<>\\]*)?[\&]?([\w-_]*)?=?([\/\w-_\?\)%\.\(\*\[\]\^<>\\]*)?$/;
		if (regexp.test(value)) { return true; } return false;
	}*/
	return true;
}

function validateLength(value, max_length) {
	if (value.length > max_length) {
	    return false;
	}
	return true;
}

function validateLink() {
	var error = 0;
	if (!validateURL($('#link').val())) {
	    error = 1; $('#linkError').html("<?=$this->translate->_('Invalid url')?>");
	} else {
	    $('#linkError').html('');
	}
	return error;
}

function validateLinkText() {
    var error = 0;
    if (!$('#link_text').val()) {
        error = 1;
        $('#linkTextError').html('<?=$this->translate('News headline is required')?>').parent('.form-group').addClass('invalid-input');
    } else if (!validateLength($('#link_text').val(), 150)) {
        error = 1;
        $('#linkTextError').html('<?=$this->translate('News headline is too long')?>').parent('.form-group').addClass('invalid-input');
    } else {
        $('#linkTextError').html('').parent('.form-group').removeClass('invalid-input');
    }

    return error;
}

function validateSource() {
    var error = 0;
    if (!validateLength($('#source').val(), 150)) {
        error = 1;
        $('#newsSourceError').html('<?=$this->translate('News source is too long')?>').parent('.form-group').addClass('invalid-input');
    } else {
        $('#newsSourceError').html('').parent('.form-group').removeClass('invalid-input');
    }

    return error;
}

function validateBlank() {
    var error = 0;
    var blurb_content = tinyMCE.get('blurb').getContent();
    if (!blurb_content && !$('#link').val() && !$('#source').val()) {
        $('#noDataError').html('<div class="alert alert-danger alert-img"><?=$this->translate('Please enter at least one field besides News Headline.')?></div>');
        error = 1;
    } else {
        $('#noDataError').html('');
    }
    return error;
}

function submitMyFrm() {
	var error = 0;
	error += validateLink();
	error += validateLinkText();
	error += validateSource();
	error += validateBlank();
	if (error<=0) {
            showRefinedMask('Submitting...');
            $('#news_form').submit();
	}
}

function myHandleEvent(e) {
    if (e.type == 'keyup' ||  e.type == 'mouseup') {
        validateBlank();
    }
    return true;
}

<?=$this->Richtextbox('blurb', array('width'=>560, 'handle_event_callback' => 'myHandleEvent', 'theme_advanced_resizing' => 'true', 'theme_advanced_resizing_max_width' => 560))?>
</script>
