var sPath = window.location.pathname;
var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
var mPath = 'http://www.creathead.it/italian/';

//Helper Functions
	
function array2json(arr) {
    var parts = [];
    var is_list = (Object.prototype.toString.apply(arr) === '[object Array]');

    for(var key in arr) {
    	var value = arr[key];
        if(typeof value == "object") { //Custom handling for arrays
            if(is_list) parts.push(array2json(value)); /* :RECURSION: */
            else parts[key] = array2json(value); /* :RECURSION: */
        } else {
            var str = "";
            if(!is_list) str = '"' + key + '":';

            //Custom handling for multiple data types
            if(typeof value == "number") str += value; //Numbers
            else if(value === false) str += 'false'; //The booleans
            else if(value === true) str += 'true';
            else str += '"' + value + '"'; //All other things
            // :TODO: Is there any more datatype we should be in the lookout for? (Functions?)

            parts.push(str);
        }
    }
    var json = parts.join(",");
    
    if(is_list) return '[' + json + ']';//Return numerical JSON
    return '{' + json + '}';//Return associative JSON
}
	
function addslashes(str) {
	str=str.replace(/\\/g,'\\\\');
	str=str.replace(/\'/g,'\\\'');
	str=str.replace(/\"/g,'\\"');
	str=str.replace(/\0/g,'\\0');
	return str;
}

function stripslashes(str) {
	str=str.replace(/\\'/g,'\'');
	str=str.replace(/\\"/g,'"');
	str=str.replace(/\\0/g,'\0');
	str=str.replace(/\\\\/g,'\\');
	return str;
}

function isEmpty(obj) {
	for(var prop in obj) {
    	if(obj.hasOwnProperty(prop))
 	   		return false;
  	}
  	return true;
}

function randomXToY(minVal,maxVal,floatVal){
	var randVal = minVal+(Math.random()*(maxVal-minVal));
	return typeof floatVal=='undefined'?Math.round(randVal):randVal.toFixed(floatVal);
}

function strip(html){
    var matchTag = /<(?:.|\s)*?>/g;
    return html.replace(matchTag, "");
}
function toggleInfo(divId){
	obj = document.getElementById(divId);

	if(obj.style.display == 'none')
		obj.style.display = 'block';
	else
		obj.style.display = 'none';
	
}	


//jQuery Extend

$.wait = function( callback, seconds){
	return window.setTimeout( callback, seconds * 1000 );
}


$.refresh = function( callback, seconds) {
	return window.setInterval( callback, seconds * 1000);
}
///////////////////////////////////////////////////REGISTRAZIONE

function loadAvatar(iframe, formvalue, fieldvalue){

	document.iscrizione.c25.value = window.frames[iframe].document.forms[formvalue].elements[fieldvalue].value;

	if (document.iscrizione.c25.value == 'no-avatar.jpg')
		document.getElementById(iframe).height = 60;
	else if(document.iscrizione.c25.value == '' && document.iscrizione.c25.value != 'no-avatar.jpg')
		document.getElementById(iframe).height = 180;
	else
		document.getElementById(iframe).height = 60;
}

function loadSUBCAT(id){
	
	if(id){	$('li#subcat').show();	}else{		$('li#subcat').hide();	}
	
	$('#c339').attr('value', '');
	
	$('li#subcat').empty().end();
	
	$.getJSON(mPath+'ajaxrequest.php', {request: 'subcat', val: id, async:false }, function(data){
				$.each(data, function(i,item){
					$('li#subcat').append('<span><input type="checkbox" onChange="addSUBCAT()" name="c339b" id="c339b" checked="checked" value="'+item["id"]+'" /><label>'+item["val"]+'</label></span>');
				});
				addSUBCAT();
		});
		
	
}

function addSUBCAT(){
	var cbvalue = '';
	$('li#subcat').contents().find('input:checked').each(function(i,item){
			 cbvalue += '-'+item.value;
    });
	
	$('#c339').attr('value', cbvalue);
	console.log(cbvalue);
	
	if(cbvalue){ 				$('#c338').attr('class', 'required subcat valid'); 				$('label.error[for=c338]').hide();		}
	else{							$('#c338').attr('class', 'required subcat error');				$('label.error[for=c338]').show();		}	
	
}

function ChangeDays(){

	var day = $('#c6').val();
	var month = $('#c7').val();
	var year = $('#c8').val();
	
	var DaysInMonth = 31;
	if (month == 04 || month == 06 || month == 09 || month == 11){	DaysInMonth = 30;	}
	if (month == 02 && (year/4)!=Math.floor(year/4)){				DaysInMonth = 28;	}
	if (month == 02 && (year/4)==Math.floor(year/4)){				DaysInMonth = 29;	}
	
	$('select#c6').empty().end();
	$('select#c6').append('<option value="">--</option>');
	
	for (i=1; i<=DaysInMonth; i++){
		if(i < 10)
			 i = '0'+i;	
		(day == i) ? s='selected' : s='' ;
		$('select#c6').append('<option value="'+i+'" '+s+'>'+i+'</option>');
	}	
}

