/*
  $Id$

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2005 osCommerce

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License v2 (1991)
  as published by the Free Software Foundation.
*/

function updateDatePullDownMenu(objForm, fieldName) {
    var pdmDays = fieldName + "_days";
    var pdmMonths = fieldName + "_months";
    var pdmYears = fieldName + "_years";

    time = new Date(objForm[pdmYears].options[objForm[pdmYears].selectedIndex].text, objForm[pdmMonths].options[objForm[pdmMonths].selectedIndex].value, 1);

    time = new Date(time - 86400000);

    var selectedDay = objForm[pdmDays].options[objForm[pdmDays].selectedIndex].text;
    var daysInMonth = time.getDate();

    for (var i=0; i<objForm[pdmDays].length; i++) {
        objForm[pdmDays].options[0] = null;
    }

    for (var i=0; i<daysInMonth; i++) {
        objForm[pdmDays].options[i] = new Option(i+1);
    }

    if (selectedDay <= daysInMonth) {
        objForm[pdmDays].options[selectedDay-1].selected = true;
    } else {
        objForm[pdmDays].options[daysInMonth-1].selected = true;
    }
}

function rowOverEffect(object) {
    if (object.className == 'moduleRow') object.className = 'moduleRowOver';
}

function rowOutEffect(object) {
    if (object.className == 'moduleRowOver') object.className = 'moduleRow';
}

function checkBox(object) {
    document.account_newsletter.elements[object].checked = !document.account_newsletter.elements[object].checked;
}

function popupWindow(url, name, params) {
    window.open(url, name, params).focus();
}


/**** Williams Mendez Functions *****/

function add2Cart(pid){
	
    $('#add_to_cart_everywhere').fadeIn('slow', function() {
        // Animation complete
        });
    
	  
}

function closeCart(){
    $('#add_to_cart_everywhere').fadeOut('slow', function() {
        // Animation complete
        }); 
}

function checkImei(){
    var its_ok = true;
    re = new RegExp(/[0-9\.\-]{15,17}/);
    $.each($('input[name=imei\\[\\]]'),function(i){
        if(this.value.length > 15 && this.value.match(re))
            return true;
        if (!this.value.match(re)) {
            this.style.background = "#fe635b";
            alert("The IMEI is a number between 15-17 digits.  Please, try again.");
            its_ok = false;
        } else {
            this.style.background = "#dce3bd";
        }
    });
    return its_ok;
}

function add2CDialog(pid)  {

    var imei = $("#imei"),
    allFields = $([]).add(imei),
    tips = $("#validateTips");

    function updateTips(t) {
        tips.text(t).effect("highlight",{},1500);
    }

    function checkLength(o,n,min,max) {

        if ( o.val().length > max || o.val().length < min ) {
            o.addClass('ui-state-error');
            updateTips("Length of " + n + " must be between "+min+" and "+max+".");
            return false;
        } else {
            return true;
        }

    }

    function checkRegexp(o,regexp,n) {

        if ( !( regexp.test( o.val() ) ) ) {
            o.addClass('ui-state-error');
            updateTips(n);
            return false;
        } else {
            return true;
        }

    }

    $("#dialog").dialog({
        bgiframe: true,
        autoOpen: false,
        height: 300,
        modal: true,
        buttons: {
            'Buy Now': function() {
                var bValid = true;
                allFields.removeClass('ui-state-error');
                bValid = bValid && checkLength(imei,"IMEI",15,21);
                bValid = bValid && checkRegexp(imei,/[0-9\.\-]{15,17}/,"The IMEI is a number between 15-17 digits.  Please, try again.");
                if (bValid) {
                    $('#buynow').submit();
                }
            },
            Cancel: function() {
                $(this).dialog('close');
            }
        },
        close: function() {
            allFields.val('').removeClass('ui-state-error');
        }
    });

    $.getJSON("/getdata.php?pid="+pid+"&getpkeywords=1",
        function(data){
            $("#img_buynow").attr("src", '/images/products/thumbnails/'+data.image);
            $('#buynow').attr('action','/products.php?'+data.products_keyword+'&action=cart_add');
        });
    $('#dialog').dialog('open');


}
