function popupWindow(page, width, height) {
	window.open(page, "_blank", "status=1,resizable=1,scrollbars=0,height="+height+",width="+width);
}

function isEmail (s) {
    var isEmail_re       = /^\s*[\w\-\+_]+(\.[\w\-\+_]+)*\@[\w\-\+_]+\.[\w\-\+_]+(\.[\w\-\+_]+)*\s*$/;
    return String(s).search (isEmail_re) != -1;
 }

function TimeStamp(){
    var dateTime  = new Date();
    var timeStamp = new String(dateTime.getFullYear());
    timeStamp += new String(dateTime.getMonth()) ;
    timeStamp += new String(dateTime.getDay()) ;
    timeStamp += dateTime.getHours()   ;
    timeStamp += dateTime.getMinutes()  ;
    timeStamp += dateTime.getSeconds()  ;
    timeStamp += dateTime.getMilliseconds();
    return timeStamp ;
}


function show_dpi() {

alert ('screen.logicalXDPI: ' + screen.logicalXDPI);
alert ('screen.deviceXDPI: ' + screen.deviceXDPI);

//document.all.dpi.innerText=screen.deviceXDPI;
//if (screen.logicalXDPI==screen.deviceXDPI)
//   document.all.scale.innerText="OFF";
//else
//   document.all.scale.innerText="ON";

}

function removeLastChar(myString) {
	var strLen = myString.length;
	myString = myString.slice(0,strLen-1); 
	return myString;
}

function min() {
if (arguments.length >= 1)
    var min_val = arguments[0];
else
    var min_val = false;
for (var i = 0; i < arguments.length; i++) {
    min_val = Math.min (min_val, arguments[i]);
    }
return (min_val);
}

function max() {
if (arguments.length >= 1)
    var max_val = arguments[0];
else
    var max_val = false;
for (var i = 0; i < arguments.length; i++) {
    max_val = Math.max (max_val, arguments[i]);
    }
return (max_val);
}

String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g,"");
}

String.prototype.ltrim = function() {
return this.replace(/^\s+/,"");
}

String.prototype.rtrim = function() {
return this.replace(/\s+$/,"");
}

function closeErrorBox() {
	var obj = document.getElementById('errorBox');
	if (obj) { obj.style.display = "none"; }
}

function getCheckedValue(radioObj) {	
	var radioLength = radioObj.length;
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return false;
}


function validateEmail(idName, errorMessage) {
	var field =  document.getElementById(idName);
	if(isEmail(field.value)) {
		return true;	
	} else {
		alert(errorMessage);
		field.focus();
		return false;	
	}
}
//checks to see if a field is empty
//idName - String - id of field to be checked
//errorMessage - String - message to display if field is empty
function validateEmpty(idName, errorMessage) {
	var field =  document.getElementById(idName);
	if(field.value == '') {
		alert(errorMessage);
		field.focus();
		return false;
	} else {
		return true;	
	}
}
//checks to see if a selection has been made in a comboBox
//idName - String - id of field to be checked
//errorMessage - String - message to display if field is empty
function validateComboBox(idName, errorMessage) {
	var comboBox = document.getElementById(idName);
	if(comboBox.value == '' || comboBox.selectedIndex ==0 || comboBox.value ==0) {
		alert(errorMessage);
		comboBox.focus();
		return false;
	} else {
		return true;	
	}
}


//checks to see a multi field entry has been completely filled out
//idArray - Array - Array of Strings containing form elements to be checked as one
//errorMessage - String - message to display if field is empty
function validateMultiField(idArray, errorMessage) {
	for(i=0; i<idArray.length; i++) {
		var field = document.getElementById(idArray[i]);
		if(field.value == '' || isNaN(field.value)) {
			alert(errorMessage);
			field.focus();
			return false;
		} else {
			return true;	
		}
	}
}

function validateZip(idName, errorMessage) {
	var field =  document.getElementById(idName);
		if(field.value == '' || field.value.length != 5) {
			alert(errorMessage);
			field.focus();
			return false;
		}	
}

function validateExtension(idName, errorMessage) {
	var field =  document.getElementById(idName);
	if(field.value != "" && field.value != null) {
		if(isNaN(field.value)) {
			alert(errorMessage);
			field.focus();
			return false;
		}
	}
}


function removeAddress(answer, recordId, o) {
	if(answer) {
			window.location="/company_locations.php?remove="+recordId+"&o="+o;	
		} else {
			return false;	
		}
}

function confirmDelete(recordId, totalAddresses, totalUnredeemed)
{
	// if only location is being deleted with unredeemed coupons
	if (totalAddresses === 1 && totalUnredeemed > 0) {
		var answer = confirm('Customers have purchased coupons for this location that must be honored. If you cannot honor them for any reason, notify us immediately. 	\n \n By removing this location all coupon offers from this location will be deleted.  Are you sure you want to delete this location?');
		return removeAddress(answer, recordId, totalUnredeemed);

	// if only location is being deleted with no outstanding coupons	
	} else if (totalAddresses === 1 && totalUnredeemed <= 0) {
		var answer = confirm('By removing this location all coupon offers from this location will be deleted.  Are you sure you want to delete this location?');
		return removeAddress(answer, recordId, totalUnredeemed);
	}
	
	//if there are unredeemed coupons for a location
	else if(totalAddresses > 1 || totalAddresses === 0 && totalUnredeemed > 0) {
		var answer = confirm('Customers have purchased coupons for this location that must be honored.  If you cannot honor them for any reason, notify us immediately.');
		return removeAddress(answer, recordId, totalUnredeemed);
	}

	//if there are no unredeemed coupons and more than one address remains
	else if(totalAddresses > 1 || totalAddresses === 0 && totalUnredeemed <= 0) {
		var answer = confirm('Are you sure you want to remove this address?');
		return removeAddress(answer, recordId, totalUnredeemed);
	}
}

function addressCheckBoxes(tableId) {
	var theTable = document.getElementById(tableId);
	var checkBoxes = theTable.getElementsByTagName('input');
	//alert(checkBoxes.length);
	for(var i=0; i<checkBoxes.length; i++) {
		checkBoxes[i].onclick = function() {
			addressCheckBoxes(tableId);
		}
		if(checkBoxes[i].checked == true) {	
			checkBoxes[i].parentNode.className = 'checked';
		} else {
			checkBoxes[i].parentNode.className = '';
		}	
	}
}

// compares 2 arrays and returns an array of the differences
diff = function(v, c, m){
    var d = [], e = -1, h, i, j, k;
    for(i = c.length, k = v.length; i--;){
        for(j = k; j && (h = c[i] !== v[--j]););
        h && (d[++e] = m ? i : c[i]);
    }
    return d;
};


//converts an array to an object so a for in loop can be used
//a ARRAY
function oc(a)
{
	var o = {};
	for(var i=0;i<a.length;i++)
	{	o[a[i]]='';
	}
	return o;
}

function stripDollarSigns(theString) {
	theString = theString.replace(/^\$|,/g,'');
	return theString;
}

//---------------------------------------------------------createCoupons
function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
		if(isNaN(num))
			num = "0";
			sign = (num == (num = Math.abs(num)));	
			num = Math.floor(num*100+0.50000000001);
			cents = num%100;
			num = Math.floor(num/100).toString();
			if(cents<10)
				cents = "0" + cents;
				for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
					num = num.substring(0,num.length-(4*i+3))+','+
					num.substring(num.length-(4*i+3));
					return (((sign)?'':'-') + '$' + num + '.' + cents);
}

//---------------------------------------------------------view List
function showPurchasedList(promoid, desc) {
	var Url = 'purchased_list.php?PromoId=' + promoid + '&PromoDesc=' + encodeURIComponent(desc);
	window.open(Url, "_blank", "status=1,resizable=1,scrollbars=1,height=900,width=900");
}


//---------------------------------------------------------void Coupons
function updateStatus(status, id, serial) {
	var validNo = serial.substr(22, 3);
	var userInput = document.getElementById('id_txt_void_'+id).value;
	if(validNo != userInput) {
		alert('Your verification numbers do not match.')
		return false;
	} else {
		document.getElementById('void').value=status;
		document.getElementById('id').value=id;
		document.getElementById('serial').value=serial;
		document.getElementById('redeemForm').submit();
	}
}

//---------------------------------------------------------INDEX
function searchState() {
	var selectBox = document.getElementById('HomeStateId');
	window.location=selectBox.options[selectBox.selectedIndex].value;
}


//---------------------------------------------------------how it works
function CheckZip(_ZipCode)
{
	var ZipCode = _ZipCode.value;
	var ZipCodeLength = ZipCode.length;
	if (ZipCodeLength == 0) {
		alert ("Please enter zipcode.");
		return false;
	}
	if (ZipCodeLength != 5 || isNaN(ZipCode)) {
		alert ("Please enter a valid 5 digit zipcode.");
		return false;
	}
	for (var j = 0; j < String(ZipCode).length; j++) {
		if ((String(ZipCode).charAt(j) < "0") || (String(ZipCode).charAt(j) > "9")) {
			alert ("Please enter a valid zipcode.");
			return false;
		}
	}
	document.frmEnterZip.submit ();
}


function zipSearch() {
	var zipbox =  document.getElementById('zipSearch');
	var message = "Search by Zipcode";
	zipbox.value=message;
	
	if(zipbox.value == message) {
		zipbox.style.color="#999";
	}
	zipbox.onclick = function() {
		if(zipbox.value==message) {
			zipbox.value = "";
			zipbox.style.color="#000";
		}
	}
	zipbox.onblur = function() {
		if(zipbox.value=="") {
			zipbox.value=message;
			zipbox.style.color="#999";
		}
	}
}

//----------------------------------------account_activity

function deletePromotion(promoId) {
	var answer = confirm('Reminder: If coupons have already been purchased for this offer, you must still honor them. Deleting the coupon offer will prevent any further purchases of this coupon.');
	if(answer) {
		window.location="/account_activity.php?a=r&p="+promoId;	
	} else {
		return false;	
	}
}


function togglePromotion(promoId, cur_state) {
	if (cur_state.charAt(0) == 'A')
	{	var answer = confirm('Reminder: If coupons have already been purchased for this offer, you must still honor them. Disabling the promtion will prevent any further purchases of this coupon until it is re-enabled.');
		if(answer) { window.location="/account_activity.php?a=d&p="+promoId; }
		else { return false; }
	}
	else
	{	window.location="/account_activity.php?a=e&p="+promoId;
	}
}


function encodeURL(stringSet) {
	if(is_array(stringSet)) {
		for(i=0; i<stringSet.length; i++){
			stringSet[i] = encodeURIComponent(stringSet[i]);
		}
		return stringSet;
	} else if(is_string(stringSet)) {
		return(encodeURIComponent(stringSet));
	}
	else {
		return stringSet;
	}
}

function decodeURL(stringSet) {
	if(is_array(stringSet)) {
		for(i=0; i<stringSet.length; i++){
			stringSet[i] = decodeURIComponent(stringSet[i]);
		}
		return stringSet;
	} else if(is_string(stringSet)) {
		return(decodeURIComponent(stringSet));
	}
	else {
		return stringSet;
	}
}

function is_string(input){
	return typeof(input)=='string';
}

function is_array(input){
	return typeof(input)=='array';
}



