
// general purpose validation function
function foc(field)
{
	//if (str>"") alert(str);
	field.select();
	field.focus();
}

function ck_num(field)
{
	val = parseInt(field.value,10);//returns bogus value
	if (isNaN(val))
	{
		foc(field);
		alert("Invalid number format !");
		return false;
	}

	return true;
}

function ck_pos_num(field)
{
	val = parseInt(field.value,10);//returns bogus value
	if (isNaN(val)) {
		foc(field);
		alert("Please enter a valid number");
		return false;
	}

	if (val>=0) return true;
	else
	{
		foc(field);
		alert("Please enter a positive number !");
		return false;
	}
}




function compare_num(field1,field2,str)
//checks if filed1<field2 and that both fields are positive numbers
{
	var return_value = true;
	num1 = parseInt(field1.value,10);
	num2 = parseInt(field2.value,10);
	if (num1>num2)
	{
		if (str!="")
		{
			foc(field1);
			alert(str);
		}
		return false;
	}
	else
	{
		return true;
	}
}

function ck_float_pos_num(field)
{
	val = parseFloat(field.value);
	if (isNaN(val) || val<0)
	{
		alert('Please enter a positive number !');
		foc(field);
		return false;
	}

	 return true;
}

function ck_text(field, zval, str)
{

	if (field.value == zval)
	{
		if (str!="")
		{
			foc(field);
			//msg="Please fill the "+field+" field!";
			alert(str);
		}
		return false;
	} else
		return true;
}

function ck_email_provider(field, provider, str)
{

	if ( field.value.indexOf(provider) != -1 )
	{
		if (str!="")
		{
			foc(field);
			alert(str);
		}
		return false;
	} else
		return true;
}

function ck_identical(field_1, field_2, str)
{
	if (field_1.value != field_2.value)
	{
		if (str!="")
		{
			foc(field_2);
			alert(str);
		}
		return false;
	} else
		return true;
}

function ck_length(field, len, str)
{
	if (field.value.length <= len)
	{
		if (str!="")
		{
			foc(field);
			alert(str);
		}
		return false;
	} else
		return true;
}
//comparator = -1 ... field<len comparator=0 field=len comparator = 1 field>len
function ck_length_ext(field,len,str,comparator)
{
	switch (comparator)
	{
		case -1:
		if (field.value.length < len)
		{
			if (str!="")
			{
				foc(field);
				alert(str);
			}
			return false;
		}
		else return true;
		break;

		case 0:
		if (field.value.length != len)
		{
			if (str!="")
			{
				foc(field);
				alert(str);
			}
			return false;
		}
		else return true;
		break;

		case 1:
		if (field.value.length > len)
		{
			if (str!="")
			{
				foc(field);
				alert(str);
			}
			return false;
		}
		else return true;
		break;
	}
}

function ck_select(field, zval, zindex, str)
{
	if (field.options[field.selectedIndex].value==zval || field.selectedIndex==zindex)
	{
		if (str!="")
		{
			//foc(field);
			field.focus();
			alert(str);
		}
		return false;
	}
	else
		return true;
}

function ck_age(fieldYear, fieldMonth, fieldDay, min_age, str)
{
	var userYear = fieldYear.value;
	var userMonth = fieldMonth.value;
	var userDay = fieldDay.value;
	
	var currentTime = new Date();
	var dateDay = currentTime.getDate();
	var dateMonth = currentTime.getMonth() + 1;
	var dateYear = currentTime.getFullYear();

	if (dateYear - userYear < 18)
		{
		alert(str);
		return false;
		}
	else
		{
		if (userYear == dateYear - 18)
			{
			if (userMonth > dateMonth)
				{
				alert(str);
				return false;
				}
			else
				{
				if (userMonth == dateMonth)
					{
					if (userDay > dateDay)
						{
						alert(str);
						return false;
						}
					else return true;
					}
				else return true;
				}
			}
		else return true;
		}
}
/*
function ck_checkbox(field, str)
{
	if (field.checked==false)
	{
		if (str!="")
		{
			field.focus();
			alert(str);
		}
		return false;
	}
	return true;
}
*/
function ck_checkbox(field, str)
{
	// checking if there is one checkbox or an array of checkboxes	
	if (typeof(field.length)!='number')
	{	
		if (field.checked==false)
		{
			field.focus();
			alert(str);
			return false;
		}
		else
			return true;
	}
	else
	{	
		for (i=0; i < field.length; i++)
			if (field[i].checked) return i+1;
	
		if (str!="")
		{
			alert(str);
			foc(field[0]);
		}
		return false;									
	}
	
}


//to be used with the CCheckBoxArray class - check-box names must be name_0, name_1, name_2 etc. and name_0 deselects the other chk
function deselect_checkbox(field, num_fields)
{
	any_chk = eval(field+"_0");
	if (any_chk.checked == true)
	{
		for (i=1; i < num_fields; i++)
		{
			other_chk = eval((field+"_")+i);
			other_chk.checked=false;
		}
	}
}
// deselects the any chk if this chk gets pressed
function deselect_any_checkbox(field,field_index)
{
	current_chk = eval(field+"_"+field_index);
	any_chk = eval(field+"_0");
	if (current_chk.checked==true)
	{
		any_chk.checked = false;
	}
}

function reset_chk(field,num_fields)
{
	for (i=1; i < num_fields; i++)
	{
		chk = eval((field+"_")+i);
		chk.checked=false;
	}
	any_chk = eval((field+"_")+0);
	any_chk.checked = true;
}

function ck_radio(field, str)
{
	for (i=0; i < field.length; i++)
		if (field[i].checked) return i+1;

	if (str!="")
	{
		foc(field[0]);
	}
	return false;
}


function validate_email(field, alert_str)
{
	var str = field.value;

	if (window.RegExp) 
	{
		var reg1str = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)";
		var reg2str = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z0-9]{2,4}|[0-9]{1,4})(\\]?)$";
		var reg1 = new RegExp(reg1str);
		var reg2 = new RegExp(reg2str);
		
		if (reg1.test(str) || !reg2.test(str)) 
		{
			alert(alert_str);
			field.select();
			return false;
		}	
	} 

	if (str.indexOf(" ") >= 0)					// space not allowed
	{
		alert('[space] is not allowed in email address field!'); 
		field.select();
		return false;
	}
	
	return true;
}


// auto fill form with supplied values
function set_form(f, names, values)
{
	var i;

	for (i=0; i < names.length; i++)
	{
		if (e = f.elements[names[i]])
		{
	//		alert(i + " " + e.name +" "+ e.type+" "+ values[i]);
			switch(e.type) {
			case "hidden" :
			case "text" :
			case "textarea" : e.value = values[i]; break;
			case "select-one" : set_select(e, values[i]); break;
			case "checkbox" : set_checkbox(e, values[i]); break;
			}

			if (e.length>0&&e[0].type=="radio")
				set_radio(e, values[i]);
		}
	}
}

// functions used by set_form

function set_text(field, val)
{
	field.value = val;
	return true;
}

function set_select(field, val)
{
	var i;
	for (i=0; i < field.options.length; i++)
		if (field.options[i].value == val)
		{
			field.selectedIndex = i;
			return true;
		}
	return false;
}

function set_checkbox(field, val)
{
	if (field.value != val) return false;
	field.checked = true;
	return true;
}

function set_radio(field, val)
{
	var i;
	var ok;
	ok = false;
	for (i=0; i < field.length; i++)
	{
//	alert(field[i].value);
		if (field[i].value == val)
		{
	//		alert("Found i = "+i);
			field[i].checked = true;
			ok = true;
		}
		else field[i].checked = false;
	}
	return ok;
}


//return the value of the radio button that is checked
//return an empty string if none are checked, or
//there are no radio buttons
function get_radio(radioObj)
{
	if(!radioObj) return "";

	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";

	for(var i = 0; i < radioLength; i++)
		if(radioObj[i].checked)
			return radioObj[i].value;

	return "";

} // END function






function isEmpty(obj)
{
	obj_type = obj.type;
	if (obj_type == "text" || obj_type == "password" || obj_type == "textarea" || obj_type == "file")	{
		var objValue;

		objValue = obj.value.replace(/\s+$/,"");

		if (objValue.length == 0) {
			obj.focus();
			return true;
		} else {
			return false;
		}
	} else if (obj_type == "select") {
		for (i=0; i < obj.length; i++) {
			if (obj.options[i].selected) {
				if(obj.options[i].value == "") {
					obj.focus();
					return true;
				} else {
					return false;
				}
			}

		}
		return true;
	} else if (obj_type == "radio" || obj_type == "checkbox") {
		if (!obj[0] && obj) {
			if (obj.checked) {
				return false;
			} else {
				obj.focus();
				return true;
			}
		} else {
			for (i=0; i < obj.length; i++) {
				if (obj[i].checked) {
					return false;
				}
			}
			obj[0].focus();
			return true;
		}
	} else {
		return false;
	}
}

function ck_date_format(year, month, day)	//checks the date
{

	if(year==-1 && month==-1 && day==-1)
		return true;

	if(year==-1 || month==-1 || day==-1)
		return false;

	start_date = new Date(year, month-1, day);
	date_year=start_date.getYear();

	if(date_year<2000) date_year=date_year + 1900;

	//alert("Start_date:"+start_date.toString());

	if((start_date.getMonth() != month-1) || (start_date.getDate() != day)  || (date_year != year))
		return false;
	else
		return true;
}

function popUp(URL,width,height)
{
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width="+width+",height="+height+"');");
}

function open_window(location, width, height)
{
	var w = width * screen.width;
	var h = height * screen.height;
	var winl = (screen.width - width) / 2;
	var wint = (screen.height - height) / 2;

	winprops = 'height='+height+',width='+width+',top='+wint+',left='+winl
	myWin = window.open(location,'picker','toolbar=no,scrollbars=yes,location=no,directories=no,status=yes,menubar=no,resizable=yes,'+winprops);
	myWin.focus();
}

function CheckAll(){
	for (var i=0; i < document.frmMessageList.elements.length; i++) {
		var e = document.frmMessageList.elements[i];
		if (e.name != 'allbox')  e.checked = document.frmMessageList.allbox.checked;
	}
}
function submitMyForm(action,message){
	if(message == 'none'){
		document.forms[0].form_action.value = action;
		document.forms[0].submit();
	}else{
		if(confirm(message)){
			document.forms[0].form_action.value = action;
			document.forms[0].submit();
		}
	}
}

function submitMyForm1(action,message){
	if(message == 'none'){
		document.forms[0].action = action + ".php";
		document.forms[0].submit();
	}else{
		if(confirm(message)){
			document.forms[0].action = action + ".php";
			document.forms[0].submit();
		}
	}
}


var modal;

function openProfile(member_id)
{	
	var proc_file = '../profile/profile-content.php?p=' + member_id;
	
	if (!modal)		
	    modal = new FbModal(
            {
               id: 'primeModal',
               onShow: function()
               {
                  this.container.setStyle('display', 'block');
                  this.container.setStyle('opacity', 1);
               },
               onHide: function()
               {
                  this.container.setStyle('display', 'none');
                  this.container.setStyle('opacity', 0);
               }
        });
    
    modal.setContent('', '<div class="loading">&nbsp;</div>');
    
    // getting window scroll size
    var scroll_height = document.body.scrollTop || window.pageYOffset || (document.body.parentElement ? document.body.parentElement.scrollTop : 0 );
     
    modal.container.setStyle('top', scroll_height);		// adding scroll height as top offset so profile popup will appear on visible area
    modal.show();
    
    var req = new Request({
    		url: proc_file, 
    		method: 'get', 
    		evalScripts: true,
    		onSuccess: function(html) {
    	
    			modal.setBody(html); 
    			var username = modal.container.getElement('.title');
    			modal.setTitle(username.innerHTML);
    			
    			
    		    ReMooz.assign('.profile-view .thumbs a', {
    		    	'origin': 'img',
    		    	'shadow': 'onOpenEnd', 	// fx is faster because shadow appears after resize animation
    		    	'resizeFactor': 0.8, 	// resize to maximum 80% of screen size
    		    	'cutOut': false, 			// don't hide the original
    		    	'opacityResize': 0.4, 	// opaque resize
    		    	'dragging': false, 		// disable dragging
    		    	'centered': true 			// resize to center of the screen, not relative to the source element
    		    });
    		    
    		    /**
    		     * Note on "shadow": value can be true, onOpenEnd (appear after resize) and false, to disable shadow
    		     * WebKit (Safari 3) uses (great looking) CSS shadows, so it ignores this option.
    		     */    			
    			
    		}
    }).send();
    
    
} // END function



// loads content into a Modal Window
function ajax_open_win(src, width, height, title)
{
	if (!modal)		
	    modal = new FbModal(
            {
               id: 'primeModal',
               onShow: function()
               {
                  this.container.setStyle('display', 'block');
                  this.container.setStyle('opacity', 1);
               },
               onHide: function()
               {
                  this.container.setStyle('display', 'none');
                  this.container.setStyle('opacity', 0);
               }
        });
    
    modal.setContent('', '<div class="loading">&nbsp;</div>');
    
    // getting window scroll size
    var scroll_height = document.body.scrollTop || window.pageYOffset || (document.body.parentElement ? document.body.parentElement.scrollTop : 0 );
     
    modal.container.setStyle('top', scroll_height);		// adding scroll height as top offset so profile popup will appear on visible area
	modal.setWidth(width);
	modal.setHeight(height);
	modal.setTitle(title);
    modal.show();
       
        
    var req = new Request({
    		url: src, 
    		method: 'get', 
    		evalScripts: true,
    		onSuccess: function(html) {    	
    			modal.setBody(html);  			    			    			    			
    		}
    }).send();    
     
     
} // END function


// open Terms of Services into Modal Window
function open_tos()
{
	var title = 'Terms of Service';
	
	ajax_open_win('../home/tos.php?content_only=1', 620, 470, title);		
}


// open Help for Payment Plans into Modal Window
function open_plans_help(src)
{
	src = src ? src: '../help/help_plans.php';					//default value for src

	var title = 'Upgrade Options';

	ajax_open_win(src, 450, 350, title);
}


