function setCookie (name, value) {
  var argv = setCookie.arguments;
  var argc = setCookie.arguments.length;
  var expires = (argc > 2) ? argv[2] : null;
  var path = (argc > 3) ? argv[3] : null;
  var domain = (argc > 4) ? argv[4] : null;
  var secure = (argc > 5) ? argv[5] : false;
  document.cookie = name + "=" + escape (value) +
    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
    ((path == null) ? "" : ("; path=" + path)) +
    ((domain == null) ? "" : ("; domain=" + domain)) +
    ((secure == true) ? "; secure" : "");
}

function setQM () {
    var rightNow = new Date();
    var expdate = new Date();
    expdate.setTime (expdate.getTime() + 30*(24 * 60 * 60 * 1000)); // dead in 1 months
    setCookie ("lpt", "qmagic", expdate, "/", "q-magic.com");
    setCookie ("uri", location.search, expdate, "/", "q-magic.com");
}

function setFocus(focusId) {
  try {
 	 document.getElementById(focusId).focus();
  } catch (err) {}
}



String.prototype.trim = function () {
    var str = this.replace(/^\s\s*/, ''),
        ws = /\s/,
        i = str.length;
    while (ws.test(str.charAt(--i)));
    return str.slice(0, i + 1);
};

function testNonEmpty(obj, checkId) {
    obj.value = obj.value.trim();
    if (obj.value == null || obj.value == "") {
        error(checkId);
    } else {
      ok(checkId);
    }
}

var errors = new Object;
function error(checkId) {
   errors[checkId] = true;
   document.getElementById(checkId + "Error").style.display = 'block';
   document.getElementById(checkId + "Checked").style.display = 'none';
}

function ok(checkId) {
   errors[checkId] = false;
   document.getElementById(checkId + "Error").style.display = 'none';
   document.getElementById(checkId + "Checked").style.display = 'block';
}

// see http://www.regular-expressions.info/email.html for details
var emailFilter = new RegExp("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$", "i");
var firstEmail = "";
function checkEmail(email) {
   return email == null ? false : emailFilter.test(email.trim());
}

function testEmail(email, checkId) {
  email.value = email.value.trim();
  if (checkEmail(email.value)) {
     ok(checkId);
  } else {
     error(checkId);
  }
}


// force validators to be called if any
function runBlur(form) {
 for (var i=0; i < form.length; ++i) {
    try {
       var field = form.elements[i];
       field.focus();
       field.blur();
    } catch(err) {}
 }
}

function validateForm(form, requiredFieldsCount) {
    runBlur(form);
    var count = 0;
    for (var entry in errors) {
       ++count;
       if (errors[entry]) {
          alert("Please fix the errors before continuing!");
          return false;
       }
    }
    if (count != requiredFieldsCount) {
       alert("Please provide data in all the required fields before continuing!");
       return false;
    }
    return true;
}