﻿// JavaScript Document
function extractNumber(obj, decimalPlaces, allowNegative){ /* for add ad (price) */
	
	re=/(,)/;	
	obj.value = obj.value.replace(re,".");
	
	var temp = obj.value;
	
	// avoid changing things if already formatted correctly
	var reg0Str = '[0-9]*';
	if (decimalPlaces > 0) {
		reg0Str += '\\.?[0-9]{0,' + decimalPlaces + '}';
	} else if (decimalPlaces < 0) {
		reg0Str += '\\.?[0-9]*';
	}
	reg0Str = allowNegative ? '^-?' + reg0Str : '^' + reg0Str;
	reg0Str = reg0Str + '$';
	var reg0 = new RegExp(reg0Str);
	if (reg0.test(temp)) return true;

	// first replace all non numbers
	var reg1Str = '[^0-9' + (decimalPlaces != 0 ? '.' : '') + (allowNegative ? '-' : '') + ']';
	var reg1 = new RegExp(reg1Str, 'g');
	temp = temp.replace(reg1, '');

	if (allowNegative) {
		// replace extra negative
		var hasNegative = temp.length > 0 && temp.charAt(0) == '-';
		var reg2 = /-/g;
		temp = temp.replace(reg2, '');
		if (hasNegative) temp = '-' + temp;
	}
	
	if (decimalPlaces != 0) {
		var reg3 = /\./g;
		var reg3Array = reg3.exec(temp);
		if (reg3Array != null) {
			// keep only first occurrence of .
			//  and the number of places specified by decimalPlaces or the entire string if decimalPlaces < 0
			var reg3Right = temp.substring(reg3Array.index + reg3Array[0].length);
			reg3Right = reg3Right.replace(reg3, '');
			reg3Right = decimalPlaces > 0 ? reg3Right.substring(0, decimalPlaces) : reg3Right;
			temp = temp.substring(0,reg3Array.index) + '.' + reg3Right;
		}
	}
	
	obj.value = temp;
}
function stringCounter(field,cntfield,maxlimit){
	if (field.value.length > maxlimit){
		field.value = field.value.substring(0, maxlimit);
	}
	else{
		cntfield.value = maxlimit - field.value.length;
	}
}

// jquery ui
$(document).ready(function(){
	$('.uiButton').livequery(function() {
		$(this).button();
	});
	$(".adEdit" ).button({
		icons:{	primary: "ui-icon-pencil" },
		text: false
	});	
	$(".adDelete" ).button({
		icons:{	primary: "ui-icon-trash" },
		text: false
	});	
	$(".adExpire" ).button({
		icons:{	primary: "ui-icon-calendar" },
		text: false
	});	
	$(".uiButtonDisabled").button({
		disabled : true
	});	
});

// inquiry
function vote(tmplPath,question,answer,hostId,myCookie){
	$(".vote-result").load("/"+tmplPath+"/vote.php?question="+question+"&answer="+answer+"&host_id="+hostId+"&cookie="+myCookie+"", function(){
	});
}


function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-+\s]+")|([\w-+]+(?:\.[\w-+]+)*)|("[\w-+\s]+")([\w-+]+(?:\.[\w-+]+)*))(@((?:[\w-+]+\.)*\w[\w-+]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
};


