if (typeof(DEBUG) == 'undefined'){
	var DEBUG= 0;
}
var MaskDiv = null;
var en = {};
var ShowMask = function(flag){
	if (flag){
		if (!MaskDiv){
			var w = document.body.scrollWidth;
			var h = document.body.scrollHeight;
			var div = document.createElement('div');
			div.style.backgroundColor = '#000';
			div.style['position'] = 'absolute';
			div.style['top'] = '0px';
			div.style['left'] = '0px';
			div.style.width = w +'px';
			div.style.height = h +'px';
			div['style']['opacity'] = '0.5';
			div['style']['filter'] = 'alpha(Opacity=50)';
			//div.className = 'MaskDiv';
			document.body.appendChild(div);
			MaskDiv = div;
		}
	}else{
		var div = MaskDiv;
		if (div){
			div.style.display = 'none';
			div.parentElement.removeChild(div);
		}
		MaskDiv = null;
	}
}

/*
flag: 
message type,look at inc IApi::SendSMS

submit_type: 
look at function SubmitSMS
0 or null: ajax with ajax/ajaxmain.inc		
1		 : ajax with inc/SendSMS.php

style: 
0 or null:   send to yourself 
	   1 :			send to some friend
*/
var ShowForm = function(flag,submit_type,style,mobile,hidecountry,vStyle){ 
	//ShowMask(1);
	mobile = mobile ? mobile :'';
	hidecountry = hidecountry ? 1:0;
	if(!submit_type){
		submit_type=0;
	}
	if(!style){
		style=0;
	}
	
	var form_div = $('form_div');
	if (!form_div){
		form_div = document.createElement('div');
		form_div.id = 'form_div';
		form_div.style['zIndex'] = 100;
	    form_div.style['position'] = 'absolute';
		form_div.style['border'] = '2px solid #fff';
	    form_div.style.backgroundColor = '#fff';
	}
    
	var prefix = 'http://' + location.hostname + '/';
    var str = '\
  	<div class="box">\
  	<div class="bar">\
  	<p>\
  		<span class="f_l">Send to phone</span><a href="javascript:;"  onclick="HideForm()"><img src="' + prefix + 'images/del.gif" width="15" height="15" class="f_r" /></a>\
  	</p>\
  	</div>\
  	<div class="sentdphone3">\
    <form id="sms_form" action="" method="post">\
    <table width="90%" border="0" cellspacing="0" cellpadding="0" style="margin-top:10px">\
    ';
    if (hidecountry){
    	
    }else{
 	str += '\
    	<tr>\
        <td height="20" align="right"><img id="country_flag" src="http://l.yimg.com/a/i/us/cl/mob/v3/gen/flags/US.png" /></td>\
        <td height="20">\
        	<select id="select_country" name="select_country" class="texbox w_132" onchange="ChangeCountry(this.value,this)">\
		';
	    for (var x in localizedCountryData){
	    	str += '<option value="'+x+'">'+localizedCountryData[x][1]+'</option>';
	    }
    	str +=  '</select></td></tr>';
    }
    str += '\
		<tr>\
        	<td height="20" align="right">' +(hidecountry?'Mobile':'<span id="prefix" class="m_r_1"> +1</span>') +'</td>\
            <td height="20"><input id="p_num" name="p_num" type="text" class="texbox w_130" value="'+ mobile +'" /></td>\
        </tr>\
        <tr>\
        	<td height="20" align="right">Your name</td>\
            <td height="20"><input id="sender" name="sender" type="text" class="texbox w_130" /></td>\
		</tr>\
        <tr>\
            <td height="20" align="right">&nbsp;</td>\
            <td height="20" align="left"><a href="javascript:void(0)" onclick="SMSSubmit('+submit_type+')"><img id="send_button" name="send_button" src="' + prefix + 'images/send_button.gif"/></img></td>\
        </tr>\
    	';

	str += '\
	</table>\
            <input id="p_value" name="p_value" type="hidden" value="" />\
            <input id="p_flag" name="p_flag" type="hidden" value="'+flag+'" />\
    </form>\
  </div>\
</div>\
';
    
    form_div.innerHTML = str;
    document.body.appendChild(form_div);
    //AdminRes.Editform_div = form_div;
    
    var left = (document.body.offsetWidth - form_div.offsetWidth)/2;
	var top = (document.body.offsetHeight - form_div.offsetHeight)/2;
    form_div.style['left'] =  left + 'px';
    form_div.style['top'] = top +'px';
    
    try{
    	ChangeCountry(InitLocalized);
    }catch(e){}
}

var HideForm = function(){
	//ShowMask(0);
	document.body.removeChild($('form_div'));
}

var CurrentPlay = function(cid,flag){
	$('divPlay').style.display = '';
	if(typeof(flag)=="undefined"){
		$('ifrPlay').src = "inc/embed.php?cid="+cid;
	}
	else{
		$('ifrPlay').src = "../inc/embed.php?cid="+cid;
	}
}

var ChangeStyle = function(style){
	for(var i=1; i<=5; i++)
	{
		id = 'p_style_'+i;
		if(i==style){
			$(id).className = 'bottom_bg2';
			$('p_style').className = 'main_box_right'+i+' f_1';
		}
		else{
			$(id).className = '';
		}
	}
}

var ChangeCountry = function(x,obj){
		if (DEBUG){
			alert(x);
		}
		try{
			$('prefix').innerHTML = ' +'+localizedCountryData[x][2];
			$('country_flag').src = flagsOfCountries+localizedCountryData[x][0]+'.png';
			$('p_value').value = localizedCountryData[x][2];
			if (!obj){
				var ddl = $('select_country');
				for(var i=0;i<ddl.options.length;i++){
					if (ddl.options[i].value == x){
						ddl.options[i].selected = true;
						break;
					}
				}
			}
		}catch(e){}
}

var ChangeCountry2= function(x,obj){
		try{
			$('prefix2').innerHTML = ' +'+localizedCountryData[x][2];
			$('p_value2').value = localizedCountryData[x][2];
			if (!obj){
				var ddl = $('select_country2');
				for(var i=0;i<ddl.options.length;i++){
					if (ddl.options[i].value == x){
						ddl.options[i].selected = true;
						break;
					}
				}
			}
			$('country_flag2').src = flagsOfCountries+localizedCountryData[x][0]+'.png';
		}catch(e){}
}

var SMSSubmit = function(type_flag){
	if(!$('p_num').value){
		alert('Invalid mobile number');
		return false;
	}
	var flag = type_flag;
	var pvalue = $('p_value') ? $('p_value').value : '';
	var cid = '';
	try{
		if (DEBUG)	alert(flag);
		if (flag == '4'){
			cid = KEY;
		}else if(flag == '2' || flag == '3'){
			cid = hPage.CurrentCID;
		}
	}catch(e){
		if (DEBUG) alert(e.message);
	}
	ChangeStatus(1);
	var strURL = 'http://' + location.hostname + '/inc/SendSMS.php?p_num='+$('p_num').value+'&sender='+$('sender').value+'&p_value='+pvalue+'&p_flag='+flag +'&c=' + cid;
	if (DEBUG){
		alert(strURL);
	}
	Ajax.GetJavaScript(strURL,function(strResponse){
		alert(strResponse);
		ChangeStatus(2);
		try{
			HideForm();
		}catch(e){}
	}); 
}

/*
change send_button status
1:clicked
else:unclicked
*/
en.sndBtn = new Array();
en.sndBtn[0] = new Image();
en.sndBtn[0].src = 'http://' + location.hostname + '/images/send_button.gif';
en.sndBtn[1] = new Image();
en.sndBtn[1].src = 'http://' + location.hostname + '/images/send_button_click.gif';
var ChangeStatus = function(status){
	if(status==1){
		$('send_button').onclick = 'javascript:void(0)';
		$('send_button').src = en.sndBtn[1].src;
	}
	else{
		$('send_button').src = en.sndBtn[0].src;
		$('send_button').onclick = null;
	}
}