
$(document).ready(function(){
			$(".fade").css('display', 'none').fadeIn(1500); 
			$("#cat").selectbox(); // search cats
});
		
$.fn.toggleSection = function(option){ // <a href="javascript:;" onclick="toggleSection('#boxId');">toggle</a>
		box = option;
		$(this).blur();
		if($(box).css('display')=='block') 
			$(box).slideUp();
		else $(box).slideDown();
		$(this).toggleClass('on');
}

// JavaScript Document
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 entityDecode(s) {
	var e = document.createElement("div");
	e.innerHTML = s;
	return e.firstChild.nodeValue;
}

document.getElementsByClassName = function(cl) {
	var retnode = [];
	var myclass = new RegExp('\\b'+cl+'\\b');
	var elem = this.getElementsByTagName('*');
	for (var i = 0; i < elem.length; i++) {
		var classes = elem[i].className;
		if (myclass.test(classes)) retnode.push(elem[i]);
	}
	return retnode;
};

function switchNodeStyle(node){
	if(node.className=="expanded") node.className="";
	else node.className="expanded";
}


function showMerchantPopup(id){

	window.open('merchant.popup.php?merchantID='+id,'merhcantPopup','width=550,height=300');

}
function showTOCPopup(){

	window.open('terms-and-conditions.html','TOCPopup','width=550,height=600,scrollbars=yes');

}
function popupCalc(url, w ,h){
	window.open(url,"popup","width="+w+",height="+h+",resizable,scrollbars=no,status=1");
}

function popupWindow(url, w ,h){
	window.open(url,"popup","width="+w+",height="+h+",resizable,scrollbars=yes,status=1");
}

/*** control comments (for partner) ****/
function deleteComment(id){
	callback='hideComment';
	xmlhttpGet('ajax.partner.del_comment.php', 'id='+id, callback, null);
}

function hideComment(response){
	//alert(response);
	var id = response;
	if(id > 0) document.getElementById('comment-'+id).className="hidden";
}

function ratingaction (num, action)
{
	var base_url=document.getElementsByTagName('base')[0].href;

	var input = document.getElementById ('rating');
	if (action == 'click')
	{
		input.value = num;
		for (i=1; i<=num; i++)
		{
			var star = document.getElementById ('your-rating-' + i);
			star.style.backgroundImage = 'url('+base_url+'images/star-yellow.gif)';
		}
	}

	if ((action == 'show') && (num > input.value))
	{
		for (i=1; i<=num; i++)
		{
			var star = document.getElementById ('your-rating-' + i);
			star.style.backgroundImage = 'url('+base_url+'images/star-yellow.gif)';
		}
	}

	if ((action == 'hide') && (num > input.value))
	{
		var star = document.getElementById ('your-rating-' + num);
		star.style.backgroundImage =
		'url('+base_url+'images/star-gray.gif)';
	}

	if (action == 'hideall')
	{
		for (i=5; i>input.value; i--)
		{
			var star = document.getElementById ('your-rating-' + i);
			star.style.backgroundImage =
			'url('+base_url+'images/star-gray.gif)';
		}
	}
}



/*** registration page ***/

function pass_test(p) {
	var intScore = 0;

	// PASSWORD LENGTH
	intScore += p.length;

	if(p.length > 0 && p.length <= 4) {                    // length 4 or less
		intScore += p.length;
	}
	else if (p.length >= 5 && p.length <= 7) {	// length between 5 and 7
		intScore += 6;
	}
	else if (p.length >= 8 && p.length <= 15) {	// length between 8 and 15
		intScore += 12;
	}
	else if (p.length >= 16) {               // length 16 or more
		intScore += 18;
	}

	// LETTERS (Not exactly implemented as dictacted above because of my limited understanding of Regex)
	if (p.match(/[a-z]/)) {              // [verified] at least one lower case letter
		intScore += 1;
	}
	if (p.match(/[A-Z]/)) {              // [verified] at least one upper case letter
		intScore += 5;
	}
	// NUMBERS
	if (p.match(/\d/)) {             	// [verified] at least one number
		intScore += 5;
	}
	if (p.match(/.*\d.*\d.*\d/)) {            // [verified] at least three numbers
		intScore += 5;
	}

	// SPECIAL CHAR
	if (p.match(/[!,@,#,$,%,^,&,*,?,_,~]/)) {           // [verified] at least one special character
	intScore += 5;
	}
	// [verified] at least two special characters
	if (p.match(/.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~]/)) {
	intScore += 5;
	}
	
	// COMBOS
	if (p.match(/(?=.*[a-z])(?=.*[A-Z])/)) {        // [verified] both upper and lower case
		intScore += 2;
	}
	if (p.match(/(?=.*\d)(?=.*[a-z])(?=.*[A-Z])/)) { // [verified] both letters and numbers
		intScore += 2;
	}
	// [verified] letters, numbers, and special characters
	if (p.match(/(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!,@,#,$,%,^,&,*,?,_,~])/)) {
		intScore += 2;
	}
	
	return intScore;
}
function passStrong(val){
	document.getElementById('divStrong').style.width = pass_test(val)*3+'px';
}


function checkUsernameForLength(whatYouTyped) {
	var fieldset = whatYouTyped.parentNode;
	var txt = whatYouTyped.value;
	if (txt.length > 0) {
		fieldset.className = "welldone";
	}
	else {
		fieldset.className = "";
	}
}
function checkNotEmpty(whatYouTyped) {
	var fieldset = whatYouTyped.parentNode;
	var txt = whatYouTyped.value;
	if (txt.length) {
		fieldset.className = "welldone";
	}
	else {
		fieldset.className = "";
	}
}

function checkGender(whatYouTyped) {
	var fieldset = whatYouTyped.parentNode;
	var val = whatYouTyped.value;
	if (val != 0) {
		fieldset.className = "welldone";
	}
	else {
		fieldset.className = "";
	}
}

function checkBirth(whatYouTyped) {
	var fieldset = whatYouTyped.parentNode;
	var val = whatYouTyped.value;
	if (val <= 2010 && val >= 1900) {
		fieldset.className = "welldone";
	}
	else {
		fieldset.className = "";
	}
	if (val=='')  fieldset.className += 'withHint';
	else fieldset.className.replace('withHint','');
}



function checkEmail(whatYouTyped) {
	var fieldset = whatYouTyped.parentNode;
	var txt = whatYouTyped.value;
	if (/^[A-Za-z0-9\-\.\_]{1,}\@([A-Za-z0-9\-\.\_]{1,}\.){1,}[a-z]{2,}$/.test(txt)) {
		fieldset.className = "welldone";
	} else {
		fieldset.className = "";
	}
}

function checkPassword(whatYouTyped) {
	var fieldset = whatYouTyped.parentNode;
	var txt = whatYouTyped.value;
	if (txt.length > 4) {
		fieldset.className = "welldone";
	} else {
		fieldset.className = "";
	}
}

function checkPasswordIdent(whatYouTyped, pass) {
	var fieldset = whatYouTyped.parentNode;
	var txt = whatYouTyped.value;
	if (txt == pass.value){
		fieldset.className = "welldone";
	} else {
		fieldset.className = "";
	}
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function prepareInputsForHints(){
	var inputs = document.getElementsByTagName("input");
	for (var i=0; i<inputs.length; i++){
		inputs[i].onfocus = function () {
			$(this).parent().children("span").css('display','inline');
		}
		inputs[i].onblur = function () {
			$(this).parent().children("span").css('display','none');
		}

	}
	$('SELECT').focus(function() {$(this).parent().children("span").css('display','inline'); });
	$('SELECT').blur(function()  {$(this).parent().children("span").css('display','none'); });
}
/*** / registration page ***/


function generatePDF(){
	var currentTime = new Date();
	var m = currentTime.getMonth()+1;
	var d = currentTime.getDate();
	var y = currentTime.getFullYear();
	$('p.datePDF').remove();
	$('#header').append('<p class="datePDF">'+m+"/"+d+"/"+y+'</p>');

	$.post('pdf_generator.php', {content: $('html').html() },
	function(data){
		location.href=data;
	});
}


// *** helper
		  $.fn.helperIn = function(text){
				if ( $(this).val() == text ){
					$(this).removeClass('hint').val('');
				}
			}
			$.fn.helperOut = function(text){
				if ( $(this).val() == '' || $(this).val() == text ){
					$(this).addClass('hint').val(text);
				}
			}
			$.fn.helperCreate = function(text){
				$(this).focus(function(){ $(this).helperIn(text); } ).blur(function (){ $(this).helperOut(text); } ).helperDestroy(text);
			}
			$.fn.helperDestroy = function(text){
				if ( $(this).val() == '' || $(this).val() == text ){
					$(this).addClass('hint').val(text);
				}
			}
/* Example
$(document).ready(function(){$('input[@name=fname]').helperCreate('first name');});
*/


/************************************* dev-tools ****************************************************/

function objDump (obj, except) {
	return _objDump (obj, [obj], 0);
}
function inArray (arr, elt){
	for (var i = 0; i < arr.length; i++) if (elt == arr[i]) return i;
	return false;
}
function _objDump (obj, used, level) {
	var rez = '';
	var spaces = " ";
	for (var i = 0; i < level; i++) spaces += " ";
	rez += spaces+('* Dumping object: ' + obj.toString () + "\n");
	for (var i in obj) {
		if (used && inArray(used ,obj[i]) !== false) {
			rez += spaces+"->"+"!Self Reference"+"\n";
			continue;
		}
		rez += (spaces+"  ->" + i + ': ');
		if ( (typeof obj[i]) == "object"){
			try{
				rez += _objDump (obj[i], used.slice(0).push(obj[i]), 1 +level) +"\n";
			}catch (eee) {rez += spaces+"->"+"!Error"+"\n";}
		}else rez +=(spaces+"->"+obj[i] + "\n");
	}
	return rez;
}

function alert2(what){
	alert(objDump(what));
}

function alert3 (what){
	if (extw == null) var extw = window.open("", "","outerWidth=600,width=500,innerWidth=400,resizable,scrollbars,status");
	extw.document.write ('<b>'+new Date()+'</b> => <pre>'+objDump(what)+'</pre><br />');
}

function a(val){
	alert(val);
}
function a2(val){
	alert2(val);
}
function a3(val){
	alert3(val);
}
function c(val){
	if (navigator.appName=="Netscape") console.log(val);
}

// ------------------------- / DEBUG ALERT --------------------
// Convert and rgb value to hex
function toHex(N) {
        if (N==null) return "00";
        N=parseInt(N);
        if (N==0 || isNaN(N)) return "00";
        N=Math.max(0,N); N=Math.min(N,255);
        N=Math.round(N);
        return "0123456789ABCDEF".charAt((N-N%16)/16) + "0123456789ABCDEF".charAt(N%16);
}