// ksclientutil.js
// Version: "4.0.0"
// Copyright (c) Kore Technologies LLC .  All rights reserved.
<!--
 function submitThisForm(frm) {
  var msg = validateForm(frm);

  if (msg == "") {
   return true;
  } else {
   var stdmsg = "The data in the following fields is either missing or invalid: " + '\n' + '\n';
   alert(stdmsg + msg);
   return false;
  }
 }


 function validateForm(frm) {
  var msg = "";
  var currElementName;
  var currElement;

  for (var i=0; i < frm.elements.length; i++) {

   currElement = frm.elements[i];
   currElementName = new String(currElement.name);

   if (needsValidation(currElementName)) {

    if (isInvalidEntry(getIsReq(currElementName),
           getIsNumReq(currElementName),
           getIsDateReq(currElementName),
           currElement)) {
      msg = msg + getFieldDesc(currElementName) + "," + '\n';
    }
   }
  }
  if (msg == "") {
   return msg;
  } else { return msg.substring(0, msg.length - 2);
  }
 }

 function getFieldDesc(elemName) {
  var firstUnderScore = elemName.indexOf("_");
  var secondUnderScore = elemName.indexOf("_", (firstUnderScore + 1));

  if (secondUnderScore == -1) {
   var firstWord = elemName.substring((firstUnderScore + 1),
elemName.length);
   return firstWord;
  } else {
   var firstWord = elemName.substring((firstUnderScore + 1),
secondUnderScore);
  }

  var thirdUnderScore = elemName.indexOf("_", (secondUnderScore + 1));

  if (thirdUnderScore == -1) {
   var secondWord = elemName.substring((secondUnderScore + 1),
elemName.length);
   return firstWord + " " + secondWord;
  } else {
   var secondWord = elemName.substring((secondUnderScore + 1),
thirdUnderScore);
  }
  var thirdWord = elemName.substring((thirdUnderScore + 1), elemName.length);


  return (firstWord + " " + secondWord + " " + thirdWord);
 }

 function needsValidation(elementName) {
  var firstLetterOfElemName = elementName.substring(0, 1);

  if (firstLetterOfElemName == "v") {
   return true;
  } else { return false;
  }
 }

 function getIsReq(elementName) {
  var secondLetterOfElemName = elementName.substring(1, 2);

  if (secondLetterOfElemName == "t") {
   return true;
  } else { return false;
  } 
 }

 function getIsNumReq(elementName) {
  var thirdLetterOfElemName = elementName.substring(2, 3);

  if (thirdLetterOfElemName == "t") {
   return true;
  } else { return false;
  } 
 }

 function getIsDateReq(elementName) {
  var fourthLetterOfElemName = elementName.substring(3, 4);

  if (fourthLetterOfElemName == "t") {
   return true;
  } else { return false;
  } 
 }

 function isInvalidEntry(req, num, dte, entryTxtBox) {
  var invalidEntry = true;

  if (req == true) {
   if (!checkForNoEntry(entryTxtBox)) {
    //if the user has entered nothing in this field
    //jump out because no other validation is required
    return invalidEntry;
   } else { invalidEntry = false;
   }
  } else {//if req == false
   if (!checkForNoEntry(entryTxtBox)) {
    //if the user has entered nothing in this field
    return false;
   } else {
    //if the user has entered something in this field
    invalidEntry = false;
   }
  }

  if (dte == true) {
   if (!isDate(entryTxtBox)) {
    //if the user has entered an invalid date in this field
    invalidEntry = true;
   } else { invalidEntry = false;
   }
  }

  if (num == true) {
   if (!validateNumericEntry(entryTxtBox)) {
    //if the user has not entered a number in this field
    invalidEntry = true;
   } else { invalidEntry = false;
   }
  } return invalidEntry;
 }

 function checkForNoEntry(textField) {
  if (textField.value == "") {
   return false;
  } else { return true;
  }
 }

 function validateNumericEntry(textField) {
  var strValue = textField.value;

  for (var i = 0; i < strValue.length; i++) {
   if(isNaN(parseInt(strValue.substring(i, i + 1), 10))) {
    //if the current character is not a number then
    //the whole string is not a number so return false jump
    return false;
   }
  } return true;
 }

 function isDate(textField) {
  var myDate = new String(textField.value);
  var delimiterFirstInstance;
  var delimiterSecondInstance;
  var delimiterType;
  var monthPart;
  var dayPart;
  var yearPart;

  //accepts delimiting characters of either "/" or "-"

  //indexOf is similar to the Vb InStr() function
  delimiterFirstInstance = myDate.indexOf("/");
  if (delimiterFirstInstance == -1) {
   //check for the other allowed delimiter
   delimiterFirstInstance = myDate.indexOf("-");
   //if it is still not found, return false
   if (delimiterFirstInstance == -1) {
    return false;
   } delimiterType = "-";
  } else { delimiterType = "/";
  }

  //indexOf is similar to the Vb InStr() function
  delimiterSecondInstance = myDate.indexOf(delimiterType,
(delimiterFirstInstance + 1));
  if (delimiterSecondInstance == -1) {
   return false;
  }


  monthPart = myDate.substring(0, delimiterFirstInstance);
  if(validateMonth(monthPart) == false) {
   return false;
  }
  yearPart = myDate.substring((delimiterSecondInstance + 1),
   (myDate.length));

  if(validateYear(yearPart) == false) {
   return false;
  }

  dayPart = myDate.substring((delimiterFirstInstance + 1),
   (delimiterSecondInstance));
  if(validateDay(monthPart, dayPart, yearPart) == false) {
   return false;
  } else { return true;
  }
 }

 function validateDay(m, d, y) {
  if((isNaN(d)) || d == "") {
   return false;
  }

  var mo = parseInt(m, 10);
  var da = parseInt(d, 10);
  var ye = parseInt(y, 10);

  if (da < 1) {
   return false;
  }

  if ((mo == 4) || (mo == 6) || (mo == 9) || (mo == 11)) {
   //it is a 30 day month
   if (da > 30) {
    return false;
   }
  } else if(mo == 2) {
   // it is february (either 28 or 29 depending on leap year)
   if (UtilityDate.isLeapYear(ye) == true) {
    if (da > 29) {
    //leap years have 29 days in february
     return false;
    }
   } else {
    if (da > 28) {
    //non leap years have 28 days in february
     return false;
    }
   }
  } else {
   // it is a 31 day month
   if (da > 31) {
    return false;
   }
  }
  //if we made it through all of the above without falling out,
  //it must be a valid day for the given month and year
  return true;
 }

 function validateMonth(mnth) {
  if((isNaN(mnth)) || mnth == "") {
   return false;
  }
  var intMonth = parseInt(mnth, 10);
  if((intMonth < 1) || (intMonth > 12)) {
   return false; //month must be between 1 and 12 (inclusive)
  } else { return true;
  }
 }

 function validateYear(yr) {
  if((isNaN(yr)) || yr == "") {
   return false;
  }
  var intYear = parseInt(yr, 10);
  if((intYear < 1970) || (intYear > 9999)) {
   return false; //year must be between 1970 and 9999 (inclusive)
  } else { return true;
  } 
 }

 function isLeapYear(yr) {
   /* classic leap year calculation:
    if the year is:
     evenly divisible by 4 and not evenly divisible by 100
     or
     evenly divisible by 400
    then it is a leap year,
    Otherwise it is not a leap year
   */

     if (((yr % 4 == 0) && (yr % 100 != 0)) || (yr % 400 == 0)) {
         return true;
   } else { return false;
   } 
 }
 
 function hasValue(val)
 {
	if( val != null  && 
		val != 'null' && 
		val != undefined && 
		val != '' && 
		val != 'undefined') {
		return true;
	} else {
		return false;
	}
 }

function Cookie(document, name, hours, path, domain, secure) {
  // any VAR in "this" that does not start with a "$" will
  // be written into the cookie (read from also)
	this.$doc  = document
	this.$name = name

	if (hours)  
		this.$expiration=new Date((new Date()).getTime()+hours*3600000); 
	else 
		this.$expiration = null
	
	if (path)   
		this.$path   = path;
	else 
		this.$path   = null;
		
	if (domain) 
		this.$domain = domain;
	else 
		this.$domain = null;
	
	if (secure) 
		this.$secure = true;
	else 
		this.$secure = false;
}

function CookieWrite() {
  var cookieval=""
  for(var prop in this) {
    if ((prop.charAt(0) == '$') || ((typeof this[prop]) == 'function') || prop == '') continue
	if (cookieval != "") cookieval += '&amp;'
	cookieval+=prop+":"+escape(this[prop])
  }
  var cookie=this.$name+"="+cookieval
  if (this.$expiration) cookie+='; expires=' + this.$expiration.toGMTString()
  if (this.$path)       cookie+='; path='    + this.$path
  if (this.$domain)     cookie+='; domain='  + this.$domain
  if (this.$secure)     cookie+='; secure'
  //alert("writting cookie="+cookie)
  this.$doc.cookie=cookie
}

function CookieRead() {
  var allcookies=this.$doc.cookie
  if (allcookies=="") {
    return false
  }
  var start= allcookies.indexOf(this.$name+'=')
  if (start== -1) {
    return false
  }
  start += this.$name.length+1
  var end=allcookies.indexOf(';',start)
  if (end == -1) end=allcookies.length
  var cookieval = allcookies.substring(start,end)
  var a = cookieval.split('&amp;')
  for (var i=0;i<a.length;i++) a[i]=a[i].split(':')
  for (var i=0;i<a.length;i++) this[a[i][0]]=unescape(a[i][1])
  return true
}

function CookieDelete() {
  var cookie = this.$name+'='
  if (this.$path)   cookie+='; path='+this.$path
  if (this.$domain) cookie+='; domain='+this.$domain
  cookie+='; expires=Fri, 02-Jan-1970 00:00:00 GMT'  // MAKE IT EXPIRE!
  this.$doc.cookie=cookie
}

new Cookie()
Cookie.prototype.write = CookieWrite;
Cookie.prototype.del   = CookieDelete;
Cookie.prototype.read  = CookieRead;

function editAddress(referUrl)
{
	var el = document.getElementById("ship_address");
	if (el==null || !el.value) {
		alert("No address to edit");
		return false;
	}
	var typeEl = document.getElementById("type_address");
	if (typeEl) {
		typeEl.value = "S";
	}
	
	var href = "kommerce_edit_usaddress.aspx?action=edit&addr_no=" + el.value;
	href += "&referUrl="+referUrl;

	window.location = href;
	return false;
}

function editAddressInPopup(referUrl)
{
	var el = document.getElementById("ship_address");
	if (el==null || !el.value) {
		alert("No address to edit");
		return false;
	}
	var typeEl = document.getElementById("type_address");
	if (typeEl) {
		typeEl.value = "S";
	}
	
	var href = "kommerce_edit_usaddress.aspx?action=edit&dsrf=Y&addr_no=" + el.value;
	href += "&referUrl="+referUrl;

	eval("javascript:makeNamedWindow('" + href + "', 'EditAddress', 450, 400)");
	return false;
}


function orderCommentsMethod()
{
	var commentsCookie = new Cookie(document, "checkoutOrderComments", 1);
	commentsCookie.Method = form1.order_comments.value;
	commentsCookie.write();
}

function shipMethod()
{
	var shipCookie = new Cookie(document, "shipCookie", 1);
	shipCookie.Method = form1.ship_method.value;
	shipCookie.write();
}
function purchaseOrder()
{
	var poCookie = new Cookie(document,"purchaseOrder",1);
	poCookie.OrderNo = form1.po_no.value;
	poCookie.write();
}
function billAddress()
{
	var addrCookie = new Cookie(document,"shipAddress",1);
	addrCookie.AddrNo = null;
	addrCookie.write();
}
function pickAddress(form1)
{
	form1.type_address[1].checked = true;		
	var addrCookie = new Cookie(document,"shipAddress",1);
	addrCookie.AddrNo = form1.ship_address.value;
	addrCookie.write();
}
function loadSettings()
{	
/*	var addrCookie = new Cookie(document,"shipAddress",1);
	addrCookie.read();
	var shipAddrNo = addrCookie.AddrNo;
	if (shipAddrNo!=null && shipAddrNo!="null") {
		//alert("ShipAddrNo: " + shipAddrNo);
		form1.type_address[1].checked = true;		
		form1.ship_address.value = addrCookie.AddrNo;
	}
*/
	var poCookie = new Cookie(document,"purchaseOrder",1);
	poCookie.read();
	var poNumber = poCookie.OrderNo;
	if (poNumber!=null && poNumber!="null") {
		//alert("poNumber: " + poNumber);
		form1.po_no.value = poNumber;
	}

	var shipCookie = new Cookie(document, "shipCookie", 1);
	shipCookie.read();
	var shipNumber = shipCookie.Method ;
	if (shipNumber!=null && shipNumber!="null") {
		//alert("shipNumber: " + shipNumber);
		form1.ship_method.value = shipNumber;
	}

	var commentsCookie = new Cookie(document, "checkoutOrderComments", 1);
	commentsCookie.read();
	var commentsNumber = commentsCookie.Method ;
	if (commentsNumber!=null && commentsNumber!="null") {
		//alert("commentsNumber: " + commentsNumber);
		form1.order_comments.value = commentsNumber;
	}
}

// Simulates PHP's date function
Date.prototype.format = function(format) {
var returnStr = '';
var replace = Date.replaceChars;
for (var i = 0; i < format.length; i++) {
var curChar = format.charAt(i);
if (replace[curChar])
returnStr += replace[curChar].call(this);
else
returnStr += curChar;
}
return returnStr;
};
Date.replaceChars = {
shortMonths: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
longMonths: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
shortDays: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
longDays: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'], 
// Day
d: function() { return (this.getDate() < 10 ? '0' : '') + this.getDate(); },
D: function() { return Date.replace.shortDays[this.getDay()]; },
j: function() { return this.getDate(); },
l: function() { return Date.replace.longDays[this.getDay()]; },
N: function() { return this.getDay() + 1; },
S: function() { return (this.getDate() % 10 == 1 && this.getDate() != 11 ? 'st' : (this.getDate() % 10 == 2 && this.getDate() != 12 ? 'nd' : (this.getDate() % 10 == 13 && this.getDate() != 1 ? 'rd' : 'th'))); },
w: function() { return this.getDay(); },
z: function() { return "Not Yet Supported"; },
// Week
W: function() { return "Not Yet Supported"; },
// Month
F: function() { return Date.replace.longMonths[this.getMonth()]; },
m: function() { return (this.getMonth() < 11 ? '0' : '') + (this.getMonth() + 1); },
M: function() { return Date.replace.shortMonths[this.getMonth()]; },
n: function() { return this.getMonth() + 1; },
t: function() { return "Not Yet Supported"; },
// Year
L: function() { return "Not Yet Supported"; },
o: function() { return "Not Supported"; },
Y: function() { return this.getFullYear(); },
y: function() { return ('' + this.getFullYear()).substr(2); },
// Time
a: function() { return this.getHours() < 12 ? 'am' : 'pm'; },
A: function() { return this.getHours() < 12 ? 'AM' : 'PM'; },
B: function() { return "Not Yet Supported"; },
g: function() { return this.getHours() == 0 ? 12 : (this.getHours() > 12 ? this.getHours() - 12 : this.getHours()); },
G: function() { return this.getHours(); },
h: function() { return (this.getHours() < 10 || (12 < this.getHours() < 22) ? '0' : '') + (this.getHours() < 10 ? this.getHours() + 1 : this.getHours() - 12); },
H: function() { return (this.getHours() < 10 ? '0' : '') + this.getHours(); },
i: function() { return (this.getMinutes() < 10 ? '0' : '') + this.getMinutes(); },
s: function() { return (this.getSeconds() < 10 ? '0' : '') + this.getSeconds(); },
// Timezone
e: function() { return "Not Yet Supported"; },
I: function() { return "Not Supported"; },
O: function() { return (this.getTimezoneOffset() < 0 ? '-' : '+') + (this.getTimezoneOffset() / 60 < 10 ? '0' : '') + (this.getTimezoneOffset() / 60) + '00'; },
T: function() { return "Not Yet Supported"; },
Z: function() { return this.getTimezoneOffset() * 60; },
// Full Date/Time
c: function() { return "Not Yet Supported"; },
r: function() { return this.toString(); },
U: function() { return this.getTime() / 1000; }
}