/***********************************************/
/* GENERAL.JS.PHP : GENERIC JS FUNCTIONS */
/* COPYRIGHT(C) 2009-2010 SPINNAKER360 PTE LTD */
/***********************************************/
/***** CUSTOM TOOL TIPS *****/
function initToolTip() {
// initToolTip() : initialize tool tips
var ct = new Tips($$('.tooltip'), {
onShow: function(){
this.tip.setStyles({
visibility:'visible',
display:'block'
});
this.tip.set("class", "openTip");
},
onHide: function(){
this.tip.setStyles({
visibility:'hidden',
display:'none'
});
}
});
}
function closeTip() {
// closeTip() : force close open tooltips
$$('.openTip').setStyles({
visibility:'hidden',
display:'none'
});
}
/***** LOGIN / LOGOUT *****/
function login(){
// login() : processes login
// (1) Check form
var userEl = $('login_email'),
passwordEl = $('login_password'),
agreeEl = $('loginAgree'),
user = userEl.value,
password = passwordEl.value,
focusEl,
errmsg = "";
if (user == "") {
userEl.addClass("inputErr");
errmsg += "Please enter your email.\n";
focusEl = userEl;
} else {
userEl.removeClass("inputErr");
}
if (password == "") {
passwordEl.addClass("inputErr");
errmsg += "Please enter your password.\n";
focusEl = focusEl || passwordEl; // give focus to userEl if invalid; otherwise give focus to passwordEl
} else {
passwordEl.removeClass("inputErr");
}
if (agreeEl.checked == false) {
errmsg += "You must agree to the Terms of Use.";
focusEl = focusEl || agreeEl;
}
// focus appropriate field if errors exist
if (focusEl) {
focusEl.focus();
}
if (errmsg) { alert(errmsg); }
else {
var data = {
"action" : "login",
"user" : user,
"pass" : password
};
var req = new Request({
url: 'ajax_user.php', async: false,
onRequest: function() {
$('loginProcessButton').addClass('spinner').disabled = true;
},
onSuccess: function(responseText) {
if (responseText=="OK") {
location.href = "index.php";
} else {
alert(responseText);
$('loginProcessButton').removeClass('spinner').disabled = false;
}
}
}).post(data);
}
}
function logoff(){
// logoff() : process logoff
var data = { "action" : "logoff" };
var req = new Request({
url: 'ajax_user.php', async: false,
onRequest: function() {
$('logoffButton').addClass('spinner').set('html', '').disabled = true;
},
onSuccess: function(responseText) {
if (responseText == "OK") {
location.href = "index.php";
} else {
$('logoffButton').removeClass('spinner').set('html', '[Logoff]').disabled = false;
alert(responseText);
}
}
}).post(data);
}
/***** POSTAL CODES *****/
function getAddress(postcode){
// getAddress() : get address for postal code
// PARAM postcode : postal code
if(postcode.length==6 && !isNaN(postcode)){
var data = {
"action" : "getAddr",
"pc" : postcode
};
var req = new Request({
url: 'ajax_postcode.php', async: false,
onRequest: function() {
$('get_address_status').set("html", "
Loading...");
},
onSuccess: function(responseText){
if(responseText==""){ $('get_address_status').set("html", "
Invalid postal code"); }
else { $('get_address_status').set("html", ""); }
$('address').value = responseText;
}
}).post(data);
}
}
function checkService(){
// checkService() : checks for service availability
var pc = $('servicePC').value;
if (pc.length==6 && isFinite(pc)) {
var data = {
"action" : "checkService",
"pc" : pc
};
popup.setMode("ajax", "ajax_service.php", data);
popup.show();
} else { alert("Please enter a valid postal code."); }
}
/***** PREVIOUS ORDERS *****/
function showPreviousOrder(orderID, prompt){
// showPreviousOrder() : show use previous order popup
// PARAM orderID : order ID
// prompt : is this a prompt from the catalog page? (for flag purpose, will only show once)
var data = {
"action" : "showPreviousOrder",
"orderID" : orderID
};
if(prompt){
$extend(data, { "prompt" : 1 });
}
popup.onShow = function(){
var productDetailsToggle = popup.contents.getElements('span[class=cartShowItemDetails]');
if(productDetailsToggle!==undefined && productDetailsToggle!==null && productDetailsToggle!=""){
var productDetails = popup.contents.getElements('div[class=cartItemDetails]');
productDetails.each(function(el, index){
var slide = new Fx.Slide(el).hide();
productDetailsToggle[index].addEvent('click', function(){
if(slide.open){ this.set("html", "[Show Details]"); }
else{ this.set("html", "[Hide Details]"); }
slide.toggle();
});
});
}
};
popup.setMode("ajax", "ajax_order.php", data);
popup.show();
}
function checkPreviousOrder(orderID){
// checkPreviousOrder() : check selected previous order if it is "safe" to add to cart
// PARAM orderID : order ID
var data = {
"action" : "checkPreviousOrder",
"orderID" : orderID
};
var req = new Request({
url: 'ajax_order.php', async: false,
onSuccess: function(responseText){
// Previous order is "clean"
if(responseText=="OK"){
usePreviousOrder(orderID, false);
}
// There are items that cannot be added to cart
else{
popup.onShow = function(){
var productDetailsToggle = popup.contents.getElements('span[class=cartShowItemDetails]');
if(productDetailsToggle!==undefined && productDetailsToggle!==null && productDetailsToggle!=""){
var productDetails = popup.contents.getElements('div[class=cartItemDetails]');
productDetails.each(function(el, index){
var slide = new Fx.Slide(el).hide();
productDetailsToggle[index].addEvent('click', function(){
if(slide.open){ this.set("html", "[Show Details]"); }
else{ this.set("html", "[Hide Details]"); }
slide.toggle();
});
});
}
};
popup.setMode("text", responseText);
popup.show();
}
}
}).post(data);
}
function usePreviousOrder(orderID, filter){
// usePreviousOrder() : uses a previous order to popuplate the cart
// PARAM orderID : order ID
// filter : filter the order? (kill hot items)
var data = {
"action" : "usePreviousOrder",
"orderID" : orderID
};
if(filter){
$extend(data, { "filter" : 1 });
}
var req = new Request({
url: 'ajax_order.php', async: false,
onSuccess: function(responseText) {
if($defined($('menuContentsR'))){
var box = "
| " + "" + " |
Synchronizing...");
},
onSuccess: function(response){
currentTime = JSON.decode(response);
currentTime['sec'] = parseInt(currentTime['sec']);
currentTime['min'] = parseInt(currentTime['min']);
currentTime['hr'] = parseInt(currentTime['hr']);
currentTime['day'] = parseInt(currentTime['day']);
currentTime['mth'] = parseInt(currentTime['mth']);
currentTime['yr'] = parseInt(currentTime['yr']);
clientTimeA = new Date();
timeKeeper = pendulum.periodical(1000);
}
}).post(data);
}
function pendulum(){
// pendulum() : keeper of time.
// (0) CALCULATE CLIENT CLOCK OFFSET
// IF DIFFERENCE BETWEEN OLD TIMESTAMP AND CURRENT TIMESTAMP >= 2000 MS, OFFSET ACCORDINGLY
clientTimeB = new Date();
var diff = parseInt((clientTimeB - clientTimeA) / 1000);
// MOD HERE : IMPLEMENT MORE CHECKS HERE IF REQUIRED - IF CLIENT MACHINE GOES BACK IN TIME...
// NOTE : CHECKS ARE DONE ON THE SERVER END EITHER WAY...
clientTimeA = clientTimeB;
// (1A) USE CLIENT CLOCK OFFSET
if(diff>=2){
var diffSec = diff%60;
var diffMin = parseInt(diff/60);
// PAUSING FOR ENTIRE HOURS?
var diffHr = parseInt(diffMin/60);
// DAYS!??!
var diffDay = parseInt(diffHr/24);
// MONTHS!!@#!@#
var diffMth = parseInt(diffDay/29);
currentTime['sec']+=diffSec;
currentTime['min']+=diffMin;
currentTime['hr']+=diffHr;
currentTime['day']+=diffDay;
currentTime['mth']+=diffMth;
}
// (1B) NORMAL 'INCREMENT'
else { currentTime['sec']++; }
if(currentTime['sec']>=60){ currentTime['min']++; currentTime['sec']-=60; }
if(currentTime['min']>=60){ currentTime['hr']++; currentTime['min']-=60; }
if(currentTime['hr']>=24){ currentTime['hr']-=24; currentTime['day']++; }
if(currentTime['day']>29){ currentTime['day']-=29; currentTime['mth']++; }
if(currentTime['mth']>12){ currentTime['mth']-=12; currentTime['yr']++; }
// (2) TIME BOMB
var diff = ((timeBomb - Date.parse(getTimeStamp())) / 1000);
// (3) SET THE TIME REMAINING
if(diff>0){
$('fulfillExpire').set("html", "Please complete your order in