
var global_valfield;

var imagesReordered = false;
var navReordered = false;
var projectsReordered = false;

function show_hidden_upload_fields() {
	jQuery(".hidden").css("display", "block");
	jQuery("#show_hidden_upload_fields_link").css("display", "none");
}

function set_focus_delayed() {
	document.getElementById(global_valfield).select();
	document.getElementById(global_valfield).focus();
}

function set_focus(field) {
  // save valfield in global variable
  global_valfield = field;
  setTimeout( 'set_focus_delayed()', 100 );
}


function noteImagesReordered() {
	imagesReordered = true;
}

function noteNavReordered() {
	navReordered = true;
}

function noteProjectsReordered() {
	projectsReordered = true;
}

function checkIfNavReordered(event) {
	if (navReordered) {
		 Event.stop(event);
		 navReordered = false;
	}
}

function checkIfImagesReordered(event) {
	if (imagesReordered) {
		 Event.stop(event);
		 imagesReordered = false;
	}
}

function checkIfProjectsReordered(event) {
	if (projectsReordered) {
		 Event.stop(event);
		 projectsReordered = false;
	}
}

function saveNewNavOrder() {
//	alert("Nav has been re-ordered");
//	return;
	// iterate through the navlist
	var links = document.getElementById("our_work_items").childNodes;
	var newOccupants = '';
	for(var i = 0; i < links.length; i++) {
		newOccupants += links[i].id.substr(9) + ',';
	}
	var queryString = 'q=' + page_id_global + ':' + newOccupants;
//	alert(queryString);
//	return;
//	turn_on_ajax_activity_indicator();
	jQuery.ajax({
		type: "POST",
		url: "/ajax_save_new_category_order.php",
		data: queryString,
		error: function(requestObject, errorString, execptionObject) {
			display_ajax_error(requestObject, errorString, execptionObject);
		},
		success: function(msg){
//			turn_off_ajax_activity_indicator();
		}
	});
}


function saveNewProjectsOrder() {
	// iterate through the project_list
	var links = document.getElementById("project_list").childNodes;
	var newOccupants = '';
	for(var i = 0; i < links.length; i++) {
		newOccupants += links[i].id.substr(8) + ',';
	}
	var queryString = 'q=' + page_parent_global + ':' + newOccupants;
//	alert(queryString);
//	return;
//	turn_on_ajax_activity_indicator();
	jQuery.ajax({
		type: "POST",
		url: "/ajax_save_new_projects_order.php",
		data: queryString,
		error: function(requestObject, errorString, execptionObject) {
			display_ajax_error(requestObject, errorString, execptionObject);
		},
		success: function(msg){
//			turn_off_ajax_activity_indicator();
		}
	});
}


function saveNewImagesOrder() {
//	alert("Images have been re-ordered");
//	return;
	// iterate through the navlist
	var links = document.getElementById("imagelist").childNodes;
	var newOccupants = '';
	for(var i = 0; i < links.length; i++) {
		newOccupants += links[i].id.substr(21) + ',';
	}
	var queryString = 'q=' + page_id_global + ':' + newOccupants;
//	alert(queryString);
//	return;
//	turn_on_ajax_activity_indicator();
	jQuery.ajax({
		type: "POST",
		url: "/ajax_save_new_project_image_order.php",
		data: queryString,
		error: function(requestObject, errorString, execptionObject) {
			display_ajax_error(requestObject, errorString, execptionObject);
		},
		success: function(msg){
//			turn_off_ajax_activity_indicator();
		}
	});
}


function display_ajax_error(requestObject, errorString, execptionObject) {
	alert('An ajax error occurred: ' + errorString);
}

//function turn_on_ajax_activity_indicator() {
//	document.getElementById("ajax_activity_indicator").src ='/i/shared/ajax_activity.gif';
//}
//
//function turn_off_ajax_activity_indicator() {
//	document.getElementById("ajax_activity_indicator").src ='/i/shared/tr.gif';
//}
//




/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=02;
var maxYear=99;

function is_integer(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function strip_chars_in_bag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function days_in_february (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

function days_array(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function is_date(dtStr){
	var daysInMonth = days_array(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : dd/mm/yy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Invalid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>days_in_february(year)) || day > daysInMonth[month]){
		alert("Invalid day")
		return false
	}
	if (strYear.length != 2 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 2 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || is_integer(strip_chars_in_bag(dtStr, dtCh))==false){
		alert("invalid date")
		return false
	}
return true
}

/* end of date validation code */

function clearDefaultText(theText) {
	if (theText.value == theText.defaultValue) {
		theText.value = ""
	}
}

function suggest_client_area_password(field_name) {
	var c = "bcdfghjklmnprstvwz";
	var v = "aeiou";
	var pass = '';
	
	// repeat for each syllable
	for(var i=0; i < 2;  i++) {
		pass += c.substr(Math.round((c.length - 1) * Math.random()), 1);
		pass += v.substr(Math.round((v.length - 1) * Math.random()), 1);
		pass += c.substr(Math.round((c.length - 1) * Math.random()), 1);
	}
	document.getElementById(field_name).value =  pass;
}

// 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.replaceChars.shortDays[this.getDay()]; },
	j: function() { return this.getDate(); },
	l: function() { return Date.replaceChars.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 == 3 && this.getDate() != 13 ? '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.replaceChars.longMonths[this.getMonth()]; },
	m: function() { return (this.getMonth() < 9 ? '0' : '') + (this.getMonth() + 1); },
	M: function() { return Date.replaceChars.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() % 12 || 12; },
	G: function() { return this.getHours(); },
	h: function() { return ((this.getHours() % 12 || 12) < 10 ? '0' : '') + (this.getHours() % 12 || 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 ? '-' : '+') + (Math.abs(this.getTimezoneOffset() / 60) < 10 ? '0' : '') + (Math.abs(this.getTimezoneOffset() / 60)) + '00'; },
	T: function() { var m = this.getMonth(); this.setMonth(0); var result = this.toTimeString().replace(/^.+ \(?([^\)]+)\)?$/, '$1'); this.setMonth(m); return result;},
	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; }
};

