// JavaScript Document

var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i

function search_news_check() {
	var object = document.search_news;
	if(object.search.value == "") {
		alert("Insert value!");
		object.search.focus();
		return false;
	}
}

function show(id,page)
{
	el = document.getElementById(id);
	if (el.style.display == 'none')
	{
		el.style.display = '';
		el = document.getElementById("menu_"+id);
		el.innerHTML = "Hide Form";
	} else {
		el.style.display = 'none';
		el = document.getElementById("menu_"+id);
		el.innerHTML = "Add " + page;
	}
}

function ShowHideMenu(id,HideText,ShowText)
{
	HideText = (HideText == "") ? 'Hide Form' : HideText;
	el = document.getElementById(id);
	if (el.style.display == 'none')
	{
		el.style.display = '';
		el = document.getElementById("menu_"+id);
		el.innerHTML = HideText;
	} else {
		el.style.display = 'none';
		el = document.getElementById("menu_"+id);
		el.innerHTML = ShowText;
	}
}

function confirmation(message) {
	var conf = confirm(message); // confirm = jendela pertanyaan (Ok/Cancel)
	if(conf) 
	{
		return true;
	}
	return false;
}

function login() {
	
}

function logout(message) 
{
	ht = document.getElementsByTagName("body");
	ht[0].style.filter = "progid:DXImageTransform.Microsoft.BasicImage(grayscale=1)";
	var conf = confirm(message); // confirm = jendela pertanyaan (Ok/Cancel)
	if(conf) 
	{
		return true;
	}
	ht[0].style.filter = "";
	return false;
}

function SetBool(value,name,url) {
	location.href = url+"&"+name+"="+value;
}

function textCounter(field,cntfield,maxlimit) {
	if (field.value.length > maxlimit) {// if too long...trim it!
		field.value = field.value.substring(0, maxlimit);
		// otherwise, update 'characters left' counter
	} else {
		cntfield.value = maxlimit - field.value.length;
	}
}

function ShowHideElement(elementID) {
	var prop
	if (document.getElementById) 
		prop = document.getElementById(elementID).style
	else if (document.all)
		prop = document.all[elementID].style
	else if (document.layers)
		prop = document.layers[elementID]
	
	if (prop.display=='none')
		prop.display = ''
	else
		prop.display = 'none'
}

function ShowHideElementManual(elementID,Display) {
	var prop
	if (document.getElementById) 
		prop = document.getElementById(elementID).style
	else if (document.all)
		prop = document.all[elementID].style
	else if (document.layers)
		prop = document.layers[elementID]
	
		prop.display = Display
}

function doProcessFilter(FormName,Option) {
	var object = eval("document."+FormName);
	if(object.time_frame.value == "custom" && (object.start.value == "" && object.end.value == "")) {
		document.getElementById("popUpCustomPost").style.display = "";
	} else {
		if(object.time_frame.value != "custom") {
			object.start.value = "";
			object.end.value = "";
		}
		if(Option == 1) {
			location.href = "?s=yes&csid="+object.clientset_id.value+"&msid="+object.mediaset_id.value+"&cid="+object.client_id.value+"&mid="+object.media_id.value+"&ntl="+object.news_title.value+"&nc="+object.news_content.value+"&pw="+object.time_frame.value+"&cts="+object.start.value+"&cte="+object.end.value;
		} else {
			location.href = "?s=yes&csid="+object.clientset_id.value+"&msid="+object.mediaset_id.value+"&cid="+object.client_id.value+"&mid="+object.media_id.value+"&ntl="+object.news_title.value+"&nc="+object.news_content.value+"&pw="+object.time_frame.value+"&cts="+object.start.value+"&cte="+object.end.value+"&sw="+object.show.value;
		}
	}
}


/* MEMBER SECTION */
function chk_MembershipType() {
	var object = document.member_registration;
	if(object.membership_type[0].checked == true && object.membership_type[0].value == "free") {
		ShowHideElementManual('paid_section','none')
	} else {
		ShowHideElementManual('paid_section','')
	}
}
function chk_AccountType() {
	var object = document.member_registration;
	if(object.account_type[0].checked == true && object.account_type[0].value == "personal") {
		ShowHideElementManual('company_section','none')
	} else {
		ShowHideElementManual('company_section','')
	}
}

function checkLogin() {
	var object = document.login;
	if(object.username.value == "") {
		alert("Insert your username!");
		object.username.focus();
		return false;
	}
	if(object.password.value == "") {
		alert("Insert your password!");
		object.password.focus();
		return false;
	}
}

function checkInsighter() {
	var object = document.frmInsighter;
	if(object.Name.value == "") {
		alert("Insert your name!");
		object.Name.focus();
		return false;
	}
	if(object.Email.value == "") {
		alert("Insert your e-mail!");
		object.Email.focus();
		return false;
	}
	if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(object.Email.value))) {
		alert("Invalid e-mail address!");
		object.Email.focus();
		return false;
	}
	if(object.Comment.value == "") {
		alert("Insert your comment!");
		object.Comment.focus();
		return false;
	}
	if(object.SecurityCode.value == "") {
		alert("Insert security code!");
		object.SecurityCode.focus();
		return false;
	}
}

function checkContact() {
	var object = document.frmContact;
	if(object.Name.value == "") {
		alert("Insert your name!");
		object.Name.focus();
		return false;
	}
	if(object.Email.value == "") {
		alert("Insert your e-mail!");
		object.Email.focus();
		return false;
	}
	if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(object.Email.value))) {
		alert("Invalid e-mail address!");
		object.Email.focus();
		return false;
	}
	if(object.Comments.value == "") {
		alert("Insert your comment!");
		object.Comments.focus();
		return false;
	}
	if(object.SecurityCode.value == "") {
		alert("Insert security code!");
		object.SecurityCode.focus();
		return false;
	}
}

function chk_member_registration() {
	var object = document.member_registration;
	if(object.username.value == "") {
		alert("Insert your username!");
		object.username.focus();
		return false;
	}
	if(object.password.value == "") {
		alert("Insert your password!");
		object.password.focus();
		return false;
	}
	if(object.password2.value == "") {
		alert("Re-type your password!");
		object.password2.focus();
		return false;
	}
	if(object.password.value != object.password2.value) {
		alert("Your password not match!");
		object.password.value = "";
		object.password2.focus();
		return false;
	}
	if(object.email.value == "") {
		alert("Insert your email address!");
		object.email.focus();
		return false;
	}
	if(object.id_card.value == "") {
		alert("Insert your ID Card!");
		object.id_card.focus();
		return false;
	}
	if(object.full_name.value == "") {
		alert("Insert your full name!");
		object.full_name.focus();
		return false;
	}
	if(object.home_address.value == "") {
		alert("Insert your home address!");
		object.home_address.focus();
		return false;
	}
	if(object.account_type.company.checked == true) {
		if(object.company_name.value == "") {
			alert("Insert your company name!");
			object.company_name.focus();
			return false;
		}
		if(object.company_address.value == "") {
			alert("Insert your company address!");
			object.company_address.focus();
			return false;
		}
		if(object.company_phone.value == "") {
			alert("Insert your company phone!");
			object.company_phone.focus();
			return false;
		}
	}
	if(object.membership_type.paid.checked == true) {
		if(object.term.value == "null") {
			alert("Choose term!");
			object.term.focus();
			return false;
		}
	}
}

function change_password() {
	var object = document.frmChangePassword;
	if(object.old_password.value == "") {
		alert("Insert your old password!");
		object.old_password.focus();
		return false;
	}
	if(object.new_password.value == "") {
		alert("Insert your new password!");
		object.new_password.focus();
		return false;
	}
	if(object.retype_password.value == "") {
		alert("Re-type your new password!");
		object.retype_password.focus();
		return false;
	}
	if(object.new_password.value != object.retype_password.value) {
		alert("Your new password not match!");
		object.retype_password.value = "";
		object.retype_password.focus();
		return false;
	}
	return confirm('Are you sure want to change your old password!');
}

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_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_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];}
}

function Search(url) {
	var q = document.getElementById("q");
	var server = document.getElementById("server");
	location.href = url+"search/?q="+q.value+"&server="+server.value;
}

function SetPositionTopCenter(id) {
	var Element = document.getElementById(id);
	Element.style.display = "";
	objw = parseFloat(Element.style.width)/2;
	Element.style.top = '120px';
	Element.style.left = Math.floor(Math.round((document.documentElement.offsetWidth/2)+document.documentElement.scrollLeft)-objw)+'px';	
}

//** Tab Content script v2.0- © Dynamic Drive DHTML code library (http://www.dynamicdrive.com)
/*tabcontent.js*/
function ddtabcontent(tabinterfaceid){
	this.tabinterfaceid=tabinterfaceid 
	this.tabs=document.getElementById(tabinterfaceid).getElementsByTagName("a") 
	this.enabletabpersistence=true
	this.hottabspositions=[] 
	this.subcontentids=[] 
	this.revcontentids=[] 
	this.selectedClassTarget="link" 
}

ddtabcontent.getCookie=function(Name){ 
	var re=new RegExp(Name+"=[^;]+", "i"); 
	if (document.cookie.match(re)) 
		return document.cookie.match(re)[0].split("=")[1] 
	return ""
}

ddtabcontent.setCookie=function(name, value){
	document.cookie = name+"="+value+";path=/" 
}

ddtabcontent.prototype={

	expandit:function(tabid_or_position){ //PUBLIC function to select a tab either by its ID or position(int) within its peers
		this.cancelautorun() 
		var tabref=""
		try{
			if (typeof tabid_or_position=="string" && document.getElementById(tabid_or_position).getAttribute("rel")) //if specified tab contains "rel" attr
				tabref=document.getElementById(tabid_or_position)
			else if (parseInt(tabid_or_position)!=NaN && this.tabs[tabid_or_position].getAttribute("rel")) //if specified tab contains "rel" attr
				tabref=this.tabs[tabid_or_position]
		}
		catch(err){alert("Invalid Tab ID or position entered!")}
		if (tabref!="")
			this.expandtab(tabref) 
	},

	setpersist:function(bool){
			this.enabletabpersistence=bool
	},

	setfade:function(bool){ 
			this.enablefade=bool
	},
	setselectedClassTarget:function(objstr){ //PUBLIC function to set which target element to assign "selected" CSS class ("linkparent" or "link")
		this.selectedClassTarget=objstr || "link"
	},

	getselectedClassTarget:function(tabref){ 
		return (this.selectedClassTarget==("linkparent".toLowerCase()))? tabref.parentNode : tabref
	},

	expandtab:function(tabref){
		var subcontentid=tabref.getAttribute("rel") 
		
		var associatedrevids=(tabref.getAttribute("rev"))? ","+tabref.getAttribute("rev").replace(/\s+/, "")+"," : ""
		this.expandsubcontent(subcontentid)
		this.expandrevcontent(associatedrevids)
		for (var i=0; i<this.tabs.length; i++){ //Loop through all tabs, and assign only the selected tab the CSS class "selected"


			this.getselectedClassTarget(this.tabs[i]).className=(this.tabs[i].getAttribute("rel")==subcontentid)? "selected" : ""
		}
		if (this.enabletabpersistence) //if persistence enabled, save selected tab position(int) relative to its peers
			ddtabcontent.setCookie(this.tabinterfaceid, tabref.tabposition)
	},

	expandsubcontent:function(subcontentid){
		for (var i=0; i<this.subcontentids.length; i++){

			var subcontent=document.getElementById(this.subcontentids[i]) //cache current subcontent obj (in for loop)	

			subcontent.style.display=(subcontent.id==subcontentid)? "block" : "none" //"show" or hide sub content based on matching id attr value

			if(this.enablefade){
			fadeFx = new Fx.Style(this.subcontentids[i], 'opacity', {
				duration: 1000, 
				transition: Fx.Transitions.Quart.easeInOut
			});

			fadeFx.start(0.2,1); 

			}
		}
	},


	expandrevcontent:function(associatedrevids){
		var allrevids=this.revcontentids
		for (var i=0; i<allrevids.length; i++){ //Loop through rev attributes for all tabs in this tab interface
			//if any values stored within associatedrevids matches one within allrevids, expand that DIV, otherwise, contract it

			document.getElementById(allrevids[i]).style.display=(associatedrevids.indexOf(","+allrevids[i]+",")!=-1)? "block" : "none"

		}
	},

	autorun:function(){ //function to auto cycle through and select tabs based on a set interval
		var currentTabIndex=this.automode_currentTabIndex //index within this.hottabspositions to begin
		var hottabspositions=this.hottabspositions //Array containing position numbers of "hot" tabs (those with a "rel" attr)
		this.expandtab(this.tabs[hottabspositions[currentTabIndex]])
		this.automode_currentTabIndex=(currentTabIndex<hottabspositions.length-1)? currentTabIndex+1 : 0 //increment currentTabIndex
	},

	cancelautorun:function(){
		if (typeof this.autoruntimer!="undefined")
			clearInterval(this.autoruntimer)
	},

	init:function(automodeperiod){
		var persistedtab=ddtabcontent.getCookie(this.tabinterfaceid) //get position of persisted tab (applicable if persistence is enabled)
		var persisterror=true //Bool variable to check whether persisted tab position is valid (can become invalid if user has modified tab structure)
		this.automodeperiod=automodeperiod || 0
		for (var i=0; i<this.tabs.length; i++){



			this.tabs[i].tabposition=i //remember position of tab relative to its peers
			if (this.tabs[i].getAttribute("rel")){
				var tabinstance=this
				this.hottabspositions[this.hottabspositions.length]=i //store position of "hot" tab ("rel" attr defined) relative to its peers
				this.subcontentids[this.subcontentids.length]=this.tabs[i].getAttribute("rel") //store id of sub content ("rel" attr value)
				this.tabs[i].onclick=function(){
					tabinstance.expandtab(this)
					tabinstance.cancelautorun() //stop auto cycling of tabs (if running)
					return false
				}
				if (this.tabs[i].getAttribute("rev")){ //if "rev" attr defined, store each value within "rev" as an array element
					this.revcontentids=this.revcontentids.concat(this.tabs[i].getAttribute("rev").split(/\s*,\s*/))
				}
				if (this.enabletabpersistence && parseInt(persistedtab)==i || !this.enabletabpersistence && this.getselectedClassTarget(this.tabs[i]).className=="selected"){
					this.expandtab(this.tabs[i]) //expand current tab if it's the persisted tab, or if persist=off, carries the "selected" CSS class
					persisterror=false //Persisted tab (if applicable) was found, so set "persisterror" to false
					//If currently selected tab's index(i) is greater than 0, this means its not the 1st tab, so set the tab to begin in automode to 1st tab:
					this.automode_currentTabIndex=(i>0)? 0 : 1
				}
			}
		} //END for loop
		if (persisterror) //if an error has occured while trying to retrieve persisted tab (based on its position within its peers)
			this.expandtab(this.tabs[this.hottabspositions[0]]) //Just select first tab that contains a "rel" attr
		if (parseInt(this.automodeperiod)>500 && this.hottabspositions.length>1){
			this.automode_currentTabIndex=this.automode_currentTabIndex || 0
			this.autoruntimer=setInterval(function(){tabinstance.autorun()}, this.automodeperiod)
		}
	} 

} 
