/******* common functions start ******/
function popUp(URL,varWidth) {
    day = new Date();
    id = day.getTime();
    eval("page" + id + " = window.open(URL, '" + id + "', 'left=410,top=70,toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width="+ varWidth +",height=500');");
}

function isEmail(a) {
    return (a.indexOf(".") > 0) && (a.indexOf("@") > 0);
}

function isEmpty( str ){
    var strRE = /^[\s ]*$/gi;
    return strRE.test( str );
}
/****** common functions end *********/

function arrange_order(orderbyfld,orderby) {
	var f = $('frmMain');
	var obyfld = f['hdnOrderbyFieldname'];
	$(obyfld).value = orderbyfld;
	var oby = f['hdnOrderby'];
	$(oby).value = orderby;
	f.submit();
}

function vendor_arrange_order(orderbyfld,orderby) {
	var f = $('frmMain_vendor');
	var obyfld = f['hdnOrderbyFieldname'];
	$(obyfld).value = orderbyfld;
	var oby = f['hdnOrderby'];
	$(oby).value = orderby;
	f.submit();
}

// for vendor listing arrange by rating
//function arrange_order(orderbyfld,orderby) {
//	var f = $('frmMain_rating');
//	var obyfld = f['hdnOrderbyFieldname_rating'];
//	$(obyfld).value = orderbyfld;
//	var oby = f['hdnOrderby_rating'];
//	$(oby).value = orderby;
//	f.submit();
//}
// end of arrange order by star

function _add() {
   	var f = $('frmMain');
    var pga = f['hdnPgaction'];
    $(pga).value = 'add';
	f.submit();
}

function _edit(fld,id,pageNo) {
   	var f = $('frmMain');
    var pga = f['hdnPgaction'];
    $(pga).value = 'edit';
    var fid = f[fld];
    $(fid).value = id;
	var pno = f['hdnPageNo'];
	$(pno).value = pageNo;
	var w = window.open('about:blank','Popup_Window');
        f.target = 'Popup_Window';
	f.submit();
}

function _delete(fld,id) {
	if(confirm('Are you sure to remove this record?')) {
        var f = $('frmMain');
        var pga = f['hdnPgaction'];
        $(pga).value = 'delete';
        var fid = f[fld];
		$(fid).value = id;
		f.submit();
    }
}

function _deleteUpgradeV1(fld,id,message) {
	if(confirm(message)) {
        var f = $('frmMain');
        var pga = f['hdnPgaction'];
        $(pga).value = 'delete';
        var fid = f[fld];
		$(fid).value = id;
		f.submit();
    }
}

function _cancel(page_name,hdnPageNo) {
	if(hdnPageNo == '') {
		hdnPageNo = 1;
	}
	var redirect = page_name + "?hdnPageNo=" + hdnPageNo;
	location.href = redirect;
}

function Change_Status(fld,id,status_action) {
	var msg;
	if(status_action=='N'){
		msg = 'Do you want to disable the record ?';
	}else{
		msg = 'Do you want to enable the record ?';
	}
	//var st = confirm('Want to change status !!');
	var st = confirm(msg);
	if(st) {
		var f = $('frmMain');
		var pga = f['hdnPgaction'];
		$(pga).value = 'change_status';
		var fid = f['fld'];
		$(fid).value = fld;
		var hid = f['hdn_fld_id'];
		$(hid).value = id;
		var s_action  = f['status_action'];
		$(s_action).value = status_action;
		f.submit();
		return true;
	}
}

function Change_Status_Vendor(fld,id,status_action,message) {
	var st = confirm(message);
	if(st) {
		var f = $('frmMain');
		var pga = f['hdnPgaction'];
		$(pga).value = 'change_status';
		var fid = f['fld'];
		$(fid).value = fld;
		var hid = f['hdn_fld_id'];
		$(hid).value = id;
		var s_action  = f['status_action'];
		$(s_action).value = status_action;
		f.submit();
		return true;
	}
}

function EnableDisableURL(fld,id,status_action,message) {
	var st = confirm(message);
	if(st) {
		var f = $('frmMain');
		var pga = f['hdnPgaction'];
		$(pga).value = 'enable_disable_url';
		var fid = f['fld'];
		$(fid).value = fld;
		var hid = f['hdn_fld_id'];
		$(hid).value = id;
		var s_action  = f['status_action'];
		$(s_action).value = status_action;
		f.submit();
		return true;
	}
}

function Change_Feature(fld,id,feature_action) {
	var f = $('frmMain');
	var pga = f['hdnPgaction'];
	$(pga).value = 'change_feature';
	var hid = f['hdn_fld_id'];
	$(hid).value = id;
	var f_action  = f['feature_action'];
	$(f_action).value = feature_action;
	f.submit();
	return true;
}

// start of Bulk_Delete
function Bulk_Delete(frmMain,chk_deleted_ids) {
	var str = "";
	var flag = 0;
	var len = document.frmMain.chk_deleted_ids.length;
	if(len>0) {
		for(i=0;i<len;i++) {
			if(document.frmMain.chk_deleted_ids[i].checked==true) {
				str+=document.frmMain.chk_deleted_ids[i].value+",";
				flag = 1;
			}
		}
	}
	else {
		if(document.frmMain.chk_deleted_ids.checked==true) {
				str+=document.frmMain.chk_deleted_ids.value+",";
				flag = 1;
		}
	}
	if(flag == 0) {
		alert("please select atleast one record");
		return false;
	}
	else {
		var f = $('frmMain');
		var pga = f['hdnPgaction'];
		$(pga).value = 'delete_selected';
		var del_sel_records = f['del_selected_records'];
		$(del_sel_records).value = str;
		return true;
	}
}
// end of Bulk_Delete

// start checkAll
var isChecked = false;
function checkAll(frmMain) {
	var check;
	check=false;
	if (isChecked) {
		isChecked = false;
		check = false;
	} else {
		isChecked = true;
		check = true;
	}		
	for(var i=0;i<=frmMain.elements.length-1;i++) {
		if (check) { 
			document.frmMain.elements[i].checked=true;
		} else {
			document.frmMain.elements[i].checked=false;
		}
	}
} 
// end of checkAll

function removeElement(divNum) {
  var d = document.getElementById('myDiv');
  var olddiv = document.getElementById(divNum);
  d.removeChild(olddiv);
}

function addElement() {
  var ni = document.getElementById('myDiv');
  var numi = document.getElementById('theValue');
  var num = (document.getElementById('theValue').value -1)+ 2;
  numi.value = num;
  var newdiv = document.createElement('div');
  var divIdName = 'my'+num+'Div';
  newdiv.setAttribute('id',divIdName);
  newdiv.innerHTML = 'Upload Image : <input name="more_hall_image[]" id="'+divIdName+'" type="file" /> <input type="button" name="btnRemove" value="Remove" onclick=\'removeElement("'+divIdName+'")\' />';
  ni.appendChild(newdiv);
}

function removeElement_video(divNum) {
  var d = document.getElementById('myDiv_video');
  var olddiv = document.getElementById(divNum);
  d.removeChild(olddiv);
}

function addElement_video() {
  var ni = document.getElementById('myDiv_video');
  var numi = document.getElementById('theValue_video');
  var num = (document.getElementById('theValue_video').value -1)+ 2;
  numi.value = num;
  var newdiv = document.createElement('div');
  var divIdName = 'my'+num+'Div';
  newdiv.setAttribute('id',divIdName);
  newdiv.innerHTML = 'Upload Video : <input name="more_hall_video[]" id="'+divIdName+'" type="file" /> <input type="button" name="btnRemove" value="Remove" onclick=\'removeElement_video("'+divIdName+'")\' />';
  ni.appendChild(newdiv);
}

function emailCheck(obj) {
	var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i
	var returnval=emailfilter.test(obj.value);
	return returnval;
}

function toggleCountry(country) {
    if(country=='Others') {
        document.getElementById("user_city").disabled="true";
    }
    if(country=='India') {
        document.getElementById("user_city").disabled="";
    }
}

function removeElementByParent(parentDiv, childDiv) {
     if (childDiv == parentDiv) {
          alert("The parent element cannot be removed.");
     } else if (document.getElementById(childDiv)) {

          var child = document.getElementById(childDiv);
          var parent = document.getElementById(parentDiv);
          parent.removeChild(child);
          return false;
     } else {
          alert("Child element has already been removed or does not exist.");
          //return false;
     }
}

function setCountry(obj) {     
        var state=document.getElementById("search_state");
        if(obj=='India') {
                state.disabled=false;
        } else {
                state.disabled=true;
        }
}

function setStateForIndia(obj,fldName) {

        var state=document.getElementById(fldName);
        if(obj=='India') {
				state.disabled=false;
        } else {
                document.getElementById(fldName).options[0].selected=true;
				state.disabled=true;
				var spanId = document.getElementById('div_city');
				spanId.innerHTML='<input type="text" name="user_city" id="user_city" value="" tabindex="9" class="starea" />';
        }
}

/***************** Only BR tags ****************************/
// Set up my regular expressions that will match the HTML tags and attributes that I want to allow
//var reAllowedAttributes = /^(face|size|style|dir|color|id|class|alignment|align|valign|rowspan|colspan|width|height|background|cellspacing|cellpadding|border|href|src|target|alt|title)$/i
//var reAllowedHTMLTags = /^(h1|h2|a|img|b|em|li|ol|p|pre|strong|ul|font|span|div|u|sub|sup|table|tbody|blockquote|tr|td)$/i
function ParseHTML(theHTML){
      var reAllowedAttributes = /^()$/i
      var reAllowedHTMLTags = /^(br)$/i

      // Start of with a test to match all HTML tags and a group for the tag name which we pass in as an extra parameter
      theHTML = theHTML.replace(/<[/]?([^> ]+)[^>]*>/g, function(match,HTMLTag)
      {
            // if the HTML tag does not match our list of allowed tags return empty string which will be used as a
            // a replacement for the pattern in our inital test.
            if(!reAllowedHTMLTags.test(HTMLTag)){
                  return "";
            }else{
                  // The HTML tag is allowed so check attributes with the tag

                  // Certain attributes are allowed so we do another replace statement looking for attributes and using another
                  // function for the replacement value.
                  match = match.replace(/ ([^=]+)="[^"]*"/g, function(match2, attributeName)
                  {
                        // If the attribute matches our list of allowed attributes we return the whole match string
                        // so we replace our match with itself basically allowing the attribute.
                        if(reAllowedAttributes.test(attributeName)){
                              return match2;
                        }else{
                              return "";  // not allowed so return blank string to wipe out the attribute value pair
                        }
                  });

            }
            return match;

      }); //end of the first replace

      //return our cleaned HTML
      return theHTML;
}
/*********************************************/

/** url validation **/
function isUrl(s) {
	var regexp = /(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
	return regexp.test(s);
}
/** url validation **/

function poptastic(url) {
        var newwindow;
    	newwindow=window.open(url,'name','height=450,width=800,left=100,top=100,resizable=no,scrollbars=yes,toolbar=no,status=no');
        if (window.focus) {newwindow.focus();}
}
