/**
  * global functions
  *
  */

function confirm_delete(message){
 return confirm ( message ? message : 'Are you sure you want to delete this item?');
}

function cm_over(obj,color){

    obj.style.backgroundColor = color ? color : '#DDDDDD';
}
function cm_out(obj,color){

    obj.style.backgroundColor  = color ? color: '';
}

/**
 *  copy to text in clipboard
 */
function clipboard(text){

  text = text.unescapeHTML();
/*      if (window.clipboardData) {
        window.clipboardData.setData("Text",text);
      } else {
*/
        var flashcopier = 'flashcopier';
        if(!document.getElementById(flashcopier)) {
          var divholder = document.createElement('div');
          divholder.id = flashcopier;
          document.body.appendChild(divholder);
        }
        document.getElementById(flashcopier).innerHTML = '';
        var divinfo = '<embed src="' + 'js/clipboard.swf" FlashVars="clipboard='+escape(text)+'" width="30" height="30" type="application/x-shockwave-flash"></embed>';
        document.getElementById(flashcopier).innerHTML = divinfo;
//      }
  return false;
}

/*
    project page functions (normal projects + custom projects)
*/

/**
 *  add new table with post fields
 * @return int ID of the last added box
 */
function project_add_keyword(type){

    // project_add_keyword()
    box_id = $F('top_' + type);
    ++box_id;

    $('top_' + type).value = box_id;

    new_box = $('fields_' + type).innerHTML;
    new_box = new_box.replace(/%%box_id%%/,box_id);

    new_box = '<div id="'+ type +'_'+ box_id +'">'+ new_box +'</div>';
    new Insertion.Bottom('project_' + type,new_box);

    return box_id;
}

/**
 *  remove post fields
 */
function project_remove_fields(box_id,type){

        $(type + '_' + box_id).innerHTML = '';
        $(type + '_' + box_id).style.display = 'none';

        if (type == 'normal'){
            calculate_project_cost();
        }else{
            calculate_cproject_cost();
        }
    return false;
}

/**
 * calculate how much credits required for requested posts
 */
function calculate_project_cost(){

    var cost_blurb = $F('user_cost_blurb');
    var cost_minipost = $F('user_cost_minipost');
    var cost_article = $F('user_cost_article');
    var user_credits = $F('user_credits');

    var total = 0;
    var cost = 0;

    var obj = $('form_project');
    for (i=0;i<obj.length;i++){ // scan form

        if (obj.elements[i].name.indexOf('post_type') > -1 ){

            var post_type = obj.elements[i].value;
        }
        if (obj.elements[i].name.indexOf('num_posts') > -1 ){

            switch(post_type){
                case 'blurb':
                    cost = cost_blurb;
                break;
                case 'minipost':
                    cost = cost_minipost;
                break;
                case 'article':
                    cost = cost_article;
                break;
                default:
                    cost = 0;
            }

            total += cost*obj.elements[i].value;
        }
    }

    $('credits_available').innerHTML = user_credits - total;
    return true;
}

/**
 * calculate how much credits required for requested posts
 */
function calculate_cproject_cost(){

    var project_cost = article_type_cost('original') + article_type_cost('rewritten') + article_type_cost('sales') ;

/*    if ($('custom_rush').value == 1){

       project_cost +=  CUSTOM_PARAMS.custom_rush;
    }
*/

    $('project_cost').innerHTML = project_cost;
    $('credits_available').innerHTML = CUSTOM_PARAMS.user_credits - project_cost;
    return true;
}

/**
 * calculate cost for requested article type
 * version 2009/02/09
 */
function article_type_cost(type){

     var i =0;
     var total =0;
     var extra_cost = 0;
     var words = 0;
     var quantity = 0;
     var word_cost = 0;

        for (i;i <= $F('top_' + type);i++){

           extra_cost = 0;
           quantity = find_div_field(type + '_' + i, type + '_quantity');
           words = find_div_field( type + '_' + i, type + '_words');

           if ($(type + '_' + i) && quantity && words){

                // check extra cost options
                switch (find_div_field(type + '_' + i, type + '_research')){
                    case 'light':
                        extra_cost = CUSTOM_PARAMS.custom_light_research;
                    break;
                    case 'medium':
                        extra_cost = CUSTOM_PARAMS.custom_medium_research;
                    break;
                    case 'heavy':
                        extra_cost = CUSTOM_PARAMS.custom_heavy_research;
                }

                // find word cost
                switch (type){
                    case 'original':
                        word_cost = CUSTOM_PARAMS.custom_original_word;
                    break;
                    case 'rewritten':
                        word_cost = CUSTOM_PARAMS.custom_rewritten_word;
                    break;
                    case 'sales':
                        word_cost = CUSTOM_PARAMS.custom_sales_word;
                }

                total += quantity * words * (word_cost + extra_cost);
           }
        }

    return total;
}

/**
 * function find input field in the DIV
 * @param div_name name of DIV object
 * @param string name of field
 * @return mixed return FALSE is field not found or return field value if field was found
 */
function find_div_field(div_id,field_name){

    var i=0;

    if (!$(div_id)) return false;
    var obj = $(div_id).descendants();

    for (i;i<obj.length;i++){

        if (obj[i].name && obj[i].name.indexOf( field_name ) > -1){ // field found

            return $F( obj[i] );
        }
    }
    return false;
}

/**
 * custom projects
 * display box with custom form for custom projects
 */
function custom_projects(section){

    if ($('section_' + section).style.display == 'block'){

        $('section_' + section).style.display = 'none';
        $('icon_' + section).src = 'images/plus.gif';

    }else{ // expand

        $('section_' + section).style.display = 'block';
        $('icon_' + section).src = 'images/minus.gif';
    }

    return false;
}

/**
 * add predefined custom fields for custom projects
 */

function add_custom_fields(type,words,research){

    var i=0;
    var last_box = project_add_keyword(type);
    var div_id = type + '_' + last_box;

    if (!$(div_id)) return false;
    var obj = $(div_id).descendants();

    for (i;i<obj.length;i++){

        if (words && obj[i].name && obj[i].name.indexOf( type + '_words' ) > -1){ // field found

             obj[i].value = words;

        }else if (research && obj[i].name && obj[i].name.indexOf( type + '_research' ) > -1){

             obj[i].value = research;

        }else if (research == 'light' && obj[i].name && obj[i].name.indexOf( type + '_second_keyword' ) > -1){

             // disable 'second keywords' field if light research selected for original articles, (no second keywords for this articles)
             obj[i].disabled = true;
             obj[i].style.backgroundColor  = '#EEEEEE';

        }else if (obj[i].name && obj[i].name.indexOf( type + '_quantity' ) > -1){

             obj[i].value = 1;
        }
    }

    calculate_cproject_cost();
    return false;
}
