/**
* Javascript functions for CV
* @author Sven S�ld <sven.syld@cvkeskus.ee>
*/

var _errors = new Array;


/**
* Opens CV viewing window
*/
function JsOpenViewCV(url) { // {{{
    JsOpenWindow(url, 700, 1000);
} // }}}

function JsOpenCVDetailWin(url) { // {{{
//avab etteantud aadressiga kindla suurusega akna
//mingeid statusbare ja varke ei lasta teha
    //JsShowObjProps(window,'window');
    var target = 'cv_edit_detail'; // sellise nimega aken
    var left = 20; // distance from left
    var top = 20; // distance from top
    var width = Math.min(620, screen.availWidth - 2*left);
    var height = screen.availHeight - 2*top - 80; // 80 tegumiriba jaoks
    //alert(screen.availHeight);
    left = screen.availWidth - width - left; // l�ki paremasse serva!
    // uindou propertid
    var props = 'width='+width+',outerWidth='+width+',height='+height+',outerHeight='+height+
                ',top='+top+',screenY='+top+',left='+left+',screenX='+left; // main n�nn
    var props2 = 'directories=0,location=0,menubar=0,resizable=1,scrollbars=1,status=1'; // other n�nn
    var win = window.open(url,target,props+','+props2);
    win.focus();
} // }}}

function JsIsValidURL(url) { // {{{
    var r = new RegExp(
         '^'
        +'(http:\\/\\/|https:\\/\\/)?' // maybe begins with protocol
        +'([^\\/]+)' // hostname -- checked later
        +'(\\/[a-z0-9@#%&\\(\\)\\.,~\\/_\\?=\\-]*)?' // maybe followed by / and path
        +'$'
        ,'i'); // case-insensitive
        
    var p = r.exec(url); // test regexp
    
    //^(http:\/\/|https:\/\/)?([^\/]+)(\/[a-z0-9@#%&\(\)\.,~\/_\-\?=]*)?$/
    if (p == null) return false; // doest match
    
    return JsIsValidHost(p[2]);
} // }}}

function JsIsValidHost(host) { // {{{
    var r = new RegExp('^([a-z0-9][a-z0-9\\-]*[a-z0-9]\\.)+[a-z0-9]{2,}$','i'); // case-insensitive
    return r.test(host);
} // }}}

function JsIsValidEmail(email) { // {{{
    var r = new RegExp('^([a-z0-9\\._\\-]+)@(.+)$','i'); // case-insensitive
    
    var p = r.exec(email); // test regexp
    if (p == null) return false; // doest match
    
    return JsIsValidHost(p[2]);
} // }}}

function JsRaiseError(text) { // {{{
    _errors[_errors.length] = text;
} // }}}

function JsCountErrors() { // {{{
    return _errors.length;
} // }}}

function JsGetErrors() { // {{{
    return _errors;
} // }}}

function JsClearErrors() { // {{{
    _errors.length = 0; // clear errors
} // }}}

/**
* Displays errors in element#cv_edit_errors and blinks borders
*/
function JsShowErrors() { // {{{
    var s = '';
    var errors = JsGetErrors();
    if (!errors.length) return;
    
    for (var i=0; i<errors.length; i++) {
        s += '<li>' + _errors[i] + '</li>\n';
    }
    
    var error_box = document.getElementById('cv_edit_errors');
    //alert(error_box);
    error_box.innerHTML = s;
    error_box.display = 'block';
    
    JsBlinkErrorBox(10); // blink 10 times
    
    JsClearErrors();
} // }}}

/**
* Blinks error box 'count' times
*/
function JsBlinkErrorBox(count) { // {{{
    if (count <= 0) return;
    var d = document.getElementById('cv_edit_errors');
    
    if (count % 2) {
        d.style['borderColor'] = 'transparent';
    } else {
        d.style['borderColor'] = 'red';
    }
    setTimeout('JsBlinkErrorBox('+(count-1)+');', 70); // next in few moments
} // }}}


/**
* Trims all form elements - removes all trailing and leading spaces
*/
function JsTrimFormElements(frm) { // {{{
    for (var i in frm) {
        var elem = frm[i];
        document.writeln(elem.name + ':' + elem.type + '<br>\n');
    }
} // }}}



