var popWin;

function stripHtml(htstring) {
    return htstring.replace(/(<([^>]+)>)/ig,""); 
}

function isEmail(str) {
  // are regular expressions supported?
  var supported = 0;
  if (window.RegExp) {
    var tempStr = "a";
    var tempReg = new RegExp(tempStr);
    if (tempReg.test(tempStr)) supported = 1;
  }
  if (!supported) 
    return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
  return (!r1.test(str) && r2.test(str));
}

function isLink( str ) {
	if(str.indexOf("http://") ==0 && str.indexOf(".") > 6) {
		return true;
	}else{
		return false;
	}
}

function isRss( str ) {
	if(str.indexOf("rssfeed://") ==0 && str.indexOf(".") > 10) {
		return true;
	}else{
		return false;
	}
}

function isvoid(){
}

function SubmitActionTarget(form, action, target) {
		form.action=action;
		form.target=target;
		form.submit();
}

function SubmitActionTargetByID(form, action, target) {
		var objForm = document.getElementById(form);
		objForm.action=action;
		objForm.target=target;
		objForm.submit();
}

function popupWindow(urlPath,width,height){
		popWin=window.open(urlPath, 'popWin', 'width='+width+',height='+height+',top=0,left=50, toolbar=no, menubar=no, scrollbars=yes, resizable=yes,location=no, status=no');
		if(typeof(popWin)=="object"){
			popWin.focus();
			popWin.moveTo(screen.availWidth/10,90);
		}
}

function popupWindow2(url) {
	window.open(url,'popupWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=320,height=370,screenX=0,screenY=0,top=0,left=0');
}

function popupWindow3(urlPath,width,height){
		popWin=window.open(urlPath, 'popWin', 'width='+width+',height='+height+',toolbar=yes, menubar=yes, scrollbars=yes, resizable=yes,location=yes, status=yes');
		if(typeof(popWin)=="object"){
			popWin.focus();
			popWin.moveTo(screen.availWidth/10,90);
		}
}

function popupWindow4(urlPath,width,height){
		popWin=window.open(urlPath, 'popWin', 'width='+width+',height='+height+',toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no');
		if(typeof(popWin)=="object"){
			popWin.focus();
			popWin.moveTo(screen.availWidth/10,90);
		}
}

function popupWindow5(url) {
	window.open(url,'popupWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=800,height=600,screenX=0,screenY=0,top=0,left=0');
}

function jumpToTarg(targ,urlPath){ // go to target url
  eval(targ+".location='"+urlPath+"'");
}

function historyGo(num){ // history back
  history.go(num);
}

function setUploadObjExtend(objID,startNum,endNum){	// expend upload image fieldbox
	var x;
	x=document.getElementById(objID);
	str='';
	str+="<table width='100%' border='0' cellspacing='0' cellpadding='0'>";
	for(i=startNum;i<endNum+1;i++){
		 j=i+2;
		 str+="<tr>";
	   str+="<td align='left' width='150'>Photo "+i+" : </td>";
	   str+="<td align='left'>";
		 str+="<input name='uploadImg[]' type='file' class='regitration_input_txtbox1' id='uploadImg"+i+"'> ";
		 str+="<input name='ImgBtn"+j+"' type='button' id='ImgBtn"+j+"' value='OK' />";
		 str+="</td>";
		 str+="</tr>";
	}
	str+="</table>";
	if(x != null){
		x.innerHTML=str;
	}
	else{
		alert('setUploadImgExtend exception!');
	}
}

function showHiddenArea(sid){		// show or hide special area with sid
	document.getElementById(sid).style.display = document.getElementById(sid).style.display == "none" ? "" : "none";
}

function showHiddenField(sid){		// show or hide special area with sid
	document.getElementById(sid).style.display =  "";
}

function hideHiddenField(sid){		// show or hide special area with sid
	document.getElementById(sid).style.display =  "none";
}

function enableField(sid){		// enable special area with sid
	document.getElementById(sid).disabled =  false;
}

function disableField(sid){		// disable special area with sid
	document.getElementById(sid).disabled =  true;
}

/**
* to control the row display attribute displayed or not displayed begin
**/
function expand_row(rowid,parent_row,parent_row_bg,id_next_parent) 
{
	if(rowid!=''){
		obj = document.getElementById(rowid);
		obj.style.display = "";//inline;block; tried,not good. empty seems both ok in firefox and IE
	}
	
	if(parent_row!=''){
		obj_parent = document.getElementById(parent_row);
		obj_parent.style.backgroundColor=parent_row_bg; 
	}
	
	if(id_next_parent!=''){
		obj_next_parent = document.getElementById(id_next_parent);
		//obj_next_parent.className = 'bordered_top';
		obj_next_parent.style.display = "";
		//alert(obj_next_parent.id);
	}

	return false;
}

function collapse_row(rowid,parent_row,parent_row_bg,id_next_parent) 
{
	if(rowid!='')
	{
		obj = document.getElementById(rowid);
		obj.style.display = "none";
	}


	if(parent_row!=''){
		obj_parent = document.getElementById(parent_row);
		obj_parent.style.backgroundColor=parent_row_bg; 
	}

	if(id_next_parent!=''){
		obj_next_parent = document.getElementById(id_next_parent);
		obj_next_parent.style.display = "none";
	}


	return false;
}

/**
* to control the row display attribute displayed or not displayed end
**/

function close_window()
{
	window.close();
}

/**
*compare the order of two date
*return true if startdate < enddate
*return true if one date is null
*return false if startdate >= enddate
*/
function checkDateEarlier(strStart,strEnd,splitter)
{
    if (( strStart == "" ) || ( strEnd == "" ))
        return true;
//    var arr1 = strStart.split("-");
//    var arr2 = strEnd.split("-");
    var arr1 = strStart.split(splitter);
    var arr2 = strEnd.split(splitter);
    var date1 = new Date(arr1[2],parseInt(arr1[1].replace(/^0/,""),10) - 1,arr1[0]);
    var date2 = new Date(arr2[2],parseInt(arr2[1].replace(/^0/,""),10) - 1,arr2[0]);
    if(arr1[1].length == 1)
        arr1[1] = "0" + arr1[1];
    if(arr1[0].length == 1)
        arr1[0] = "0" + arr1[0];
    if(arr2[1].length == 1)
        arr2[1] = "0" + arr2[1];
    if(arr2[0].length == 1)
        arr2[0]="0" + arr2[0];
    var d1 = arr1[2] + arr1[1] + arr1[0];
    var d2 = arr2[2] + arr2[1] + arr2[0];

    if(parseInt(d1,10) > parseInt(d2,10))
       return false;
    else
       return true;
}//

function checkSameWeekDay(strStart,strEnd,splitter) {
	var arr1 = strStart.split(splitter);
    var arr2 = strEnd.split(splitter);
    var date1 = new Date(arr1[2],parseInt(arr1[1].replace(/^0/,""),10) - 1,arr1[0]);
    var date2 = new Date(arr2[2],parseInt(arr2[1].replace(/^0/,""),10) - 1,arr2[0]);
   // alert(date1.getDay() + "/" + date2.getDay());
    if (date1.getDay() == date2.getDay()) {
    	return true;
    }else{
    	return false;
    }
}

function checkvalidateforbiddenchars(str){
	var forbidden = new Array(">","<","'","\"");
	for(var k=0;k<str.length;k++){
		for(var i=0;i<forbidden.length;i++){
			if(str.charAt(k)==forbidden[i]){
				return false;
			}
		}
	}
	return true;
}
function checkvalidateforbiddenchars2(str){
	var forbidden = new Array(">","<");
	for(var k=0;k<str.length;k++){
		for(var i=0;i<forbidden.length;i++){
			if(str.charAt(k)==forbidden[i]){
				return false;
			}
		}
	}
	return true;
}

function checkvalidateforbiddenchars3(str){
	var forbidden = new Array(">","<","'","\""," ");
	for(var k=0;k<str.length;k++){
		for(var i=0;i<forbidden.length;i++){
			if(str.charAt(k)==forbidden[i]){
				return false;
			}
		}
	}
	return true;
}

function CheckAllByCrlBtn(tagName,ctlBtnName)		//ȫѡ
{
  objTagBox = document.getElementsByName(tagName);
  objCtlBox = document.getElementById(ctlBtnName);
  
  for (var i=0;i<objTagBox.length;i++)
  {
    var e = objTagBox[i];
    e.checked = objCtlBox.checked;
  }
}

function check_checkbox(formname,objid){
	obj = document.forms[formname].elements[objid];
	if(typeof(obj)=="object"){
		if(obj.length>0){
//			alert(obj.valueOf());
			var tmpflag;
			if(obj[0].checked == false){
					tmpflag = true;
			}else{
					tmpflag = false;
			}

		  for (i=0;i<obj.length;i++){
					obj[i].checked = tmpflag;
		  }
	  }
	  else{
			if(obj.checked == false){
				obj.checked = true;
			}else{
				obj.checked = false;
			}
		}
	}
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}

// JavaScript Document

function CategoryFormValidateSubmit(form, dataMessageArray){
	var objForm;
	var j,s;
	var AlertMsg;
	AlertMsg = "";
	objForm = document.getElementById(form);
	var r1 = new RegExp("([0-9.])$");
	var r2 = new RegExp(/\d{2}\/\d{2}\/\d{4}/);
	
	var dateStart=objForm.elements["CF_date_from"].value;
	var dateEnd=objForm.elements["CF_date_to"].value;
 	var myDate1 = new Date();
 	
 	var today = myDate1.getDate()+'/'+(myDate1.getMonth()+1)+'/'+ myDate1.getFullYear();
 	var dateSeperator=PLANNING_PERIOD_DATE_FORMAT_SEPERATOR;
 	
 	///
 	var dateFormat = PLANNING_PERIOD_DATE_FORMAT.split("-");
 	var day = 1;
 	var month = 0;
 	var year = 2;
 	for ( flag=0; flag<=2; flag++ ) {
	 	switch ( dateFormat[flag] ) {
	 		case 'DD' :
	 			day = flag;
	 			break;
	 		case 'MM' :
	 			month = flag;
	 			break;
	 		case 'YYYY' :
	 			year = flag;
	 			break;
	 	}
 	}
 	///
 	
 	var startDateArr = dateStart.split(dateSeperator);
 	var startDateStamp = startDateArr[day]+'/'+startDateArr[month]+'/'+startDateArr[year];

 	var endDateArr = dateEnd.split(dateSeperator);
 	var endDateStamp = endDateArr[day]+'/'+endDateArr[month]+'/'+endDateArr[year];

	var is_checkFromToDate = false;


	j=false;
	
	// MODIFIED BY BEN ON 11JUNE2007
	// TASK 13339
	// TWO DATE FROM AND TO ARE NOT MANDATORY, BUT JS CHECKS WHEN ONE OF FIELDS IS FILLED.  
	if ( (dateStart == '') && (dateEnd == '') ) {	
	}else{
		if ( dateStart != '' ) {
			if ( dateEnd == '' ){
				AlertMsg += dataMessageArray["PLEASE_INDICATE_A_COMPLETION_DATE_OF_PERIOD"] + "\n";
				j = true;
			}
			else
			{				
				is_checkFromToDate = checkFromToDate();
			}
		}
		
		if ( dateEnd != '' ) {
			if ( dateStart == '' ){
				AlertMsg += dataMessageArray["PLEASE_INDICATE_A_DATE_OF_BEGINNING_OF_PERIOD"] + "\n";
				j = true;
			}
			else
			{
				if (is_checkFromToDate != true){
					checkFromToDate();	
				}				
			}
		}
		
	}
	
	
	function checkFromToDate() {
	
				if(!r2.test(dateStart)){
					AlertMsg += dataMessageArray["WRONG_BEGIN_DATE_FORMAT"] + "\n";
					j = true;
				}
				if(!r2.test(dateEnd)){
					AlertMsg += dataMessageArray["WRONG_BEGIN_DATE_FORMAT"] + "\n";
					j = true;
				}
				// begin_date < today
				if(!checkDateEarlier(today,startDateStamp,"/")){
					AlertMsg += dataMessageArray["BEGIN_DATE_LESS_TODAY"] + "asdfasdf\n";
					j = true;
				}
				// end_date < begin_date
				if(!checkDateEarlier(startDateStamp,endDateStamp,"/")){
					AlertMsg += dataMessageArray["END_DATE_LESS_BEGIN_DATE"] + "\n";
					j = true;
				}
				// end_date < today
				if(!checkDateEarlier(today,endDateStamp,"/")){
					AlertMsg += dataMessageArray["END_DATE_LESS_TODAY"] + "\n";
					j = true;
				}
				
				return true;
	}
	
//	if(objForm.elements["location_selection_mode"][0].checked){
//		if(objForm.elements["region_id"].value == ''){
//			AlertMsg += dataMessageArray["PLEASE_INDICATE_THE_AREA_OF_YOUR_RESEARCH"] + "\n";
//			j = true;
//		}
//	}
	
	hiddenFieldsAreaSelection = document.getElementById('CF_HiddenFieldsAreaSelection');
	if(hiddenFieldsAreaSelection.style.display!='none'){
//		if(objForm.elements["location_selection_mode"][1].checked){
//			if(objForm.elements["city"].value == '' && objForm.elements["zipcode"].value == ''){
//				AlertMsg += dataMessageArray["PLEASE_INDICATE_THE_TOWN_OF_YOUR_RESEARCH"] + "\n";
//				j = true;
//			}
//			if(objForm.elements["action_range"].value == ''){
//				AlertMsg += dataMessageArray["PLEASE_INDICATE_THE_EXTENT_OF_YOUR_RESEARCH"] + "\n";
//				j = true;
//			}
//			else{
//				if(isNaN(objForm.elements["action_range"].value)){
//					AlertMsg += dataMessageArray["OPERATING_RANGE_MUST_BE_AN_INTEGER"] + "\n";
//					j = true;
//				}
//			}
//		}
	}
	else{  // for 
		objForm.elements["CF_location_selection_mode"][0].checked = true;
	}
	
//	alert(objForm.elements["CF_location_selection_mode"][0].value+':'+objForm.elements["CF_location_selection_mode"][1].value);
	if( j ){
		alert( AlertMsg );
		j = false;
		AlertMsg = "";
		return false;
	}
	else{
		xajax_OnSubmit(xajax.getFormValues('CategoryForm'));
	}
}

function isInt(x) {
   var y=parseInt(x);
   if (isNaN(y)) return false;
   return x==y && x.toString()==y.toString();
 } 
 
function isEmpty( value ) {
	if( value.search(/^\s*$/) != -1 ) {
		return true;
	}else{
		return false;
	}
}
 