function get_radio(form_name, element_name){
    value = false;
    var elts = document.forms[form_name].elements[element_name];
    if (!elts) {
        alert('Error #201: elements '+form_name+'.'+element_name+' not found.');
        return false;
    }
    var elts_cnt  = (typeof(elts.length) != 'undefined')
                  ? elts.length
                  : 0;
    if (elts_cnt) {
        for (var i = 0; i < elts_cnt; i++) {
            if (elts[i].checked) {
                value = elts[i].value;
            }
        }
    }
    return value;
}

function get_selected(select_id){
  box = document.getElementById(select_id);
  if (box) {
      return box.options[box.selectedIndex].value;
  } else {
    alert('get_selected: '+select_id+' not found');
      return false;
  }
}

function getFirstValueByName(the_form, field_name)
{
    var elts = document.getElementsByName(field_name);
    var elts_cnt  = (typeof(elts.length) != 'undefined')
                  ? elts.length
                  : 0;
    //alert( field_name + ' found: ' + elts_cnt );
    if (elts_cnt) {
        el_value = elts[0].value;
    } else {
        el_value = false;
    }
    return el_value;
}

function getValueById(id){
    input = document.getElementById(id);
    if (!input) {
        return false;
    }
    return input.value;
}

function getCheckboxById(id){
    input = document.getElementById(id);
    if (!input) {
        return false;
    }
    return input.checked;
}

function getCheckboxByName(the_form, field_name)
{
    var elts = document.getElementsByName(field_name);
    var elts_cnt  = (typeof(elts.length) != 'undefined')
                  ? elts.length
                  : 0;
    //alert( field_name + ' found: ' + elts_cnt );
    if (elts_cnt) {
        if (elts[0].checked) {
            el_value = elts[0].value;
        } else {
            //alert( field_name + ' not checked');
            el_value = 0;
        }
    } else {
        alert( field_name + ' not found');
        el_value = false;
    }
    return el_value;
}



/*******************************/

function goto_country(){
	box = document.destinations.country;
	destination = box.options[box.selectedIndex].value;
	if (destination>'') {
		window.location.href=destination;
	}
}

function preview_image(img_src) {
    pic = new Image();
    pic.src = img_src;
    setTimeout('view_image(pic.src, pic.width, pic.height);', 500);
}

function view_image(img_src, img_width, img_height){
    if (img_width>screen.availWidth-150 || img_width==0) {
        win_width = screen.availWidth-150;
    } else {
        win_width = img_width+20;
    }
    if (img_height>screen.availHeight-150 || img_height==0) {
        win_height = screen.availHeight-150;
    } else {
        win_height = img_height+20;
    }
    win_top  = Math.abs((screen.availHeight - win_height)/3);
    win_left = Math.abs((screen.availWidth -win_width)/3);
    photoWindow = window.open('', '', "resizable=yes,top=" + win_top + ',left=' + win_left + ",width="+win_width+',height='+win_height+",status=0,menubar=0,toolbar=0,scrollbars=yes");
    photoWindow.document.write('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>');
    photoWindow.document.write('<head><title>'+img_src+'</title><style type="text/css"><!-- body {margin:0;padding:0} --></style>');
    photoWindow.document.write('<scr'+'ipt type=text/javascript>');
    photoWindow.document.write('document.onkeypress = function CloseOnEsc(key) { if(document.all) { var keyCode = window.event.keyCode; } else { if (key.which == 0) {window.close();return;}   }  if (keyCode == 27) {window.close();return;} }');
    photoWindow.document.write('</scr'+'ipt>');
    photoWindow.document.write('</head><body><img src="'+img_src+'" ');
    if ( (img_width>1) && (img_height>1) ) {
        photoWindow.document.write('width="'+img_width+'" height="'+img_height+'"');
    }
    photoWindow.document.write(' border=0 onclick="javascript:window.close();">');
    photoWindow.document.write('</body></html>');
    photoWindow.document.bgColor="#f0f0f0";
    photoWindow.document.close()
}

function igallery_image(img_src, img_width, img_height, img_title){
    if (img_width>screen.availWidth-150 || img_width==0) {
        win_width = screen.availWidth-150;
    } else {
        win_width = img_width+20;
    }
    if (img_height>screen.availHeight-150 || img_height==0) {
        win_height = screen.availHeight-150;
    } else {
        win_height = img_height+20;
    }
    win_top  = Math.abs((screen.availHeight - win_height)/3);
    win_left = Math.abs((screen.availWidth -win_width)/3);
    photoWindow = window.open('', '', "resizable=yes,top=" + win_top + ',left=' + win_left + ",width="+win_width+',height='+win_height+",status=0,menubar=0,toolbar=0,scrollbars=yes");
    photoWindow.document.write('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>');
    photoWindow.document.write('<head><title>'+img_title+'</title><style type="text/css"><!-- body {margin:0;padding:0} --></style>');
    photoWindow.document.write('<scr'+'ipt type=text/javascript>');
    photoWindow.document.write('document.onkeypress = function CloseOnEsc(key) { if(document.all) { var keyCode = window.event.keyCode; } else { if (key.which == 0) {window.close();return;}   }  if (keyCode == 27) {window.close();return;} }');
    photoWindow.document.write('</scr'+'ipt>');
    photoWindow.document.write('</head><body><img src="'+img_src+'" ');
    if ( (img_width>1) && (img_height>1) ) {
        photoWindow.document.write('width="'+img_width+'" height="'+img_height+'"');
    }
    photoWindow.document.write(' border=0 onclick="javascript:window.close();">');
    photoWindow.document.write('</body></html>');
    photoWindow.document.bgColor="#f0f0f0";
    photoWindow.document.close()
}

function switch_obj(obj_id){
    obj=document.getElementById(obj_id);
    if (!obj) {
        return false;
    }
    visible_class = 'show';
    hidden_class = 'hide';
    cn=obj.className;
    p=cn.indexOf(visible_class);
    if (p>-1){
        remove_class(obj, visible_class);
        add_class(obj, hidden_class);
    } else {
        remove_class(obj, hidden_class);
        add_class(obj, visible_class);
    }
    return true;
}


function show_obj(obj_id){
    obj=document.getElementById(obj_id);
    if (!obj) {
        return false;
    }
    remove_class(obj, 'hide');
    add_class(obj, 'show');
}

function hide_obj(obj_id){
    obj=document.getElementById(obj_id);
    if (!obj) {
        return false;
    }
    remove_class(obj, 'show');
    add_class(obj, 'hide');
}

function add_class(e,c) {
  e.className=e.className+" "+c;
}

function remove_class(e,c) {
  cn=e.className;
  p=cn.indexOf(c);
  if (p>-1){e.className=cn.substr(0,p)+cn.substr(p+c.length);  }
}

function SetCookie(cookieName, cookieValue, nHours) {
 var today = new Date();
 var expire = new Date();
 if (nHours==null || nHours==0) nHours=1;
 expire.setTime(today.getTime() + 3600000*nHours);
 cookie = cookieName+"="+escape(cookieValue) + ";expires="+expire.toGMTString()+";path=/";
 document.cookie = cookie;
}

function GetCookie(name){
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

function DeleteCookie(name, path, domain){
    if (GetCookie(name)) {
        document.cookie = name + "=" +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}


function switch_location(obj_id){
  obj=document.getElementById(obj_id);
  if (!obj) {
  	return false;
  }
  target = obj.options[obj.selectedIndex].value;
  if (target > '') {
    top.location.href=target;
  }
}



/**********************************************************************
 * Highlighting form fields on focus
 * http://wufoo.com/
 **********************************************************************/

var highlight_array = new Array();

function showFocus(input_id) {
    row_id = 'row_'+input_id;
    row=document.getElementById(row_id);
    if (!row) {
        return false;
    }
    add_class(row, "fob_focused");
}

function hideFocus(input_id) {
    row_id = 'row_'+input_id;
    row=document.getElementById(row_id);
    if (!row) {
        return false;
    }
    remove_class(row, "fob_focused");
}

function fob_initForm(){
	fob_initializeFocus();
}

// for radio and checkboxes, they have to be cleared manually, so they are added to the
// global array highlight_array so we dont have to loop through the dom every time.
function fob_initializeFocus(){
	fields = getElementsByClassName(document, "*", "fob_field");
	for(i = 0; i < fields.length; i++) {
		if(fields[i].type == 'radio' || fields[i].type == 'checkbox' || fields[i].type == 'file') {
			fields[i].onclick = function(){clearSafariRadios(); showFocus(this.id); };
			fields[i].onfocus = function(){clearSafariRadios(); showFocus(this.id); };
			highlight_array.splice(highlight_array.length,0,fields[i]);
		}
		else {
			fields[i].onfocus = function(){clearSafariRadios();showFocus(this.id);};
			fields[i].onblur = function(){hideFocus(this.id);};
		}
	}
}

function clearSafariRadios() {
	for(var i = 0; i < highlight_array.length; i++) {
	    hideFocus(highlight_array[i].id);
	}
}

// http://www.robertnyman.com/2005/11/07/the-ultimate-getelementsbyclassname/
function getElementsByClassName(oElm, strTagName, strClassName) {
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];
		if(oRegExp.test(oElement.className)){
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements);
}

/****************************** onFocus end ******************************/

  function remove_item(item_id){
	//document.basket_form.f_action.value = 'recalc';
	//document.location.href = '/order/?rid='+item_id;
	document.basket_form.f_action.value = 'remove';
	document.basket_form.f_arg.value = item_id;
	document.basket_form.submit();
  }

  function basket_clear(){
	//document.location.href = '/order/?empty_basket=true';
	document.basket_form.f_action.value = 'clear';
	document.basket_form.submit();

  }

  function basket_recalc(){
  	//alert('Hop!');
	document.basket_form.f_action.value = 'recalc';
	document.basket_form.submit();
	//if(document.basket_form.onsubmit()){	}

  }

  function order_submit_step1(){
	document.basket_form.f_action.value = 'send';
	document.basket_form.submit();
  }


function add_to_basket(item_id){
    window.frames['basket_frame'].document.location.href = '/order/basket/'+item_id+'/';
    open_basket();
}

function set_filter(filter){
    //
}

function set_currency(currency_id){
	fields = getElementsByClassName(document, "td", "currency_selector");
	for(i = 0; i < fields.length; i++) {
        remove_class(fields[i], 'active');
        add_class(fields[i], 'inactive');
	}

    SetCookie('currency', currency_id, 24);

    obj=document.getElementById('currency' + currency_id);
    if (!obj){
        return false;
    }
    remove_class(obj, 'inactive');
    add_class(obj, 'active');
    document.location.href = document.location.href;
}

function select_tab(tab_id){

	for(i = 1; i < 8; i++) {
        obj=document.getElementById('tab_header_' + i);
        if (!obj){
            continue;
        }
        remove_class(obj, 'a');

        obj=document.getElementById('tab_' + i);
        if (!obj){
            continue;
        }
        remove_class(obj, 'show');
        add_class(obj, 'hide');
	}

    obj=document.getElementById('tab_header_' + tab_id);
    if (!obj){
        return false;
    }
    //remove_class(obj, 'inactive');
    add_class(obj, 'a');

    obj=document.getElementById('tab_' + tab_id);
    if (!obj){
        return false;
    }
    remove_class(obj, 'hide');
    add_class(obj, 'show');
    return true;
}

function select_variation(product_id, variation_index){

	for(i = 0; i < 7; i++) {

	    /* selector */
        obj_id = 'variation_td_' + product_id + '_' + i;
        obj=document.getElementById(obj_id);
        if (!obj){
            continue;
        }
        remove_class(obj, 'variation_active');
        add_class(obj, 'variation_inactive');

        /* price */
        obj_id = 'variation_price_' + product_id + '_' + i;
        obj=document.getElementById(obj_id);
        if (!obj){
            continue;
        }
        remove_class(obj, 'show');
        add_class(obj, 'hide');

        /* image */
        obj_id = 'variation_image_' + product_id + '_' + i;
        obj=document.getElementById(obj_id);
        if (!obj){
            continue;
        }
        remove_class(obj, 'show');
        add_class(obj, 'hide');

        /* link */
        obj_id = 'variation_link_' + product_id + '_' + i;
        obj=document.getElementById(obj_id);
        if (!obj){
            continue;
        }
        remove_class(obj, 'show');
        add_class(obj, 'hide');
	}

    /* selector */
    obj_id = 'variation_td_' + product_id + '_' + variation_index;
    obj=document.getElementById(obj_id);
    if (!obj){
       alert('Variation '+obj_id+' not found.');
       return false;
    }
    remove_class(obj, 'variation_inactive');
    add_class(obj, 'variation_active');

    /* price */
    obj_id = 'variation_price_' + product_id + '_' + variation_index;
    obj=document.getElementById(obj_id);
    if (!obj){
       alert('Variation '+obj_id+' not found.');
    }
    remove_class(obj, 'hide');
    add_class(obj, 'show');

    /* image */
    obj_id = 'variation_image_' + product_id + '_' + variation_index;
    obj=document.getElementById(obj_id);
    if (!obj){
       alert('Variation '+obj_id+' not found.');
    }
    remove_class(obj, 'hide');
    add_class(obj, 'show');

    /* link */
    obj_id = 'variation_link_' + product_id + '_' + variation_index;
    obj=document.getElementById(obj_id);
    if (!obj){
       alert('Variation '+obj_id+' not found.');
    }
    remove_class(obj, 'hide');
    add_class(obj, 'show');

    return true;
}

function save_ext_filter_status(obj_id){
     obj=document.getElementById(obj_id);
    if (!obj) {
        return false;
    }
    visible_class = 'show';
    hidden_class = 'hide';
    cn=obj.className;
    p=cn.indexOf(visible_class);
    if (p>-1){
        /**  hide */
        remove_class(obj, visible_class);
        add_class(obj, hidden_class);
        status = 0;
    } else {
        /**  show */
        remove_class(obj, hidden_class);
        add_class(obj, visible_class);
        status = 1;
    }
    SetCookie('filter_ex', status, 24);
    return true;
}

function open_glossary(url){
    win_width = 400;
    win_height = 400;
    if (win_width>screen.availWidth-150 || win_width==0) {
        win_width = screen.availWidth-150;
    }
    if (win_height>screen.availHeight-150 || win_height==0) {
        win_height = screen.availHeight-150;
    }
    win_top  = Math.abs((screen.availHeight - win_height)/3);
    win_left = Math.abs((screen.availWidth -win_width)/3);
    nw = window.open(url, '', "resizable=yes,top=" + win_top + ',left=' + win_left + ",width="+win_width+',height='+win_height+",status=0,menubar=0,toolbar=0,scrollbars=yes");
    return false;
}

function compare(product_id){
    win_width   = 600;
    win_height  = 700;
    url = '/catalog/compare/?pid[]=' + product_id + '';
    if (win_width>screen.availWidth-150 || win_width==0) {
        win_width = screen.availWidth-150;
    }
    if (win_height>screen.availHeight-150 || win_height==0) {
        win_height = screen.availHeight-150;
    }
    win_top  = Math.abs((screen.availHeight - win_height)/3);
    win_left = Math.abs((screen.availWidth -win_width)/3);
    nw = window.open(url, 'compare', "resizable=yes,top=" + win_top + ',left=' + win_left + ",width="+win_width+',height='+win_height+",status=0,menubar=0,toolbar=0,scrollbars=yes");
    return false;
}

function open_basket(){
    win_width = 450;
    win_height = 200;
    win_top  = Math.abs((screen.availHeight - win_height)/2);
    win_left = Math.abs((screen.availWidth -win_width)/2);
    window_url = '/order/basket/message/';
    basketWindow = window.open(window_url, 'basket', "resizable=no,top=" + win_top + ',left=' + win_left + ",width="+win_width+',height='+win_height+",status=0,menubar=0,toolbar=0,scrollbars=yes");
    //if (basketWindow && basketWindow.top) {
    //} else {
    //    alert('Товар добавлен в корзину.');
    //}
}

function goto_basket() {
    if (!window.opener || window.opener.closed){
        window.open('http://mobimarket.com.ua/order/', '');
        return false;
    }
	window.opener.top.location='/order/';
	window.close();
}

function goto_order() {
    if (!window.opener || window.opener.closed){
        window.open('http://mobimarket.com.ua/order/', '');
        return false;
    }
	window.opener.top.location='/order/#order';
	window.close();
}

function subscribe_status(product_id){
    win_width = 450;
    win_height = 500;
    win_top  = Math.abs((screen.availHeight - win_height)/2);
    win_left = Math.abs((screen.availWidth -win_width)/2);
    window_url = '/popups/status_subscription/'+product_id+'/';
    basketWindow = window.open(window_url, '', "resizable=no,top=" + win_top + ',left=' + win_left + ",width="+win_width+',height='+win_height+",status=0,menubar=0,toolbar=0,scrollbars=yes");
}

function subscribe_price(product_id){
    win_width = 450;
    win_height = 500;
    win_top  = Math.abs((screen.availHeight - win_height)/2);
    win_left = Math.abs((screen.availWidth -win_width)/2);
    window_url = '/popups/price_subscription/'+product_id+'/';
    basketWindow = window.open(window_url, '', "resizable=no,top=" + win_top + ',left=' + win_left + ",width="+win_width+',height='+win_height+",status=0,menubar=0,toolbar=0,scrollbars=yes");
}

function photo_image_next(toc_id){
    var obj_id      = 'thumbnail_bg';
    var obj         = document.getElementById(obj_id);
    if (!obj) {
        return false;
    }
    item_index = prev_item_index;
    item_index++;
    if (item_index == items_total){
        item_index = 0;
    }
    obj.style.backgroundImage = 'url('+photos[item_index][1]+')';
    //obj.attributes.item_index.value = item_index;

    obj_id = 'scroller_status';
    var obj = document.getElementById(obj_id);
    if (!obj) {
        return false;
    }
    obj.innerHTML = (item_index+1) + '/'+items_total;

    obj         = document.getElementById('icon_' + prev_item_index);
    if (!obj) {
        return false;
    }
    remove_class(obj, "active");
    add_class(obj, "inactive");

    obj         = document.getElementById('icon_' + item_index);
    if (!obj) {
        return false;
    }
    remove_class(obj, "inactive");
    add_class(obj, "active");

    prev_item_index = item_index;
}

function photo_image_prev(toc_id){
    var obj_id      = 'thumbnail_bg';
    var obj         = document.getElementById(obj_id);
    if (!obj) {
        return false;
    }
    item_index = prev_item_index;
    item_index--;
    if (item_index < 0){
        item_index = items_total-1;
    }
    obj.style.backgroundImage = 'url('+photos[item_index][1]+')';

    obj_id = 'scroller_status';
    var obj = document.getElementById(obj_id);
    if (!obj) {
        return false;
    }
    obj.innerHTML = (item_index+1) + '/'+items_total;

    obj         = document.getElementById('icon_' + prev_item_index);
    if (!obj) {
        return false;
    }
    remove_class(obj, "active");
    add_class(obj, "inactive");
    obj         = document.getElementById('icon_' + item_index);
    if (!obj) {
        return false;
    }
    remove_class(obj, "inactive");
    add_class(obj, "active");

    prev_item_index = item_index;
}


function photo_image_goto(item_index){
    var obj_id      = 'thumbnail_bg';
    var obj         = document.getElementById(obj_id);
    if (!obj) {
        return false;
    }
    obj.style.backgroundImage = 'url('+photos[item_index][1]+')';

    obj_id = 'scroller_status';
    var obj = document.getElementById(obj_id);
    if (!obj) {
        return false;
    }
    obj.innerHTML = (item_index+1) + '/'+items_total;

    obj         = document.getElementById('icon_' + prev_item_index);
    if (!obj) {
        return false;
    }
    remove_class(obj, "active");
    add_class(obj, "inactive");

    obj         = document.getElementById('icon_' + item_index);
    if (!obj) {
        return false;
    }
    remove_class(obj, "inactive");
    add_class(obj, "active");

    prev_item_index = item_index;
}

function search_image_fullsize(window_url){
    img_width   = fullsize_width+150;
    img_height  = fullsize_height+150;
    if (img_width>screen.availWidth-150 || img_width==0) {
        win_width = screen.availWidth-150;
    } else {
        win_width = img_width;
    }
    if (img_height>screen.availHeight-150 || img_height==0) {
        win_height = screen.availHeight-150;
    } else {
        win_height = img_height;
    }
    win_top  = Math.abs((screen.availHeight - win_height)/3);
    win_left = Math.abs((screen.availWidth -win_width)/3);
    //window_url = base_url + 'photos/' + product_id + '/0/';
    //alert(window_url);
    photoWindow = window.open(window_url, '', "resizable=yes,top=" + win_top + ',left=' + win_left + ",width="+win_width+',height='+win_height+",status=0,menubar=0,toolbar=0,scrollbars=yes");
}

