function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


/* Other functions */
NS4 = (document.layers) ? 1 : 0;
IE4 = (document.all) ? 1 : 0;
W3C = (document.getElementById) ? 1 : 0;	

function menu_over(this_ele, this_class) 
{
	this_ele.className = this_class;
	return true; 
}

function validate_prop_form(frm)
{
	if (frm.name.value == "")
	{
	alert("Please Fill out Name Field.")
	return false;
	}
	else if (frm.title.value == "")
	{
	alert("Please Fill out Title Field.");
	return false
	}
	else
	return true;
}

function confirm_delete(objForm)
{
	if (confirm('Are you sure you want to delete this record?') == 1)
	{
		return true;
	}
	else
	{
		return false;
	}
}

function validate_multiple_checkbox(objField)
{
	if (typeof objField.length != 'undefined')
	{
		intNumEntries = objField.length;
		for (i = 0; i < intNumEntries; i++)
		{
			if (objField[i].checked == true)
			{
				return true;
			}
		}
	}
	else
	{
		if (objField.checked == true)
		{
			return true;
		}
	}
	return false;
}

function confirm_multiple_delete(objForm, objField)
{
	if (validate_multiple_checkbox(objField))
	{
		if (confirm('Are you sure you want to delete the selected items?') == 1)
		{
			objForm.mode.value = 'del';
			return true;
		}
		else
		{
			return false;
		}
	}
	else
	{
		alert('Please choose the items you want to delete!');
		return false;
	}
}

function confirm_multiple_hide(objForm, objField, strValue)
{
	if (validate_multiple_checkbox(objField))
	{
		if (confirm('Are you sure you want to ' + strValue + ' the selected items?') == 1)
		{
			objForm.mode.value = strValue;
			return true;
		}
		else
		{
			return false;
		}
	}
	else
	{
		alert('Please choose the items you want to ' + strValue + '!');
		return false;
	}
}

function confirm_multiple_feature(objForm, objField, strValue)
{
	if (strValue == 'feature')
	{
		strTag = 'want to feature';
	}
	else if (strValue == 'unfeature')
	{
		strTag = 'don\'t want to feature';
	}
	
	if (validate_multiple_checkbox(objField))
	{
		if (confirm('Are you sure you ' + strTag + ' the selected items?') == 1)
		{
			objForm.mode.value = strValue;
			return true;
		}
		else
		{
			return false;
		}
	}
	else
	{
		alert('Please choose the items you ' + strTag +'!');
		return false;
	}
}

function showChildren ( name, img ) 
{
	if (W3C) 
	{
    	ele = document.getElementById(name).style;
		visible='block';
		hidden='none';
  	}
	else if (NS4)
	{
	    ele = document.layers[name];
		visible='block';
		hidden='none';
  	}
	else
	{
    	ele = document.all[name].style;
		visible='block';
		hidden='none';
  	}
  	if (ele.display == visible)
	{
		ele.display = hidden;
		eval('document.'+img+'.src="../images/folder_close.gif"');
	//set php variable
  	}
	else if (ele.display == hidden)
	{
		ele.display = visible;
		eval('document.'+img+'.src="../images/folder_open.gif"');
	}
}	

/* Validate Forms */
function validate_text(this_value, this_name) 
{
	if (this_value == '') 
	{
		return this_name+'\n';
	} 
	else 
	{
		return '';
	}
}

function validate_select(this_value, this_name, this_error_choice) 
{
	if (this_value == this_error_choice) 
	{
		return this_name+'\n';
	}
	else 
	{
		return '';
	}
}

function validate_select_multiple(objField, intMin, strName) 
{
	intLenOption = objField.length;
	if (intLenOption < intMin)
	{
		return strName + '\n';	
	}	
	else
	{
		return '';
	}
}

function validate_email(this_value) {
	var re = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
	if (!re.test(this_value)) {
		return 'Invalid email address \n';
	} else {
		return '';
	}
}

function with_space(strValue)
{
	var i, isWithSpace, strValue, intStrLength, chrToken

	isWithSpace  = false;
	strValue     = strValue.toString()
	intStrLength = strValue.length
	for (i = 0; i < intStrLength; i++) 
	{
		chrToken = strValue.charAt(i)
		if (chrToken == " ") 
		{
			isWithSpace = true
		}
	}
	return isWithSpace
}

function validate_password(strPasswd, strConfirmPasswd) 
{
	strErrorMsg = ''
	if (strPasswd != strConfirmPasswd) 
	{
        strErrorMsg = 'Password doesn\'t match \n'
	}
	else if (strPasswd.length < 4)
	{
		strErrorMsg = 'Password should not be less than 4 characters \n'
	}
	else if (with_space(strPasswd))
	{
		strErrorMsg = 'Password should not be contain spaces \n'
	}
	else
	{
		strErrorMsg = ''
	}
	return strErrorMsg
}

function is_integer(value) {
	var blnInt = true;
	inputStr = value.toString();
	for (var i = 0; i < inputStr.length; i++) {
		var oneChar = inputStr.charAt(i);			
		if ((oneChar < "0" || oneChar > "9")) {
			blnInt = false;
		}
	}
	return (blnInt);
}

function is_float(value)
{
     var template = "x.ff";
     new_value = value/1;
     if(isNaN(new_value)) 
	 {	
	 	return false
	 }
     if ((pos = value.indexOf(".")) > 0)
     {
          templatepos = template.indexOf(".");
          tmpf = template.length-templatepos-1;
          valf = value.length-pos-1;
          if(valf > tmpf) return false;
          else return true;
     }
     return true;
}

function display_error_msg(strErrorMsg) 
{
	strErrorMsg = 'Please fill out the following fields correctly : \n' + strErrorMsg;
	alert(strErrorMsg);
	return true;
}

/* Specific Form Validation */
function validate_change_password_form(objForm)
{
	strErrorMsg = ''
	strErrorMsg = strErrorMsg + validate_text(objForm.password.value, 'Password');
	strErrorMsg = strErrorMsg + validate_text(objForm.confirm_password.value, 'Confirm Password');
	strErrorMsg = strErrorMsg + validate_password(objForm.password.value, objForm.confirm_password.value);
 	if (strErrorMsg) 
	{
		display_error_msg(strErrorMsg);
		return false
	}
	else
	{
		return true
	}
}

function validate_category_form(objForm)
{
	strErrorMsg = ''
	strErrorMsg = strErrorMsg + validate_text(objForm.name.value, 'Name');
	strErrorMsg = strErrorMsg + validate_text(objForm.description.value, 'Description');
// 	strErrorMsg = strErrorMsg + validate_select(objForm.parent.options[objForm.parent.selectedIndex].value, 'Parent Category');
 	if (strErrorMsg) 
	{
		display_error_msg(strErrorMsg);
		return false
	}
	else
	{
		return true
	}
}

function validate_catalog_form(objForm, strType)
{
	strErrorMsg = ''
	strErrorMsg = strErrorMsg + validate_text(objForm.name.value, 'Name');
	strErrorMsg = strErrorMsg + validate_text(objForm.description.value, 'Description');
 	strErrorMsg = strErrorMsg + validate_select(objForm.category.options[objForm.category.selectedIndex].value, 'PCategory');
	if (strType == 'add')
	{
		//strErrorMsg = strErrorMsg + validate_text(objForm.full_image.value, 'Full Image');
	}
 	if (objForm.price.value != '' && !is_float(objForm.price.value))
	{
		strErrorMsg = strErrorMsg + 'Invalid price \n';
	}
	if (strErrorMsg) 
	{
		display_error_msg(strErrorMsg);
		return false
	}
	else
	{
		return true
	}
}

function validate_bug_form(objForm, strType)
{
	strErrorMsg = ''
	strErrorMsg = strErrorMsg + validate_text(objForm.summary.value, 'Summary');
	strErrorMsg = strErrorMsg + validate_text(objForm.description.value, 'Description');
 	strErrorMsg = strErrorMsg + validate_select(objForm.category.options[objForm.category.selectedIndex].value, 'PCategory');
 	strErrorMsg = strErrorMsg + validate_select(objForm.priority.options[objForm.priority.selectedIndex].value, 'Priority');
	strErrorMsg = strErrorMsg + validate_text(objForm.submitted.value, 'Submitted');

	if (strErrorMsg) 
	{
		display_error_msg(strErrorMsg);
		return false
	}
	else
	{
		return true
	}
}

function validate_edit_image_form(objForm, strType)
{
	strErrorMsg = ''
	strErrorMsg = strErrorMsg + validate_text(objForm.name.value, 'Name');
	if (isUpdate == false)
		strErrorMsg = strErrorMsg + validate_text(objForm.image_file.value, 'File');
	if (strErrorMsg) 
	{
		display_error_msg(strErrorMsg);
		return false
	}
	else
	{
		return true
	}
}

function validate_edit_file_form(objForm, strType)
{
	strErrorMsg = ''
	strErrorMsg = strErrorMsg + validate_text(objForm.name.value, 'Name');
	strErrorMsg = strErrorMsg + validate_text(objForm.content_file.value, 'File');
	if (strErrorMsg) 
	{
		display_error_msg(strErrorMsg);
		return false
	}
	else
	{
		return true
	}
}


/* Specific Form Validation */
function validate_feedback_form(objForm)
{
	strErrorMsg = ''
	strErrorMsg = strErrorMsg + validate_email(objForm.email.value);
	strErrorMsg = strErrorMsg + validate_text(objForm.name.value, 'Name');
	strErrorMsg = strErrorMsg + validate_text(objForm.subject.value, 'Subject');
	strErrorMsg = strErrorMsg + validate_text(objForm.message.value, 'Message');

	if (strErrorMsg) 
	{
		display_error_msg(strErrorMsg);
		return false
	}
	else
	{
		return true
	}
}

function confirm_unsaved_changes(strUrl)
{
	if (confirm('You may have unsaved changes on this page. \n Are you sure you want to go to another page?\n') == 1)
	{
		document.location.href = strUrl;
	}
	else
	{
		return false;
	}
}


function movePage(pos) {
 list = document.catalog_rank_form.elements['lstCategory[]'];
 if (list.options[list.selectedIndex].value != '') {
  switch (pos) {
   case 'top':
    len = list.length;
    sel_index = list.selectedIndex;
    if (sel_index != 1) {
     sel_value = list.options[list.selectedIndex].value;
     sel_text = list.options[list.selectedIndex].text;
     this_value = list.options[1].value;
     this_text = list.options[1].text;
     list.options[1].value = sel_value;
     list.options[1].text = sel_text;
     j = 2;
     for (i = 2; i < (len - 1); i++) {
      if (i != sel_index) {
       last_value = list.options[i].value;
       last_text = list.options[i].text;
       list.options[j].value = this_value;
       list.options[j].text = this_text;
       this_value = last_value;
       this_text = last_text;
       j++;
      }
     }
     list.options[j].value = this_value;
     list.options[j].text = this_text;
     list.options[1].selected = true;
     list.options[sel_index].selected = false;
    }
    break;
   case 'up':
    len = list.length;
    sel_index = list.selectedIndex;
    swap_index = sel_index - 1;
    if (swap_index != 0) {
     sel_value = list.options[sel_index].value;
     sel_text = list.options[sel_index].text;
     swap_value = list.options[swap_index].value;
     swap_text = list.options[swap_index].text;
     //swap text and values
     list.options[sel_index].value = swap_value;
     list.options[sel_index].text = swap_text;
     list.options[swap_index].value = sel_value;
     list.options[swap_index].text = sel_text;
     list.options[sel_index].selected = false;
     list.options[swap_index].selected = true;
    }
    break;
   case 'down':
    len = list.length;
    sel_index = list.selectedIndex;
    swap_index = sel_index + 1;
    if (swap_index != (len - 1)) {
     sel_value = list.options[sel_index].value;
     sel_text = list.options[sel_index].text;
     swap_value = list.options[swap_index].value;
     swap_text = list.options[swap_index].text;
     //swap text and values
     list.options[sel_index].value = swap_value;
     list.options[sel_index].text = swap_text;
     list.options[swap_index].value = sel_value;
     list.options[swap_index].text = sel_text;
     list.options[sel_index].selected = false;
     list.options[swap_index].selected = true;
    }    
    break;
   case 'bottom':
    len = list.length;
    sel_index = list.selectedIndex;
    last_index = parseInt(len - 2);
    if (sel_index != last_index) {
     sel_value = list.options[list.selectedIndex].value;
     sel_text = list.options[list.selectedIndex].text;
     this_value = list.options[last_index].value;
     this_text = list.options[last_index].text;
     list.options[last_index].value = sel_value;
     list.options[last_index].text = sel_text;
     j = parseInt(len - 3);
     for (i = (len - 3); i > 0; i--) {
      if (i != sel_index) {
       last_value = list.options[i].value;
       last_text = list.options[i].text;
       list.options[j].value = this_value;
       list.options[j].text = this_text;
       this_value = last_value;
       this_text = last_text;
       j--;
      }
     }
     list.options[j].value = this_value;
     list.options[j].text = this_text;
     list.options[last_index].selected = true;
     list.options[sel_index].selected = false;
    }
    break;
  }
 }
} 

function selectall()
		  {	
		  	list = document.catalog_rank_form.elements['lstCategory[]'];
		    i=list.length;
			if (i>1)
			{
				for(j=1;j<i-1;j++)
					{
					if (list.options[j].value != 'x') 
						{
						list.options[j].selected = true;
						}
					}
			}else
			{
				list.options[1].selected = true;
			}
			//alert('1');
			document.catalog_rank_form.submit();
		  }
		  
var popUpWin=0;
function popUpWindow(URLStr, left, top, width, height)
{
	if(popUpWin)
	{
		if(!popUpWin.closed) popUpWin.close();
	}
	popUpWin = open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menub ar=no,scrollbar=yes,resizable=yes,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
}

function redirect(URLStr) 
{
	location = URLStr; 
}

function checkUncheck(form,fieldname,value)
{	
	obj = form.elements[fieldname];
	if (obj == null)
		return;
		
	if (obj.length > 1)
	{
		for (i = 0; i < obj.length; i++)
			obj(i).checked = value;
	}
	else
	{
		obj.checked = value;
	}
}

function selectlist(form,fieldname)
{
	field = form.elements[fieldname];

	if (field == null)
		return;

	for (i=0; i < field.length; i++) 
	{  
		field.options[i].selected = true;  
	}
}

function transferlist(form,source,destination)
{
	try
	{
		m1 = form.elements[source];
		m2 = form.elements[destination];
	
		if (m1 == null || m2 == null)
			return 
			
		m1len = m1.length;
		
		if (m1len == 0)
			return;
		
		for (i=0; i < m1len ; i++)
		{
			if (m1.options[i].selected == true ) 
			{
				m2len = m2.length;
				opt = new Option();
				opt.text = m1.options[i].text;
				opt.value = m1.options[i].value;
				m2.options[m2len]= opt;
			}
		}
	
		for (i = (m1len -1); i >= 0; i--)
		{
			if (m1.options[i].selected == true ) 
			{
				m1.options[i] = null;
			}
		}
	}
	finally
	{
		
	}
}

function addlist(form,source,destination)
{
	try
	{
		m1 = form.elements[source];
		m2 = form.elements[destination];
		if (m2.options[0].text == "no item selected") m2.length = 0;

		m2len = m2.length;
		opt = new Option();
		opt.text = m1.value;
		opt.value = m1.value;
		m2.options[m2len]= opt;
	}
	finally
	{
		
	}
}


function swapOptions(form,fieldname,i,j) 
{
	obj = form.elements[fieldname];

var o = obj.options;
	var i_selected = o[i].selected;
	var j_selected = o[j].selected;
	var temp = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
	var temp2= new Option(o[j].text, o[j].value, o[j].defaultSelected, o[j].selected);
	o[i] = temp2;
	o[j] = temp;
	o[i].selected = j_selected;
	o[j].selected = i_selected;
}

function moveoptionlist(form,fieldname,updown) 
{
	obj = form.elements[fieldname];
	
	if (updown == "down")
	{
		for (i=obj.options.length-1; i>=0; i--) 
		{
			if (obj.options[i].selected) 
			{
				if (i != (obj.options.length-1) && ! obj.options[i+1].selected) 
				{
					swapOptions(form,fieldname,i,i+1);
					obj.options[i+1].selected = true;
				}
			}
		}

	}
	else
	{
		for (i=0; i<obj.options.length; i++) 
		{
			if (obj.options[i].selected) 
			{
				if (i != 0 && !obj.options[i-1].selected) 
				{
					swapOptions(form,fieldname,i,i-1);
					obj.options[i-1].selected = true;
				}
			}
		}
	}
}
