// JavaScript Document

//////////////////////////
// Open a new browser window

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}


//////////////////////////
// Preload and swap images

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];}
}


//////////////////////////
// Show / Hide bars on home page

function show_hide(obj) {
	var obj_content = document.getElementById(obj).innerHTML;
	var obj_table = obj + 'Table';
	
	if (obj_content == 'عرض') { 
		document.getElementById(obj).innerHTML = 'إخفاء' ;
		document.getElementById(obj_table).style.display='block';
		MM_swapImage(obj+'icon','','Bar_minus.gif',1);
	}

	else if (obj_content == 'إخفاء') { 
		document.getElementById(obj).innerHTML = 'عرض' ;
		document.getElementById(obj_table).style.display='none';
		MM_swapImage(obj+'icon','','Bar_plus.gif',1);
	}
}


//////////////////////////
// Website's blue sections/tabs on home page

function tabs(obj,group) {
			
	if (obj == 1) { 
		document.getElementById(group + '1').style.display='block'; 
		document.getElementById(group + '2').style.display='none'; 
		document.getElementById(group + '3').style.display='none'; 
		
		if (group == 'radioTable') { 
			document.getElementById('Tabs').style.backgroundImage='url(http://www.radiodijla.com/tab_bg_radio1.jpg)';
			document.getElementById('LiveLink').style.color='#CC0000';
		}
		else { 
			document.getElementById(group + 'Title1').style.backgroundImage='url(http://www.radiodijla.com/bg_active.jpg)';
			document.getElementById(group + 'Title1').style.borderBottom='1px solid #FFFFFF';
			document.getElementById(group + 'Title1Style').style.font='bold 11pt arial';
			
			document.getElementById(group + 'Title2').style.backgroundImage='url(http://www.radiodijla.com/bg_inactive.jpg)';
			document.getElementById(group + 'Title2').style.borderBottom='1px solid #CCCCCC';
			document.getElementById(group + 'Title2Style').style.font='9pt tahoma';
			
			document.getElementById(group + 'Title3').style.backgroundImage='url(http://www.radiodijla.com/bg_inactive.jpg)';
			document.getElementById(group + 'Title3').style.borderBottom='1px solid #CCCCCC';
			document.getElementById(group + 'Title3Style').style.font='9pt tahoma';
		}
	}

	else if (obj == 2) { 
		//document.getElementById(group + '1').style.display='none';
		document.getElementById(group + '2').style.display='block';
		document.getElementById(group + '3').style.display='none';
		
		if (group == 'radioTable') { 
			document.getElementById('Tabs').style.backgroundImage='url(http://www.radiodijla.com/tab_bg_radio2.jpg)';
			document.getElementById('LiveLink').style.color='#000000';
		}
		else { 
			//document.getElementById(group + 'Title1').style.backgroundImage='url(http://www.radiodijla.com/bg_inactive.jpg)';
			//document.getElementById(group + 'Title1').style.borderBottom='1px solid #CCCCCC';
			//document.getElementById(group + 'Title1Style').style.font='9pt tahoma';
			
			document.getElementById(group + 'Title2').style.backgroundImage='url(http://www.radiodijla.com/bg_active.jpg)';
			document.getElementById(group + 'Title2').style.borderBottom='1px solid #FFFFFF';
			document.getElementById(group + 'Title2Style').style.font='bold 11pt arial';
			
			document.getElementById(group + 'Title3').style.backgroundImage='url(http://www.radiodijla.com/bg_inactive.jpg)';
			document.getElementById(group + 'Title3').style.borderBottom='1px solid #CCCCCC';
			document.getElementById(group + 'Title3Style').style.font='9pt tahoma';
		}
	}
	
	else if (obj == 3) { 
		//document.getElementById(group + '1').style.display='none'; 
		document.getElementById(group + '2').style.display='none'; 
		document.getElementById(group + '3').style.display='block'; 
		
		if (group == 'radioTable') { 
			document.getElementById(group + '1').style.display='none'; 
			document.getElementById('Tabs').style.backgroundImage='url(http://www.radiodijla.com/tab_bg_radio3.jpg)';
			document.getElementById('LiveLink').style.color='#000000';
		}
		else { 
			//document.getElementById(group + 'Title1').style.backgroundImage='url(http://www.radiodijla.com/bg_inactive.jpg)';
			//document.getElementById(group + 'Title1').style.borderBottom='1px solid #CCCCCC';
			//document.getElementById(group + 'Title1Style').style.font='9pt tahoma';
			
			document.getElementById(group + 'Title2').style.backgroundImage='url(http://www.radiodijla.com/bg_inactive.jpg)';
			document.getElementById(group + 'Title2').style.borderBottom='1px solid #CCCCCC';
			document.getElementById(group + 'Title2Style').style.font='9pt tahoma';
			
			document.getElementById(group + 'Title3').style.backgroundImage='url(http://www.radiodijla.com/bg_active.jpg)';
			document.getElementById(group + 'Title3').style.borderBottom='1px solid #FFFFFF';
			document.getElementById(group + 'Title3Style').style.font='bold 11pt arial';
		}
	}
}


//////////////////////////
// Switching between bottom and top tabs (in the radio square) on the home page

function cpTransition(a, i, t, d) 
{
    i.filters[0].Apply();
	a.filters[0].Apply();	
	
	if(a.style.visibility == "visible") {
		i.style.display = "block";
		i.style.visibility = "visible";
	}
	
	a.style.visibility = "hidden";
	a.style.display = "none";
	
	i.filters[0].duration=d;
	i.filters[0].transition=t;
    i.filters[0].Play();
}

function cpTransitionReverse(a, i, t, d) 
{
    i.filters[0].Apply();
	a.filters[0].Apply();
	
	if(i.style.visibility == "visible") {
		i.style.visibility = "hidden";
		i.style.display = "none";
	}
	
	a.style.display = "block";
	a.style.visibility = "visible";

	a.filters[0].duration=d;
	a.filters[0].transition=t;
    a.filters[0].Play();
}


////////////////////////////////////////////////////////////
// Voting

function submitme(url) {
	if (document.all.yourAnswer.value > 0) {
		url = url + "option_selected=" + document.all.yourAnswer.value 
				  + "&poll_id=" + document.all.poll_id.value;

		var coll = document.all.item("option_selected");
		if (coll) {
			for (var i=0; i<coll.length; i++) {
				coll(i).checked = false;
				coll(i).disabled = true;
			}
		}

		var voteButton = document.all.item("voteButton");
		if (voteButton) 
			voteButton.removeAttribute("href"); 

		document.all.yourAnswer.value = 0;
		window.open(url,'','scrollbars=no,width=500,height=280,resizable=yes');	
	}
}

////////////////////////////////////////////////////////////
// Checking empty fields in shareopinion form

function sharechecking() {
	var name = document.all.name.value.replace(/^\s*|\s*$/g,'');
	var comment = document.all.comment.value.replace(/^\s*|\s*$/g,'');
	var country = document.all.country.value;
	
	document.all.name.value = name;
	document.all.comment.value = comment;
	
	if((comment.length == "")||(name.length == "")||(country.length == "")) {
		alert('لا يمكن المتابعة دون ملء كل الحقول');
		return false;
	}
	
	if(comment.length < 10) {
		alert('مشاركتك قصيرة جداً.. حاول مرة أخرى');
		return false;
	}

	else { return true }
}

function sharesubmitting() {
	if( sharechecking() ) {
		document.contactForm.submit();
		
		document.all.comment.value='';
		document.all.name.value='';
		document.all.item("continent").selectedIndex = '0';
		document.contactForm.country.options[0]=new Option("إختر إسم القارة أولاً","",true,true);
		
		document.all.remaining.value='600';

		//window.open('http://www.radiodijla.com/thankscomment.htm','','scrollbars=no,width=260,height=200');
	}
}


////////////////////////////////////////////////////////////
// Checking empty fields in shareopinion form - Comment Only

function sharechecking_comment() {
	var comment = document.all.comment.value.replace(/^\s*|\s*$/g,'');

	document.all.comment.value = comment;
	
	if(comment.length == "") {
		alert('لا يمكن المتابعة دون إضافة تعليق');
		return false;
	}
	
	if(comment.length < 10) {
		alert('مشاركتك قصيرة جداً.. حاول مرة أخرى');
		return false;
	}

	else { return true }
}

function sharesubmitting_comment() {
	if( sharechecking_comment() ) {
		document.contactForm.submit();
		
		document.all.comment.value='';
		
		document.all.remaining.value='600';

		//window.open('http://www.radiodijla.com/thankscomment.htm','','scrollbars=no,width=260,height=200');
	}
}


////////////////////////////////////////////////////////////
// Checking empty fields in contacts forms

function checking() {
	var name = document.all.nameText.value;
	var email = document.all.emailText.value;
	var letter = document.all.mainText.value;
	
	var ProgsList = document.all.item("programs");
	if (ProgsList) {
		selected = document.all.item("programs").selectedIndex;
		list = document.all.item("programs").options[selected].value;
		if(list == "") { 
			alert('لا يمكن المتابعة دون اختيار أحد البرامج');
			return false;
		}
	}
	
	if((name=="")||(email=="")||(letter=="")) {
		alert('لا يمكن المتابعة دون ملء كل الحقول');
		return false;
	}
	
	if(name=="أدخل إسمك هنا..") {
		alert('لا يمكن المتابعة دون إدخال إسمك');
		return false;
	}
	
	if((name.length < 3) || (name.length > 24)) {
		alert('  عدد حروف الإسم يجب أن يكون محصوراً\n بين 3 إلى 14 حرفاً ومن ضمنها الفراغات');
		return false;
	}

	if((letter.length < 10) || (letter.length > 250)) {
		alert('  عدد حروف الرسالة يجب أن يكون محصوراً\n بين 10 إلى 250 حرفاً ومن ضمنها الفراغات');
		return false;
	}

	if (checkEmail(email)==false){
		return false
	}
	
	else { return true }
}

function submitting() {
	if( checking() ) {
		document.contactus.submit();
		
		var ProgsList = document.all.item("programs");
		if (ProgsList) { document.all.item("programs").selectedIndex = '0';	}
		
		document.all.nameText.value='';
		document.all.emailText.style.direction='rtl';
		document.all.emailText.value='';
		document.all.mainText.value='';
		//window.open('http://www.radiodijla.com/thanks.htm','','scrollbars=no,width=260,height=40');
	}
}

//////////////////////////
// Checking empty fields of the contact-studio form

function checkingFields() {
	var nameText 	= document.all.name.value.replace(/^\s*|\s*$/g,'');
	var countryText = document.all.country.value.replace(/^\s*|\s*$/g,'');
	var msgText 	= document.all.msg.value.replace(/^\s*|\s*$/g,'');
	
	document.all.name.value = nameText;
	document.all.country.value = countryText;
	document.all.msg.value = msgText;
	
	if((nameText == "")||(countryText == "")||(msgText == "")) {
		alert('لا يمكن المتابعة دون ملء كل الحقول');
		return false;
	}
	
	if((nameText.length < 3) || (nameText.length > 24)) {
		alert('  عدد حروف الإسم يجب أن يكون محصوراً\n بين 3 إلى 14 حرفاً ومن ضمنها الفراغات');
		return false;
	}
	
	if((countryText.length < 3) || (countryText.length > 26)) {
		alert('  عدد حروف البلد يجب أن يكون محصوراً\n بين 3 إلى 14 حرفاً ومن ضمنها الفراغات');
		return false;
	}
	
	if((msgText.length < 10) || (msgText.length > 500)) {
		alert('  عدد حروف الرسالة يجب أن يكون محصوراً\n بين 10 إلى 500 حرفاً ومن ضمنها الفراغات');
		return false;
	}

	else { return true }
}

function submittingFields(HomePage) {
	if( checkingFields() ) {
		document.contactForm.submit();
		
		if (HomePage == 'HomePage') {
			
			document.all.item("continent").selectedIndex = '0';
			document.contactForm.country.options.length = 0;
			document.contactForm.country.options[0]=new Option("إختر إسم القارة أولاً","",true,true);
			
			document.all.name.value='';
			document.all.country.value='';
			document.all.msg.value='';
			
			//window.open('http://www.radiodijla.com/messenger/thanks.htm','','scrollbars=no,width=320,height=140');
		}
	}
}


////////////////////////////////////////////////
// Checking the registration form

function checkingReg() {
	var firstnameReg 	= document.all.first_name.value.replace(/^\s*|\s*$/g,'');
	var lastnameReg 	= document.all.last_name.value.replace (/^\s*|\s*$/g,'');
	var usernameReg 	= document.all.username.value.replace  (/^\s*|\s*$/g,'');
	
	var passwordReg 	= document.all.password.value.replace(/^\s*|\s*$/g,'');
	
	var emailReg 		= document.all.email.value.replace(/^\s*|\s*$/g,'');
	
	var genderReg 		= document.all.gender.value;
	
	var birthyearReg 	= document.all.birth_year.value.replace(/^\s*|\s*$/g,'');	
	
	//-----------
	var locationReg 	= document.all.location.value;
	
	if (locationReg == "inside_iraq") {
		countryReg == document.all.country.value;
	}
	
	else if (locationReg == "outside_iraq") {
		countryReg == document.all.iraqi_province.value;
	}
	//-----------
	
	document.all.first_name.value 	= firstnameReg;
	document.all.last_name.value  	= lastnameReg;
	document.all.username.value 	= usernameReg;
	document.all.password.value 	= passwordReg;
	document.all.email.value 		= emailReg;
	document.all.birth_year.value 	= birthyearReg;

	// ||(countryReg == "")||(continentReg == "")
	if((firstnameReg == "")||(lastnameReg == "")||(usernameReg == "")||(passwordReg == "")||(emailReg == "")||(birthyearReg == "")||(!genderReg)||(!locationReg)||(!countryReg)) {
		alert('لا يمكن المتابعة دون ملء كل الحقول');
		return false;
	}
	
	if(firstnameReg.length < 2) {
		alert('يجب أن لا يقل الإسم الأول أو إسم العائلة عن حرفين');
		setcursor('first_name');
		return false;
	}
	
	if(lastnameReg.length < 2) {
		alert('يجب أن لا يقل الإسم الأول أو إسم العائلة عن حرفين');
		setcursor('last_name');
		return false;
	}
	
	if((usernameReg.length < 4) || (usernameReg.length > 20)) {
		alert('يجب أن يكون عدد حروف إسم المستخدم محصوراً بين 4 إلى 20 حرفاً ومن ضمنه الفراغات');
		setcursor('username');
		return false;
	}
	
	if((passwordReg.length < 6) || (passwordReg.length > 12)) {
		alert('يجب أن يكون عدد حروف كلمة السر محصوراً بين 6 إلى 12 حرفاً');
		setcursor('password');
		return false;
	}
	
	if (checkEmail(emailReg)==false){
		return false;
	}
	
	
	if((birthyearReg.length < 4) || (birthyearReg.length > 4)) {
		alert('يجب أن يكون عدد أرقام سنة الميلاد 4');
		setcursor('birth_year');
		return false;
	}

	else { return true; }
}

function submittingReg() {
	if( checkingReg() ) {
		document.contactForm.submit();
	}
}


// Checking the update form
function checkingUpdate() {
	var usernameUpdate 	= document.all.username.value.replace(/^\s*|\s*$/g,'');
	var passwordUpdate 	= document.all.password.value.replace(/^\s*|\s*$/g,'');
	var phoneUpdate 		= document.all.phone.value.replace(/^\s*|\s*$/g,'');

	document.all.username.value 	= usernameUpdate;
	document.all.password.value 	= passwordUpdate;
	document.all.phone.value 		= phoneUpdate;
	
	return true;
}

function submittingUpdate() {
	if( checkingUpdate() ) {
		document.contactForm.submit();
	}
}

function checkEmail(str) {
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);

	if (str.indexOf(at)==-1){
	   alert("صياغة غير صحيحة للبريد الإلكتروني");
	   setcursor('email');
	   return false;
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   alert("صياغة غير صحيحة للبريد الإلكتروني");
	   setcursor('email');
	   return false;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    alert("صياغة غير صحيحة للبريد الإلكتروني");
	    setcursor('email');
	    return false;
	}

	 if (str.indexOf(at,(lat+1))!=-1){
	    alert("صياغة غير صحيحة للبريد الإلكتروني");
	    setcursor('email');
	    return false;
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    alert("صياغة غير صحيحة للبريد الإلكتروني");
	    setcursor('email');
	    return false;
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
	    alert("صياغة غير صحيحة للبريد الإلكتروني");
	    setcursor('email');
	    return false;
	 }
		
	 if (str.indexOf(" ")!=-1){
	    alert("صياغة غير صحيحة للبريد الإلكتروني");
	    setcursor('email');
	    return false;
	 }

	 else { return true }
}


// Bring the cursor to the end of line after focus
function setcursor (myObject)
{
	document.getElementById(myObject).focus();
	document.getElementById(myObject).value += '';
}


//////////////////////////////////////////
// Expandable fields/texts


var enablepersist="off" //Enable saving state of content structure using session cookies? (on/off)
var collapseprevious="yes" //Collapse previously open content when opening present? (yes/no)

if (document.getElementById){
document.write('<style type="text/css">')
document.write('.switchcontent{display:none;}')
document.write('</style>')
}

function getElementbyClass(classname){
ccollect=new Array()
var inc=0
var alltags=document.all? document.all : document.getElementsByTagName("*")
for (i=0; i<alltags.length; i++){
if (alltags[i].className==classname)
ccollect[inc++]=alltags[i]
}
}

function contractcontent(omit){
var inc=0
while (ccollect[inc]){
if (ccollect[inc].id!=omit)
ccollect[inc].style.display="none"
inc++
}
}

function expandcontent(cid){
if (typeof ccollect!="undefined"){
if (collapseprevious=="yes")
contractcontent(cid)
document.getElementById(cid).style.display=(document.getElementById(cid).style.display="block")? "block" : "none"
}
}

function revivecontent(){
contractcontent("omitnothing")
selectedItem=getselectedItem()
selectedComponents=selectedItem.split("|")
for (i=0; i<selectedComponents.length-1; i++)
document.getElementById(selectedComponents[i]).style.display="block"
}

function get_cookie(Name) { 
var search = Name + "="
var returnvalue = "";
if (document.cookie.length > 0) {
offset = document.cookie.indexOf(search)
if (offset != -1) { 
offset += search.length
end = document.cookie.indexOf(";", offset);
if (end == -1) end = document.cookie.length;
returnvalue=unescape(document.cookie.substring(offset, end))
}
}
return returnvalue;
}

function getselectedItem(){
if (get_cookie(window.location.pathname) != ""){
selectedItem=get_cookie(window.location.pathname)
return selectedItem
}
else
return ""
}

function saveswitchstate(){
var inc=0, selectedItem=""
while (ccollect[inc]){
if (ccollect[inc].style.display=="block")
selectedItem+=ccollect[inc].id+"|"
inc++
}

document.cookie=window.location.pathname+"="+selectedItem
}

function do_onload(){
uniqueidn=window.location.pathname+"firsttimeload"
getElementbyClass("switchcontent")
if (enablepersist=="on" && typeof ccollect!="undefined"){
document.cookie=(get_cookie(uniqueidn)=="")? uniqueidn+"=1" : uniqueidn+"=0" 
firsttimeload=(get_cookie(uniqueidn)==1)? 1 : 0 //check if this is 1st page load
if (!firsttimeload)
revivecontent()
}
}


if (window.addEventListener)
window.addEventListener("load", do_onload, false)
else if (window.attachEvent)
window.attachEvent("onload", do_onload)
else if (document.getElementById)
window.onload=do_onload

if (enablepersist=="on" && document.getElementById)
window.onunload=saveswitchstate

////////////////////////////////////////////////////////////
// Weather corner

function setall(obj) {
	var a = obj.split('&');
	document.all.text1.innerHTML = a[0];
	document.all.text2.innerHTML = a[1];
	document.all.text3.innerHTML = a[2];
	document.all.text4.innerHTML = a[3];
} 

//////////////////////////
// Dijla Guide

function changeBanner(url,cat,item) {
 	var url,cat,item;
	document.getElementById('banner').innerHTML = '<a href="' + url + '" target="_blank"><img src="http://www.radiodijla.com/guide/banners/' + cat + '/' + item + '.jpg" width="700" height="80" border="0" style="border:1px solid #DDDDDD"></a>';
 }
 
 function section(obj) {
 	var obj;
	
	if (obj == "institutional") { 
		document.getElementById('banner').innerHTML = '<a href="http://www.shurakaal-iraq.com/?osadcampaign=dijla" target="_blank"><img src="http://www.radiodijla.com/guide/banners/institutional/1.jpg" width="700" height="80" border="0" style="border:1px solid #DDDDDD"></a>';
		
		document.getElementById('institutional').style.display = 'block';
		document.getElementById('entertainment').style.display = 'none';
		document.getElementById('commercial').style.display = 'none';
		
		document.getElementById('institutional_red_arrow').style.display = 'block';
		document.getElementById('entertainment_red_arrow').style.display = 'none';
		document.getElementById('commercial_red_arrow').style.display = 'none';
	}
	
	else if (obj == "entertainment") { 
		document.getElementById('banner').innerHTML = '<a href="http://www.iraqiolympic.org" target="_blank"><img src="http://www.radiodijla.com/guide/banners/entertainment/1.jpg" width="700" height="80" border="0" style="border:1px solid #DDDDDD"></a>';
		
		document.getElementById('institutional').style.display = 'none';
		document.getElementById('entertainment').style.display = 'block';
		document.getElementById('commercial').style.display = 'none';
		
		document.getElementById('institutional_red_arrow').style.display = 'none';
		document.getElementById('entertainment_red_arrow').style.display = 'block';
		document.getElementById('commercial_red_arrow').style.display = 'none';
	}
	
	else if (obj == "commercial") { 
		document.getElementById('banner').innerHTML = '<a href="http://www.bdcbasra.com" target="_blank"><img src="http://www.radiodijla.com/guide/banners/commercial/1.jpg" width="700" height="80" border="0" style="border:1px solid #DDDDDD"></a>';
		
		document.getElementById('institutional').style.display = 'none';
		document.getElementById('entertainment').style.display = 'none';
		document.getElementById('commercial').style.display = 'block';
		
		document.getElementById('institutional_red_arrow').style.display = 'none';
		document.getElementById('entertainment_red_arrow').style.display = 'none';
		document.getElementById('commercial_red_arrow').style.display = 'block';
	}
 }
 
 
