wpcpgenWait(0);

function wpcpgenWait(load) {
  if (typeof jQuery == 'undefined') {
    if(load == 0) {
      var head = document.getElementsByTagName("head")[0];
      script = document.createElement('script');
      script.id = 'jQuery';
      script.type = 'text/javascript';
      script.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js';
      head.appendChild(script);
    }

    setTimeout('wpcpgenWait(1)', 100);
  } else {
    wpcpgenLoad();
  }
}

function wpcpgenLoad() {
  var head = document.getElementsByTagName("head")[0];
  script = document.createElement('script');
  script.type = 'text/javascript';
  script.src  = wpcp_jspath + 'js/jquery.PrintArea.js';
  head.appendChild(script);
}

function wpcpgenOpenCoupon(print, lock, cpid, pw, ph, ptype) {
  var is_locked = wpcpGetCookie('wpcp_' + cpid);

  if(wpcp_isexpired) {
    alert(wpcp_no_coupons);
    return false;
  }

  if(lock == '1' && is_locked) {
    alert(wpcp_already);
    return false;
  }

  if(lock == '1') {
    wpcpSetCookie('wpcp_' + cpid, 1, 5);
  }

  if(print == '1') {
    jQuery('#wpcp-' + ptype + '-' + cpid).printArea({mode: 'popup', popWd: pw, popHt: ph});
  } else {
    return true;
  }
  
  return false;
}

function wpcpGetCookie(c_name) {
  var i,x,y,ARRcookies = document.cookie.split(";");

  for (i=0;i<ARRcookies.length;i++) {
    x = ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
    y = ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
    x = x.replace(/^\s+|\s+$/g,"");

    if (x == c_name) {
      return unescape(y);
    }
  }
}

function wpcpSetCookie(c_name, value, exdays) {
  var exdate = new Date();
  exdate.setDate(exdate.getDate() + exdays);
  var c_value = escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
  document.cookie = c_name + "=" + c_value;
}
